Only fetching headlines if requested to speed up RPi responses.g

This commit is contained in:
Roland 2023-04-12 18:51:26 -06:00
parent 8faac3f3a1
commit bba6a341b7
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
12 changed files with 80 additions and 38 deletions

View file

@ -64,6 +64,7 @@ async def search_archives(_: Request, body: schema.ArchiveSearchRequest):
limit = archive_limit_limiter(body.limit)
offset = body.offset or 0
file_groups, total = lib.search_archives(search_str, domain, limit, offset, body.order_by, body.tag_names)
file_groups, total = lib.search_archives(search_str, domain, limit, offset, body.order_by, body.tag_names,
body.headline)
ret = dict(file_groups=file_groups, totals=dict(file_groups=total))
return json_response(ret)

View file

@ -375,7 +375,8 @@ ARCHIVE_ORDERS = {
}
def search_archives(search_str: str, domain: str, limit: int, offset: int, order: str, tag_names: List[str]) \
def search_archives(search_str: str, domain: str, limit: int, offset: int, order: str, tag_names: List[str],
headline: bool = False) \
-> Tuple[List[dict], int]:
# Always filter FileGroups to Archives.
wheres = ["fg.model = 'archive'"]
@ -405,6 +406,15 @@ def search_archives(search_str: str, domain: str, limit: int, offset: int, order
params.update(params_)
joins.append(join_)
if headline:
headline = ''',
ts_headline(fg.title, websearch_to_tsquery(%(search_str)s)) AS "title_headline",
ts_headline(fg.b_text, websearch_to_tsquery(%(search_str)s)) AS "b_headline",
ts_headline(fg.c_text, websearch_to_tsquery(%(search_str)s)) AS "c_headline",
ts_headline(fg.d_text, websearch_to_tsquery(%(search_str)s)) AS "d_headline"'''
else:
headline = ''
if domain:
params['domain'] = domain
wheres.append('a.domain_id = (select id from domains where domains.domain = %(domain)s)')
@ -416,11 +426,8 @@ def search_archives(search_str: str, domain: str, limit: int, offset: int, order
stmt = f'''
SELECT
fg.id -- always get `file_group.id` for `handle_file_group_search_results`
{select_columns},
ts_headline(fg.title, websearch_to_tsquery(%(search_str)s)) AS "title_headline",
ts_headline(fg.b_text, websearch_to_tsquery(%(search_str)s)) AS "b_headline",
ts_headline(fg.c_text, websearch_to_tsquery(%(search_str)s)) AS "c_headline",
ts_headline(fg.d_text, websearch_to_tsquery(%(search_str)s)) AS "d_headline"
{select_columns}
{headline}
FROM file_group fg
LEFT JOIN archive a ON a.file_group_id = fg.id
{join}

View file

@ -31,6 +31,7 @@ class ArchiveSearchRequest:
limit: Optional[int] = None
order_by: Optional[str] = None
tag_names: List[str] = field(default_factory=list)
headline: bool = False
@dataclass

View file

@ -145,6 +145,7 @@ class VideoSearchRequest:
order_by: Optional[str] = DEFAULT_VIDEO_ORDER
channel_id: Optional[int] = None
tag_names: List[str] = field(default_factory=list)
headline: bool = False
@dataclass

View file

@ -45,6 +45,7 @@ async def search(_: Request, body: schema.VideoSearchRequest):
body.channel_id,
body.order_by,
body.tag_names,
body.headline,
)
ret = {'file_groups': list(file_groups), 'totals': {'file_groups': videos_total}}

View file

@ -77,6 +77,7 @@ def search_videos(
channel_id: int = None,
order: str = None,
tag_names: List[str] = None,
headline: bool = False,
) -> Tuple[List[dict], int]:
tag_names = tag_names or []
# Only search videos.
@ -106,6 +107,15 @@ def search_videos(
params.update(params_)
joins.append(join_)
if headline:
headline = ''',
ts_headline(fg.title, websearch_to_tsquery(%(search_str)s)) AS "title_headline",
ts_headline(fg.b_text, websearch_to_tsquery(%(search_str)s)) AS "b_headline",
ts_headline(fg.c_text, websearch_to_tsquery(%(search_str)s)) AS "c_headline",
ts_headline(fg.d_text, websearch_to_tsquery(%(search_str)s)) AS "d_headline"'''
else:
headline = ''
# Convert the user-friendly order by into a real order by, restrict what can be interpolated by using the
# whitelist.
order_by = VIDEO_ORDERS[DEFAULT_VIDEO_ORDER]
@ -127,11 +137,8 @@ def search_videos(
join = '\n'.join(joins)
stmt = f'''
SELECT
{select_columns},
ts_headline(fg.title, websearch_to_tsquery(%(search_str)s)) AS "title_headline",
ts_headline(fg.b_text, websearch_to_tsquery(%(search_str)s)) AS "b_headline",
ts_headline(fg.c_text, websearch_to_tsquery(%(search_str)s)) AS "c_headline",
ts_headline(fg.d_text, websearch_to_tsquery(%(search_str)s)) AS "d_headline"
{select_columns}
{headline}
FROM file_group fg
{join}
{where}