Search code examples
node.jsinfluxdb

How to write a set of measurements to InfluxDB with NodeJS?


I am trying to inject a set of CPU temperatures into an InfluxDB instance. The following code will inject only the last sample in the set (cpu=5). How does one inject a series of points into the measurement?

const Influx = require("influx");
const os = require("os");

const influx = new Influx.InfluxDB({
    host: "localhost",
    database: "example",
    schema: [
        {
            measurement: 'cpu-temp',
            fields: { temp: Influx.FieldType.FLOAT, cpu: Influx.FieldType.INTEGER, socket: Influx.FieldType.INTEGER },
            tags: [ 'host' ]
        }
    ]
});


function writeTemps() {
    //In real life this would use something `sensors` package to obtain the data and transformed into this structure
    const data = [];
    for (let i = 0; i < 6; i++) {
        data.push({
            measurement: "cpu-temp",
            tags: {
                host: os.hostname()
            },
            fields: {
                cpu: i,
                temp: Math.round(((Math.random() * 24) + 24 * 10)) / 10,
                socket: 1
            }
        });
    }
    influx.writePoints(data).then(() => console.log("worked"), (e) => console.error(e));
}

writeTemps();

Example data in the measurement:

> select * from "cpu-temp"
name: cpu-temp
time                cpu host          socket temp
----                --- ----          ------ ----
1554481960188163700 5   Deckard.local 1      26.2
1554481961157513900 5   Deckard.local 1      24.3
1554481962159479300 5   Deckard.local 1      24.5
1554481963161301300 5   Deckard.local 1      24.9
1554481964166741100 5   Deckard.local 1      24.7
1554481965168176800 5   Deckard.local 1      26.2
1554481966168756700 5   Deckard.local 1      24.9
1554481967140210800 5   Deckard.local 1      25.6
1554481968145122000 5   Deckard.local 1      25.9
1554481969144965800 5   Deckard.local 1      25.9
1554481970150685100 5   Deckard.local 1      24.8
1554481971155935600 5   Deckard.local 1      25.7
1554481972160289200 5   Deckard.local 1      24.2
1554481973167241600 5   Deckard.local 1      26.3
1554481974172369600 5   Deckard.local 1      24.2
1554481975176451200 5   Deckard.local 1      25.1
1554481976179103100 5   Deckard.local 1      24.6
1554481977183923100 5   Deckard.local 1      26.2
1554481978189576000 5   Deckard.local 1      25.9
1554481979193856300 5   Deckard.local 1      24.2
1554481980199759900 5   Deckard.local 1      26.3
1554481981205830500 5   Deckard.local 1      24.4
>

Solution

  • Your code inserts all records. However you don't insert unique points, so your points are "deduplicated" on the InfluxDB level. How is defined unique point?

    https://docs.influxdata.com/influxdb/v1.7/troubleshooting/frequently-asked-questions/#how-does-influxdb-handle-duplicate-points

    A point is uniquely identified by the measurement name, tag set, and timestamp.

    So change your measurement schema and insert cpu as a tag, not as a field and you will be inserting unique points => you will see all records in the InfluxDB.