Skip to content

Commit

Permalink
Add validation rules to MostRecentDateHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
remoteeng00 committed Feb 16, 2023
1 parent 27ebdbe commit 778b749
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
8 changes: 8 additions & 0 deletions config_web/genomics.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
r"/{pre}/{ver}/prevalence-by-location-all-lineages",
"web.handlers.v2.genomics.PrevalenceAllLineagesByLocationHandler",
),
(
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 = [
Expand Down
3 changes: 2 additions & 1 deletion web/handlers/v2/genomics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
from .lineage import LineageHandler
from .lineage_mutations import LineageMutationsHandler
from .location import LocationHandler
from .prevalence_by_location_and_time import PrevalenceByLocationAndTimeHandler
from .most_recent_date import MostRecentCollectionDateHandler, MostRecentSubmissionDateHandler
from .prevalence_all_lineages_by_location import PrevalenceAllLineagesByLocationHandler
from .prevalence_by_location_and_time import PrevalenceByLocationAndTimeHandler
54 changes: 31 additions & 23 deletions web/handlers/v2/genomics/most_recent_date.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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")
)
Expand All @@ -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"

0 comments on commit 778b749

Please sign in to comment.