Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

writePoint write a reduced array on same measurement #297

Open
mikeccuk2005 opened this issue Sep 15, 2017 · 15 comments
Open

writePoint write a reduced array on same measurement #297

mikeccuk2005 opened this issue Sep 15, 2017 · 15 comments

Comments

@mikeccuk2005
Copy link

mikeccuk2005 commented Sep 15, 2017

I tested writePoint by using an array and writing them individually, which gives different result
There is a use scenario where I need to write multiple measurement on the same measurement, I it occur to me that the method will only write one per unique measurement name.
I already tried adding different time value to the fields before send them to write.
To avoid this problem I make have a loop that write them individually.

Example code

const Influx = require('influx')
const database = 'my_test_db_' + Math.random()
const client = new Influx.InfluxDB({
   host: 'localhost',
   port: 8086,
   database
})

const my_time = new Date().getTime()
const points1 = [
   {
      measurement: 'response_times',
      fields: { time: my_time + 1, host: 'host1', duration: Math.random() },
   },
   {
      measurement: 'response_times',
      fields: { time: my_time + 2, host: 'host1', duration: Math.random() },
   },
   {
      measurement: 'response_times',
      fields: { time: my_time + 3, host: 'host1', duration: Math.random() },
   },
   {
      measurement: 'response_times',
      fields: { time: my_time + 4, host: 'host2', duration: Math.random() },
   },
   {
      measurement: 'response_times',
      fields: { time: my_time + 5, host: 'host1', duration: Math.random() },
   }
]

const points2 = [
   {
      measurement: 'response_times2',
      fields: { time: my_time + 1, host: 'host1', duration: Math.random() },
   },
   {
      measurement: 'response_times2',
      fields: { time: my_time + 2, host: 'host1', duration: Math.random() },
   },
   {
      measurement: 'response_times2',
      fields: { time: my_time + 3, host: 'host1', duration: Math.random() },
   },
   {
      measurement: 'response_times2',
      fields: { time: my_time + 4, host: 'host2', duration: Math.random() },
   },
   {
      measurement: 'response_times2',
      fields: { time: my_time + 5, host: 'host1', duration: Math.random() },
   }
]

client.createDatabase(database).then(async () => {
   let result = await client.writePoints(points1).then(() => client.query('select * from "response_times"'))
   let test1_result = result.length

   let test2_result = []
   for (let i = 0; i < points2.length; i++) {
      let result = await client.writePoints(points2).then(() => client.query('select * from "response_times2"'))
      test2_result.push(result.length)
   }

   console.log(JSON.stringify({ test2_result, test1_result }))

})

result

{"test2_result":[1,2,3,4,5],"test1_result":1}
@lms-lamhuynh
Copy link

lms-lamhuynh commented Oct 3, 2017

Has this ridiculous issue been fixed? I think I'm having the same issue. I wrote multiple points and only the last point get stored.

var points = [{
    measurement: 'alarms',
    fields: {
        name: 'alarm_1',
        value: 40
    },
    tags: {
        api_key: '818d1c9c6a5c43fd8982f32e7fe5ce70'
    }
}, {
    measurement: 'alarms',
    fields: {
        name: 'alarm_2',
        value: 40
    },
    tags: {
        api_key: '818d1c9c6a5c43fd8982f32e7fe5ce70'
    }
}]

influxdb.writePoints(points, ....);

Only alarm_2 is stored into the DB
This error happen because the timestamp is the same for every point (I used the default timestamp of nanoseconds).

@popey456963
Copy link

Can confirm what @lms-lamhuynh says, currently, the implementation of influx.writePoints is non-functional for arrays of points when a default timestamp is used.

Any updates on if this could be fixed? It pains me to have to use several hundred HTTP queries when one would suffice. Might also consider a few more tests cases for this area, using multiple points in writePoints seems like a logical thing to test.

@MVMS1994
Copy link

This happened to me as well. If I'm passing a list of values, it's writing only the last element.

@stale
Copy link

stale bot commented Jun 1, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 1, 2019
@stale
Copy link

