88import pandas as pd
99from delphi_epidata import Epidata
1010
11- covidcast .covidcast ._ASYNC_CALL = True # pylint: disable=protected-access
11+ from .date_utils import _date_to_api_string , _parse_datetimes
12+
1213
1314@dataclass
1415class Complaint :
@@ -34,6 +35,7 @@ def to_md(self):
3435 message = self .message , updated = self .last_updated .strftime ("%Y-%m-%d" ))
3536
3637
38+
3739def check_source (data_source , meta , params , grace , logger ): # pylint: disable=too-many-locals
3840 """Iterate over all signals from a source and check for problems.
3941
@@ -74,30 +76,28 @@ def check_source(data_source, meta, params, grace, logger): # pylint: disable=t
7476 signal = row ["signal" ],
7577 start_day = start_date .strftime ("%Y-%m-%d" ),
7678 end_day = end_date .strftime ("%Y-%m-%d" ),
77- geo_type = row ["geo_type" ])
79+ geo_type = row ["geo_type" ],
80+ time_type = row ["time_type" ])
7881
7982 response = Epidata .covidcast (
8083 data_source ,
8184 row ["signal" ],
8285 time_type = row ["time_type" ],
8386 geo_type = row ["geo_type" ],
84- time_values = Epidata .range (start_date . strftime ( "%Y%m%d" ), end_date . strftime ( "%Y%m%d" )),
87+ time_values = Epidata .range (_date_to_api_string ( start_date ), _date_to_api_string ( end_date )),
8588 geo_value = "*" ,
8689 )
8790
88- if response ["result" ] != 1 :
89- # Something failed in the API and we did not get real metadata
90- raise RuntimeError ("Error when fetching signal data from the API" , response ["message" ])
91-
92- latest_data = pd .DataFrame .from_dict (response ["epidata" ])
93- latest_data ["issue" ] = pd .to_datetime (latest_data ["issue" ], format = "%Y%m%d" )
94- latest_data ["time_value" ] = pd .to_datetime (latest_data ["time_value" ], format = "%Y%m%d" )
95- latest_data .drop ("direction" , axis = 1 , inplace = True )
96-
97- current_lag_in_days = (now - datetime .strptime (str (row ["max_time" ]), "%Y%m%d" )).days
91+ current_lag_in_days = (now - row ["max_time" ]).days
9892 lag_calculated_from_api = False
93+ latest_data = None
94+
95+ if response ["result" ] == 1 :
96+ latest_data = pd .DataFrame .from_dict (response ["epidata" ])
97+ latest_data ["issue" ] = latest_data .apply (lambda x : _parse_datetimes (x .issue , x .time_type ), axis = 1 )
98+ latest_data ["time_value" ] = latest_data .apply (lambda x : _parse_datetimes (x .time_value , x .time_type ), axis = 1 )
99+ latest_data .drop ("direction" , axis = 1 , inplace = True )
99100
100- if latest_data is not None :
101101 unique_dates = [pd .to_datetime (val ).date ()
102102 for val in latest_data ["time_value" ].unique ()]
103103 current_lag_in_days = (datetime .now ().date () - max (unique_dates )).days
0 commit comments