From 1d6656cccf0de77fa395db76d3eebff50eecb8ec Mon Sep 17 00:00:00 2001 From: norohind <60548839+norohind@users.noreply.github.com> Date: Sun, 21 Nov 2021 13:46:23 +0300 Subject: [PATCH] add diffing feature --- model.py | 13 +++++++++++++ sql_requests.py | 30 ++++++++++++++++++++++++++++++ web.py | 16 ++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/model.py b/model.py index 128ee13..5304ccd 100644 --- a/model.py +++ b/model.py @@ -56,3 +56,16 @@ def insert_leaderboard_db(leaderboard_list: dict) -> None: db.executemany( sql_requests.insert_leader_board, leaderboard) + + +def get_diff_action_id(action_id: int) -> list: + """ + Takes action_id and returns which squadrons has been changed in leaderboard as in action_id and + experience they got in compassion to action_id - 1 for the same leaderboard and platform + + :param action_id: + :return: + """ + + sql_req: sqlite3.Cursor = db.execute(sql_requests.select_diff_by_action_id, {'action_id': action_id}) + return sql_req.fetchall() diff --git a/sql_requests.py b/sql_requests.py index dc1c46b..75f06c1 100644 --- a/sql_requests.py +++ b/sql_requests.py @@ -78,3 +78,33 @@ from ( group by sum_score order by timestamp desc limit :limit);""" + +select_diff_by_action_id = """select + new_stats.tag, + new_stats.score as TotalExperience, + old_stats.score as TotalExperienceOld, + new_stats.score - old_stats.score as TotalExperienceDiff, + new_stats.leaderboard_type as LeaderBoardType, + new_stats.platform as Platform +from ( + select * + from squads_stats_states + where action_id = :action_id) new_stats +inner join + ( + select * + from squads_stats_states + where action_id in ( + select distinct squads_stats_states.action_id + from squads_stats_states, ( + select timestamp, platform, leaderboard_type, action_id + from squads_stats_states + where action_id = :action_id limit 1) sub1 + where + squads_stats_states.platform = sub1.platform and + squads_stats_states.leaderboard_type = sub1.leaderboard_type and + squads_stats_states.action_id < sub1.action_id + order by squads_stats_states.action_id desc + limit 1)) old_stats +on new_stats.squadron_id = old_stats.squadron_id +where TotalExperienceDiff > 0;""" \ No newline at end of file diff --git a/web.py b/web.py index 736ce89..a6ebecb 100644 --- a/web.py +++ b/web.py @@ -40,6 +40,21 @@ class ActivityHtml: # what? f-strings? .format? never heard about them +class ActivityDiff: + def on_get(self, req: falcon.request.Request, resp: falcon.response.Response, action_id: int) -> None: + """ + Give squads tags and diff in their experience for specified action_id - 1 (smart -1) + + :param action_id: + :param req: + :param resp: + :return: + """ + + resp.content_type = falcon.MEDIA_JSON + resp.text = json.dumps(model.get_diff_action_id(action_id)) + + class JS: def on_get(self, req: falcon.request.Request, resp: falcon.response.Response, file: str) -> None: resp.content_type = falcon.MEDIA_JS @@ -57,6 +72,7 @@ app = falcon.App() app.add_route('/activity/{leaderboard}', Activity()) app.add_route('/js/{file}', JS()) app.add_route('/{leaderboard}', ActivityHtml()) +app.add_route('/diff/{action_id}', ActivityDiff()) if __name__ == '__main__': waitress.serve(app, host='127.0.0.1', port=9485)