forked from mblackstock/node-red-contrib-influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfluxdb.js
executable file
·266 lines (240 loc) · 9.78 KB
/
influxdb.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
var _ = require('lodash');
module.exports = function(RED) {
"use strict";
var Influx = require('influx');
/**
* Config node. Currently we only connect to one host.
*/
function InfluxConfigNode(n) {
RED.nodes.createNode(this,n);
this.hostname = n.hostname;
this.port = n.port;
this.database= n.database;
this.name = n.name;
this.usetls = n.usetls;
if (typeof this.usetls === 'undefined') {
this.usetls = false;
}
// for backward compatibility with old 'protocol' setting
if (n.protocol === 'https') {
this.usetls = true;
}
if (this.usetls && n.tls) {
var tlsNode = RED.nodes.getNode(n.tls);
if (tlsNode) {
this.hostOptions = {};
tlsNode.addTLSOptions(this.hostOptions);
}
}
}
RED.nodes.registerType("influxdb",InfluxConfigNode,{
credentials: {
username: {type:"text"},
password: {type: "password"}
}
});
/**
* Output node to write to an influxdb measurement
*/
function InfluxOutNode(n) {
RED.nodes.createNode(this,n);
this.measurement = n.measurement;
this.influxdb = n.influxdb;
this.precision = n.precision;
this.retentionPolicy = n.retentionPolicy;
this.influxdbConfig = RED.nodes.getNode(this.influxdb);
if (this.influxdbConfig) {
var node = this;
var client = new Influx.InfluxDB({
hosts: [ {
host: this.influxdbConfig.hostname,
port: this.influxdbConfig.port,
protocol: this.influxdbConfig.usetls ? "https" : "http",
options: this.influxdbConfig.hostOptions
}
],
database: this.influxdbConfig.database,
username: this.influxdbConfig.credentials.username,
password: this.influxdbConfig.credentials.password
});
node.on("input",function(msg) {
var measurement;
var writeOptions = {};
var measurement = msg.hasOwnProperty('measurement') ? msg.measurement : node.measurement;
if (!measurement) {
node.error(RED._("influxdb.errors.nomeasurement"),msg);
return;
}
var precision = msg.hasOwnProperty('precision') ? msg.precision : node.precision;
var retentionPolicy = msg.hasOwnProperty('retentionPolicy') ? msg.retentionPolicy : node.retentionPolicy;
if (precision) {
writeOptions.precision = precision;
}
if (retentionPolicy) {
writeOptions.retentionPolicy = retentionPolicy;
}
// format payload to match new writePoints API
var points = [];
var point;
var timestamp;
if (_.isArray(msg.payload) && msg.payload.length > 0) {
// array of arrays
if (_.isArray(msg.payload[0]) && msg.payload[0].length > 0) {
msg.payload.forEach(function(nodeRedPoint) {
point = {
measurement: measurement,
fields: nodeRedPoint[0],
tags: nodeRedPoint[1]
}
if (point.fields.time) {
point.timestamp = point.fields.time;
delete point.fields.time;
}
points.push(point);
});
} else {
// array of non-arrays, assume one point with both fields and tags
point = {
measurement: measurement,
fields: msg.payload[0],
tags: msg.payload[1]
};
if (point.fields.time) {
point.timestamp = point.fields.time;
delete point.fields.time;
}
points.push(point);
}
} else {
if (_.isPlainObject(msg.payload)) {
point = {
measurement: measurement,
fields: msg.payload
};
} else {
// just a value
point = {
measurement: measurement,
fields: {value:msg.payload}
};
}
if (point.fields.time) {
point.timestamp = point.fields.time;
delete point.fields.time;
}
points.push(point);
}
client.writePoints(points, writeOptions).catch(function(err) {
node.error(err,msg);
});
});
} else {
this.error(RED._("influxdb.errors.missingconfig"));
}
}
RED.nodes.registerType("influxdb out",InfluxOutNode);
/**
* Output node to write batches of points to influxdb
*/
function InfluxBatchNode(n) {
RED.nodes.createNode(this,n);
this.influxdb = n.influxdb;
this.precision = n.precision;
this.retentionPolicy = n.retentionPolicy;
this.influxdbConfig = RED.nodes.getNode(this.influxdb);
if (this.influxdbConfig) {
var node = this;
var client = new Influx.InfluxDB({
hosts: [ {
host: this.influxdbConfig.hostname,
port: this.influxdbConfig.port,
protocol: this.influxdbConfig.usetls ? "https" : "http",
options: this.influxdbConfig.hostOptions
}
],
database: this.influxdbConfig.database,
username: this.influxdbConfig.credentials.username,
password: this.influxdbConfig.credentials.password
});
node.on("input",function(msg) {
var writeOptions = {};
var precision = msg.hasOwnProperty('precision') ? msg.precision : node.precision;
var retentionPolicy = msg.hasOwnProperty('retentionPolicy') ? msg.retentionPolicy : node.retentionPolicy;
if (precision) {
writeOptions.precision = precision;
}
if (retentionPolicy) {
writeOptions.retentionPolicy = retentionPolicy;
}
client.writePoints(msg.payload, writeOptions).catch(function(err) {
node.error(err,msg);
});
});
} else {
this.error(RED._("influxdb.errors.missingconfig"));
}
}
RED.nodes.registerType("influxdb batch",InfluxBatchNode);
/**
* Input node to make queries to influxdb
*/
function InfluxInNode(n) {
RED.nodes.createNode(this, n);
this.influxdb = n.influxdb;
this.query = n.query;
this.precision = n.precision;
this.retentionPolicy = n.retentionPolicy;
this.rawOutput = n.rawOutput;
this.influxdbConfig = RED.nodes.getNode(this.influxdb);
if (this.influxdbConfig) {
var node = this;
var client = new Influx.InfluxDB({
hosts: [{
host: this.influxdbConfig.hostname,
port: this.influxdbConfig.port,
protocol: this.influxdbConfig.usetls ? "https" : "http",
options: this.influxdbConfig.hostOptions
}
],
database: this.influxdbConfig.database,
username: this.influxdbConfig.credentials.username,
password: this.influxdbConfig.credentials.password
});
node.on("input", function (msg) {
var query;
var rawOutput;
var queryOptions = {};
var precision;
var retentionPolicy;
query = msg.hasOwnProperty('query') ? msg.query : node.query;
if (!query) {
node.error(RED._("influxdb.errors.noquery"), msg);
return;
}
rawOutput = msg.hasOwnProperty('rawOutput') ? msg.rawOutput : node.rawOutput;
precision = msg.hasOwnProperty('precision') ? msg.precision : node.precision;
retentionPolicy = msg.hasOwnProperty('retentionPolicy') ? msg.retentionPolicy : node.retentionPolicy;
if (precision) {
queryOptions.precision = precision;
}
if (retentionPolicy) {
queryOptions.retentionPolicy = retentionPolicy;
}
if (rawOutput) {
var queryPromise = client.queryRaw(query, queryOptions);
} else {
var queryPromise = client.query(query, queryOptions);
}
queryPromise.then(function (results) {
msg.payload = results;
node.send(msg);
}).catch(function (err) {
node.error(err);
});
});
} else {
this.error(RED._("influxdb.errors.missingconfig"));
}
}
RED.nodes.registerType("influxdb in",InfluxInNode);
}