WIP: change data format optimize front

This commit is contained in:
norohind 2022-02-04 13:35:03 +03:00
parent 1ecb7b4b26
commit 212d22ebac
Signed by: norohind
GPG Key ID: 01C3BECC26FB59E1
5 changed files with 81 additions and 64 deletions

View File

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

View File

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

View File

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

View File

@ -1,52 +1,4 @@
<script src="/js/json2htmltable.js"></script>
<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');
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")
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 = '<td><a href="' + '{{ target_new_url }}' + table.rows[i].cells[target_column_id].innerText + ' ">' + table.rows[i].cells[target_column_id].innerText + '</a></td>';
}
}
xhr.onerror = function() { // происходит, только когда запрос совсем не получилось выполнить
table.innerHTML = 'Loading Error';
};
xhr.send()
table.innerHTML = 'Loading: waiting for server response';
})
{{ @table_builder.js }}
</script>

View File

@ -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 = '<td><a href="' + '{{ target_new_url }}' + table.rows[i].cells[target_column_id].innerText + ' ">' + table.rows[i].cells[target_column_id].innerText + '</a></td>';
}
}
xhr.onerror = function() { // происходит, только когда запрос совсем не получилось выполнить
table.innerHTML = 'Loading Error';
};
xhr.send()
table.innerHTML = 'Loading: waiting for server response';
})