1212 get_epiweek ,
1313 group_by_property ,
1414)
15+ from alternative_interface .helper import covidcast_fluview_locations_mapping
1516
1617
1718def epiweeks_in_date_range (start_date_str : str , end_date_str : str ):
@@ -72,6 +73,7 @@ def days_in_date_range(start_date_str: str, end_date_str: str):
7273def get_available_geos (indicators ):
7374 geo_values = []
7475 grouped_indicators = group_by_property (indicators , "data_source" )
76+ sources = grouped_indicators .keys ()
7577 for data_source , indicators in grouped_indicators .items ():
7678 indicators_str = "," .join (indicator ["name" ] for indicator in indicators )
7779 response = requests .get (
@@ -98,6 +100,27 @@ def get_available_geos(indicators):
98100 .prefetch_related ("geo_level" )
99101 .order_by ("level" )
100102 ]
103+ if "fluview" in sources :
104+ geographic_granularities .extend (
105+ [
106+ {
107+ "id" : f"{ geo_unit .geo_level .name } :{ geo_unit .geo_id } " ,
108+ "geoType" : geo_unit .geo_level .name ,
109+ "text" : geo_unit .display_name ,
110+ "geoTypeDisplayName" : geo_unit .geo_level .display_name ,
111+ }
112+ for geo_unit in GeographyUnit .objects .filter (
113+ geo_level__name__in = [
114+ "census-region" ,
115+ "us-territory" ,
116+ "us-city" ,
117+ "ny_minus_jfk" ,
118+ ]
119+ )
120+ .prefetch_related ("geo_level" )
121+ .order_by ("level" )
122+ ]
123+ )
101124 grouped_geographic_granularities = group_by_property (
102125 geographic_granularities , "geoTypeDisplayName"
103126 )
@@ -113,26 +136,57 @@ def get_available_geos(indicators):
113136
114137
115138def get_covidcast_data (indicator , start_date , end_date , geo , api_key ):
116- if indicator ["_endpoint" ] == "covidcast" :
117- time_values = f"{ start_date } --{ end_date } "
118- if indicator ["time_type" ] == "week" :
119- start_day , end_day = get_epiweek (start_date , end_date )
120- time_values = f"{ start_day } -{ end_day } "
121- geo_type , geo_value = geo .split (":" )
122- params = {
123- "time_type" : indicator ["time_type" ],
124- "time_values" : time_values ,
125- "data_source" : indicator ["data_source" ],
126- "signal" : indicator ["name" ],
127- "geo_type" : geo_type ,
128- "geo_values" : geo_value .lower (),
129- "api_key" : api_key if api_key else settings .EPIDATA_API_KEY ,
130- }
131- response = requests .get (f"{ settings .EPIDATA_URL } covidcast" , params = params )
132- if response .status_code == 200 :
133- response_data = response .json ()
134- if len (response_data ["epidata" ]):
135- return response_data ["epidata" ]
139+ time_values = f"{ start_date } --{ end_date } "
140+ if indicator ["time_type" ] == "week" :
141+ start_day , end_day = get_epiweek (start_date , end_date )
142+ time_values = f"{ start_day } -{ end_day } "
143+ geo_type , geo_value = geo .split (":" )
144+ params = {
145+ "time_type" : indicator ["time_type" ],
146+ "time_values" : time_values ,
147+ "data_source" : indicator ["data_source" ],
148+ "signal" : indicator ["name" ],
149+ "geo_type" : geo_type ,
150+ "geo_values" : geo_value .lower (),
151+ "api_key" : api_key if api_key else settings .EPIDATA_API_KEY ,
152+ }
153+ response = requests .get (f"{ settings .EPIDATA_URL } covidcast" , params = params )
154+ if response .status_code == 200 :
155+ response_data = response .json ()
156+ if len (response_data ["epidata" ]):
157+ return response_data ["epidata" ]
158+ return []
159+
160+
161+ def get_fluview_data (indicator , geo , start_date , end_date , api_key ):
162+ region = None
163+ try :
164+ region = covidcast_fluview_locations_mapping [geo ]
165+ except KeyError :
166+ region = geo .split (":" )[1 ]
167+ time_values = f"{ start_date } --{ end_date } "
168+ if indicator ["time_type" ] == "week" :
169+ start_day , end_day = get_epiweek (start_date , end_date )
170+ time_values = f"{ start_day } -{ end_day } "
171+ params = {
172+ "regions" : region ,
173+ "epiweeks" : time_values ,
174+ "api_key" : api_key if api_key else settings .EPIDATA_API_KEY ,
175+ }
176+ print (indicator )
177+ response = requests .get (f"{ settings .EPIDATA_URL } { indicator ['data_source' ]} " , params = params )
178+ if response .status_code == 200 :
179+ data = response .json ()
180+ if len (data ["epidata" ]):
181+ return [
182+ {
183+ "time_value" : el ["epiweek" ],
184+ "value" : el [indicator ["name" ]],
185+ "signal" : indicator ["name" ],
186+ "time_type" : indicator ["time_type" ],
187+ }
188+ for el in data ["epidata" ]
189+ ]
136190 return []
137191
138192
@@ -379,20 +433,30 @@ def get_chart_data(indicators, geography):
379433 chart_data ["initialViewEnd" ] = end_date
380434
381435 # Fetch data from a wider range (2020 to today) for scrolling
382- data_start_date = "2010 -01-01"
436+ data_start_date = "1990 -01-01"
383437 data_end_date = today .strftime ("%Y-%m-%d" )
384438
385439 for indicator in indicators :
386440 title = generate_epivis_custom_title (indicator , geo_display_name )
387441 color = generate_random_color ()
388442 indicator_time_type = indicator .get ("time_type" , "week" )
389- data = get_covidcast_data (
390- indicator ,
391- data_start_date ,
392- data_end_date ,
393- geography ,
394- settings .EPIDATA_API_KEY ,
395- )
443+ data = None
444+ if indicator ["_endpoint" ] == "covidcast" :
445+ data = get_covidcast_data (
446+ indicator ,
447+ data_start_date ,
448+ data_end_date ,
449+ geography ,
450+ settings .EPIDATA_API_KEY ,
451+ )
452+ elif indicator ["data_source" ] in ["fluview" , "fluview_clinical" ]:
453+ data = get_fluview_data (
454+ indicator ,
455+ geography ,
456+ data_start_date ,
457+ data_end_date ,
458+ settings .EPIDATA_API_KEY ,
459+ )
396460 if data :
397461 # Prepare series with full data range for scrolling
398462 series = prepare_chart_series_multi (
0 commit comments