-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
166 lines (138 loc) · 5.18 KB
/
index.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
const https = require("https");
const moment = require("moment");
const calculateMealStats = require("./calculateMealStats");
const formatMealStats = require("./formatMealStats");
const _ = require("lodash");
const inquirer = require("inquirer");
inquirer.prompt([
{
type: "input",
name: "siteUrl",
message: "What is your nightscout site's base URL?",
default: () => ("https://mysite.herokuapp.com/")
},
{
type: "input",
name: "startDate",
message: "What start date would you like to use for meal analysis?",
default: () => (moment(new Date()).add(-1, 'months').format('l'))
},
{
type: "input",
name: "endDate",
message: "What end date would you like to use?",
default: () => (moment(new Date()).format('l'))
}
]).then(answers => {
const baseUrl = answers.siteUrl;
const startDate = moment(answers.startDate, 'l');
const endDate = moment(answers.endDate, 'l');
makeItDo(baseUrl, startDate, endDate);
});
const makeItDo = (baseUrl, startDate, endDate) => {
const buildUrl = baseUrl => {
let parts = [];
parts.push("find[created_at][$gte]="+moment(startDate).add(-1, "days").format("YYYY-MM-DD"));
parts.push("find[created_at][$lte]="+moment(endDate).add(2, "days").format("YYYY-MM-DD"));
const url = baseUrl + "api/v1/treatments?" + parts.join("&");
return url;
};
https.get(buildUrl(baseUrl), res => {
let data = '';
res.on("data", chunk => {
data += chunk;
});
res.on("end", () => {
displayMealData(data);
});
}).on('error', e => {
console.error(e);
});
const displayMealData = mealData => {
const entries = JSON.parse(mealData);
const momented = _.map(entries, x => {
return Object.assign({}, x, {
created_at: moment(x.created_at)
});
});
const filtered = _.filter(
momented,
x => {
return x.created_at.isBetween(startDate, endDate, 'days', '[]')
&& ["Meal Bolus", "Combo Bolus", "Snack Bolus"].indexOf(x.eventType) >= 0
}
);
const sorted = _.sortBy(filtered, [x => (new Date(x.created_at))]);
// console.log(`\nFound ${sorted.length} entries between ${startDate.format("MMMM Do YYYY")} and ${endDate.format("MMMM Do YYYY")}: \n`);
_.each(sorted, entry => {
console.log(formatEntry(entry));
});
const printEntries = col => _.each(col, printEntry);
// breakfast
// console.log("Breakfast (between 7 and 11)")
// const breakfast = _.filter(sorted, x => (x.created_at.hour() >= 7 && x.created_at.hour() <= 11));
// _.each(breakfast, entry => {
// printEntry(entry);
// });
const betweenHours = (x, start, end) => (x.created_at.hour() >= start && x.created_at.hour() <= end);
const findEntriesBetweenHours = (collection, start, end) => (
_.filter(
collection,
x => (betweenHours(x, start, end))
)
);
const breakfastStart = 7;
const breakfastEnd = 11;
const findBreakfasts = collection => (findEntriesBetweenHours(collection, breakfastStart, breakfastEnd));
const lunchStart = 11;
const lunchEnd = 15;
const findLunches = collection => (findEntriesBetweenHours(collection, lunchStart, lunchEnd));
const dinnerStart = 17;
const dinnerEnd = 21;
const findDinners = collection => (findEntriesBetweenHours(collection, dinnerStart, dinnerEnd));
const carbSum = col => (_.reduce(col, (sum, n) => (sum + n.carbs), 0));
const insulinSum = col => (_.reduce(col, (sum, n) => (sum + n.insulin), 0));
const formatMealSummary = (meal, insulin, carbs) => {
return `${meal}: ${carbs} g`;
};
let breakfastCarbsByDay = [];
let lunchCarbsByDay = [];
let dinnerCarbsByDay = [];
for(let date = moment(startDate); date.isSameOrBefore(endDate); date.add(1, "days")) {
console.log(date.format("MMMM Do YYYY:"));
const dateEntries = _.filter(sorted, x => x.created_at.isSame(date, "day"))
const breakfastEntries = findBreakfasts(dateEntries);
const breakfastCarbs = carbSum(breakfastEntries)
breakfastCarbsByDay.push(breakfastCarbs);
console.log(formatMealSummary("Breakfast", insulinSum(breakfastEntries), breakfastCarbs));
const lunchEntries = findLunches(dateEntries);
const lunchCarbs = carbSum(lunchEntries)
lunchCarbsByDay.push(lunchCarbs);
console.log(formatMealSummary("Lunch", insulinSum(lunchEntries), carbSum(lunchEntries)));
const dinnerEntries = findDinners(dateEntries);
const dinnerCarbs = carbSum(dinnerEntries)
dinnerCarbsByDay.push(dinnerCarbs);
console.log(formatMealSummary("Dinner", insulinSum(dinnerEntries), carbSum(dinnerEntries)));
}
console.log("\nSTATS");
console.log(formatMealStats("Breakfast", calculateMealStats(breakfastCarbsByDay)));
console.log(formatMealStats("Lunch", calculateMealStats(lunchCarbsByDay)));
console.log(formatMealStats("Dinner", calculateMealStats(dinnerCarbsByDay)));
};
}
const formatEntryType = entry => (entry.eventType);
const formatEntryCarbs = entry => ((entry.carbs || 0) + "g");
const formatEntryInsulin = entry => ((entry.insulin || 0) + " units");
const formatEntryTime = entry => (entry.created_at.format("dddd, MMMM Do YYYY, h:mm:ss a"));
const formatEntry = entry => (
formatEntryType(entry) +
": " +
formatEntryInsulin(entry) +
" for " +
formatEntryCarbs(entry) +
" at " +
formatEntryTime(entry)
);
const printEntry = entry => {
console.log(formatEntry(entry));
};