-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculate.js
66 lines (46 loc) · 1.65 KB
/
calculate.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
/*
calculate.js
Calculate various metrics on the timeline follow-back data.
*/
function calc_use_days(eventlist, category) {
let use_days = new Set();
for (let event of eventlist) {
if (event.extendedProps.category == category)
use_days.add(event.start.toISOString().substring(0,10));
}
return Array.from(use_days);
}
function calc_days_used(eventlist, category) {
let use_days = calc_use_days(eventlist, category);
return use_days.length;
}
function calc_total_occasions(eventlist, category) {
let total = 0;
for (let event of eventlist) {
if (event.extendedProps.category == category)
total = total + Number(event.extendedProps.occasions);
}
return total;
}
/* For a particular substance and unit of that substance, return the total
* amount used.
*/
function calc_total_units(eventlist, substance, units) {
let total = 0;
for (let event of eventlist) {
if (event.extendedProps.substance == substance && event.extendedProps.units == units)
total = total + (Number(event.extendedProps.occasions) * Number(event.extendedProps.amount));
}
return total;
}
function calc_days_since_last_use(eventlist, category, date) {
let events = eventlist.filter(x=>x.extendedProps.category == category);
let use_days_str = calc_use_days(eventlist, category);
if (use_days_str.length < 1)
return NaN;
let last_day = use_days_str.reduce(function(acc, cur) {
let d = dayjs(cur, 'YYYY-MM-DD');
return (d > acc) ? d : acc;
}, dayjs(0));
return dayjs.duration(dayjs().startOf('day').diff(last_day)).asDays();
}