-
Notifications
You must be signed in to change notification settings - Fork 4
/
activityStats2.js
345 lines (310 loc) · 9.67 KB
/
activityStats2.js
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
/* eslint-disable camelcase */
/* eslint-disable require-jsdoc */
"use strict";
//
// DOM elements
//
const html_div_chart = document.getElementById("div-chart");
const html_div_chart_cnt = document.getElementById("div-chart-act-count");
const html_sel_date_agg = document.getElementById("sel-date-agg");
const html_sel_type = document.getElementById("sel-type");
const html_sel_measure = document.getElementById("sel-measure");
//
// Global variables
//
const data_all = [];
const data_all_comparison = {};
const promises = []; // array of promises for async fetching
//
// Data fetching
//
const fetch_data = async (session, date_agg) => {
const url = `https://entorb.net/strava/download/${session}/activityStats2_${date_agg}.json`;
try {
const response = await fetch(url);
const data = await response.json();
console.log(`done data download ${date_agg}`);
data_all[date_agg] = data;
} catch (error) {
console.log(`failed data download ${date_agg}`);
}
};
// Start the async fetching
promises.push(fetch_data(session, "week"));
promises.push(fetch_data(session, "month"));
promises.push(fetch_data(session, "quarter"));
promises.push(fetch_data(session, "year"));
//
// Chart functions
//
function chart_create(html_div_chart) {
const chart = echarts.init(html_div_chart);
// https://echarts.apache.org/en/option.html#color
// const chart_colors = ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc'];
chart.setOption({
// title: { text: 'Items per Minute' },
tooltip: {},
legend: { show: false },
grid: {
top: "12%",
left: "1%",
right: "10%",
containLabel: true
},
xAxis: { type: "category" },
yAxis: { type: "value" },
dataZoom: [
{
type: "slider",
show: true,
start: 0,
end: 100
// handleSize: 8,
}
]
});
return chart;
}
const chart = chart_create(html_div_chart);
const chart_cnt = chart_create(html_div_chart_cnt);
function chart_update(data_all) {
console.log("fnc chart_update()");
const date_agg = html_sel_date_agg.value;
const type = html_sel_type.value;
const measure = html_sel_measure.value;
const data_echarts_x = [...data_all[date_agg][type]["date"]];
const data_echarts_y = [...data_all[date_agg][type][measure]];
// filter out null values
const data_echarts_y_non_null = data_echarts_y.filter(
(value) => value !== null
);
const y_min = Math.min(...data_echarts_y_non_null);
const y_max = Math.max(...data_echarts_y_non_null);
const y_delta = y_max > y_min ? y_max - y_min : 1;
fillGapsInDateDataInPlace(data_echarts_x, data_echarts_y);
chart.setOption({
xAxis: { data: data_echarts_x },
yAxis: {
min:
y_min - 0.1 * y_delta > 0
? Math.floor(y_min - 0.1 * y_delta)
: Math.floor(y_min)
},
series: [
{
type: "bar",
name: measure,
data: data_echarts_y,
barWidth: "100%",
// smooth: true,
// symbolSize: 10,
// silent: true,
// animation: false,
markLine: {
show: true,
animation: false,
data: [
{
name: "average",
type: "average"
}
]
}
}
],
title: {
text: `Strava Stats: ${type} ${date_agg} ${measure}`,
left: "center"
}
});
}
function chart_cnt_update(data_all_comparison) {
console.log("fnc chart_cnt_update()");
const date_agg = html_sel_date_agg.value;
let measure = html_sel_measure.value;
const act_types = Object.keys(data_all[date_agg]);
const series = [];
for (const type of act_types) {
if (!(measure in data_all_comparison[date_agg][type])) {
measure = "count";
}
series.push({
type: "bar",
stack: "x",
data: data_all_comparison[date_agg][type][measure],
name: type
});
}
chart_cnt.setOption({
xAxis: {
data: data_all_comparison[date_agg]["date"]
},
yAxis: {},
series: series,
title: {
text: "Strava Stats: All Activity " + capitalize_words(measure),
left: "center"
},
legend: {
show: true,
orient: "vertical",
right: 10,
top: "center"
}
});
}
function charts_update() {
console.log("fnc charts_update()");
chart_update(data_all);
chart_cnt_update(data_all_comparison);
}
function calc_data_for_act_comparison(data_all_comparison) {
console.log("fnc calc_data_for_act_comparison()");
for (const date_agg of ["week", "month", "quarter", "year"]) {
data_all_comparison[date_agg] = {};
// loop over act_types and extract min start and max end date
let start = "";
let end = "";
const act_types = Object.keys(data_all[date_agg]);
for (const type of act_types) {
const myArray = data_all[date_agg][type]["date"];
const currentStart = myArray[0];
const currentEnd = myArray[myArray.length - 1];
if (start === "" || currentStart < start) {
start = currentStart;
}
if (end === "" || currentEnd > end) {
end = currentEnd;
}
}
// now add min start and max end to data for each act_type and fill the gaps
for (const type of act_types) {
data_all_comparison[date_agg][type] = {};
const data_x = [...data_all[date_agg][type]["date"]];
const data_y = [...data_all[date_agg][type]["count"]];
const data_y2 = [...data_all[date_agg][type]["hours(sum)"]];
const data_y3 = [...data_all[date_agg][type]["kilometers(sum)"]];
// add start
if (data_x[0] != start) {
data_x.unshift(start);
data_y.unshift(null);
data_y2.unshift(null);
data_y3.unshift(null);
}
// add end
if (data_x[data_x.length - 1] != end) {
data_x.push(end);
data_y.push(null);
data_y2.push(null);
data_y3.push(null);
}
// fill gaps
fillGapsInDateDataInPlace(data_x, data_y, data_y2, data_y3);
// store data to global array
if (!("date" in data_all_comparison[date_agg])) {
data_all_comparison[date_agg]["date"] = data_x;
}
data_all_comparison[date_agg][type]["count"] = data_y;
data_all_comparison[date_agg][type]["hours(sum)"] = data_y2;
data_all_comparison[date_agg][type]["kilometers(sum)"] = data_y3;
}
}
console.log(data_all_comparison);
}
//
// Small helpers
//
// Formats value "Something_Is_HERE" to "Something Is Here"
function capitalize_words(str, separator) {
console.log("fnc capitalize_words()");
const allLowerCaseValue = str.split(separator).join(" ").toLowerCase();
return allLowerCaseValue.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
// fill gaps in the data
// supports x data of years (integer), quarters ("2023-Q2"), weeks ("2023-W02"), month "2023-03"
function fillGapsInDateDataInPlace(
data_echarts_x,
data_echarts_y,
data_echarts_y2 = [],
data_echarts_y3 = [],
data_echarts_y4 = []
) {
console.log("fnc fillGapsInDateDataInPlace()");
const minDate = data_echarts_x[0];
const maxDate = data_echarts_x[data_echarts_x.length - 1];
let currentDate = minDate;
let currentIndex = 0;
while (currentDate <= maxDate) {
const isDateMissing = data_echarts_x[currentIndex] !== currentDate;
if (isDateMissing) {
data_echarts_x.splice(currentIndex, 0, currentDate); // start, deleteCount, item)
// loop over all 4 (optional) y data sets
[data_echarts_y, data_echarts_y2, data_echarts_y3, data_echarts_y4]
.filter((data) => data.length > 0) // only work on the non-empty data sets
.forEach((data) => data.splice(currentIndex, 0, null)); // insert data
}
currentIndex++;
currentDate = getNextDate(currentDate);
// if (currentIndex > 100) { return }// TODO
}
}
// calculate the next date for my period
// supports x data of years (integer), quarters ("2023-Q2"), month "2023-03"
function getNextDate(date) {
// console.log(["fnc getNextDate()", date]);
if (typeof date === "number") {
// 2023
return date + 1;
} else if (date.includes("-Q")) {
// "2023-Q2"
const [year, quarter] = date.split("-Q").map(Number);
const nextQuarter = (quarter % 4) + 1;
const nextYear = nextQuarter === 1 ? year + 1 : year;
return `${nextYear}-Q${nextQuarter}`;
} else if (date.includes("-W")) {
// "2023-W02"
const [year, week] = date.split("-W").map(Number);
const nextWeek = week === 53 ? 0 : week + 1;
const nextYear = nextWeek === 0 ? year + 1 : year;
return `${nextYear}-W${nextWeek.toString().padStart(2, "0")}`;
} else if (date.includes("-")) {
// "2023-02"
const [year, month] = date.split("-").map(Number);
const nextMonth = month === 12 ? 1 : month + 1;
const nextYear = nextMonth === 1 ? year + 1 : year;
return `${nextYear}-${nextMonth.toString().padStart(2, "0")}`; // 2 digit month
}
}
//
// GUI helpers
//
function populate_select_type() {
console.log("fnc populate_select_type()");
const act_types = Object.keys(data_all["year"]);
const act_types_map = helper_array_to_object_of_key_eq_value(act_types);
helper_populate_select(html_sel_type, act_types_map, "Run", true);
}
//
// Event listeners
//
html_sel_date_agg.addEventListener("change", () => {
charts_update();
});
html_sel_type.addEventListener("change", () => {
charts_update();
});
html_sel_measure.addEventListener("change", () => {
charts_update();
});
//
// Initialize the chart after all data is fetched
//
// Wait for all async promises to be done (all data is fetched)
Promise.all(promises).then(function () {
console.log("All data fetched");
populate_select_type();
calc_data_for_act_comparison(data_all_comparison);
charts_update();
});