forked from ottopaulsen/MMM-Tibber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumptions.js
75 lines (69 loc) · 2.06 KB
/
consumptions.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
// Run a REST query and return the result as a consumption array
"use strict";
const jsonpointer = require("jsonpointer");
class Item {
constructor(from, consumption, consumptionUnit) {
this.from = from;
this.consumption = consumption;
this.consumptionUnit = consumptionUnit;
}
toString() {
return this.from + ": " + this.consumption + " " + this.consumptionUnit;
}
}
exports.prometheus = function (config) {
const to = new Date();
let from = new Date(to);
from.setHours(to.getHours() - 23);
from.setMinutes(0);
from.setSeconds(0);
from.setMilliseconds(0);
const toTime = JSON.stringify(to).replace(/\"/g, "");
const fromTime = JSON.stringify(from).replace(/\"/g, "");
let query = "http://" + config.host + ":" + config.port + "/api/v1/query_range?query=increase(" + config.metric;
if (config.tags && config.tags.length > 0) {
query = query + "{";
config.tags.forEach((t) => {
query = query + t.tag + "=" + '"' + t.value + '"';
});
query = query + "}";
}
query = query + "[1h])&start=" + fromTime + "&end=" + toTime + "&step=3600";
// console.log("Query: ", query);
return import("node-fetch")
.then(() => {
return fetch(query, {
headers: {
// Authorization: "Bearer " + tibberToken,
"Content-Type": "application/json"
},
method: "GET",
resolveWithFullResponse: true,
followRedirect: false
})
.then((res) => {
const body = JSON.parse(res.body);
const arr = jsonpointer.get(body, "/data/result/0/values");
const consumptionArr = !arr
? []
: arr.map((v) => {
const time = jsonpointer.get(v, "/0");
const value = jsonpointer.get(v, "/1");
let from = new Date(time * 1000);
from.setHours(from.getHours() - 1);
const item = new Item(from, Math.round(value * 100) / 100, "kWh");
return item;
});
return {
label: config.label,
data: consumptionArr
};
})
.catch((e) => {
console.error("Failed to read from Prometheus: ", e);
});
})
.catch((e) => {
console.log("Import fetch error: " + e);
});
};