stale bot commented Aug 3, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Aug 3, 2019
@popey456963
Copy link

This is definitely still an issue and one that prevents me using this library.

@stale stale bot removed the stale label Aug 5, 2019
@stale
Copy link

stale bot commented Oct 4, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 4, 2019
@popey456963
Copy link

See above statement.

@stale stale bot removed the stale label Oct 7, 2019
@minenkom
Copy link

minenkom commented Oct 14, 2019

Setting incremented timestamp for each point manually do the job.

@stale
Copy link

stale bot commented Dec 14, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Dec 14, 2019
@stale stale bot closed this as completed Dec 21, 2019
@eliseumds
Copy link

This should be reopened, no?

@bencevans bencevans reopened this Aug 7, 2020
@WarrenK-design
Copy link

This is still a problem.

const Influx = require('influx');
const client = new Influx.InfluxDB({
    host: 'localhost',
    database: 'my_db',
    username: 'pi',
    password: 'raspberry',
    schema: [
      {
        measurement: 'power',
        fields: { 
            Value: Influx.FieldType.FLOAT,
            Unit:  Influx.FieldType.STRING
        },
        tags: ['DeviceID']
      }
    ]
  })

// Write to the power measurement 
const points1 = [
  {
     measurement: 'power',
     tags: {DeviceID: '1'},
     fields: {Value:5, Unit: 'Watts'},
  },
  {
    measurement: 'power',
    tags: {DeviceID: '1'},
    fields: {Value:10, Unit: 'Watts'},
  }
]

client.writePoints(points1);

Then the output from influxdb

> select * from power
name: power
time                DeviceID Unit  Value
----                -------- ----  -----
1599128996306290377 1        Watts 10

It is annoying but when you think of it from the point of view of sensor data, you cant have two different readings at exactly the same time for the same device, there would be no point in taking these readings. You need to change the time for the readings manually as in the original post.

// Get the current time 
var time = new Date();
current_time = time.getTime()

const points1 = [
  {
     measurement: 'power',
     tags: {DeviceID: '1'},
     fields: {Value:5, Unit: 'Watts'},
     timestamp: current_time, //Add the timestamp 
  },
  {
    measurement: 'power',
    tags: {DeviceID: '1'},
    fields: {Value:5, Unit: 'Watts'},
    timestamp: current_time+1, //Offset by 1ms 
  }
]

client.writePoints(points1);

Now in influx

name: power
time          DeviceID Unit  Value
----          -------- ----  -----
1599129944147 1        Watts 5
1599129944148 1        Watts 5

@Ha2ryZhang
Copy link

See above statement.

@mikeccuk2005
Copy link
Author

mikeccuk2005 commented Sep 9, 2020

I think There should be some kind of warning if the array has the same timestamp?
https://node-influx.github.io/file/src/index.js.html#lineNumber680
See here
line 701 if (timestamp !== undefined) <-- If default ran twice or more. Should there be a warning?

If not At least this document https://node-influx.github.io/class/src/index.js~InfluxDB.html, should set an example with multiple points; currently example is only one.
Also the Doc should say this defect has a work around; Adding different timestamp.

@rnd-debug
Copy link
Contributor

There might be 2 use cases:

  • these 2 values should be stored with the same timestamps as tags are different:
 [{  measurement: 'power',
     tags: {DeviceID: '1'}, fields: {Value:5, Unit: 'Watts'},
  },
  { measurement: 'power',
    tags: {DeviceID: '2'}, fields: {Value:10, Unit: 'Watts'},
  }]
  • these values might be stored with different timestamps or with the same (first value being overwritten), according to some user-provided config:
 [{  measurement: 'power',
     tags: {DeviceID: '1'}, fields: {Value:5, Unit: 'Watts'},
  },
  { measurement: 'power',
    tags: {DeviceID: '1'}, fields: {Value:10, Unit: 'Watts'},
  }]

@bencevans does it make sense to provide an additional parameter in IWriteOptions? Something like shouldCreateTimestamps : Boolean, true if node-influx needs to create one timestamp per array item?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants