Skip to content

Commit 9d91be7

Browse files
committed
adding conditional to fail if api fails
1 parent 329d340 commit 9d91be7

File tree

4 files changed

+55
-19
lines changed

4 files changed

+55
-19
lines changed

_delphi_utils_python/delphi_utils/validator/datafetcher.py

+27-11
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,13 @@ def get_geo_signal_combos(data_source, api_key):
118118
source_signal_mappings = {i['source']:i['db_source'] for i in
119119
meta_response.json()}
120120

121-
with warnings.catch_warnings():
122-
warnings.simplefilter("ignore")
121+
response = Epidata.covidcast_meta()
123122

124-
response = Epidata.covidcast_meta()
125-
126-
if response["result"] != 1:
127-
# Something failed in the API and we did not get real metadata
128-
raise RuntimeError("Error when fetching metadata from the API", response["message"])
123+
if response["result"] != 1:
124+
# Something failed in the API and we did not get real metadata
125+
raise RuntimeError("Error when fetching metadata from the API", response["message"])
129126

127+
else:
130128
meta = pd.DataFrame.from_dict(response["epidata"])
131129
# note: this will fail for signals with weekly data, but currently not supported for validation
132130
meta = meta[meta["time_type"] == "day"]
@@ -183,12 +181,32 @@ def fetch_api_reference(data_source, start_date, end_date, geo_type, signal_type
183181
time_type="day",
184182
geo_type=geo_type,
185183
time_values=Epidata.range(start_date.strftime("%Y%m%d"), end_date.strftime("%Y%m%d")),
186-
geo_value="*"
184+
geo_value="*",
187185
)
188186
if response["result"] != 1:
189-
# Something failed in the API and we did not get real metadata
187+
# Something failed in the API and we did not get real signal data
190188
raise RuntimeError("Error when fetching signal data from the API", response["message"])
191189

190+
if response["message"] not in {"success", "no results"}:
191+
warnings.warn(
192+
"Problem obtaining data",
193+
RuntimeWarning,
194+
message=response["message"],
195+
data_source=data_source,
196+
signal=signal,
197+
time_value=params["time_values"],
198+
geo_type=geo_type,
199+
)
200+
logger.info(f"Trying calling covidcast again")
201+
response = Epidata.covidcast(
202+
data_source,
203+
signal_type,
204+
time_type="day",
205+
geo_type=geo_type,
206+
time_values=Epidata.range(start_date.strftime("%Y%m%d"), end_date.strftime("%Y%m%d")),
207+
geo_value="*",
208+
)
209+
192210
api_df = None
193211
if len(response["epidata"]) > 0:
194212
api_df = pd.DataFrame.from_dict(response["epidata"])
@@ -204,8 +222,6 @@ def fetch_api_reference(data_source, start_date, end_date, geo_type, signal_type
204222

205223
if api_df is None:
206224
raise APIDataFetchError("Error: no API data was returned " + error_context)
207-
if not isinstance(api_df, pd.DataFrame):
208-
raise APIDataFetchError("Error: API return value was not a dataframe " + error_context)
209225

210226
column_names = ["geo_id", "val",
211227
"se", "sample_size", "time_value"]

sir_complainsalot/delphi_sir_complainsalot/check_source.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ def __str__(self):
3030
def to_md(self):
3131
"""Markdown formatted form of complaint."""
3232
return "*{source}* `{signal}` ({geos}) {message}; last updated {updated}.".format(
33-
source=self.data_source, signal=self.signal, geos=", ".join(self.geo_types),
34-
message=self.message, updated=self.last_updated.strftime("%Y-%m-%d"))
35-
33+
source=self.data_source,
34+
signal=self.signal,
35+
geos=", ".join(self.geo_types),
36+
message=self.message,
37+
updated=self.last_updated.strftime("%Y-%m-%d"),
38+
)
3639

3740

