From 34e374a55ebdf3eb4421c9406b7fb80c64d64c69 Mon Sep 17 00:00:00 2001 From: Worros Date: Fri, 14 Nov 2008 08:15:28 +1000 Subject: [PATCH] Patch from sqlcoder to make the query for the profit graph smarter/faster --- pyfpdb/FpdbSQLQueries.py | 28 ++++++++++++++++++++++++++++ pyfpdb/GuiGraphViewer.py | 10 ++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/pyfpdb/FpdbSQLQueries.py b/pyfpdb/FpdbSQLQueries.py index e582c681..2672de23 100644 --- a/pyfpdb/FpdbSQLQueries.py +++ b/pyfpdb/FpdbSQLQueries.py @@ -616,6 +616,34 @@ class FpdbSQLQueries: WHERE Players.name = %s AND HandsPlayers.handId = %s AND Players.siteId = %s AND (tourneysPlayersId IS NULL)""" + if(self.dbname == 'MySQL InnoDB') or (self.dbname == 'PostgreSQL'): + self.query['getRingProfitAllHandsPlayerIdSite'] = """ + SELECT hp.handId, hp.winnings, SUM(ha.amount) costs, hp.winnings - SUM(ha.amount) profit + FROM HandsPlayers hp + INNER JOIN Players pl ON hp.playerId = pl.id + INNER JOIN Hands h ON h.id = hp.handId + INNER JOIN HandsActions ha ON ha.handPlayerId = hp.id + WHERE pl.name = %s + AND pl.siteId = %s + AND hp.tourneysPlayersId IS NULL + GROUP BY hp.handId, hp.winnings, h.handStart + ORDER BY h.handStart""" + elif(self.dbname == 'SQLite'): + #Probably doesn't work. + self.query['getRingProfitAllHandsPlayerIdSite'] = """ + SELECT hp.handId, hp.winnings, SUM(ha.amount) costs, hp.winnings - SUM(ha.amount) profit + FROM HandsPlayers hp + INNER JOIN Players pl ON hp.playerId = pl.id + INNER JOIN Hands h ON h.id = hp.handId + INNER JOIN HandsActions ha ON ha.handPlayerId = hp.id + WHERE pl.name = %s + AND pl.siteId = %s + AND hp.tourneysPlayersId IS NULL + GROUP BY hp.handId, hp.winnings, h.handStart + ORDER BY h.handStart""" + + + if __name__== "__main__": from optparse import OptionParser diff --git a/pyfpdb/GuiGraphViewer.py b/pyfpdb/GuiGraphViewer.py index 49a00d20..9b42d288 100644 --- a/pyfpdb/GuiGraphViewer.py +++ b/pyfpdb/GuiGraphViewer.py @@ -20,6 +20,7 @@ import pygtk pygtk.require('2.0') import gtk import os +from time import time #import pokereval try: @@ -65,7 +66,9 @@ class GuiGraphViewer (threading.Thread): self.ax = self.fig.add_subplot(111) #Get graph data from DB + starttime = time() line = self.getRingProfitGraph(name, site) + print "Graph generated in: %s" %(time() - starttime) self.ax.set_title("Profit graph for ring games") @@ -90,7 +93,9 @@ class GuiGraphViewer (threading.Thread): #end of def showClicked def getRingProfitGraph(self, name, site): - self.cursor.execute(self.sql.query['getRingWinningsAllGamesPlayerIdSite'], (name, site)) + #self.cursor.execute(self.sql.query['getRingWinningsAllGamesPlayerIdSite'], (name, site)) + self.cursor.execute(self.sql.query['getRingProfitAllHandsPlayerIdSite'], (name, site)) + # returns (HandId,Winnings,Costs,Profit) winnings = self.db.cursor.fetchall() profit=range(len(winnings)) @@ -99,7 +104,8 @@ class GuiGraphViewer (threading.Thread): spent = self.db.cursor.fetchone() profit[i]=(i, winnings[i][1]-spent[0]) - y=map(lambda x:float(x[1]), profit) +# y=map(lambda x:float(x[1]), profit) + y=map(lambda x:float(x[3]), winnings) line = cumsum(y) return line/100 #end of def getRingProfitGraph