From 79a99594f67a6484ea3ea9c0cb82ef0978293184 Mon Sep 17 00:00:00 2001 From: remoteeng00 Date: Fri, 17 Feb 2023 02:52:48 +0700 Subject: [PATCH] Add validation rules to MostRecentDateHandler --- config_web/genomics.py | 8 +++ web/handlers/v2/genomics/__init__.py | 3 +- web/handlers/v2/genomics/most_recent_date.py | 54 +++++++++++--------- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/config_web/genomics.py b/config_web/genomics.py index adf0eef1..4be9ba9b 100644 --- a/config_web/genomics.py +++ b/config_web/genomics.py @@ -23,6 +23,14 @@ r"/{pre}/{ver}/prevalence-by-position", "web.handlers.v2.genomics.PrevalenceByAAPositionHandler", ), + ( + r"/{pre}/{ver}/most-recent-collection-date-by-location", + "web.handlers.v2.genomics.MostRecentCollectionDateHandler", + ), + ( + r"/{pre}/{ver}/most-recent-submission-date-by-location", + "web.handlers.v2.genomics.MostRecentSubmissionDateHandler", + ), ] APP_LIST_SWITCHED_TO_V2 = [ diff --git a/web/handlers/v2/genomics/__init__.py b/web/handlers/v2/genomics/__init__.py index 41807705..7adc09b7 100644 --- a/web/handlers/v2/genomics/__init__.py +++ b/web/handlers/v2/genomics/__init__.py @@ -2,6 +2,7 @@ from .lineage import LineageHandler from .lineage_mutations import LineageMutationsHandler from .location import LocationHandler -from .prevalence_all_lineages_by_location import PrevalenceAllLineagesByLocationHandler from .prevalence_by_aa_position import PrevalenceByAAPositionHandler +from .most_recent_date import MostRecentCollectionDateHandler, MostRecentSubmissionDateHandler +from .prevalence_all_lineages_by_location import PrevalenceAllLineagesByLocationHandler from .prevalence_by_location_and_time import PrevalenceByLocationAndTimeHandler diff --git a/web/handlers/v2/genomics/most_recent_date.py b/web/handlers/v2/genomics/most_recent_date.py index dec3105a..35f86162 100644 --- a/web/handlers/v2/genomics/most_recent_date.py +++ b/web/handlers/v2/genomics/most_recent_date.py @@ -1,30 +1,38 @@ +import pandas as pd + +from web.handlers.genomics.base import BaseHandler +from web.handlers.genomics.util import create_nested_mutation_query + class MostRecentDateHandler(BaseHandler): field = "date_collected" + name = "most-recent-date" + kwargs = dict(BaseHandler.kwargs) + kwargs["GET"] = { + "pangolin_lineage": {"type": str, "default": None}, + "mutations": {"type": str, "default": None}, + "location_id": {"type": str, "default": None}, + } - @gen.coroutine - def _get(self): - query_pangolin_lineage = self.get_argument("pangolin_lineage", None) - query_location = self.get_argument("location_id", None) - query_mutations = self.get_argument("mutations", None) + async def _get(self): + query_pangolin_lineage = self.args.pangolin_lineage + query_location = self.args.location_id + query_mutations = self.args.mutations query_mutations = query_mutations.split(",") if query_mutations is not None else [] query = { "size": 0, "query": {}, - "aggs": { - "date_collected": { - "terms": { - "field": self.field, - "size": 10000 - } - } - } + "aggs": {"date_collected": {"terms": {"field": self.field, "size": 10000}}}, } - query_pangolin_lineage = query_pangolin_lineage.split(",") if query_pangolin_lineage is not None else [] - query_obj = create_nested_mutation_query(lineages = query_pangolin_lineage, mutations = query_mutations, location_id = query_location) + query_pangolin_lineage = ( + query_pangolin_lineage.split(",") if query_pangolin_lineage is not None else [] + ) + query_obj = create_nested_mutation_query( + lineages=query_pangolin_lineage, mutations=query_mutations, location_id=query_location + ) query["query"] = query_obj - resp = yield self.asynchronous_fetch(query) - #print(resp) + resp = await self.asynchronous_fetch(query) + # print(resp) path_to_results = ["aggregations", "date_collected", "buckets"] buckets = resp for i in path_to_results: @@ -35,15 +43,12 @@ def _get(self): for i in buckets: if len(i["key"].split("-")) == 1 or "XX" in i["key"]: continue - flattened_response.append({ - "date": i["key"], - "date_count": i["doc_count"] - }) + flattened_response.append({"date": i["key"], "date_count": i["doc_count"]}) df_response = ( pd.DataFrame(flattened_response) .assign( - date = lambda x: pd.to_datetime(x["date"], format="%Y-%m-%d"), - date_count = lambda x: x["date_count"].astype(int) + date=lambda x: pd.to_datetime(x["date"], format="%Y-%m-%d"), + date_count=lambda x: x["date_count"].astype(int), ) .sort_values("date") ) @@ -57,6 +62,9 @@ def _get(self): class MostRecentCollectionDateHandler(MostRecentDateHandler): field = "date_collected" + name = "most-recent-collection-date-by-location" + class MostRecentSubmissionDateHandler(MostRecentDateHandler): field = "date_submitted" + name = "most-recent-submission-date-by-location"