Skip to content

Commit 44974ac

Browse files
committed
moved time diff into constants and extended to 36 hours
1 parent 3998241 commit 44974ac

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

nhsn/delphi_nhsn/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Registry for signal names."""
22

3+
from datetime import timedelta
4+
35
GEOS = ["state", "nation", "hhs"]
46

57
MAIN_DATASET_ID = "ua7e-t2fy"
@@ -62,3 +64,5 @@
6264
f"{NUM_HOSP_REPORTING_FLU}_prelim": float,
6365
f"{NUM_HOSP_REPORTING_RSV}_prelim": float,
6466
}
67+
68+
RECENTLY_UPDATED_DIFF = timedelta(hours=36)

nhsn/delphi_nhsn/pull.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
import random
66
import time
7-
from datetime import datetime, timedelta
7+
from datetime import datetime
88
from pathlib import Path
99
from typing import Optional
1010
from urllib.error import HTTPError
@@ -13,7 +13,15 @@
1313
from delphi_utils import create_backup_csv
1414
from sodapy import Socrata
1515

16-
from .constants import MAIN_DATASET_ID, PRELIM_DATASET_ID, PRELIM_SIGNALS_MAP, PRELIM_TYPE_DICT, SIGNALS_MAP, TYPE_DICT
16+
from .constants import (
17+
MAIN_DATASET_ID,
18+
PRELIM_DATASET_ID,
19+
PRELIM_SIGNALS_MAP,
20+
PRELIM_TYPE_DICT,
21+
RECENTLY_UPDATED_DIFF,
22+
SIGNALS_MAP,
23+
TYPE_DICT,
24+
)
1725

1826

1927
def check_last_updated(socrata_token, dataset_id, logger):
@@ -40,7 +48,7 @@ def check_last_updated(socrata_token, dataset_id, logger):
4048

4149
updated_timestamp = datetime.utcfromtimestamp(int(response["rowsUpdatedAt"]))
4250
now = datetime.utcnow()
43-
recently_updated_source = (now - updated_timestamp) < timedelta(days=1)
51+
recently_updated_source = (now - updated_timestamp) < RECENTLY_UPDATED_DIFF
4452

4553
prelim_prefix = "Preliminary " if dataset_id == PRELIM_DATASET_ID else ""
4654
if recently_updated_source:

nhsn/tests/test_pull.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import glob
22
import time
3+
from datetime import datetime
34
from unittest.mock import patch, MagicMock
45
import os
56
import pytest
@@ -12,7 +13,7 @@
1213
pull_data_from_file,
1314
check_last_updated
1415
)
15-
from delphi_nhsn.constants import TYPE_DICT, PRELIM_TYPE_DICT, PRELIM_DATASET_ID, MAIN_DATASET_ID
16+
from delphi_nhsn.constants import TYPE_DICT, PRELIM_TYPE_DICT, PRELIM_DATASET_ID, MAIN_DATASET_ID, RECENTLY_UPDATED_DIFF
1617

1718
from delphi_utils import get_structured_logger
1819
from conftest import TEST_DATA, PRELIM_TEST_DATA, TEST_DIR
@@ -158,7 +159,7 @@ def test_pull_nhsn_data_backup(self, mock_socrata, dataset, caplog, params):
158159

159160

160161
@pytest.mark.parametrize('dataset', DATASETS, ids=["data", "prelim_data"])
161-
@pytest.mark.parametrize("updatedAt", [time.time(), time.time() - 172800], ids=["updated", "stale"])
162+
@pytest.mark.parametrize("updatedAt", [time.time(), time.time() - 172800, time.time() - 108000], ids=["updated", "stale", "updated_late"])
162163
@patch("delphi_nhsn.pull.Socrata")
163164
def test_check_last_updated(self, mock_socrata, dataset, updatedAt, caplog):
164165
mock_client = MagicMock()
@@ -169,8 +170,9 @@ def test_check_last_updated(self, mock_socrata, dataset, updatedAt, caplog):
169170
check_last_updated(mock_client, dataset["id"], logger)
170171

171172
# Check that get method was called with correct arguments
172-
now = time.time()
173-
if now - updatedAt < 60:
173+
now_datetime = datetime.utcfromtimestamp(time.time())
174+
updatedAt_datetime = datetime.utcfromtimestamp(updatedAt)
175+
if now_datetime - updatedAt_datetime < RECENTLY_UPDATED_DIFF:
174176
assert f"{dataset['msg_prefix']}NHSN data was recently updated; Pulling data" in caplog.text
175177
else:
176178
stale_msg = f"{dataset['msg_prefix']}NHSN data is stale; Skipping"

0 commit comments

Comments
 (0)