diff options
author | Bryan Newbold <bnewbold@archive.org> | 2020-08-06 22:49:50 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@archive.org> | 2020-08-06 22:49:50 -0700 |
commit | 34c7b278e29f7f33b11f11bba1c5d640450a09e9 (patch) | |
tree | 586c6013d1b5a286c081dde84cb711511c03fd57 | |
parent | 7fed7d4526305b31c623407dc1cd3f99877ea697 (diff) | |
download | fatcat-scholar-34c7b278e29f7f33b11f11bba1c5d640450a09e9.tar.gz fatcat-scholar-34c7b278e29f7f33b11f11bba1c5d640450a09e9.zip |
report ES API query time as server-timing header
-rw-r--r-- | fatcat_scholar/search.py | 4 | ||||
-rw-r--r-- | fatcat_scholar/web.py | 10 |
2 files changed, 13 insertions, 1 deletions
diff --git a/fatcat_scholar/search.py b/fatcat_scholar/search.py index 1e0e705..891abff 100644 --- a/fatcat_scholar/search.py +++ b/fatcat_scholar/search.py @@ -83,6 +83,7 @@ class FulltextHits(BaseModel): limit: int deep_page_limit: int query_time_ms: int + query_wall_time_ms: int results: List[Any] @@ -248,6 +249,7 @@ def do_fulltext_search( search = search.params(track_total_hits=True) search = search[offset : (offset + limit)] + query_start = datetime.datetime.now() try: resp = search.execute() except elasticsearch.exceptions.RequestError as e: @@ -261,6 +263,7 @@ def do_fulltext_search( # all other errors print("elasticsearch non-200 status code: {}".format(e.info), file=sys.stderr) raise IOError(str(e.info)) + query_delta = datetime.datetime.now() - query_start # convert from objects to python dicts results = [] @@ -315,5 +318,6 @@ def do_fulltext_search( limit=limit, deep_page_limit=deep_page_limit, query_time_ms=int(resp.took), + query_wall_time_ms=int(query_delta.total_seconds() * 1000), results=results, ) diff --git a/fatcat_scholar/web.py b/fatcat_scholar/web.py index 2d2678a..80cc2b1 100644 --- a/fatcat_scholar/web.py +++ b/fatcat_scholar/web.py @@ -6,7 +6,7 @@ So far there are few endpoints, so we just put them all here! import sys import babel.support -from fastapi import FastAPI, APIRouter, Request, Depends +from fastapi import FastAPI, APIRouter, Request, Depends, Response from fastapi.staticfiles import StaticFiles from dynaconf import settings from typing import Optional, Any @@ -154,6 +154,7 @@ async def web_help(request: Request, lang: LangPrefix = Depends(LangPrefix)) -> @web.get("/search", include_in_schema=False) async def web_search( request: Request, + response: Response, query: FulltextQuery = Depends(FulltextQuery), lang: LangPrefix = Depends(LangPrefix), content: ContentNegotiation = Depends(ContentNegotiation), @@ -173,6 +174,12 @@ async def web_search( except IOError as e: search_error = dict(type="backend", message=str(e)) status_code = 500 + + headers = dict() + if hits and hits.query_wall_time_ms: + headers["Server-Timing"] = f'es_wall;desc="Search API Request";dur={hits.query_wall_time_ms}' + if hits.query_time_ms: + headers["Server-Timing"] += f', es;desc="Search Internal Time";dur={hits.query_time_ms}' return i18n_templates[lang.code].TemplateResponse( "search.html", { @@ -183,6 +190,7 @@ async def web_search( "search_error": search_error, "query": query, }, + headers=headers, status_code=status_code, ) |