-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_test.py
354 lines (277 loc) · 13.2 KB
/
local_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import duckdb,os,time,pytz,json
import streamlit as st
import pandas as pd
import numpy as np
import altair as alt
import matplotlib.pyplot as plt
import seaborn as sns
from pprint import pprint
from datetime import datetime,timedelta
from peloton_client import peloton_client
################# Backend Loading #################
# Load Json
workout_json_file_path = "workout_data.json"
workout_counts_json_file_path = "workout_counts.json"
if os.path.exists(workout_json_file_path):
workout_df = pd.read_json(workout_json_file_path)
workout_df['created_at'] = workout_df['created_at'].dt.tz_localize('UTC')
workout_df['created_at'] = workout_df['created_at'].dt.tz_convert('US/Eastern')
print(workout_df.dtypes)
print(workout_df.info())
workout_counts_df = pd.read_json(workout_counts_json_file_path, orient='index')
workout_counts_df.reset_index(inplace=True)
workout_counts_df.columns = ['Type', 'Count']
else:
st.write("Lol where's the jsons")
def padding_fivepx():
st.markdown("<div style='height: 5px;'></div>", unsafe_allow_html=True)
#time shenanigans
timezone = pytz.timezone('US/Eastern')
current_date = datetime.now(timezone).date()
midnight_datetime = timezone.localize(datetime.combine(current_date, datetime.min.time()))
past_year_date = current_date - timedelta(days=365)
past_year_datetime = timezone.localize(datetime.combine(past_year_date, datetime.min.time()))
past_year_df = workout_df[workout_df['created_at'] > past_year_datetime]
################get peloton api data######################
username = st.secrets["peloton"]["username"]
password = st.secrets["peloton"]["password"]
client = peloton_client.PelotonClient(username=username, password=password)
def extract_data(input_data):
output_dict = {}
for x in input_data:
output_dict[x.get('slug')] = x.get('value')
return output_dict
#Getting & structuring the workout data
def get_workout_data(workouts):
workout_data = []
for workout in workouts:
output_dict = {}
workout_metrics = client.fetch_workout_metrics(workout.get('id'))
workout_core_stats = extract_data(workout_metrics.get('summaries'))
workout_core_averages = extract_data(workout_metrics.get('average_summaries'))
#Code for getting HR Zones
effort_zones_data = workout.get('effort_zones')
if not effort_zones_data:
effort_zones_data = {}
# Handle non-serializable types in effort_zones_data
for zone in ['heart_rate_z1_duration', 'heart_rate_z2_duration', 'heart_rate_z3_duration', 'heart_rate_z4_duration', 'heart_rate_z5_duration']:
output_dict[zone] = json.dumps(effort_zones_data.get('heart_rate_zone_durations', {}).get(zone, {}))
# print(type(effort_zones_data), effort_zones_data)
heart_rate_zone_durations = effort_zones_data.get('heart_rate_zone_durations', {})
try:
output_dict = {
'distance': workout_core_stats.get('distance'),
'output': workout_core_stats.get('total_output'),
'cals': workout_core_stats.get('calories'),
'speed': workout_core_averages.get('avg_speed'),
'duration': workout.get('ride').get('duration') / 60,
'title': workout.get('ride').get('title'),
'created_at': workout.get('created_at'),
'discipline': workout.get('fitness_discipline'),
'instructor': workout.get('ride').get('instructor', {}).get('name') if workout.get('ride') and workout.get('ride').get('instructor') else None,
'heart rate': workout_core_stats.get('heart_rate'),
'heart_rate_z1_duration': heart_rate_zone_durations.get('heart_rate_z1_duration', 0),
'heart_rate_z2_duration': heart_rate_zone_durations.get('heart_rate_z2_duration', 0),
'heart_rate_z3_duration': heart_rate_zone_durations.get('heart_rate_z3_duration', 0),
'heart_rate_z4_duration': heart_rate_zone_durations.get('heart_rate_z4_duration', 0),
'heart_rate_z5_duration': heart_rate_zone_durations.get('heart_rate_z5_duration', 0)
}
workout_data.append(output_dict)
print(workout.get('ride').get('title'))
except Exception as e:
print(f"Error parsing workout: {e}")
print(f"Output Dict: \n{e}")
return workout_data
#Get workout count info
def get_user_overview(overview):
workout_counts = overview.get('workout_counts', {}).get('workouts', [])
workout_counts_dict = {}
for workout in workout_counts:
workout_name = workout.get('name', '')
count = workout.get('count', 0)
workout_counts_dict[workout_name] = count
return workout_counts_dict
def get_peloton_data():
workout_data_file_path = "workout_data.json"
try:
# Check if workout_data.json exists
if not os.path.isfile(workout_data_file_path):
print("getting all data")
# Fetch all workout data if workout_data.json doesn't exist
workouts = client.fetch_workouts(fetch_all=True)
else:
# Otherwise, fetch limited workout data
print("getting latest data")
workouts = client.fetch_workouts()
workouts_data = get_workout_data(workouts)
# Grab workout counts
overview = client.fetch_user_overview()
overview_data = get_user_overview(overview)
# Write workout counts to json
workout_counts_file_path = "workout_counts.json"
with open(workout_counts_file_path, "w") as json_file:
json.dump(overview_data, json_file)
# If workout_data.json doesn't exist, write all workout data to the file
if not os.path.isfile(workout_data_file_path):
with open(workout_data_file_path, "w") as json_file:
json.dump(workouts_data, json_file)
else:
# Read existing workout_data JSON
with open('workout_data.json', 'r') as json_file:
existing_data = json.load(json_file)
#Get timestamp of most recent workout
latest_timestamp = max(entry['created_at'] for entry in existing_data)
print(latest_timestamp)
# Retrieve new workout data & update workout_data json
new_workout_data = [data for data in workouts_data if data['created_at'] > latest_timestamp]
if new_workout_data:
existing_data.extend(new_workout_data)
existing_data.sort(key=lambda x: x['created_at'])
latest_timestamp = max(entry['created_at'] for entry in existing_data)
# Save the updated JSON back to the file
with open('workout_data.json', 'w') as json_file:
json.dump(existing_data, json_file, indent=4)
print("New data appended if any.")
else:
print("No new data")
except Exception as e:
print(f"error: {e}")
################# Frontend #################
st.header("tatertotss's Metrics Dashboard")
padding_fivepx()
#Get latest data button
get_data_button = st.button("Get Latest Data")
if get_data_button:
# Display a loading indicator
with st.spinner("Fetching data..."):
# Call the get_peloton_data function
get_peloton_data()
# Display the fetched data
st.write("Data updated")
st.dataframe(workout_df)
# Container 1
with st.container():
col1, col2 = st.columns(2)
# Column 1
with col1:
# Todays Metrics
st.subheader("Today's Metrics")
padding_fivepx()
today_df = workout_df[workout_df['created_at'] > midnight_datetime]
if today_df.empty:
st.write("🦗")
st.write("No workouts yet...")
st.write("Go generate some data, tatertotss! 😉")
else:
# Display workout titles and instructors
st.write("**Workouts Taken:**")
for index, row in today_df.iterrows():
st.write(f"- {row['title']}, {row['instructor']}")
# Display total calories burned
total_cals_burned = today_df['cals'].sum()
st.write("🔥 **Total Calories Burned:**", str(total_cals_burned))
# Display total distance covered
if today_df['distance'].isnull().all():
total_distance_covered = "N/A"
else:
total_distance_covered = today_df['distance'].sum()
st.write("⚡ **Total Distance Covered:**", str(total_distance_covered))
# Display total kilojoules jouled
if today_df['output'].isnull().all():
total_kilojoules = "N/A"
else:
# Calculate the sum of 'output' column
total_kilojoules = today_df['output'].sum()
st.write("🚀 **Total Kilojoules Jouled:**", str(total_kilojoules))
# Column 2
with col2:
# All classes taken by discipline
st.subheader("All classes taken by discipline")
workout_counts_df_filtered = workout_counts_df[workout_counts_df['Count'] > 3]
source = pd.DataFrame({
'classes taken': workout_counts_df_filtered['Count'],
'workout type': workout_counts_df_filtered['Type']
})
chart = alt.Chart(source).mark_arc(innerRadius=50).encode(
theta="classes taken",
color=alt.Color("workout type:N", sort=alt.EncodingSortField(field="classes taken", order='descending')),
tooltip=["workout type:N", "classes taken:Q"],
).properties(
width=300,
height=300,
# title='Number of classes taken by discipline'
)
# st.write("## Altair Chart")
st.altair_chart(chart, use_container_width=True)
#Container 2
with st.container():
col3, col4 = st.columns(2)
#Col 3
with col3:
# Create and display first chart, col3
workout_df['month'] = workout_df['created_at'].dt.month
workout_df['year'] = workout_df['created_at'].dt.year
monthly_distance = workout_df.groupby(['year', 'month'])['distance'].sum().reset_index()
st.subheader("Distance by Month and Year")
chart = alt.Chart(monthly_distance).mark_line().encode(
x='month:O', # 'O' for ordinal scale since it represents months
y='distance:Q', # 'Q' for quantitative scale for distance
color='year:N', # 'N' for nominal scale for years
tooltip=['year:N', 'month:O', 'distance:Q'] # Add tooltip for year, month, and distance
).properties(
width=300,
height=300,
# title='Distance by Month and Year' # Set the title of the chart
)
st.altair_chart(chart, use_container_width=True)
#Col 4
with col4:
# Create and display first chart, col4
st.subheader("Workout Counts by Instructor")
filtered_workout_df = workout_df[~workout_df['instructor'].isin(['JUST WALK', 'JUST RIDE', 'JUST CARDIO', 'JUST LIFT'])]
instructors_more_than_ten_classes = filtered_workout_df['instructor'].value_counts()[filtered_workout_df['instructor'].value_counts() > 10].index
filtered_workout_df = filtered_workout_df[filtered_workout_df['instructor'].isin(instructors_more_than_ten_classes)]
instructor_counts = filtered_workout_df['instructor'].value_counts().reset_index()
instructor_counts.columns = ['instructor', 'count']
chart = alt.Chart(instructor_counts).mark_bar().encode(
y=alt.Y('instructor:N', sort='-x'),
x='count:Q',
tooltip=['instructor:N', 'count:Q'],
color=alt.Color('instructor:N', legend=None)
).properties(
# title='Workout Counts by Instructor',
width=300,
height=300
)
st.altair_chart(chart, use_container_width=True)
#Container 3
with st.container():
#Github-like year calendar:
#past_year_df created in backend loading section towards the top
past_year_df = past_year_df.copy()
past_year_df['date'] = past_year_df['created_at'].dt.date.astype(str)
# Calculate the total distance covered per day
daily_total_distance = past_year_df.groupby('date')['distance'].sum().reset_index()
daily_total_distance['date'] = pd.to_datetime(daily_total_distance['date'])
###OFFSET DATES CODE:
daily_total_distance['date'] += pd.Timedelta(days=1) #add an offset for display
# Create the Altair chart with tooltips
chart = alt.Chart(daily_total_distance, title="Daily Total Distance Covered").mark_rect().encode(
alt.X("date(date):O", title="Day"),
alt.Y("month(date):O", title="Month"),
alt.Color("distance:Q", title="Total Distance", scale=alt.Scale(scheme='viridis')),
tooltip=[
alt.Tooltip("date(date)", title="Date"),
alt.Tooltip("month(date):O", title="Month"),
alt.Tooltip("distance:Q", title="Total Distance"),
],
).configure_view(
step=13,
strokeWidth=0
).configure_axis(
domain=False
)
st.altair_chart(chart, use_container_width=True)
##test area
# st.write(daily_total_distance)
# print(daily_total_distance.dtypes)