-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinflux.js
58 lines (50 loc) · 1.76 KB
/
influx.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
const Influx = require('influx')
class InfluxHelpr {
constructor(host = 'influxdb', db = 'openiot', username = 'openiot', password = 'openiot') {
this.HOST = host
this.DB = db
this.USERNAME = username
this.PASSWORD = password
this.influx = null
}
async init(schema = []) {
try {
// create influx client
this.influx = new Influx.InfluxDB({
host: this.HOST,
database: this.DB,
username: this.USERNAME,
password: this.PASSWORD,
})
// ensure we have db
const dbNames = await this.influx.getDatabaseNames()
if (!dbNames.includes(this.DB)) {
console.log(`Creating database ${this.DB}`)
await this.influx.createDatabase(this.DB)
}
console.log(`Connected to InfluxDB`)
} catch (err) {
console.error(err)
}
}
async write(points, appId, gwId, topic) {
try {
const dataToWrite = points.map(p => ({
measurement: p.measurement,
tags: p.tags || {},
fields: p.fields || {},
timestamp: p.timestamp ? new Date(p.timestamp) : new Date()
}))
if (dataToWrite.length) {
console.log(`Writing ${dataToWrite.length} points to InfluxDB`)
await this.influx.writePoints(dataToWrite)
console.log(`${dataToWrite.length} points written to InfluxDB`)
} else {
console.log('No new points to add')
}
} catch (err) {
console.error(`Error saving data to InfluxDB! ${err.stack}`)
}
}
}
module.exports = InfluxHelpr