From c5ae70a5f8fc34f78d2a9b93382d00a823cef375 Mon Sep 17 00:00:00 2001 From: norohind <60548839+norohind@users.noreply.github.com> Date: Mon, 31 Jan 2022 18:21:29 +0300 Subject: [PATCH] make pretty keys optional --- model/postgres_sql_requests.py | 24 ++++++++--------- templates/json2html_template.html | 18 ++++++++++--- web.py | 45 ++++++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 19 deletions(-) diff --git a/model/postgres_sql_requests.py b/model/postgres_sql_requests.py index 0c63dc5..eb7da05 100644 --- a/model/postgres_sql_requests.py +++ b/model/postgres_sql_requests.py @@ -26,11 +26,11 @@ values (%(action_id)s, %(LB_type)s, %(platform)s, %(squadron)s, %(score)s, %(percentile)s, %(rank)s, %(name)s, %(tag)s);""" select_activity_pretty_names = """select -sum_score::bigint as "TotalExperience", -to_char(timestamp, 'YYYY-MM-DD HH24:MI:SS') as "Timestamp UTC", -action_id::bigint as "ActionId", -sum_score_old::bigint as "TotalExperienceOld", -(sum_score - sum_score_old)::bigint as "Diff" +sum_score::bigint as "sum_score", +to_char(timestamp, 'YYYY-MM-DD HH24:MI:SS') as "timestamp", +action_id::bigint as "action_id", +sum_score_old::bigint as "sum_score_old", +(sum_score - sum_score_old)::bigint as "diff" from ( select @@ -56,13 +56,13 @@ where (sum_score - sum_score_old) <> 0 limit %(limit)s;""" select_diff_by_action_id = """select - coalesce(new_stats.name, old_stats.name) as "SquadronName", - coalesce(new_stats.tag, old_stats.tag) as "Tag", - coalesce(new_stats.score, 0) as "TotalExperience", - coalesce(old_stats.score, 0) as "TotalExperienceOld", - coalesce(new_stats.score, 0) - coalesce(old_stats.score, 0) as "TotalExperienceDiff", - coalesce(new_stats.leaderboard_type, old_stats.leaderboard_type) as "LeaderBoardType", - coalesce(new_stats.platform, old_stats.platform) as "Platform" + coalesce(new_stats.name, old_stats.name) as "squadron_name", + coalesce(new_stats.tag, old_stats.tag) as "tag", + coalesce(new_stats.score, 0) as "total_experience", + coalesce(old_stats.score, 0) as "total_experience_old", + coalesce(new_stats.score, 0) - coalesce(old_stats.score, 0) as "total_experience_diff", + coalesce(new_stats.leaderboard_type, old_stats.leaderboard_type) as "leaderboard_type", + coalesce(new_stats.platform, old_stats.platform) as "platform" from ( select * from squads_stats_states diff --git a/templates/json2html_template.html b/templates/json2html_template.html index 0c50be3..c9a0993 100644 --- a/templates/json2html_template.html +++ b/templates/json2html_template.html @@ -1,14 +1,26 @@ - \ No newline at end of file diff --git a/web.py b/web.py index 32eb502..0ac27dd 100644 --- a/web.py +++ b/web.py @@ -25,6 +25,32 @@ platform - one of PC """ +keys_mapping = { + 'sum_score': 'TotalExperience', + 'timestamp': 'Timestamp UTC', + 'action_id': 'Action ID', + 'sum_score_old': 'Total Experience Old', + 'diff': 'Diff', + 'squadron_name': 'Squadron Name', + 'tag': 'Tag', + 'total_experience': 'Total Experience', + 'total_experience_old': 'Total Experience Old', + 'total_experience_diff': 'Total Experience Diff', + 'leaderboard_type': 'Leaderboard Type', + 'platform': 'Platform' +} + + +def prettify_keys(rows: list[dict]) -> list[dict]: + for row in rows: + for key in list(row.keys()): + pretty_key = keys_mapping.get(key) + if pretty_key is not None: + row[pretty_key] = row.pop(key) + + return rows + + logger = get_main_logger() model.open_model() @@ -43,8 +69,14 @@ class Activity: 'low_timestamp': req.params.get('after', None) } + pretty_keys: bool = req.params.get('pretty_keys', 'false').lower() == 'true' + try: - resp.text = json.dumps(model.get_activity_changes(**args_activity_changes)) + model_response: list[dict] = model.get_activity_changes(**args_activity_changes) + if pretty_keys: + model_response = prettify_keys(model_response) + + resp.text = json.dumps(model_response) except Exception as e: logger.warning( @@ -62,7 +94,7 @@ class ActivityHtml: resp.text = render( 'table_template.html', { - 'target_column_name': 'ActionId', + 'target_column_name': keys_mapping['action_id'], 'target_new_url': '/diff/', }) @@ -79,7 +111,12 @@ class ActivityDiff: """ resp.content_type = falcon.MEDIA_JSON - resp.text = json.dumps(model.get_diff_action_id(action_id)) + pretty_keys: bool = req.params.get('pretty_keys', 'false').lower() == 'true' + model_response = model.get_diff_action_id(action_id) + if pretty_keys: + model_response = prettify_keys(model_response) + + resp.text = json.dumps(model_response) class ActivityDiffHtml: @@ -135,7 +172,7 @@ class LeaderboardByActionIDHTML: resp.text = render( 'table_template.html', { - 'target_column_name': 'Tag', + 'target_column_name': keys_mapping['tag'], 'target_new_url': '/api/leaderboard-state/by-action-id/' } )