-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
69 lines (62 loc) · 1.91 KB
/
app.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
const express = require('express')
const {InfluxDB} = require('@influxdata/influxdb-client')
const app = express()
const port = 3000
const token = 'super_secret_token'
const org = 'your_email'
const client = new InfluxDB({url: 'https://us-west-2-1.aws.cloud2.influxdata.com', token: token})
const queryApi = client.getQueryApi(org)
app.use(express.static('public'))
app.set('port', port);
app.listen(app.get('port'), () => {
console.log(`Listening on ${app.get('port')}.`);
});
//more details can be found here https://github.com/influxdata/influxdb-client-js
app.get('/api/cpu_total_usage', (req, res) => {
let csv = []
const query =
`from(bucket: "plotlyGraph")
|> range(start: -5m)
|> filter(fn: (r) => r["_measurement"] == "cpu")
|> filter(fn: (r) => r["_field"] == "usage_system")
|> filter(fn: (r) => r["cpu"] == "cpu-total")
|> yield(name: "mean")`
queryApi.queryRows(query, {
next(row, tableMeta) {
o = tableMeta.toObject(row)
csv.push(o)
console.log(`${o._time} ${o._measurement}: ${o._field}=${o._value}`)
},
error(error) {
console.error(error)
res.end()
},
complete() {
res.json(csv)
},
})
})
app.get('/api/cpu_total_user', (req, res) => {
let csv = []
const query =
`from(bucket: "plotlyGraph")
|> range(start: -5m)
|> filter(fn: (r) => r["_measurement"] == "cpu")
|> filter(fn: (r) => r["_field"] == "usage_user")
|> filter(fn: (r) => r["cpu"] == "cpu-total")
|> yield(name: "mean")`
queryApi.queryRows(query, {
next(row, tableMeta) {
o = tableMeta.toObject(row)
csv.push(o)
console.log(`${o._time} ${o._measurement}: ${o._field}=${o._value}`)
},
error(error) {
console.error(error)
res.end()
},
complete() {
res.json(csv)
},
})
})