1
0
mirror of https://github.com/norohind/SquadsActivityMonitor.git synced 2025-04-10 04:00:00 +03:00

make pretty keys optional

This commit is contained in:
norohind 2022-01-31 18:21:29 +03:00
parent b9cab7f667
commit c5ae70a5f8
Signed by: norohind
GPG Key ID: 01C3BECC26FB59E1
3 changed files with 68 additions and 19 deletions

@ -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

@ -1,14 +1,26 @@
<script src="/js/json2htmltable.js"></script>
<script type="text/javascript">
<script type="text/javascript">
window.addEventListener("load", () => {
let xhr = new XMLHttpRequest();
let api_endpoint = location.protocol + '//' + location.host + '/api' + location.pathname + location.search;
if (location.search.substring(0, 1) == '?') { // params already exists
api_endpoint = api_endpoint + '&pretty_keys=true';
} else {
api_endpoint = api_endpoint + '?pretty_keys=true';
}
table = document.getElementById('table0div');
xhr.open('GET', api_endpoint)
xhr.onload = function() {
table = document.getElementById('table0div');
table.innerHTML = '';
if (xhr.status != 200) {
table.innerHTML = 'Error: ' + xhr.status + '; ' + xhr.response;
} else {
table.innerHTML = '';
}
table.appendChild(buildHtmlTable(JSON.parse(xhr.response))); // build table
var table = document.querySelector("#table0div > table")
@ -35,6 +47,6 @@
};
xhr.send()
table.innerHTML = 'Loading...';
table.innerHTML = 'Loading: waiting for server response';
})
</script>

45
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/'
}
)