mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-22 16:06:19 -05:00
Merge pull request #107 from lrnselfreliance/feature/download-display-speedup
Using custom sql to speed up download query.
This commit is contained in:
commit
3011535a17
3 changed files with 64 additions and 9 deletions
|
|
@ -97,6 +97,12 @@ def from_timestamp(timestamp: float) -> datetime:
|
|||
return local_timezone(datetime.fromtimestamp(timestamp))
|
||||
|
||||
|
||||
def recursive_replace_tz(obj, tz=pytz.utc):
|
||||
"""Recursively replace the timezone of any datetimes."""
|
||||
from wrolpi.common import recursive_map
|
||||
return recursive_map(obj, lambda i: i.replace(tzinfo=tz) if isinstance(i, datetime) else i)
|
||||
|
||||
|
||||
class TZDateTime(types.TypeDecorator):
|
||||
impl = types.DateTime
|
||||
cache_ok = True
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ from sqlalchemy.dialects.postgresql import JSONB
|
|||
from sqlalchemy.orm import Session
|
||||
|
||||
from wrolpi.common import Base, ModelHelper, logger, iterify, wrol_mode_check, zig_zag
|
||||
from wrolpi.dates import TZDateTime, now, Seconds, local_timezone
|
||||
from wrolpi.dates import TZDateTime, now, Seconds, local_timezone, recursive_replace_tz
|
||||
from wrolpi.db import get_db_session, get_db_curs, get_db_context, optional_session
|
||||
from wrolpi.errors import InvalidDownload, UnrecoverableDownloadError
|
||||
from wrolpi.vars import PYTEST
|
||||
|
|
@ -578,14 +578,63 @@ class DownloadManager:
|
|||
"""
|
||||
return list(filter(lambda i: i.listable, self.instances))
|
||||
|
||||
@optional_session
|
||||
def get_fe_downloads(self, session: Session = None):
|
||||
"""
|
||||
Get downloads for the Frontend.
|
||||
"""
|
||||
# Downloads should be sorted by their status in a particular order.
|
||||
_status_order = '''CASE
|
||||
WHEN (status = 'pending') THEN 0
|
||||
WHEN (status = 'failed') THEN 1
|
||||
WHEN (status = 'new') THEN 2
|
||||
WHEN (status = 'deferred') THEN 3
|
||||
WHEN (status = 'complete') THEN 4
|
||||
END'''
|
||||
|
||||
def get_fe_downloads(self):
|
||||
"""Get downloads for the Frontend."""
|
||||
# Use custom SQL because SQLAlchemy is slow.
|
||||
with get_db_curs() as curs:
|
||||
stmt = f'''
|
||||
SELECT
|
||||
downloader,
|
||||
frequency,
|
||||
id,
|
||||
last_successful_download,
|
||||
next_download,
|
||||
status,
|
||||
url
|
||||
FROM download
|
||||
WHERE frequency IS NOT NULL
|
||||
ORDER BY
|
||||
{self._status_order},
|
||||
next_download,
|
||||
frequency
|
||||
'''
|
||||
curs.execute(stmt)
|
||||
recurring_downloads = list(map(dict, curs.fetchall()))
|
||||
recurring_downloads = recursive_replace_tz(recurring_downloads)
|
||||
|
||||
stmt = f'''
|
||||
SELECT
|
||||
downloader,
|
||||
frequency,
|
||||
id,
|
||||
last_successful_download,
|
||||
next_download,
|
||||
status,
|
||||
url
|
||||
FROM download
|
||||
WHERE frequency IS NULL
|
||||
ORDER BY
|
||||
{self._status_order},
|
||||
last_successful_download DESC,
|
||||
id
|
||||
LIMIT 100
|
||||
'''
|
||||
curs.execute(stmt)
|
||||
once_downloads = list(map(dict, curs.fetchall()))
|
||||
once_downloads = recursive_replace_tz(once_downloads)
|
||||
|
||||
data = dict(
|
||||
recurring_downloads=self.get_recurring_downloads(session=session),
|
||||
once_downloads=self.get_once_downloads(session=session, limit=100),
|
||||
recurring_downloads=recurring_downloads,
|
||||
once_downloads=once_downloads,
|
||||
)
|
||||
return data
|
||||
|
||||
|
|
|
|||
|
|
@ -178,4 +178,4 @@ def assert_dict_contains(d1: dict, d2: dict):
|
|||
if isinstance(d1[k2], dict):
|
||||
assert_dict_contains(d1[k2], d2[k2])
|
||||
else:
|
||||
assert d1[k2] == d2[k2], f'{k2} of value "{d1[k2]}" does not equal {d2[k2]} in dict 1'
|
||||
assert d1[k2] == d2[k2], f'{k2} of value {d1[k2]} does not equal {d2[k2]} in dict 1'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue