Fixed a variety of bugs in storeSessionsCache() and the corresponding SQL statements

This commit is contained in:
chaz@pokeit.co 2010-12-08 16:32:26 -05:00
parent 9235d12d59
commit 95d14911f3
2 changed files with 119 additions and 101 deletions

View File

@ -2026,19 +2026,25 @@ class Database:
pass pass
def storeSessionsCache(self, pids, startTime, game, pdata): 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) bigBet = int(Decimal(game['bb'])*200)
check_sessionscache = self.sql.query['check_sessionscache'] select_sessionscache = self.sql.query['select_sessionscache']
check_sessionscache = check_sessionscache.replace('%s', self.sql.query['placeholder']) select_sessionscache = select_sessionscache.replace('%s', self.sql.query['placeholder'])
update_sessionscache = self.sql.query['update_sessionscache'] select_sessionscache_mid = self.sql.query['select_sessionscache_mid']
update_sessionscache = update_sessionscache.replace('%s', self.sql.query['placeholder']) 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 = self.sql.query['update_sessionscache_start']
update_sessionscache_start = update_sessionscache_start.replace('%s', self.sql.query['placeholder']) update_sessionscache_start = update_sessionscache_start.replace('%s', self.sql.query['placeholder'])
update_sessionscache_end = self.sql.query['update_sessionscache_end'] update_sessionscache_end = self.sql.query['update_sessionscache_end']
update_sessionscache_end = update_sessionscache_end.replace('%s', self.sql.query['placeholder']) update_sessionscache_end = update_sessionscache_end.replace('%s', self.sql.query['placeholder'])
insert_sessionscache = self.sql.query['insert_sessionscache'] insert_sessionscache = self.sql.query['insert_sessionscache']
insert_sessionscache = insert_sessionscache.replace('%s', self.sql.query['placeholder']) insert_sessionscache = insert_sessionscache.replace('%s', self.sql.query['placeholder'])
merge_sessionscache = self.sql.query['merge_sessionscache'] merge_sessionscache = self.sql.query['merge_sessionscache']
@ -2083,76 +2089,61 @@ class Database:
cursor = self.get_cursor() cursor = self.get_cursor()
for row in inserts: for row in inserts:
check = [] threshold = []
check.append(row[-1]-THRESHOLD) threshold.append(row[-1]-THRESHOLD)
check.append(row[-1]+THRESHOLD) threshold.append(row[-1]+THRESHOLD)
num = cursor.execute(check_sessionscache, check) cursor.execute(select_sessionscache, threshold)
#DEBUG log.info(_("check yurself: '%s'") % (num.rowcount)) num = cursor.rowcount
if (num == 1):
# Try to do the update first: # Try to do the update first:
if ((self.backend == self.PGSQL and cursor.statusmessage == "UPDATE 1") #print "DEBUG: found 1 record to update"
or (self.backend == self.MYSQL_INNODB and num == 1) update_mid = row + row[-1:]
or (self.backend == self.SQLITE and num.rowcount == 1)): cursor.execute(select_sessionscache_mid, update_mid[-2:])
update = row + row[-1:] mid = cursor.rowcount
mid = cursor.execute(update_sessionscache, update) if (mid == 0):
#DEBUG log.info(_("update '%s' rows, no change to session times ") % str(mid.rowcount)) update_startend = row[-1:] + row + threshold
if ((self.backend == self.PGSQL and cursor.statusmessage != "UPDATE 1") cursor.execute(select_sessionscache_start, update_startend[-3:])
or (self.backend == self.MYSQL_INNODB and mid == 0) start = cursor.rowcount
or (self.backend == self.SQLITE and mid.rowcount == 0)): if (start == 0):
update_start = row[-1:] + row + check #print "DEBUG:", start, " start record found. Update stats and start time"
start = cursor.execute(update_sessionscache_start, update_start) cursor.execute(update_sessionscache_end, update_startend)
#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))
else: else:
pass #print "DEBUG: 1 end record found. Update stats and end time time"
cursor.execute(update_sessionscache_start, update_startend)
else: else:
pass #print "DEBUG: update stats mid-session"
elif ((self.backend == self.PGSQL and cursor.statusmessage != "UPDATE 1" and "UPDATE" in cursor.statusmessage) cursor.execute(update_sessionscache_mid, update_mid)
or (self.backend == self.MYSQL_INNODB and num > 1) elif (num > 1):
or (self.backend == self.SQLITE and num.rowcount > 1)): # Multiple matches found - merge them into one session and update:
#DEBUG log.info(_("multiple matches")) #print "DEBUG:", num, "matches found"
pass cursor.execute(merge_sessionscache, threshold)
#merge two sessions if there are multiple matches
cursor.execute(merge_sessionscache, check)
merge = cursor.fetchone() merge = cursor.fetchone()
cursor.execute(delete_sessions, check) cursor.execute(delete_sessions, threshold)
cursor.execute(insert_sessionscache, merge) cursor.execute(insert_sessionscache, merge)
update = row + row[-1:] update_mid = row + row[-1:]
mid = cursor.execute(update_sessionscache, update) cursor.execute(select_sessionscache_mid, update_mid[-2:])
#DEBUG log.info(_("update '%s' rows, no change to session times ") % str(mid.rowcount)) mid = cursor.rowcount
if ((self.backend == self.PGSQL and cursor.statusmessage != "UPDATE 1") if (mid == 0):
or (self.backend == self.MYSQL_INNODB and mid == 0) update_startend = row[-1:] + row + threshold
or (self.backend == self.SQLITE and mid.rowcount == 0)): cursor.execute(select_sessionscache_start, update_startend[-3:])
update_start = row[-1:] + row + check start = cursor.rowcount
start = cursor.execute(update_sessionscache_start, update_start) if (start == 0):
#DEBUG log.info(_("update '%s' rows, and updated sessionStart") % str(start.rowcount)) #print "DEBUG:", start, " start record found. Update stats and start time"
if ((self.backend == self.PGSQL and cursor.statusmessage != "UPDATE 1") cursor.execute(update_sessionscache_end, update_startend)
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))
else: else:
pass #print "DEBUG: 1 end record found. Update stats and end time time"
cursor.execute(update_sessionscache_start, update_startend)
else: else:
pass #print "DEBUG: update stats mid-session"
cursor.execute(update_sessionscache_mid, update_mid)
elif ((self.backend == self.PGSQL and cursor.statusmessage != "UPDATE 1") elif (num == 0):
or (self.backend == self.MYSQL_INNODB and num == 0) # No matches found, insert new session:
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
insert = row + row[-1:] insert = row + row[-1:]
insert = insert[-2:] + insert[:-2] 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) cursor.execute(insert_sessionscache, insert)
else: else:
# Something bad happened
pass pass
def isDuplicate(self, gametypeID, siteHandNo): def isDuplicate(self, gametypeID, siteHandNo):

View File

@ -4019,26 +4019,57 @@ class Sql:
(case when tourneyTypeId+0=%s then 1 else 0 end) end)=1 (case when tourneyTypeId+0=%s then 1 else 0 end) end)=1
AND styleKey=%s""" AND styleKey=%s"""
self.query['check_sessionscache'] = """ self.query['get_hero_hudcache_start'] = """select min(hc.styleKey)
UPDATE SessionsCache SET from HudCache hc
sessionStart=sessionStart, where hc.playerId in <playerid_list>
sessionEnd=sessionEnd, and hc.styleKey like 'd%'"""
ringHDs=ringHDs,
tourHDs=tourHDs,
totalProfit=totalProfit,
bigBets=bigBets
WHERE sessionEnd>=%s
AND sessionStart<=%s"""
self.query['insert_sessionscache'] = """ ####################################
INSERT INTO SessionsCache ( # Queries to rebuild/modify sessionscache
sessionStart, ####################################
self.query['select_sessionscache'] = """
SELECT sessionStart,
sessionEnd, sessionEnd,
ringHDs, ringHDs,
tourHDs, tourHDs,
totalProfit, totalProfit,
bigBets) bigBets
VALUES (%s, %s, %s, %s, %s, %s)""" 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'] = """ self.query['update_sessionscache_start'] = """
UPDATE SessionsCache SET UPDATE SessionsCache SET
@ -4062,30 +4093,26 @@ class Sql:
AND sessionEnd>=%s AND sessionEnd>=%s
AND sessionStart<=%s""" AND sessionStart<=%s"""
self.query['update_sessionscache'] = """ self.query['insert_sessionscache'] = """
UPDATE SessionsCache SET INSERT INTO SessionsCache (
ringHDs=ringHDs+%s, sessionStart,
tourHDs=tourHDs+%s, sessionEnd,
totalProfit=totalProfit+%s, ringHDs,
bigBets=bigBets+%s tourHDs,
WHERE sessionStart<=%s totalProfit,
AND sessionEnd>=%s""" bigBets)
VALUES (%s, %s, %s, %s, %s, %s)"""
self.query['merge_sessionscache'] = """ self.query['merge_sessionscache'] = """
SELECT min(sessionStart), max(sessionEnd), sum(ringHDs), sum(tourHDs), sum(totalProfit), sum(bigBets) SELECT min(sessionStart), max(sessionEnd), sum(ringHDs), sum(tourHDs), sum(totalProfit), sum(bigBets)
FROM SessionsCache FROM SessionsCache
WHERE sessionStart>=%s WHERE sessionEnd>=%s
AND sessionEnd<=%s""" AND sessionStart<=%s"""
self.query['delete_sessions'] = """ self.query['delete_sessions'] = """
DELETE FROM SessionsCache DELETE FROM SessionsCache
WHERE sessionStart>=%s WHERE sessionEnd>=%s
AND sessionEnd<=%s""" AND sessionStart<=%s"""
self.query['get_hero_hudcache_start'] = """select min(hc.styleKey)
from HudCache hc
where hc.playerId in <playerid_list>
and hc.styleKey like 'd%'"""
if db_server == 'mysql': if db_server == 'mysql':
self.query['analyze'] = """ self.query['analyze'] = """