1
from(bucket: "metrics")
|> range(start: -5m)
|> filter(fn: (r) => r["_measurement"] == "cpu")
|> filter(fn: (r) => r["_field"] == "usage")
|> last()

Running this query will return the data only if it was saved in the last 5 minutes.

What I am looking for is, if there is not data for the time range provided, then get the latest data (could be 10m old or 5d old). I know that prometheus does return last data and we are trying to move from prometheus to influxDB and are stuck with this problem.

Also, just increasing the range to say -10d would not work because the volume of data is very high (hundreds of record per second are being written).

We are experimenting with down sampling as well to see if that will work for us, but wanted to know if there was a way to get it from the source bucket itself.

  • It's not possible this way. Look at the documentation for GetRecord() and find the last record by providing a very wide range and then using that that you can run your expensive query for some specific time. – mfahadi May 18 '22 at 09:49
  • Have you actually tried it with range start increased? All functions in your query are pushdown functions, so volume of data should not be a problem. https://docs.influxdata.com/influxdb/v2.2/query-data/optimize-queries/#pushdown-functions-and-function-combinations – alespour May 25 '22 at 17:01

1 Answers1

1

I have the same issue too, and found a solution that may help.

Firstly use all time range from 0 to now() (influxdb-client-python #172)
It will take long time if date is huge.

So add limit on it, use tail() to specify n records to query data descending by time.

And here's my code:

from(bucket: "future_ticks")
|> range(start: 0, stop: now())
|> filter(fn: (r) => r["_measurement"] == "TXFR1")
|> filter(fn: (r) => r._field == "close")
|> aggregateWindow(every: 1m, fn: last, createEmpty: false, timeSrc: "_start")
|> tail(n: 77)
|> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")

By that I can get needed data within 77 minutes without specify time range.

Paul Werner
  • 87
  • 1
  • 8
joyehsu
  • 11
  • 2