3841
def check_source(data_source, meta, params, grace, logger): # pylint: disable=too-many-locals
@@ -76,7 +79,8 @@ def check_source(data_source, meta, params, grace, logger): # pylint: disable=t
7679
start_day=start_date.strftime("%Y-%m-%d"),
7780
end_day=end_date.strftime("%Y-%m-%d"),
7881
geo_type=row["geo_type"],
79-
time_type=row["time_type"])
82+
time_type=row["time_type"],
83+
)
8084

8185
response = Epidata.covidcast(
8286
data_source,
@@ -91,7 +95,7 @@ def check_source(data_source, meta, params, grace, logger): # pylint: disable=t
9195
lag_calculated_from_api = False
9296
latest_data = None
9397

94-
if response["result"] == 1:
98+
if response["result"] == 1 and response["message"] == "success":
9599
latest_data = pd.DataFrame.from_dict(response["epidata"])
96100
latest_data["time_value"] = _parse_datetimes(latest_data, "time_value")
97101
latest_data["issue"] = _parse_datetimes(latest_data, "issue")
@@ -101,6 +105,10 @@ def check_source(data_source, meta, params, grace, logger): # pylint: disable=t
101105
current_lag_in_days = (datetime.now().date() - max(unique_dates)).days
102106
lag_calculated_from_api = True
103107

108+
else:
109+
# Something failed in the API and we did not get real signal data
110+
raise RuntimeError("Error when fetching signal data from the API", message=response["message"])
111+
104112
logger.info("Signal lag",
105113
current_lag_in_days = current_lag_in_days,
106114
data_source = data_source,

sir_complainsalot/delphi_sir_complainsalot/date_utils.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from datetime import datetime
22

3-
from epiweeks import Week
43
import pandas as pd
4+
from epiweeks import Week
5+
56

67
def _date_to_api_string(d: datetime, time_type: str = "day") -> str:
78
"""Convert a date object to a YYYYMMDD or YYYYMM string expected by the API."""
@@ -11,6 +12,7 @@ def _date_to_api_string(d: datetime, time_type: str = "day") -> str:
1112
return Week.fromdate(d).cdcformat()
1213
raise ValueError(f"Unknown time_type: {time_type}")
1314

15+
1416
def _parse_datetimes(df: pd.DataFrame, col: str, date_format: str = "%Y%m%d") -> pd.Series:
1517
"""Convert a DataFrame date or epiweek column into datetimes.
1618
@@ -19,10 +21,12 @@ def _parse_datetimes(df: pd.DataFrame, col: str, date_format: str = "%Y%m%d") ->
1921
format and return the date of the first day of the week.
2022
"""
2123
df[col] = df[col].astype("str")
24+
2225
def parse_row(row):
2326
if row["time_type"] == "day":
2427
return pd.to_datetime(row[col], format=date_format)
2528
if row["time_type"] == "week":
2629
return pd.to_datetime(Week(int(row[col][:4]), int(row[col][-2:])).startdate())
2730
return row[col]
28-
return df.apply(parse_row, axis=1)
31+
32+
return df.apply(parse_row, axis=1)

sir_complainsalot/delphi_sir_complainsalot/run.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ def run_module():
3030
start_time = time.time()
3131
params = read_params()
3232
Epidata.auth = ("epidata", params["api_credentials"])
33-
meta = pd.DataFrame.from_dict(Epidata.covidcast_meta().get("epidata", dict()))
33+
response = Epidata.covidcast_meta()
34+
35+
meta = None
36+
if response["result"] == 1:
37+
meta = pd.DataFrame.from_dict(response["epidata"])
38+
else:
39+
# Something failed in the API and we did not get real metadata
40+
raise RuntimeError("Error when fetching signal data from the API", response["message"])
41+
3442
meta["max_time"] = _parse_datetimes(meta, "max_time")
3543
slack_notifier = None
3644
if "channel" in params and "slack_token" in params:

0 commit comments

Comments
 (0)