-
Notifications
You must be signed in to change notification settings - Fork 73
/
web.js
210 lines (210 loc) · 7.24 KB
/
web.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
"use strict";
exports.__esModule = true;
var http = require("http");
var moment = require("moment");
var postgres = require("./classes/postgres.js");
var lastFetchedData = {};
function loadCurrentData(key) {
console.log("Refreshing latest " + key + " entry...");
var query;
if (key == "gym" || key == "meditated") {
query =
"SELECT * FROM raw_data WHERE key = $1 AND value != '0' ORDER BY timestamp DESC LIMIT 1";
}
else {
query =
"SELECT * FROM raw_data WHERE key = $1 ORDER BY timestamp DESC LIMIT 1";
}
console.log(query);
postgres.client.query({
text: query,
values: [key]
}, function (err, res) {
if (err) {
console.error(err);
return;
}
var lastRow = res.rows[0];
if (lastRow != null) {
lastFetchedData[key] = {
time: moment(Number(lastRow.timestamp)).format(),
value: Number(lastRow.value)
};
}
});
}
console.log("Launching up API web server...");
var interval = 5 * 60 * 1000;
var keys = [
"mood",
"sleepDurationWithings",
"emailsInbox",
"weight",
"gym",
"macrosCarbs",
"macrosProtein",
"macrosFat",
"weeklyComputerTime",
"meditated"
];
var _loop_1 = function (i) {
var key = keys[i];
setInterval(function () {
loadCurrentData(key);
}, interval);
loadCurrentData(key);
lastFetchedData[key] = {
time: null,
value: null
};
};
for (var i = 0; i < keys.length; i++) {
_loop_1(i);
}
postgres.client.query({
text: "SELECT SUM(value::int) FROM raw_data WHERE key = 'rescue_time_daily_computer_used'"
}, function (err, res) {
if (err) {
console.error(err);
return;
}
var hoursComputerUsed = res.rows[0]["sum"] / 60.0;
var daysDifference = moment().diff(moment("2020-03-22"), "days");
hoursComputerUsed += daysDifference * 6.5;
lastFetchedData["totalComputerUsageHours"] = {
time: moment().format(),
value: Math.round(hoursComputerUsed)
};
});
postgres.client.query({
text: "SELECT COUNT(*) FROM raw_data"
}, function (err, res) {
if (err) {
console.error(err);
return;
}
lastFetchedData["totalAmountOfEntries"] = {
time: moment().format(),
value: Number(res.rows[0]["count"])
};
});
interval = 15 * 60 * 1000;
var keysStartingWith = ["rescue_time", "swarm", "weather", "dailySteps"];
function loadKeysCountData(key) {
var keyPlusHash = key + "%";
var query = "SELECT COUNT(*) AS value FROM raw_data WHERE key LIKE $1";
console.log(query);
postgres.client.query({
text: query,
values: [keyPlusHash]
}, function (err, res) {
if (err) {
console.error(err);
return;
}
var lastRow = res.rows[0];
if (lastRow != null) {
lastFetchedData[key] = {
time: moment().format(),
value: Number(lastRow.value)
};
}
});
}
var _loop_2 = function (i) {
var key = keysStartingWith[i];
setInterval(function () {
loadKeysCountData(key);
}, interval);
lastFetchedData[key] = {
time: null,
value: null
};
loadKeysCountData(key);
};
for (var i = 0; i < keysStartingWith.length; i++) {
_loop_2(i);
}
postgres.client.query({
text: "SELECT COUNT(*) AS value FROM raw_data WHERE source = 'tag_days' OR source = 'add_time_range'"
}, function (err, res) {
lastFetchedData["timeRanges"] = {
time: moment().format(),
value: Number(res.rows[0].value)
};
});
setInterval(function () {
postgres.client.query({
text: "SELECT COUNT(*) AS value FROM raw_data"
}, function (err, res) {
var manually = Number(res.rows[0].value) -
lastFetchedData["timeRanges"].value -
lastFetchedData["rescue_time"].value -
lastFetchedData["swarm"].value -
lastFetchedData["weather"].value -
lastFetchedData["dailySteps"].value;
lastFetchedData["manuallyEntered"] = {
time: moment().format(),
value: manually
};
});
}, 10000);
function updateOverviewTable() {
var keysForDashboard = ["gym", "veggies", "withingsSleepingHR"];
var queryToUse = "SELECT";
var weekTimestamp = moment()
.subtract(7, "days")
.unix() * 1000;
var monthTimestamp = moment()
.subtract(30, "days")
.unix() * 1000;
var quarterTimestamp = moment()
.subtract(90, "days")
.unix() * 1000;
var yearTimestamp = moment()
.subtract(365, "days")
.unix() * 1000;
for (var i = 0; i < keysForDashboard.length; i++) {
var athTimestamp = moment("2019-04-12").unix() * 1000;
var key = keysForDashboard[i];
if (key == "mood") {
athTimestamp = moment("2018-02-01").unix() * 1000;
}
queryToUse += "(SELECT ROUND(AVG(value::numeric), 4) FROM raw_data WHERE timestamp > " + weekTimestamp + " AND key='" + key + "') as " + key + "Week,";
queryToUse += "(SELECT ROUND(AVG(value::numeric), 4) FROM raw_data WHERE timestamp > " + monthTimestamp + " AND key='" + key + "') as " + key + "Month,";
queryToUse += "(SELECT ROUND(AVG(value::numeric), 4) FROM raw_data WHERE timestamp > " + quarterTimestamp + " AND key='" + key + "') as " + key + "Quarter,";
queryToUse += "(SELECT ROUND(AVG(value::numeric), 4) FROM raw_data WHERE timestamp > " + yearTimestamp + " AND key='" + key + "') as " + key + "Year,";
queryToUse += "(SELECT ROUND(AVG(value::numeric), 4) FROM raw_data WHERE timestamp > " + athTimestamp + " AND key='" + key + "') as " + key + "AllTime,";
queryToUse += "(SELECT COUNT(value::numeric) FROM raw_data WHERE timestamp > " + weekTimestamp + " AND key='" + key + "') as " + key + "WeekCount,";
queryToUse += "(SELECT COUNT(value::numeric) FROM raw_data WHERE timestamp > " + monthTimestamp + " AND key='" + key + "') as " + key + "MonthCount,";
queryToUse += "(SELECT COUNT(value::numeric) FROM raw_data WHERE timestamp > " + quarterTimestamp + " AND key='" + key + "') as " + key + "QuarterCount,";
queryToUse += "(SELECT COUNT(value::numeric) FROM raw_data WHERE timestamp > " + yearTimestamp + " AND key='" + key + "') as " + key + "YearCount,";
queryToUse += "(SELECT COUNT(value::numeric) FROM raw_data WHERE timestamp > " + athTimestamp + " AND key='" + key + "') as " + key + "AllTimeCount";
if (i != keysForDashboard.length - 1) {
queryToUse += ",";
}
}
console.log(queryToUse);
postgres.client.query({
text: queryToUse
}, function (err, res) {
lastFetchedData["overviewTable"] = {
time: moment().format(),
value: res.rows[0]
};
});
}
setInterval(updateOverviewTable, 60 * 60 * 1000);
updateOverviewTable();
http
.createServer(function (req, res) {
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept"
});
res.write(JSON.stringify(lastFetchedData));
return res.end();
})
.listen(process.env.PORT);
//# sourceMappingURL=web.js.map