-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.js
More file actions
143 lines (121 loc) · 2.79 KB
/
Copy pathmetrics.js
File metadata and controls
143 lines (121 loc) · 2.79 KB
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
const prisma = require('../db');
const { normalizeRange, rangeToMs, typeToColumn } = require('../lib/telemetry');
function cleanLimit(value, fallback) {
const limit = Number.parseInt(value, 10);
if (Number.isNaN(limit)) {
return fallback;
}
if (limit < 1) {
return 1;
}
if (limit > 200) {
return 200;
}
return limit;
}
function formatRow(row, column, type) {
return {
id: row.id,
time_date: row.timeDate,
model: row.model,
type,
payload: row[column]
};
}
async function saveMetric({ model, type, payload, timeDate = new Date() }) {
const column = typeToColumn(type);
if (!column) {
throw new Error(`bad metric type: ${type}`);
}
const data = {
model,
timeDate
};
data[column] = payload;
return prisma.metric.create({ data });
}
async function listLatestLogs({ model, type, limit }) {
const column = typeToColumn(type);
if (!column) {
throw new Error(`bad metric type: ${type}`);
}
const rows = await prisma.metric.findMany({
where: {
model,
[column]: {
not: null
}
},
orderBy: {
timeDate: 'desc'
},
take: cleanLimit(limit, 50)
});
return rows.map((row) => formatRow(row, column, type));
}
async function getTrends({ model, range, type }) {
const windowName = normalizeRange(range);
if (!windowName) {
throw new Error(`bad range: ${range}`);
}
const to = new Date();
const from = new Date(to.getTime() - rangeToMs(windowName));
const rows = await prisma.metric.findMany({
where: {
model,
timeDate: {
gte: from,
lte: to
}
},
orderBy: {
timeDate: 'asc'
}
});
if (type) {
const column = typeToColumn(type);
if (!column) {
throw new Error(`bad metric type: ${type}`);
}
return {
model,
range: windowName,
from: from.toISOString(),
to: to.toISOString(),
type,
count: rows.filter((row) => row[column] !== null).length,
rows: rows
.filter((row) => row[column] !== null)
.map((row) => formatRow(row, column, type))
};
}
const series = {
energy_demand: [],
inst_power: [],
elec_measurements: []
};
for (const row of rows) {
if (row.energyDemand !== null) {
series.energy_demand.push(formatRow(row, 'energyDemand', 'energy_demand'));
}
if (row.instPower !== null) {
series.inst_power.push(formatRow(row, 'instPower', 'inst_power'));
}
if (row.elecMeasurements !== null) {
series.elec_measurements.push(formatRow(row, 'elecMeasurements', 'elec_measurements'));
}
}
return {
model,
range: windowName,
from: from.toISOString(),
to: to.toISOString(),
count: rows.length,
series
};
}
module.exports = {
getTrends,
listLatestLogs,
saveMetric
};