From: Ant Zucaro Date: Wed, 29 Aug 2012 02:01:12 +0000 (-0400) Subject: Add a cutoff date. X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=1d1edd64bab1ca82c4e664affd1e795db7a10827;p=xonotic%2Fxonstat.git Add a cutoff date. The cutoff date is the date before which games are not considered. In other words, a player must play within the cutoff date range in order to have his or her badge regenerated. This is to prevent generating images for every single player, every single run. --- diff --git a/xonstat/batch/badges/gen_badges.py b/xonstat/batch/badges/gen_badges.py index cd1a73c..8672562 100644 --- a/xonstat/batch/badges/gen_badges.py +++ b/xonstat/batch/badges/gen_badges.py @@ -4,6 +4,7 @@ import sys from datetime import datetime import sqlalchemy as sa import sqlalchemy.sql.functions as func +from sqlalchemy import distinct from pyramid.paster import bootstrap from xonstat.models import * @@ -13,6 +14,9 @@ from render import Skin # maximal number of query results (for testing, set to 0 to get all) #NUM_PLAYERS = 100 +# we look for players who have activity within the past DELTA hours +DELTA = 6 + skin_classic = Skin( bg = "asfalt", ) @@ -69,18 +73,23 @@ req = env['request'] req.matchdict = {'id':3} print "Requesting player data from db ..." +cutoff_dt = datetime.utcnow() - timedelta(hours=DELTA) start = datetime.now() players = [] if locals().has_key('NUM_PLAYERS'): - players = DBSession.query(Player).\ + players = DBSession.query(distinct(Player.player_id)).\ filter(Player.player_id == PlayerElo.player_id).\ + filter(Player.player_id == PlayerGameStat.player_id).\ + filter(PlayerGameStat.create_dt > cutoff_dt).\ filter(Player.nick != None).\ filter(Player.player_id > 2).\ filter(Player.active_ind == True).\ limit(NUM_PLAYERS).all() else: - players = DBSession.query(Player).\ + players = DBSession.query(distinct(Player.player_id)).\ filter(Player.player_id == PlayerElo.player_id).\ + filter(Player.player_id == PlayerGameStat.player_id).\ + filter(PlayerGameStat.create_dt > cutoff_dt).\ filter(Player.nick != None).\ filter(Player.player_id > 2).\ filter(Player.active_ind == True).\ @@ -94,18 +103,18 @@ print "Query took %.2f seconds" % (total_seconds) print "Creating badges for %d players ..." % len(players) start = datetime.now() data_time, render_time = 0,0 -for player in players: - req.matchdict['id'] = player.player_id +for player_id in players: + req.matchdict['id'] = player_id sstart = datetime.now() - skin.get_data(player) + skin.get_data(player_id) sstop = datetime.now() td = sstop-sstart total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 data_time += total_seconds sstart = datetime.now() - skin.render_image("output/%d.png" % player.player_id) + skin.render_image("output/%d.png" % player_id) sstop = datetime.now() td = sstop-sstart total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 diff --git a/xonstat/batch/badges/render.py b/xonstat/batch/badges/render.py index 07da084..d0fa997 100644 --- a/xonstat/batch/badges/render.py +++ b/xonstat/batch/badges/render.py @@ -442,9 +442,12 @@ class Skin: ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL) ctx.set_font_size(self.deaths_fontsize) ctx.set_source_rgb(self.deaths_color[0], self.deaths_color[1], self.deaths_color[2]) - txt = "%d death" % deaths - if deaths != 1: - txt += "s" + if deaths is not None: + txt = "%d death" % deaths + if deaths != 1: + txt += "s" + else: + txt = "" xoff, yoff, tw, th = ctx.text_extents(txt)[:4] ctx.move_to(self.deaths_pos[0]-xoff-tw/2, self.deaths_pos[1]+yoff) ctx.show_text(txt) @@ -475,7 +478,7 @@ class Skin: writepng(output_filename, imgdata, self.width, self.height) - def get_data(self, player): + def get_data(self, player_id): """Return player data as dict. This function is similar to the function in player.py but more optimized @@ -486,8 +489,7 @@ class Skin: # wins/losses # kills/deaths # duel/dm/tdm/ctf elo + rank - - player_id = player.player_id + player = DBSession.query(Player).filter(Player.player_id == player_id).one() games_played = DBSession.query( Game.game_type_cd, func.count(), func.sum(PlayerGameStat.alivetime)).\