From 7ff01015ae1482bdf14028ac85727033ef6b32fc Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Thu, 4 Aug 2022 12:00:51 +0200 Subject: [PATCH] feat(chalice): refactored errors search --- api/chalicelib/core/errors.py | 19 ++----------------- api/chalicelib/core/sessions.py | 10 +++------- api/schemas.py | 4 ++-- 3 files changed, 7 insertions(+), 26 deletions(-) diff --git a/api/chalicelib/core/errors.py b/api/chalicelib/core/errors.py index a5ad62ea7..e72842906 100644 --- a/api/chalicelib/core/errors.py +++ b/api/chalicelib/core/errors.py @@ -545,8 +545,7 @@ def search(data: schemas.SearchErrorsSchema, project_id, user_id, flows=False): else: if len(statuses) == 0: query = cur.mogrify( - """SELECT error_id, status, parent_error_id, payload, - FALSE AS favorite, + """SELECT error_id, COALESCE((SELECT TRUE FROM public.user_viewed_errors AS ve WHERE errors.error_id = ve.error_id @@ -564,26 +563,12 @@ def search(data: schemas.SearchErrorsSchema, project_id, user_id, flows=False): for r in rows: r.pop("full_count") if r["error_id"] in statuses: - r["status"] = statuses[r["error_id"]]["status"] - r["parent_error_id"] = statuses[r["error_id"]]["parentErrorId"] - r["favorite"] = statuses[r["error_id"]]["favorite"] r["viewed"] = statuses[r["error_id"]]["viewed"] - r["stack"] = format_first_stack_frame(statuses[r["error_id"]])["stack"] else: - r["status"] = "untracked" - r["parent_error_id"] = None - r["favorite"] = False r["viewed"] = False - r["stack"] = None - offset = len(rows) - rows = [r for r in rows if r["stack"] is None - or (len(r["stack"]) == 0 or len(r["stack"]) > 1 - or len(r["stack"]) > 0 - and (r["message"].lower() != "script error." or len(r["stack"][0]["absPath"]) > 0))] - offset -= len(rows) return { - 'total': total - offset, + 'total': total, 'errors': helper.list_to_camel_case(rows) } diff --git a/api/chalicelib/core/sessions.py b/api/chalicelib/core/sessions.py index a9c793a6c..1b06274a4 100644 --- a/api/chalicelib/core/sessions.py +++ b/api/chalicelib/core/sessions.py @@ -187,16 +187,12 @@ def search_sessions(data: schemas.SessionsSearchPayloadSchema, project_id, user_ meta_keys = [] with pg_client.PostgresClient() as cur: if errors_only: - main_query = cur.mogrify(f"""SELECT DISTINCT er.error_id, ser.status, ser.parent_error_id, ser.payload, - COALESCE((SELECT TRUE - FROM public.user_favorite_sessions AS fs - WHERE s.session_id = fs.session_id - AND fs.user_id = %(userId)s), FALSE) AS favorite, - COALESCE((SELECT TRUE + main_query = cur.mogrify(f"""SELECT DISTINCT er.error_id, + COALESCE((SELECT TRUE FROM public.user_viewed_errors AS ve WHERE er.error_id = ve.error_id AND ve.user_id = %(userId)s LIMIT 1), FALSE) AS viewed - {query_part};""", full_args) + {query_part};""", full_args) elif count_only: main_query = cur.mogrify(f"""SELECT COUNT(DISTINCT s.session_id) AS count_sessions, diff --git a/api/schemas.py b/api/schemas.py index 34399af67..644df328d 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -734,7 +734,7 @@ class ErrorSort(str, Enum): sessions_count = 'sessions' -class SearchErrorsSchema(SessionsSearchPayloadSchema): +class SearchErrorsSchema(FlatSessionsSearchPayloadSchema): sort: ErrorSort = Field(default=ErrorSort.occurrence) density: Optional[int] = Field(7) status: Optional[ErrorStatus] = Field(default=ErrorStatus.all) @@ -766,7 +766,7 @@ class MobileSignPayloadSchema(BaseModel): keys: List[str] = Field(...) -class CustomMetricSeriesFilterSchema(FlatSessionsSearchPayloadSchema, SearchErrorsSchema): +class CustomMetricSeriesFilterSchema(SearchErrorsSchema): startDate: Optional[int] = Field(None) endDate: Optional[int] = Field(None) sort: Optional[str] = Field(None)