I need to aggregate data for a “daily” statistic. I need to store the daily values long-term, but also use them to visualize the most recent days. Basically a graph that displays the daily values for the last 2 years, including yesterday.
I understand that Flux comes with time zone support so the aggregation per-day should run smoothly.
However the task configuration is outside Flux context: In the “every” property I would use “1d”. But this would relate to UTC, not to my timezone.
This means that the value for yesterday will only be available after several hours (depending on timezone).
You can use the offset to delay the execution of the query in the task configuration options. Then you can explicitly define the range relative to UTC so that you can make sure you’re capturing it (so specifying range(start: , stop: ) as opposed to range(start: -task.every)).
Is that clear? Working with timezones and thinking about time is so tricky.
So this means I need to update the tasks whenever the timezone changes, but also whenever the offset of the timezone changes (due to daylight savings). I can either do that or run the query manually once per day.
I guess I have a pretty clear picture on how to proceed.
That is really annoying to have to change the tasks, with every winter/summer time change.
Isn’t there a more elegant way to do this?
It would be so more simple if the task could start using the local time setting of the OS. But what I have seen, that even using the option to configure this with Cron, it still using the UTC-times (as opposite when you would use Cron on the OS).
There should be an elegant/simple way of doing this. It really causes a negative impact when you have to take care of changing tasks as well even the time zone changes. Developers should have a look into this.
This seems to work when done interactively.
By changing the “offset” variable value it can be changed to account for the winter/summer time.
If there would be a way to get the “offset” automatically from the Timezone, or out of the database (and update that based on the winter/summer time) this could automated to change with summer/winter-time.
But I’m not a programmer and rather new to Flux, so wouldn’t know how to that.
Still thought, this is annoying not to have this basic functionality in Flux.
We were able to make it work with cron option so that local timezone is respected. However there is a bug so on first time, the task runs at UTC. From second time on, it will run at the correct time (timezone-aware).
We’re still not perfectly happy with it, so currently we keep 48 values per day (every 30 minutes) so that we will always be able to compute a daily figure for any timezone.
The task didn’t run properly
After experimenting I now found out that if you use: option location = timezone.location(name: "Europe/Amsterdam")
The Today() and Now() functions shift the time and add the Timezone offset to the time!
This is completely undocumented.
So in my case 00:00 UTC = 02:00 Local time.
This means however if you use this as I did to select a range, this is compared to the UTC-time-stamps of the data, which then of course gives you the complete wrong range.
Mmm, it can be even more buggy: I have 4 tasks that run with ‘cron’, and yesterday only 1 of them actually ran at 00:00 local time, the others still (after 3 times) use 00:00 UTC.
Documentation gives no clarity how this should work, but this is buggy for sure.
What a mess!
By using the option location = timezone.location(name: "Europe/Amsterdam")
the today() function started to use the local time, so the offset can be 0 (and hopefully does not have to be changed when switching between summer/winter time).
I have added the timeShift at the end so it uses the 00:00 local time of the day for which the totals where calculated as the timestamp for the measurement, instead of 00:00 local time of the “next” day.