-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetcher.js
46 lines (42 loc) · 1007 Bytes
/
fetcher.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
const rescuetime = require("node-rescuetime");
var format = require("date-fns/format");
const getToday = apiKey => {
const rescue = rescuetime(apiKey);
const todayString = format(new Date(), "YYYY-MM-DD");
const params = {
perspective: "rank",
restrict_kind: "overview",
restrict_begin: todayString,
restrict_end: todayString,
resolution_time: "minute",
format: "json"
};
return rescue
.analytics(params)
.then(data => transformToChartData(data.data.rows));
};
const sortRow = (
[_rankA, _secondsA, _peopleA, categoryA],
[_rankB, _secondsB, _peopleB, categoryB]
) => {
if (categoryA < categoryB) {
return -1;
}
if (categoryA > categoryB) {
return 1;
}
return 0;
};
const transformToChartData = rows =>
rows.sort(sortRow).reduce((acc, [rank, seconds, people, category]) => {
const hours = seconds / 3600;
acc.labels.push(category);
acc.dataSet.push(parseFloat(hours.toFixed(2)));
return acc;
}, {
labels: [],
dataSet: []
});
module.exports = {
getToday
};