dylanplummer commited on
Commit
5aeaeae
·
1 Parent(s): 4896170

add cache

Browse files
Files changed (1) hide show
  1. app.py +48 -37
app.py CHANGED
@@ -45,45 +45,57 @@ city_county_name_map = cities['county_name'].to_dict()
45
 
46
  cached_report = None
47
  cache_time = 0
 
 
48
 
49
  def full_report():
50
- client = BetaAnalyticsDataClient()
51
-
52
- request = RunReportRequest(
53
- property=f"properties/{PROPERTY_ID}",
54
- dimensions=[Dimension(name="nthDay"),
55
- Dimension(name='eventName'),
56
- Dimension(name="continent"),
57
- Dimension(name="country"),
58
- Dimension(name="countryId"),
59
- Dimension(name="city")],
60
- metrics=[Metric(name="eventValue")],
61
- #return_property_quota=True,
62
- date_ranges=[DateRange(start_date="2023-06-30", end_date="today")],
63
- )
64
- response = client.run_report(request)
65
-
66
- res = {'day': [], 'jumps': [], 'continent': [], 'country': [], 'iso': [], 'city': []}
67
-
68
- for row in response.rows:
69
- event_name = row.dimension_values[1].value
70
- if event_name == FINISHED_EXERCISE:
71
- day = int(row.dimension_values[0].value)
72
- continent = row.dimension_values[2].value
73
- country = row.dimension_values[3].value
74
- country_iso = row.dimension_values[4].value
75
- city = row.dimension_values[5].value
76
- event_value = float(row.metric_values[0].value)
77
- res['day'].append(day)
78
- res['jumps'].append(event_value)
79
- res['continent'].append(continent)
80
- res['country'].append(country)
81
- res['iso'].append(country_iso)
82
- res['city'].append(city)
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
-
85
-
86
- df = pd.DataFrame.from_dict(res)
87
  total_jumps = int(df['jumps'].sum())
88
  print(f"Total jumps: {total_jumps}")
89
  df['iso'] = df['iso'].map(alpha_2_map)
@@ -117,7 +129,6 @@ def full_report():
117
  county_df['county'] = county_df['city'].map(city_county_map)
118
  county_df['count_name'] = county_df['city'].map(city_county_name_map)
119
  county_df = county_df.groupby(['county', 'count_name']).sum().reset_index()
120
- print(county_df)
121
 
122
  county_map = px.choropleth(county_df, geojson=counties, locations='county', color='jumps',
123
  color_continuous_scale="plasma",
 
45
 
46
  cached_report = None
47
  cache_time = 0
48
+ reload_cache = False
49
+ reload_every = 10 * 60 # 10 minutes
50
 
51
  def full_report():
52
+ global cached_report, cache_time, reload_cache
53
+ if time.time() - cache_time > reload_every:
54
+ reload_cache = False
55
+ if not reload_cache:
56
+ print("Loading report...")
57
+ reload_cache = True
58
+ cache_time = time.time()
59
+ client = BetaAnalyticsDataClient()
60
+
61
+ request = RunReportRequest(
62
+ property=f"properties/{PROPERTY_ID}",
63
+ dimensions=[Dimension(name="nthDay"),
64
+ Dimension(name='eventName'),
65
+ Dimension(name="continent"),
66
+ Dimension(name="country"),
67
+ Dimension(name="countryId"),
68
+ Dimension(name="city")],
69
+ metrics=[Metric(name="eventValue")],
70
+ #return_property_quota=True,
71
+ date_ranges=[DateRange(start_date="2023-06-30", end_date="today")],
72
+ )
73
+ response = client.run_report(request)
74
+
75
+ res = {'day': [], 'jumps': [], 'continent': [], 'country': [], 'iso': [], 'city': []}
76
+
77
+ for row in response.rows:
78
+ event_name = row.dimension_values[1].value
79
+ if event_name == FINISHED_EXERCISE:
80
+ day = int(row.dimension_values[0].value)
81
+ continent = row.dimension_values[2].value
82
+ country = row.dimension_values[3].value
83
+ country_iso = row.dimension_values[4].value
84
+ city = row.dimension_values[5].value
85
+ event_value = float(row.metric_values[0].value)
86
+ res['day'].append(day)
87
+ res['jumps'].append(event_value)
88
+ res['continent'].append(continent)
89
+ res['country'].append(country)
90
+ res['iso'].append(country_iso)
91
+ res['city'].append(city)
92
+
93
+ df = pd.DataFrame.from_dict(res)
94
+ cached_report = df.copy(deep=True)
95
+ else:
96
+ print("Using cached report...")
97
+ df = cached_report.copy(deep=True)
98
 
 
 
 
99
  total_jumps = int(df['jumps'].sum())
100
  print(f"Total jumps: {total_jumps}")
101
  df['iso'] = df['iso'].map(alpha_2_map)
 
129
  county_df['county'] = county_df['city'].map(city_county_map)
130
  county_df['count_name'] = county_df['city'].map(city_county_name_map)
131
  county_df = county_df.groupby(['county', 'count_name']).sum().reset_index()
 
132
 
133
  county_map = px.choropleth(county_df, geojson=counties, locations='county', color='jumps',
134
  color_continuous_scale="plasma",