influxdata / flux

Flux is a lightweight scripting language for querying databases (like InfluxDB) and working with data. It's part of InfluxDB 1.7 and 2.0, but can be run independently of those.
https://influxdata.com
MIT License
760 stars 152 forks source link

range, stop relative to start or vise versa #5397

Closed slaesh closed 1 year ago

slaesh commented 1 year ago

currently trying to query a stupid thing. having a given start as ISO-string, trying to fetch 7 days of data using a 1d mean'ed window.. which should just give me 7 points back.. (data is present of course)

it would be awesome to just do something similar to

range(start: my-start-value, stop: $start+7d)

currently though I am trying to calculate the proper start date.. which seems not to be that easy as it sounds? Oo sometime I get 8 entries back, sometimes its 7. sometimes there are entries with "null" values.

how to do it properly? without worring about timezones, offset, and what so ever? =)

thanks!

slaesh commented 1 year ago

InfluxDB v2.2.0

Server: a2f8538

Frontend: cc65325

slaesh commented 1 year ago

doing a query starting with

import "date"

var1 = date.add(d: 1h, to: 2023-03-19T00:00:00Z)

from(bucket: "bbb")
  |> range(start: 2023-03-19T00:00:00Z, stop: var1)

gives the following error: error @3:8-3:12: record is missing label add

slaesh commented 1 year ago

even though the docs are saying it should be available

image

slaesh commented 1 year ago

okay, updating to v2.6.0 solved the error

slaesh commented 1 year ago

flux query:

    import "date"

    startDate = 2023-03-19T23:00:00.000Z
    startPlus7d = date.add(d: 7d, to: startDate)

    from(bucket:"bbbb") 
    |> range(start: startDate, stop: startPlus7d)
    |> filter(fn: (r) => r._measurement == "wow")
    |> filter(fn: (r) => r["_field"] == "somethingcool")
    |> aggregateWindow(every: 1h, fn: mean)
    |> aggregateWindow(every: 1d, fn: sum)

results into this array.. with 8 entries influxResult

[
  {
    result: '_result',
    table: 0,
    _time: '2023-03-20T00:00:00Z',
    _start: '2023-03-19T23:00:00Z',
    _stop: '2023-03-26T23:00:00Z',
    _field: 'somethingcool',
    _measurement: 'wow',
    _value: null
  },
  {
    result: '_result',
    table: 0,
    _time: '2023-03-21T00:00:00Z',
    _start: '2023-03-19T23:00:00Z',
    _stop: '2023-03-26T23:00:00Z',
    _field: 'somethingcool',
    _measurement: 'wow',
    _value: 2495.3879475000003
  },
  {
    result: '_result',
    table: 0,
    _time: '2023-03-22T00:00:00Z',
    _start: '2023-03-19T23:00:00Z',
    _stop: '2023-03-26T23:00:00Z',
    _field: 'somethingcool',
    _measurement: 'wow',
    _value: 2681.4281917499993
  },
  {
    result: '_result',
    table: 0,
    _time: '2023-03-23T00:00:00Z',
    _start: '2023-03-19T23:00:00Z',
    _stop: '2023-03-26T23:00:00Z',
    _field: 'somethingcool',
    _measurement: 'wow',
    _value: 3749.6810583749993
  },
  {
    result: '_result',
    table: 0,
    _time: '2023-03-24T00:00:00Z',
    _start: '2023-03-19T23:00:00Z',
    _stop: '2023-03-26T23:00:00Z',
    _field: 'somethingcool',
    _measurement: 'wow',
    _value: 3127.0013066250003
  },
  {
    result: '_result',
    table: 0,
    _time: '2023-03-25T00:00:00Z',
    _start: '2023-03-19T23:00:00Z',
    _stop: '2023-03-26T23:00:00Z',
    _field: 'somethingcool',
    _measurement: 'wow',
    _value: 3328.9653731249996
  },
  {
    result: '_result',
    table: 0,
    _time: '2023-03-26T00:00:00Z',
    _start: '2023-03-19T23:00:00Z',
    _stop: '2023-03-26T23:00:00Z',
    _field: 'somethingcool',
    _measurement: 'wow',
    _value: 4659.32670525
  },
  {
    result: '_result',
    table: 0,
    _time: '2023-03-26T23:00:00Z',
    _start: '2023-03-19T23:00:00Z',
    _stop: '2023-03-26T23:00:00Z',
    _field: 'somethingcool',
    _measurement: 'wow',
    _value: 3075.397038
  }
]
slaesh commented 1 year ago

and what does the _time actually mean?

for the second entry.. shouldnt it be either 2023-03-19T23:00:00Z or 2023-03-20T23:00:00Z. so, start or end of that entry.

{
    result: '_result',
    table: 0,
    _time: '2023-03-21T00:00:00Z',
    _start: '2023-03-19T23:00:00Z',
    _stop: '2023-03-26T23:00:00Z',
    _field: 'somethingcool',
    _measurement: 'wow',
    _value: 2495.3879475000003
}
github-actions[bot] commented 1 year ago

This issue has had no recent activity and will be closed soon.