Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation rules to MostRecentDateHandler #13

Merged
merged 3 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config_web/genomics.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
r"/{pre}/{ver}/location-lookup",
"web.handlers.v2.genomics.LocationDetailsHandler",
),
(
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 = [
Expand Down
2 changes: 2 additions & 0 deletions web/handlers/v2/genomics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .lineage_mutations import LineageMutationsHandler
from .location import LocationHandler
from .prevalence_by_aa_position import PrevalenceByAAPositionHandler
from .most_recent_date import MostRecentCollectionDateHandler, MostRecentSubmissionDateHandler
from .prevalence_by_aa_position import PrevalenceByAAPositionHandler
from .location_details import LocationDetailsHandler
from .mutation_details import MutationDetailsHandler
from .mutations import MutationHandler
Expand Down
70 changes: 70 additions & 0 deletions web/handlers/v2/genomics/most_recent_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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},
}

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}}},
}
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 = await self.asynchronous_fetch(query)
# print(resp)
path_to_results = ["aggregations", "date_collected", "buckets"]
buckets = resp
for i in path_to_results:
buckets = buckets[i]
if len(buckets) == 0:
return {"success": True, "results": []}
flattened_response = []
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"]})
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),
)
.sort_values("date")
)
df_response = df_response.iloc[-1]
df_response.loc["date"] = df_response["date"].strftime("%Y-%m-%d")
df_response.loc["date_count"] = int(df_response["date_count"])
dict_response = df_response.to_dict()
resp = {"success": True, "results": dict_response}
return resp


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"