3

I query data from InfluxDB 2 using Flux query like:

from(bucket: "first")
  |> range(start: time(v: 0))
  |> filter(fn: (r) => r["_measurement"] == "test" and r._field == "volume")
  |> keep(columns: ["_time", "_value"])
  |> last()

This gives me a correct result, but it gives me the _time column as a human-readable date. I need a timestamp instead. Seconds, milliseconds, nanoseconds, doesn't matter.

I tried adding |> map(fn: (r) => ({ r with timestamp: uint(v: r._time) })) before keep(... and modifying keep(... to keep the new field. And this still works correctly, but it significantly increases the response time (and this is understandable, why).

I'm pretty sure that I'm doing this wrong, so how do I do this?

updated: I managed not to increase the response time. I moved |> map below last(), but I still think this is not the best way of doing this.

Anton Ivanov
  • 199
  • 1
  • 11
  • Exactly what I was searching for. Your solution works but do let me know if you found "the best way". Thanks – avm Feb 01 '23 at 10:39

1 Answers1

0

If you just want an accurate timestamp and format is not relevant, then use string instead of uint for better response time-

|> map(fn: (r) => ({ r with timestamp: string(v: r._time) }))
ex output with nanosecond precision- 2023-01-01T01:50:25.663017890Z

I don't know your use case, but this is probably the best way to get the actual timestamp stored by influxdb.

avm
  • 387
  • 3
  • 16