-
Notifications
You must be signed in to change notification settings - Fork 16
using epidata metadata to determine stalenes for nhsn pulling #2142
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
Open
aysim319
wants to merge
6
commits into
main
Choose a base branch
from
2137-add-more-buffer-for-updates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
44974ac
moved time diff into constants and extended to 36 hours
aysim319 7b419b0
added comment
aysim319 2f2f63a
working on converting into api calls instead of time diff
aysim319 8b6a0e6
finished converting raw time diff to using api
aysim319 40d6cd2
forgot to mock in test_run
aysim319 9c9b550
forgot to mock in other tests in test_pull
aysim319 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,16 +4,25 @@ | |||||||||
import logging | ||||||||||
import random | ||||||||||
import time | ||||||||||
from datetime import datetime, timedelta | ||||||||||
from datetime import datetime, timedelta, timezone | ||||||||||
from pathlib import Path | ||||||||||
from typing import Optional | ||||||||||
from urllib.error import HTTPError | ||||||||||
|
||||||||||
import pandas as pd | ||||||||||
from delphi_epidata import Epidata | ||||||||||
from delphi_utils import create_backup_csv | ||||||||||
from sodapy import Socrata | ||||||||||
|
||||||||||
from .constants import MAIN_DATASET_ID, PRELIM_DATASET_ID, PRELIM_SIGNALS_MAP, PRELIM_TYPE_DICT, SIGNALS_MAP, TYPE_DICT | ||||||||||
from .constants import ( | ||||||||||
MAIN_DATASET_ID, | ||||||||||
PRELIM_DATASET_ID, | ||||||||||
PRELIM_SIGNALS_MAP, | ||||||||||
PRELIM_TYPE_DICT, | ||||||||||
RECENTLY_UPDATED_DIFF, | ||||||||||
SIGNALS_MAP, | ||||||||||
TYPE_DICT, | ||||||||||
) | ||||||||||
|
||||||||||
|
||||||||||
def check_last_updated(socrata_token, dataset_id, logger): | ||||||||||
|
@@ -38,17 +47,31 @@ def check_last_updated(socrata_token, dataset_id, logger): | |||||||||
client = Socrata("data.cdc.gov", socrata_token) | ||||||||||
response = client.get_metadata(dataset_id) | ||||||||||
|
||||||||||
updated_timestamp = datetime.utcfromtimestamp(int(response["rowsUpdatedAt"])) | ||||||||||
now = datetime.utcnow() | ||||||||||
recently_updated_source = (now - updated_timestamp) < timedelta(days=1) | ||||||||||
updated_timestamp = datetime.fromtimestamp(int(response["rowsUpdatedAt"]), tz=timezone.utc) | ||||||||||
|
||||||||||
# pulling last updated from the api | ||||||||||
meta_df = pd.DataFrame(Epidata.covidcast_meta()["epidata"]) | ||||||||||
signal_suffix = "prelim" if dataset_id == PRELIM_DATASET_ID else "ew" | ||||||||||
nhsn_meta_df = meta_df[(meta_df["data_source"] == "nhsn") & (meta_df["signal"].str.endswith(signal_suffix))] | ||||||||||
est = timezone(timedelta(hours=-5)) | ||||||||||
last_updated = datetime.fromtimestamp(nhsn_meta_df["last_update"].min(), tz=est) | ||||||||||
|
||||||||||
# currently set to run twice a week, RECENTLY_UPDATED_DIFF may need adjusting based on the cadence | ||||||||||
recently_updated_source = (updated_timestamp - last_updated) > RECENTLY_UPDATED_DIFF | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i dont think this math is quite right... why wouldnt we want to proceed any time socrata has a newer timestamp than we do? the form you have here has the potential to delay processing if updates are frequent enough or if
Suggested change
|
||||||||||
|
||||||||||
prelim_prefix = "Preliminary " if dataset_id == PRELIM_DATASET_ID else "" | ||||||||||
if recently_updated_source: | ||||||||||
logger.info( | ||||||||||
f"{prelim_prefix}NHSN data was recently updated; Pulling data", updated_timestamp=updated_timestamp | ||||||||||
f"{prelim_prefix}NHSN data was recently updated; Pulling data", | ||||||||||
updated_timestamp=updated_timestamp, | ||||||||||
metadata_timestamp=last_updated, | ||||||||||
) | ||||||||||
else: | ||||||||||
logger.info(f"{prelim_prefix}NHSN data is stale; Skipping", updated_timestamp=updated_timestamp) | ||||||||||
logger.info( | ||||||||||
f"{prelim_prefix}NHSN data is stale; Skipping", | ||||||||||
updated_timestamp=updated_timestamp, | ||||||||||
metadata_timestamp=last_updated, | ||||||||||
) | ||||||||||
# pylint: disable=W0703 | ||||||||||
except Exception as e: | ||||||||||
logger.info("error while processing socrata metadata; treating data as stale", error=str(e)) | ||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is gonna have issues because of DST changes, however this timestamp should be for UTC already.
it shouldnt make too much of a difference because the probability of it biting us should be rare, but i think youll also want a max instead of a min (in case we change signal names or discontinue signals, among other things).