File tree Expand file tree Collapse file tree 2 files changed +17
-11
lines changed
Expand file tree Collapse file tree 2 files changed +17
-11
lines changed Original file line number Diff line number Diff line change 66
77import requests
88from django .conf import settings
9+ from django .core .cache import cache
910from django .http import JsonResponse
1011from epiweeks import Week
1112from delphi_utils import get_structured_logger
@@ -814,14 +815,18 @@ def get_num_locations_from_meta(indicators):
814815 indicators = set (
815816 (indicator ["source" ], indicator ["name" ]) for indicator in indicators
816817 )
817- try :
818- metadata = requests .get (
819- f"{ settings .EPIDATA_URL } covidcast_meta/" , timeout = 5
820- ).json ()["epidata" ]
821- for r in metadata :
822- if (r ["data_source" ], r ["signal" ]) in indicators :
823- timeseries_count += r ["num_locations" ]
824- except Exception as e :
825- print (f"Error fetching covidcast metadata: { e } " )
826- return 0
818+ metadata = cache .get ("covidcast_meta" )
819+ if metadata is None :
820+ try :
821+ metadata = requests .get (
822+ f"{ settings .EPIDATA_URL } covidcast_meta/" , timeout = 5
823+ ).json ()["epidata" ]
824+ # Cache for 24 hours
825+ cache .set ("covidcast_meta" , metadata , 60 * 60 * 24 )
826+ except Exception as e :
827+ print (f"Error fetching covidcast metadata: { e } " )
828+ return 0
829+ for r in metadata :
830+ if (r ["data_source" ], r ["signal" ]) in indicators :
831+ timeseries_count += r ["num_locations" ]
827832 return timeseries_count
Original file line number Diff line number Diff line change @@ -245,7 +245,8 @@ def get_context_data(self, **kwargs):
245245 ),
246246 ).order_by ("beta_last" , "-is_top_priority" , "-delphi_hosted" , "name" )
247247 related_indicators = self .get_related_indicators (
248- filter .indicators_qs , filter .qs .values_list ("id" , flat = True )
248+ filter .indicators_qs .select_related ("indicator_set" , "source" ),
249+ filter .qs .values_list ("id" , flat = True ),
249250 )
250251 context ["related_indicators" ] = json .dumps (related_indicators )
251252 context ["num_of_timeseries" ] = get_num_locations_from_meta (
You can’t perform that action at this time.
0 commit comments