From 4d490b08b35eb17651e093fa9b602e3774672a2c Mon Sep 17 00:00:00 2001 From: schneefux Date: Sun, 26 Feb 2017 19:52:49 +0100 Subject: upsert player stats on implicit update --- api.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'api.py') diff --git a/api.py b/api.py index 5c7528d..7c3bc10 100644 --- a/api.py +++ b/api.py @@ -78,7 +78,10 @@ class Worker(object): # upsert under special conditions data = await srccon.fetchrow( query, object_id, explicit_player) - await self._playerinto(destcon, data, table) + if data["name"] == explicit_player: + await self._playerinto(destcon, data, + table, + True) else: data = await srccon.fetchrow( query, object_id) @@ -87,19 +90,33 @@ class Worker(object): data = await srccon.fetchrow( "DELETE FROM match WHERE id=$1", object_id) - async def _playerinto(self, conn, data, table): - """Insert a player named tuple into a table. - Upserts specific columns.""" + async def _playerinto(self, conn, data, table, do_upsert_date): + """Upserts a player named tuple into a table.""" + if do_upsert_date: + # explicit update for this player -> store + lmcd = data["lastMatchCreatedDate"] + data = dict(data) + del data["lastMatchCreatedDate"] + items = list(data.items()) keys, values = [x[0] for x in items], [x[1] for x in items] placeholders = ["${}".format(i) for i, _ in enumerate(values, 1)] + # upsert all values except lmcd if they are more recent query = """ - INSERT INTO player ("{0}") VALUES ({1}) + INSERT INTO player ("{0}", "lastMatchCreatedDate") + VALUES ({1}, 'epoch'::timestamp) ON CONFLICT("apiId") DO UPDATE SET ("{0}") = ({1}) - WHERE player."lastMatchCreatedDate" < EXCLUDED."lastMatchCreatedDate" + WHERE player.played < EXCLUDED.played """.format( "\", \"".join(keys), ", ".join(placeholders)) - await conn.execute(query, (*data)) + await conn.execute(query, *data.values()) + + if do_upsert_date: + # upsert lmcd because it was an explicit request + await conn.execute(""" + UPDATE player SET "lastMatchCreatedDate"=$1 + WHERE player."lastMatchCreatedDate" < $1 + """, lmcd) async def _into(self, conn, data, table): """Insert a named tuple into a table.""" -- cgit v1.3.1