From 95d14911f315fc14a40e620860170f3553fb0f4e Mon Sep 17 00:00:00 2001 From: "chaz@pokeit.co" Date: Wed, 8 Dec 2010 16:32:26 -0500 Subject: [PATCH] Fixed a variety of bugs in storeSessionsCache() and the corresponding SQL statements --- pyfpdb/Database.py | 125 +++++++++++++++++++++------------------------ pyfpdb/SQL.py | 95 ++++++++++++++++++++++------------ 2 files changed, 119 insertions(+), 101 deletions(-) diff --git a/pyfpdb/Database.py b/pyfpdb/Database.py index 567ab166..cf69ddb0 100644 --- a/pyfpdb/Database.py +++ b/pyfpdb/Database.py @@ -2026,19 +2026,25 @@ class Database: pass def storeSessionsCache(self, pids, startTime, game, pdata): - """Update cached sessions. If update fails because no record exists, do an insert.""" + """Update cached sessions. If update fails because no record exists, do an insert""" - THRESHOLD = timedelta(seconds=int(self.sessionTimeout * 60)) #convert minutes to seconds + THRESHOLD = timedelta(seconds=int(self.sessionTimeout * 60)) bigBet = int(Decimal(game['bb'])*200) - check_sessionscache = self.sql.query['check_sessionscache'] - check_sessionscache = check_sessionscache.replace('%s', self.sql.query['placeholder']) - update_sessionscache = self.sql.query['update_sessionscache'] - update_sessionscache = update_sessionscache.replace('%s', self.sql.query['placeholder']) + select_sessionscache = self.sql.query['select_sessionscache'] + select_sessionscache = select_sessionscache.replace('%s', self.sql.query['placeholder']) + select_sessionscache_mid = self.sql.query['select_sessionscache_mid'] + select_sessionscache_mid = select_sessionscache_mid.replace('%s', self.sql.query['placeholder']) + select_sessionscache_start = self.sql.query['select_sessionscache_start'] + select_sessionscache_start = select_sessionscache_start.replace('%s', self.sql.query['placeholder']) + + update_sessionscache_mid = self.sql.query['update_sessionscache_mid'] + update_sessionscache_mid = update_sessionscache_mid.replace('%s', self.sql.query['placeholder']) update_sessionscache_start = self.sql.query['update_sessionscache_start'] update_sessionscache_start = update_sessionscache_start.replace('%s', self.sql.query['placeholder']) update_sessionscache_end = self.sql.query['update_sessionscache_end'] update_sessionscache_end = update_sessionscache_end.replace('%s', self.sql.query['placeholder']) + insert_sessionscache = self.sql.query['insert_sessionscache'] insert_sessionscache = insert_sessionscache.replace('%s', self.sql.query['placeholder']) merge_sessionscache = self.sql.query['merge_sessionscache'] @@ -2083,77 +2089,62 @@ class Database: cursor = self.get_cursor() for row in inserts: - check = [] - check.append(row[-1]-THRESHOLD) - check.append(row[-1]+THRESHOLD) - num = cursor.execute(check_sessionscache, check) - #DEBUG log.info(_("check yurself: '%s'") % (num.rowcount)) - - # Try to do the update first: - if ((self.backend == self.PGSQL and cursor.statusmessage == "UPDATE 1") - or (self.backend == self.MYSQL_INNODB and num == 1) - or (self.backend == self.SQLITE and num.rowcount == 1)): - update = row + row[-1:] - mid = cursor.execute(update_sessionscache, update) - #DEBUG log.info(_("update '%s' rows, no change to session times ") % str(mid.rowcount)) - if ((self.backend == self.PGSQL and cursor.statusmessage != "UPDATE 1") - or (self.backend == self.MYSQL_INNODB and mid == 0) - or (self.backend == self.SQLITE and mid.rowcount == 0)): - update_start = row[-1:] + row + check - start = cursor.execute(update_sessionscache_start, update_start) - #DEBUG log.info(_("update '%s' rows, and updated sessionStart") % str(start.rowcount)) - if ((self.backend == self.PGSQL and cursor.statusmessage != "UPDATE 1") - or (self.backend == self.MYSQL_INNODB and start == 0) - or (self.backend == self.SQLITE and start.rowcount == 0)): - update_end = row[-1:] + row + check - end = cursor.execute(update_sessionscache_end, update_end) - #DEBUG log.info(_("update '%s' rows, and updated sessionEnd") % str(end.rowcount)) + threshold = [] + threshold.append(row[-1]-THRESHOLD) + threshold.append(row[-1]+THRESHOLD) + cursor.execute(select_sessionscache, threshold) + num = cursor.rowcount + if (num == 1): + # Try to do the update first: + #print "DEBUG: found 1 record to update" + update_mid = row + row[-1:] + cursor.execute(select_sessionscache_mid, update_mid[-2:]) + mid = cursor.rowcount + if (mid == 0): + update_startend = row[-1:] + row + threshold + cursor.execute(select_sessionscache_start, update_startend[-3:]) + start = cursor.rowcount + if (start == 0): + #print "DEBUG:", start, " start record found. Update stats and start time" + cursor.execute(update_sessionscache_end, update_startend) else: - pass + #print "DEBUG: 1 end record found. Update stats and end time time" + cursor.execute(update_sessionscache_start, update_startend) else: - pass - elif ((self.backend == self.PGSQL and cursor.statusmessage != "UPDATE 1" and "UPDATE" in cursor.statusmessage) - or (self.backend == self.MYSQL_INNODB and num > 1) - or (self.backend == self.SQLITE and num.rowcount > 1)): - #DEBUG log.info(_("multiple matches")) - pass - #merge two sessions if there are multiple matches - cursor.execute(merge_sessionscache, check) + #print "DEBUG: update stats mid-session" + cursor.execute(update_sessionscache_mid, update_mid) + elif (num > 1): + # Multiple matches found - merge them into one session and update: + #print "DEBUG:", num, "matches found" + cursor.execute(merge_sessionscache, threshold) merge = cursor.fetchone() - cursor.execute(delete_sessions, check) + cursor.execute(delete_sessions, threshold) cursor.execute(insert_sessionscache, merge) - update = row + row[-1:] - mid = cursor.execute(update_sessionscache, update) - #DEBUG log.info(_("update '%s' rows, no change to session times ") % str(mid.rowcount)) - if ((self.backend == self.PGSQL and cursor.statusmessage != "UPDATE 1") - or (self.backend == self.MYSQL_INNODB and mid == 0) - or (self.backend == self.SQLITE and mid.rowcount == 0)): - update_start = row[-1:] + row + check - start = cursor.execute(update_sessionscache_start, update_start) - #DEBUG log.info(_("update '%s' rows, and updated sessionStart") % str(start.rowcount)) - if ((self.backend == self.PGSQL and cursor.statusmessage != "UPDATE 1") - or (self.backend == self.MYSQL_INNODB and start == 0) - or (self.backend == self.SQLITE and start.rowcount == 0)): - update_end = row[-1:] + row + check - end = cursor.execute(update_sessionscache_end, update_end) - #DEBUG log.info(_("update '%s' rows, and updated sessionEnd") % str(end.rowcount)) + update_mid = row + row[-1:] + cursor.execute(select_sessionscache_mid, update_mid[-2:]) + mid = cursor.rowcount + if (mid == 0): + update_startend = row[-1:] + row + threshold + cursor.execute(select_sessionscache_start, update_startend[-3:]) + start = cursor.rowcount + if (start == 0): + #print "DEBUG:", start, " start record found. Update stats and start time" + cursor.execute(update_sessionscache_end, update_startend) else: - pass + #print "DEBUG: 1 end record found. Update stats and end time time" + cursor.execute(update_sessionscache_start, update_startend) else: - pass - - elif ((self.backend == self.PGSQL and cursor.statusmessage != "UPDATE 1") - or (self.backend == self.MYSQL_INNODB and num == 0) - or (self.backend == self.SQLITE and num.rowcount == 0)): - #move the last 2 items in WHERE clause of row from the end of the array - # to the beginning for the INSERT statement - #print "DEBUG: using INSERT: %s" % num + #print "DEBUG: update stats mid-session" + cursor.execute(update_sessionscache_mid, update_mid) + elif (num == 0): + # No matches found, insert new session: insert = row + row[-1:] insert = insert[-2:] + insert[:-2] - #DEBUG log.info(_("insert row: '%s'") % (insert)) + #print "DEBUG: No matches found. Insert record", insert cursor.execute(insert_sessionscache, insert) else: - pass + # Something bad happened + pass def isDuplicate(self, gametypeID, siteHandNo): dup = False diff --git a/pyfpdb/SQL.py b/pyfpdb/SQL.py index ae54c880..f6bb068b 100644 --- a/pyfpdb/SQL.py +++ b/pyfpdb/SQL.py @@ -4019,26 +4019,57 @@ class Sql: (case when tourneyTypeId+0=%s then 1 else 0 end) end)=1 AND styleKey=%s""" - self.query['check_sessionscache'] = """ - UPDATE SessionsCache SET - sessionStart=sessionStart, - sessionEnd=sessionEnd, - ringHDs=ringHDs, - tourHDs=tourHDs, - totalProfit=totalProfit, - bigBets=bigBets - WHERE sessionEnd>=%s - AND sessionStart<=%s""" - - self.query['insert_sessionscache'] = """ - INSERT INTO SessionsCache ( - sessionStart, + self.query['get_hero_hudcache_start'] = """select min(hc.styleKey) + from HudCache hc + where hc.playerId in + and hc.styleKey like 'd%'""" + + #################################### + # Queries to rebuild/modify sessionscache + #################################### + + self.query['select_sessionscache'] = """ + SELECT sessionStart, sessionEnd, ringHDs, tourHDs, totalProfit, - bigBets) - VALUES (%s, %s, %s, %s, %s, %s)""" + bigBets + FROM SessionsCache + WHERE sessionEnd>=%s + AND sessionStart<=%s""" + + self.query['select_sessionscache_mid'] = """ + SELECT sessionStart, + sessionEnd, + ringHDs, + tourHDs, + totalProfit, + bigBets + FROM SessionsCache + WHERE sessionEnd>=%s + AND sessionStart<=%s""" + + self.query['select_sessionscache_start'] = """ + SELECT sessionStart, + sessionEnd, + ringHDs, + tourHDs, + totalProfit, + bigBets + FROM SessionsCache + WHERE sessionStart>%s + AND sessionEnd>=%s + AND sessionStart<=%s""" + + self.query['update_sessionscache_mid'] = """ + UPDATE SessionsCache SET + ringHDs=ringHDs+%s, + tourHDs=tourHDs+%s, + totalProfit=totalProfit+%s, + bigBets=bigBets+%s + WHERE sessionStart<=%s + AND sessionEnd>=%s""" self.query['update_sessionscache_start'] = """ UPDATE SessionsCache SET @@ -4050,7 +4081,7 @@ class Sql: WHERE sessionStart>%s AND sessionEnd>=%s AND sessionStart<=%s""" - + self.query['update_sessionscache_end'] = """ UPDATE SessionsCache SET sessionEnd=%s, @@ -4062,30 +4093,26 @@ class Sql: AND sessionEnd>=%s AND sessionStart<=%s""" - self.query['update_sessionscache'] = """ - UPDATE SessionsCache SET - ringHDs=ringHDs+%s, - tourHDs=tourHDs+%s, - totalProfit=totalProfit+%s, - bigBets=bigBets+%s - WHERE sessionStart<=%s - AND sessionEnd>=%s""" + self.query['insert_sessionscache'] = """ + INSERT INTO SessionsCache ( + sessionStart, + sessionEnd, + ringHDs, + tourHDs, + totalProfit, + bigBets) + VALUES (%s, %s, %s, %s, %s, %s)""" self.query['merge_sessionscache'] = """ SELECT min(sessionStart), max(sessionEnd), sum(ringHDs), sum(tourHDs), sum(totalProfit), sum(bigBets) FROM SessionsCache - WHERE sessionStart>=%s - AND sessionEnd<=%s""" + WHERE sessionEnd>=%s + AND sessionStart<=%s""" self.query['delete_sessions'] = """ DELETE FROM SessionsCache - WHERE sessionStart>=%s - AND sessionEnd<=%s""" - - self.query['get_hero_hudcache_start'] = """select min(hc.styleKey) - from HudCache hc - where hc.playerId in - and hc.styleKey like 'd%'""" + WHERE sessionEnd>=%s + AND sessionStart<=%s""" if db_server == 'mysql': self.query['analyze'] = """