diff --git a/model/__init__.py b/model/__init__.py index 6e63bce..fe00d9f 100644 --- a/model/__init__.py +++ b/model/__init__.py @@ -13,3 +13,4 @@ if config.log_level == 'DEBUG': model.get_activity_changes = utils.measure(model.get_activity_changes, 'model.get_activity_changes') model.get_leaderboard_sum_history = utils.measure(model.get_leaderboard_sum_history, 'model.get_leaderboard_sum_history') + model.get_latest_leaderboard = utils.measure(model.get_latest_leaderboard, 'model.get_latest_leaderboard') diff --git a/model/abstract_model.py b/model/abstract_model.py index e7a692f..cb03d81 100644 --- a/model/abstract_model.py +++ b/model/abstract_model.py @@ -25,12 +25,12 @@ class AbstractModel(abc.ABC): raise NotImplemented @abc.abstractmethod - def get_leaderboard_sum_history(self, platform: str, leaderboard_type: str) -> list[dict]: + def get_leaderboard_sum_history(self, platform: str, leaderboard_type: str) -> list: raise NotImplemented @abc.abstractmethod - def get_leaderboard_by_action_id(self, action_id: int) -> list[dict]: + def get_leaderboard_by_action_id(self, action_id: int) -> list: raise NotImplemented - def get_latest_leaderboard(self, platform: str, leaderboard_type: str) -> list[dict]: + def get_latest_leaderboard(self, platform: str, leaderboard_type: str) -> list: raise NotImplemented diff --git a/model/postgres_model.py b/model/postgres_model.py index 07e6bae..36ca47e 100644 --- a/model/postgres_model.py +++ b/model/postgres_model.py @@ -36,6 +36,10 @@ def errors_catcher(func: callable) -> callable: return decorated +def get_header_row(description: tuple) -> list[str]: + return [column.name for column in description] + + class PostgresModel(AbstractModel): db: psycopg2.extensions.connection @@ -84,16 +88,18 @@ class PostgresModel(AbstractModel): if limit is None: limit = 10 - with self.db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cursor: + with self.db.cursor() as cursor: cursor.execute(postgres_sql_requests.select_activity_pretty_names, { 'LB_type': utils.LeaderboardTypes(leaderboard_type.lower()).value, 'platform': utils.Platform(platform.upper()).value, 'limit': limit, 'high_timestamp': high_timestamp, 'low_timestamp': low_timestamp - }) + } + ) result: list = cursor.fetchall() + result.insert(0, get_header_row(cursor.description)) if not cache.disabled: cache.set(cache_key, json.dumps(result)) @@ -156,9 +162,10 @@ class PostgresModel(AbstractModel): logger.debug(f'Not cached result for {cache_key}') - with self.db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cursor: + with self.db.cursor(cursor_factory=psycopg2.extras.DictCursor) as cursor: cursor.execute(postgres_sql_requests.select_diff_by_action_id, {'action_id': action_id}) result: list = cursor.fetchall() + result.insert(0, get_header_row(cursor.description)) if not cache.disabled: cache.set(cache_key, json.dumps(result)) @@ -166,7 +173,7 @@ class PostgresModel(AbstractModel): return result @errors_catcher - def get_leaderboard_sum_history(self, platform: str, leaderboard_type: str) -> list[dict]: + def get_leaderboard_sum_history(self, platform: str, leaderboard_type: str) -> list: cache_key = f'sum_history_{platform}_{leaderboard_type}' cached_result: typing.Union[str, None] = cache.get(cache_key) @@ -174,14 +181,17 @@ class PostgresModel(AbstractModel): logger.debug(f'Cached result for {cache_key}') return json.loads(cached_result) - with self.db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cursor: + with self.db.cursor() as cursor: cursor.execute( postgres_sql_requests.select_leaderboard_sum_history, { 'LB_type': utils.LeaderboardTypes(leaderboard_type.lower()).value, - 'platform': utils.Platform(platform.upper()).value} + 'platform': utils.Platform(platform.upper()).value + } ) + result: list = cursor.fetchall() + result.insert(0, get_header_row(cursor.description)) if not cache.disabled: cache.set(cache_key, json.dumps(result)) @@ -189,7 +199,7 @@ class PostgresModel(AbstractModel): return result @errors_catcher - def get_leaderboard_by_action_id(self, action_id: int) -> list[dict]: + def get_leaderboard_by_action_id(self, action_id: int) -> list: cache_key = f'leaderboard_by_action_id_{action_id}' cached_result: typing.Union[str, None] = cache.get(cache_key) @@ -198,7 +208,7 @@ class PostgresModel(AbstractModel): logger.debug(f'Cached result for {cache_key}') return json.loads(cached_result) - with self.db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cursor: + with self.db.cursor() as cursor: cursor.execute( postgres_sql_requests.select_leaderboard_by_action_id, { @@ -206,7 +216,8 @@ class PostgresModel(AbstractModel): } ) - result: list[dict] = cursor.fetchall() + result: list = cursor.fetchall() + result.insert(0, get_header_row(cursor.description)) if not cache.disabled: cache.set(cache_key, json.dumps(result)) @@ -214,7 +225,7 @@ class PostgresModel(AbstractModel): return result @errors_catcher - def get_latest_leaderboard(self, platform: str, leaderboard_type: str) -> list[dict]: + def get_latest_leaderboard(self, platform: str, leaderboard_type: str) -> list: cache_key = f'latest_leaderboard_{platform}_{leaderboard_type}' cached_result: typing.Union[str, None] = cache.get(cache_key) @@ -224,7 +235,7 @@ class PostgresModel(AbstractModel): logger.debug(f'Not cached result for {cache_key}') - with self.db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cursor: + with self.db.cursor() as cursor: cursor.execute( postgres_sql_requests.select_latest_leaderboard, { @@ -232,7 +243,9 @@ class PostgresModel(AbstractModel): 'LB_type': leaderboard_type.lower() } ) - result: list[dict] = cursor.fetchall() + + result: list = cursor.fetchall() + result.insert(0, get_header_row(cursor.description)) if not cache.disabled: cache.set(cache_key, json.dumps(result)) diff --git a/templates/json2html_template.html b/templates/json2html_template.html index c9a0993..705fec3 100644 --- a/templates/json2html_template.html +++ b/templates/json2html_template.html @@ -1,52 +1,4 @@ \ No newline at end of file diff --git a/templates/table_builder.js b/templates/table_builder.js new file mode 100644 index 0000000..fa52ebb --- /dev/null +++ b/templates/table_builder.js @@ -0,0 +1,51 @@ +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'); + if (xhr.status != 200) { + table.innerHTML = 'Error: ' + xhr.status + '; ' + xhr.response; + } else { + table.innerHTML = ''; + } + + table_in_json = JSON.parse(xhr.response); + + table.appendChild(buildHtmlTable(table_in_json)); // build table + + var table = document.querySelector("#table0div > table") + var header = table.rows[0] + + for (var i = 0, cell; cell = header.cells[i]; i++) { + if (cell.innerText.includes('{{ target_column_name }}')) { + var target_column_id = i; + break; + } + } + + if (target_column_id == null) { // don't to anything if no action_id in the table + return; + } + + for (var i = 1, row; row = table.rows[i]; i++) { // append to target column filed href + row.cells[target_column_id].innerHTML = '