From cc9c34d002891a4fdb916f40b41fabb7d4e4a6df Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Mon, 5 Dec 2011 12:10:18 -0500 Subject: [PATCH] Save the match_id as game_meta, and use it to keep games unique. POST requests have a "match ID" parameter being sent as an "I" record within the header. This should be stored in the games table accordingly. Furthermore, a unique constraint should be placed on the games table such that all combinations of server_id and match_id are unique in that table. This prevents duplicate games from being entered by either manual means or via xonstat-queue. This change stores the 'I' record in the games table upon submission and will raise an HTTP 200 exception class if a duplicate entry is found. It is set to 200 because this is an OK submission, but a duplicate and thus should not be resubmitted. --- xonstat/views/submission.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/xonstat/views/submission.py b/xonstat/views/submission.py index e0048d6..09c0bb6 100755 --- a/xonstat/views/submission.py +++ b/xonstat/views/submission.py @@ -81,6 +81,7 @@ def has_required_metadata(metadata): if 'T' not in metadata or\ 'G' not in metadata or\ 'M' not in metadata or\ + 'I' not in metadata or\ 'S' not in metadata: flg_has_req_metadata = False @@ -215,7 +216,7 @@ def get_or_create_map(session=None, name=None): def create_game(session=None, start_dt=None, game_type_cd=None, - server_id=None, map_id=None, winner=None): + server_id=None, map_id=None, winner=None, match_id=None): """ Creates a game. Parameters: @@ -230,10 +231,20 @@ def create_game(session=None, start_dt=None, game_type_cd=None, game_id = session.execute(seq) game = Game(game_id=game_id, start_dt=start_dt, game_type_cd=game_type_cd, server_id=server_id, map_id=map_id, winner=winner) - session.add(game) - log.debug("Created game id {0} on server {1}, map {2} at \ - {3}".format(game.game_id, - server_id, map_id, start_dt)) + game.match_id = match_id + + try: + session.query(Game).filter(Game.server_id==server_id).\ + filter(Game.match_id==match_id).one() + # if a game under the same server and match_id found, + # this is a duplicate game and can be ignored + raise pyramid.httpexceptions.HTTPOk + except NoResultFound, e: + # server_id/match_id combination not found. game is ok to insert + session.add(game) + log.debug("Created game id {0} on server {1}, map {2} at \ + {3}".format(game.game_id, + server_id, map_id, start_dt)) return game @@ -437,7 +448,7 @@ def parse_body(request): if key in 'S' 'n': value = unicode(value, 'utf-8') - if key in 'V' 'T' 'G' 'M' 'S' 'C' 'R' 'W': + if key in 'V' 'T' 'G' 'M' 'S' 'C' 'R' 'W' 'I': game_meta[key] = value if key == 'P': @@ -525,7 +536,7 @@ def stats_submit(request): start_dt=datetime.datetime( *time.gmtime(float(game_meta['T']))[:6]), server_id=server.server_id, game_type_cd=game_meta['G'], - map_id=gmap.map_id) + map_id=gmap.map_id, match_id=game_meta['I']) log.debug(gmap) # find or create a record for each player -- 2.39.2