-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsage-data.js
56 lines (44 loc) · 1.48 KB
/
sage-data.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
module.exports = function (RED) {
"use strict"
const got = require("got")
const queryEndpoint = "https://data.sagecontinuum.org/api/v1/query"
const queryInterval = 5000
function SageDataNode(config) {
RED.nodes.createNode(this, config)
const node = this
const filter = JSON.parse(config.filter)
node.queryStart = "-10s"
node.interval_id = setInterval(function() {
node.emit("input", {})
}, queryInterval)
node.on("input", function (msg, send, done) {
got.post(queryEndpoint, {
json: {
start: node.queryStart,
filter: filter,
}
})
.then(resp => {
const msgs = resp.body.split("\n").filter(s => s != "").map(JSON.parse)
if (msgs.length == 0) {
done()
return
}
msgs.sort((a, b) => a.timestamp.localeCompare(b.timestamp))
const lastMsg = msgs[msgs.length - 1]
node.queryStart = lastMsg.timestamp
msgs.forEach(send)
done()
})
.catch(err => {
done({"error": err})
})
})
}
RED.nodes.registerType("sage data", SageDataNode)
SageDataNode.prototype.close = function () {
if (this.interval_id != null) {
clearInterval(this.interval_id)
}
}
}