-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactivity.ts
294 lines (265 loc) · 8.97 KB
/
activity.ts
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
import type { Moment } from "moment";
import type { Utils } from "./_utils";
import type { EngineAPI } from "./@types/jsengine.types";
import type { App } from "obsidian";
declare global {
interface Window {
renderChart: (chartData: object, container: HTMLElement) => void;
}
}
interface RadarDataSet {
label: string;
data: number[];
}
interface ActivityResult {
activities: string[];
count: number[];
weeks: number;
}
interface RadarChartOptions {
type: string;
data: {
labels: string[];
datasets: {
label: string;
data: number[];
backgroundColor: string;
borderColor: string;
borderWidth: number;
}[];
};
options: {
scales: {
r: {
min: number;
max: number;
angleLines: {
color: string;
};
grid: {
color: string;
};
};
};
};
}
export class Activity {
activities: Record<string, string[]> = {
"✨": ["✨", "🍀", "☘️"],
"🔴": ["🔴"],
"🔵": ["🔵"],
"💚": ["🟢", "🏊", "💃"],
"💦": ["✨", "🍀", "💧", "☘️"],
};
colors = [
"236,201,134",
"92,122,98",
"62,125,121",
"69,117,174",
"142,103,135",
"167,92,112",
];
app: App;
constructor() {
this.app = window.customJS.app;
console.log("loaded Templates");
}
utils = (): Utils => window.customJS.Utils;
/**
* Counts the tags within the specified date range.
* @param {Moment} begin The beginning date (inclusive).
* @param {Moment} end The ending date (inclusive).
* @returns {Promise<Object>} An object with activity keys (labels) and
* the count for each activity found between the starting and ending dates.
* @see utils.tagsForDates
*/
countTags = async (begin: Moment, end: Moment): Promise<ActivityResult> => {
const keys = Object.keys(this.activities);
const count = keys.map(() => 0);
const prefix = "me/✅/";
const tags = await this.utils().tagsForDates(begin, end);
for (const t of tags) {
if (t.startsWith(prefix)) {
const value = t.slice(prefix.length);
console.log(begin.format("MM-DD"), "checking", t, value);
for (const [i, key] of keys.entries()) {
if (this.activities[key].includes(value)) {
count[i]++;
}
}
}
}
const weeks = end.diff(begin, "weeks");
// console.log(begin.format('YYYY-MM-DD'), end.format('YYYY-MM-DD'),
// count, weeks);
if (weeks > 1) {
count.forEach((c, i) => {
count[i] = Math.round(c / weeks);
});
// console.log("averaged per week", count, weeks);
}
return {
activities: keys,
count,
weeks: weeks === 0 ? 1 : weeks,
};
};
/**
* Renders a radar chart in the specified container.
* @param {HTMLElement} container The container to render the chart in.
* @param {string[]} labels The labels for the radar chart.
* @param {Object} series The data series for the radar chart.
*/
renderRadarChart = (
container: HTMLElement,
labels: string[],
series: RadarDataSet[],
) => {
const chartOptions: RadarChartOptions = {
type: "radar",
data: {
labels,
datasets: [],
},
options: {
scales: {
r: {
min: 0,
max: 7,
angleLines: {
color: "#898989",
},
grid: {
color: "#898989",
},
},
},
},
};
series.forEach((dataset, i) => {
chartOptions.data.datasets.push({
label: dataset.label,
data: dataset.data,
backgroundColor: `rgba(${this.colors[i]}, 0.1)`,
borderColor: `rgb(${this.colors[i]})`,
borderWidth: 2,
});
});
const chartData = {
chartOptions: chartOptions,
width: "80%",
};
window.renderChart(chartData, container);
};
/**
* Renders a bar chart in the specified container.
* @param {HTMLElement} container The container to render the chart in.
* @param {ActivityResult} input The input data for the bar chart.
* @param {number} factor The factor to normalize the data.
*/
renderBarChart = (container: HTMLElement, input: ActivityResult) => {
const chartOptions = {
type: "bar",
data: {
labels: input.activities,
datasets: [
{
label: "Activities",
data: input.count,
backgroundColor: [
"rgb(187, 79, 108)",
"rgb(142, 103, 135)",
"rgb(53, 120, 175)",
"rgb(61, 126, 123)",
"rgb(92, 122, 99)",
"rgb(234, 175, 0)",
],
beginAtZero: true,
borderWidth: 1,
},
],
},
options: {
animation: false,
events: ["click"],
transitions: {
active: {
animation: {
duration: 0,
},
},
},
scales: {
y: {
min: 0,
max: 7,
grid: {
color: "rgb(183, 183, 183)",
},
},
},
maintainAspectRatio: false,
plugins: {
legend: {
display: false,
},
},
},
};
const chartData = {
chartOptions: chartOptions,
width: "80%",
};
window.renderChart(chartData, container);
};
/**
* Creates a report with charts for the specified date ranges.
* @param {JSEngine} engine The engine to create markdown (not used).
* @returns {Promise<HTMLElement>} A promise that resolves to an HTML element containing the report.
*/
createReport = async (_: EngineAPI): Promise<HTMLElement> => {
const current = window.moment();
const monday = window.moment(current).day(1);
const lastMonday = window.moment(monday).subtract(1, "weeks");
const prev4 = window.moment(monday).subtract(4, "weeks");
const prev12 = window.moment(monday).subtract(12, "weeks");
const prev48 = window.moment(monday).subtract(48, "weeks");
const dataWeek = await this.countTags(
monday,
window.moment(monday).day(7),
);
console.log("this week", dataWeek);
const lastWeek = await this.countTags(
lastMonday,
window.moment(lastMonday).day(7),
);
const last4 = await this.countTags(prev4, monday);
const last12 = await this.countTags(prev12, monday);
const last48 = await this.countTags(prev48, monday);
const container = createEl("div");
let chart = container.createEl("div");
this.renderRadarChart(chart, Object.keys(this.activities), [
{ label: "this week", data: dataWeek.count },
{ label: "last week", data: lastWeek.count },
{ label: "4 weeks", data: last4.count },
{ label: "12 weeks", data: last12.count },
{ label: "48 weeks", data: last48.count },
]);
container.createEl("h2").setText("This Week");
chart = container.createEl("div");
this.renderBarChart(chart, dataWeek);
container.createEl("h2").setText("Last Week");
chart = container.createEl("div");
this.renderBarChart(chart, lastWeek);
container.createEl("h2").setText("Last 4 weeks (average)");
chart = container.createEl("div");
this.renderBarChart(chart, last4);
container.createEl("h2").setText("Last 12 weeks (average)");
chart = container.createEl("div");
this.renderBarChart(chart, last12);
container.createEl("h2").setText("Last 48 weeks (average)");
chart = container.createEl("div");
this.renderBarChart(chart, last48);
return container;
};
}