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

Incorrect results with difference() because it doesn't initialize with the last value before query time range #5174

Closed
fetzerch opened this issue Sep 6, 2022 · 8 comments

Comments

@fetzerch
Copy link

fetzerch commented Sep 6, 2022

Currently difference (and most likely also derivative) return incorrect results since they don't initialize the first value with the previous value before the query time range. They only allow to completely drop the first value (keepFirst) or assume a first value of 0 (initialZero). This doesn't work well with e.g. counters.

Example:
I have a bucket with counters for water & gas consumption and want to calculate the daily consumption:
The counter increases by 1 every time a certain amount has been consumed (e.g. 1 liter for water).

I'm using this query:

from(bucket: "smartmeter")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "water.consumption.counter")
  |> difference()
  |> aggregateWindow(every: 1d, fn: sum, timeSrc: "_start")

If I select a single day only, I get an incorrect value for this day:

2022-04-06 00:00:00 -> 0.221

If I select also the previous day, I get the correct value, but again an incorrect value for the previous day:

2022-04-05 00:00:00 -> 0.156
2022-04-06 00:00:00 -> 0.222  <-- This is the correct value

In this case initialZero doesn't help, because in the example the first entry would have to be initialized with 1.

It would be really nice if difference/derivative could be fixed to initialize properly.

A possible workaround would be to get #702 implemented. I assume range(start: v.timeRangeStart, stop: v.timeRangeStop, previousN=1) together with difference(keepFirst: false) would solve the problem.

Note that this has been requested already some time ago for InfluxQL in influxdata/influxdb#5943.

@github-actions
Copy link

github-actions bot commented Mar 5, 2023

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

@fetzerch
Copy link
Author

fetzerch commented Mar 5, 2023

I think it's worth to keep this open, because the issue still exists.

@github-actions
Copy link

github-actions bot commented May 6, 2023

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

@fetzerch
Copy link
Author

I think it's worth to keep this open, because the issue still exists.

@github-actions
Copy link

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

@fetzerch
Copy link
Author

I think it's worth to keep this open, because the issue still exists.

@github-actions
Copy link

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

@fetzerch
Copy link
Author

Issue still exists, but I cannot reopen it.

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

1 participant