node-red-contrib-influxdb
Advanced tools
Comparing version 0.5.0 to 0.5.1
146
influxdb.js
@@ -82,21 +82,37 @@ var _ = require('lodash'); | ||
function addFieldsToPoint(point, fields) { | ||
for (const prop in fields) { | ||
const value = fields[prop]; | ||
if (prop === 'time') { | ||
point.timestamp(value); | ||
} else if (typeof value === 'number') { | ||
if (Number.isInteger(value)) { | ||
point.intField(prop, value); | ||
function addFieldToPoint(point, name, value) { | ||
function isNumeric(value) { | ||
return /^-?\d+$/.test(value); | ||
} | ||
if (name === 'time') { | ||
point.timestamp(value); | ||
} else if (typeof value === 'number') { | ||
point.floatField(name, value); | ||
} else if (typeof value === 'string') { | ||
// string values with numbers ending with 'i' are considered integers | ||
if (value.slice(-1) === 'i') { | ||
let intValue = value.substring(0, value.length-1); | ||
if (isNumeric(intValue)) { | ||
value = parseInt(intValue); | ||
point.intField(name, value); | ||
} else { | ||
point.floatField(prop, value); | ||
point.stringField(name, value); | ||
} | ||
} else if (typeof value === 'string') { | ||
point.stringField(prop, value); | ||
} else if (typeof value === 'boolean') { | ||
point.booleanField(prop, value); | ||
} else { | ||
point.stringField(name, value); | ||
} | ||
} else if (typeof value === 'boolean') { | ||
point.booleanField(name, value); | ||
} | ||
} | ||
function addFieldsToPoint(point, fields) { | ||
for (const prop in fields) { | ||
const value = fields[prop]; | ||
addFieldToPoint(point, prop, value) | ||
} | ||
} | ||
function writePoints(msg, node) { | ||
@@ -109,65 +125,61 @@ node.client.closed == false ? null : node.client.closed = false; | ||
} | ||
var point; | ||
if (_.isArray(msg.payload) && msg.payload.length > 0) { | ||
// array of arrays | ||
if (_.isArray(msg.payload[0]) && msg.payload[0].length > 0) { | ||
msg.payload.forEach(element => { | ||
point = new Point(measurement); | ||
var fields = element[0]; | ||
try { | ||
if (_.isArray(msg.payload) && msg.payload.length > 0) { | ||
// array of arrays | ||
if (_.isArray(msg.payload[0]) && msg.payload[0].length > 0) { | ||
msg.payload.forEach(element => { | ||
let point = new Point(measurement); | ||
let fields = element[0]; | ||
addFieldsToPoint(point, fields); | ||
let tags = element[1]; | ||
for (const prop in tags) { | ||
point.tag(prop, tags[prop]); | ||
} | ||
node.client.writePoint(point); | ||
}); | ||
} else { | ||
// array of non-arrays, assume one point with both fields and tags | ||
let point = new Point(measurement); | ||
let fields = msg.payload[0]; | ||
addFieldsToPoint(point, fields); | ||
var tags = element[1]; | ||
const tags = msg.payload[1]; | ||
for (const prop in tags) { | ||
point.tag(prop, tags[prop]); | ||
} | ||
node.client.writePoint(point); | ||
}); | ||
} else { | ||
// array of non-arrays, assume one point with both fields and tags | ||
point = new Point(measurement); | ||
var fields = msg.payload[0]; | ||
addFieldsToPoint(point, fields); | ||
const tags = msg.payload[1]; | ||
for (const prop in tags) { | ||
point.tag(prop, tags[prop]); | ||
node.client.writePoint(point) | ||
} | ||
node.client.writePoint(point) | ||
} | ||
} else { | ||
if (_.isPlainObject(msg.payload)) { | ||
point = new Point(measurement); | ||
var fields = msg.payload; | ||
addFieldsToPoint(point, fields); | ||
node.client.writePoint(point); | ||
} else { | ||
// just a value | ||
point = new Point(measurement); | ||
var value = msg.payload; | ||
if (typeof value === 'number') { | ||
if (Number.isInteger(value)) { | ||
point.intField('value', value); | ||
} else { | ||
point.floatField('value', value); | ||
} | ||
} else if (typeof value === 'string') { | ||
point.stringField('value', value); | ||
} else if (typeof value === 'boolean') { | ||
point.booleanField('value', value); | ||
if (_.isPlainObject(msg.payload)) { | ||
let point = new Point(measurement); | ||
let fields = msg.payload; | ||
addFieldsToPoint(point, fields); | ||
node.client.writePoint(point); | ||
} else { | ||
// just a value | ||
let point = new Point(measurement); | ||
let value = msg.payload; | ||
addFieldToPoint(point, 'value', value); | ||
node.client.writePoint(point); | ||
} | ||
node.client.writePoint(point); | ||
} | ||
node.client | ||
.close() | ||
.catch(error => { | ||
msg.influx_error = { | ||
errorMessage: error | ||
}; | ||
node.error(error, msg); | ||
}) | ||
} catch (error) { | ||
msg.influx_error = { | ||
errorMessage: error | ||
}; | ||
node.error(error, msg); | ||
} | ||
node.client | ||
.close() | ||
.catch(error => { | ||
msg.influx_error = { | ||
errorMessage: error | ||
}; | ||
node.error(error, msg); | ||
}) | ||
} | ||
@@ -174,0 +186,0 @@ |
{ | ||
"name": "node-red-contrib-influxdb", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "Node-RED nodes to save and query data from an influxdb time series database", | ||
@@ -36,5 +36,5 @@ "main": "influxdb.js", | ||
"influx": "5.6.3", | ||
"@influxdata/influxdb-client": "^1.7.0", | ||
"@influxdata/influxdb-client": "^1.8.0", | ||
"lodash": "4.17.20" | ||
} | ||
} |
@@ -31,3 +31,3 @@ # node-red-contrib-influxdb | ||
When a v1.x InfluxDb configuration is used, the InfluxQL query syntax must be used; when a v1.8-Flux or 2.0 configuration is used, the Flux query syntax is used. | ||
With a v1.x InfluxDb configuration, use the InfluxQL query syntax; when a v1.8-Flux or 2.0 configuration, use the Flux query syntax. | ||
@@ -51,4 +51,6 @@ For example, here is a simple flow to query all of the points in the `test` measurement of the `aTimeSeries` database, where the query is in the configuration of the influxdb input node (copy and paste to your NR editor). We are using a v1.x InfluxDb here, so an InfluxQL query is used. | ||
The fields and tags to write are in ***msg.payload***. If the message is a string, number, or boolean, it will be written as a single value to the specified measurement (called *value*). | ||
The fields and tags to write are in ***msg.payload***. If the message is a string, number, or boolean, it will be written as a single field to the specified measurement called *value*. | ||
>Note: Javascript numbers are *always* written as a float. To write an integer type, use a number in a string with an 'i' suffix, for example, to write the integer `1234` use the string `'1234i'`. | ||
For example, the following flow injects a single random field called `value` into the measurement `test` in the database `aTimeSeries` with the current timestamp. | ||
@@ -63,3 +65,3 @@ | ||
If ***msg.payload*** is an object containing multiple properties, the fields will be written to the measurement. | ||
If ***msg.payload*** is an object containing multiple properties, all of the the fields will be written to the measurement. | ||
@@ -81,3 +83,3 @@ For example, the following flow injects three fields, `numValue`, `randomValue` and `strValue` into the same measurement with the current timestamp. | ||
For example, the following simple flow injects three fields as above, along with two tags, `tag1` and `tag2`: | ||
For example, the following simple flow uses an InfluxDb 1.x database and injects three fields as above, along with two tags, `tag1` and `tag2`: | ||
@@ -101,3 +103,3 @@ [{"id":"eba91e98.1456e","type":"influxdb","z":"b061b303.4f9e5","hostname":"127.0.0.1","port":"8086","database":"aTimeSeries","name":"aTimeSeries"},{"id":"7f25337e.80dacc","type":"inject","z":"b061b303.4f9e5","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":101,"y":248,"wires":[["bb0ff0.ff44f01"]]},{"id":"bb0ff0.ff44f01","type":"function","z":"b061b303.4f9e5","name":"Fields and Tags","func":"msg.payload = [{\n numValue: 12,\n randomValue: Math.random()*10,\n strValue: \"message2\"\n},\n{\n tag1:\"sensor1\",\n tag2:\"device2\"\n}];\nreturn msg;","outputs":1,"noerr":0,"x":272,"y":248,"wires":[["8e2713fa.71d8f"]]},{"id":"8e2713fa.71d8f","type":"influxdb out","z":"b061b303.4f9e5","influxdb":"eba91e98.1456e","name":"","measurement":"test","x":460,"y":248,"wires":[]}] | ||
For example, the following flow injects two points with timestamps specified. | ||
For example, the following flow injects two points into an InfluxDb 1.x database with timestamps specified. | ||
@@ -132,3 +134,3 @@ [{"id":"eba91e98.1456e","type":"influxdb","z":"b061b303.4f9e5","hostname":"127.0.0.1","port":"8086","database":"aTimeSeries","name":"aTimeSeries"},{"id":"9555a67c.6aaa58","type":"function","z":"b061b303.4f9e5","name":"multiple readings","func":"msg.payload = [\n [{\n numValue: 10,\n randomValue: Math.random()*10,\n strValue: \"message1\",\n time: new Date(\"2015-12-28T19:41:13Z\").getTime()\n },\n {\n tag1:\"sensor1\",\n tag2:\"device2\"\n }],\n [{\n numValue: 20,\n randomValue: Math.random()*10,\n strValue: \"message2\",\n time: new Date(\"2015-12-28T19:41:14Z\").getTime()\n },\n {\n tag1:\"sensor1\",\n tag2:\"device2\"\n }]\n];\nreturn msg;","outputs":1,"noerr":0,"x":278,"y":335,"wires":[["f485378d.0b7ac8"]]},{"id":"68b911d9.9746f","type":"inject","z":"b061b303.4f9e5","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":104,"y":335,"wires":[["9555a67c.6aaa58"]]},{"id":"f485378d.0b7ac8","type":"influxdb out","z":"b061b303.4f9e5","influxdb":"eba91e98.1456e","name":"","measurement":"test","x":479,"y":334,"wires":[]}] | ||
Note how timestamps are specified - the number of milliseconds since 1 January 1970 00:00:00 UTC. In this case do not forget to set "ms" in "Time Precision" (Advanced Query Options) of the "Influx Out Node". | ||
Note how timestamps are specified here - the number of milliseconds since 1 January 1970 00:00:00 UTC. In this case do not forget to set the precision to "ms" in "Time Precision" (Advanced Query Options) of the "Influx Out Node". | ||
@@ -135,0 +137,0 @@ ### The Batch Output Node (InfluxDb 1.x Only) |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
94933
1238
185
0