From dd8f2754e9f803427ccdd74f8b888f3a835ffd66 Mon Sep 17 00:00:00 2001 From: norohind <60548839+norohind@users.noreply.github.com> Date: Mon, 17 Jan 2022 01:26:01 +0300 Subject: [PATCH] web: optimization --- model/postgres_model.py | 15 +++++++++++++++ web.py | 6 +++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/model/postgres_model.py b/model/postgres_model.py index 449b4fe..5a5abf6 100644 --- a/model/postgres_model.py +++ b/model/postgres_model.py @@ -1,5 +1,6 @@ import json import typing +import datetime import psycopg2.extensions import psycopg2.extras @@ -63,6 +64,20 @@ class PostgresModel(AbstractModel): logger.debug(f'Not cached result for {cache_key}') + if low_timestamp is None and high_timestamp is None and limit is None: + # let's apply an optimization: use yesterday's date as minimal date + high_timestamp = '3307-12-12' + low_timestamp = (datetime.date.today() - datetime.timedelta(days=1)).strftime('%Y-%m-%d') + + if low_timestamp is None: + low_timestamp = '0001-01-01' + + if high_timestamp is None: + high_timestamp = '3307-12-12' + + if limit is None: + limit = 10 + with self.db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cursor: cursor.execute(postgres_sql_requests.select_activity_pretty_names, { 'LB_type': utils.LeaderboardTypes(leaderboard_type.lower()).value, diff --git a/web.py b/web.py index 87c4e2c..32eb502 100644 --- a/web.py +++ b/web.py @@ -38,9 +38,9 @@ class Activity: args_activity_changes = { 'platform': platform, 'leaderboard_type': leaderboard, - 'limit': req.params.get('limit', 10), - 'high_timestamp': req.params.get('before', '3307-12-12'), - 'low_timestamp': req.params.get('after', '0001-01-01') + 'limit': req.params.get('limit', None), + 'high_timestamp': req.params.get('before', None), + 'low_timestamp': req.params.get('after', None) } try: