InfluxQL/Grafana: Get maximum per day - grafana

I want to get the maximum of a time series per day, so one data point each day at time 00:00:00. The maximum should be calculated over the range 00:00:00 until 23:59:59 for each day.
What i got so far:
SELECT max("temperature") FROM "Temperature" WHERE $timeFilter GROUP BY time(1d)
($timeFilter is used by Grafana for displaying only the selected time range)
With that query i get the output data points at the wrong time.
EDIT:
When i run
> precision rfc3339
> SELECT max("temperature") FROM "Temperature" WHERE time > now() - 7d GROUP BY time(1d) fill(null)
name: Temperature
time max
---- ---
2020-03-22T00:00:00Z 4.5
2020-03-23T00:00:00Z 9.687
2020-03-24T00:00:00Z 10.75
2020-03-25T00:00:00Z 8.5
2020-03-26T00:00:00Z 11.062
2020-03-27T00:00:00Z 10.25
...
in the CLI, the timestamps seem right.
But in Grafana the data points are placed at 02:00 each day.
Thanks!

Result from the InfluxDB is in the UTC. But Grafana interpolates timestamp to your browser timezone by default (so your browser/local environment reports your local timezone UTC+2). You can change this behavior in the dashboard configuration, for example you can keep timestamps in the UTC:

I think i found a solution myself:
Click '+' next to GROUP BY and select tz(), then enter the desired time zone.

Related

Subract Additional Time from $__timeFilter

I want to subract additional time in $__timeFilter in grafana. Like if I have selected Last 7 days, I want to run 2 queries which do a comparison like one query gives me avg cpu utilization for last 7 days and another one gives me avg cpu utilzation for now() - 14d to now() - 7d. And this is dynamic. I can get for 6hrs, 2days or anything selected.
My database is TimescaleDB and grafana version in 8.3.5
Edit
Query is
select avg(cpu) from cpu_utilization where $__timeFilter(timestamp)
Whatever is selected in the time filter in grafana, the query is manipulated accordingly
Now with grafana understands this query becomes as following. if I select last 24hrs
select avg(cpu) from cpu_utilization where timestamp BETWEEN '2022-09-07 05:32:10' and '2022-09-08 05:32:10'
This is normal behaviour. Now I wanted that if I select last 24hrs, this query to behave as it is but an additional query becomes
select avg(cpu) from cpu_utilization where timestamp BETWEEN '2022-09-06 05:32:10' and '2022-09-07 05:32:10'
(I just don't want it for last 24hrs, but any relative time period selected in the filter)
Answer : https://stackoverflow.com/a/73658919/14817486
You can use the global variables $__to and $__from.
For example, ${__from:date:seconds} will give you a timestamp in seconds. You can then subtract 7 days (= 604800 seconds) from it and use it in your query's WHERE clause. Depending on your SQL dialect, that might be by using TIMESTAMP(), TO_TIMESTAMP() or something similar. So it would look similar to this:
[...] WHERE timestamp BETWEEN TO_TIMESTAMP(${__from:date:seconds}-604800) AND TO_TIMESTAMP(${__to:date:seconds}-604800) [...]
Interesting question! If I understood correctly, you could use the timestamp column as the reference as the grafana is already filtering by this to the comparison query. So you can get the min(timestamp) and max(timestamp) to know the limits of your period and then build something from it.
Like min(timestamp) - INTERVAL '7 days' would give you the start of the previous range, and max(timestamp) - INTERVAL '7 days' would offer the final scope.

Is there a way to automatically increment the dates in my InlfuxQL query?

I have a Grafana dashboard , version v8.1.6 (4a4083716c),where I display the output voltage, current and power of a solar panel. I am using the watt2kwh node to convert my power reading that is in Watt to Watt-Hour. The interval between successive measurements is 10 seconds. Node-Red, version 2.0.6, is used to populate my database.
In Grafana I would like to show the total accumulated power for the current day from 00:00 to 00:00 of the next day. I am successfully doing this with the query below:
SELECT sum("value") FROM "solar/ina219/energy" WHERE time> '2021-10-10 00:00:00' AND time< '2021-10-11 00:00:00'
But each day I must manually change the dates. Can I automate the changing of the dates using InfluxQL? (or pure SQL)
Or would it be easier implementing this in Node-Red and then just fetching the accumulated energy from the database?
Below is a screenshot of the simple panel:
Any help will be appreciated!
Thank you.
After some research I concluded that the InfluxQL query language cannot do this and I would have to use Flux.

Issue selecting timespan values in KDB

I am facing an issue while selecting the following following timespan :
t:([] date:2#.z.d ; time: 10D21:28:47.425287000 10D12:18:23.287989000 )
date time
--------------------------------
2018.03.15 10D21:28:47.425287000
2018.03.15 10D12:18:23.287989000
when i run the following query, i am not getting the second record back
select from t where time within (12:00;13:00)
I am expecting the 2nd record from the table :
date time
-------------------------------
2018.03.15 10D12:18:23.287989000
Is the 10 in the time value 10D12:18:23.287989000 intentional ?
The reason behind the data not coming back is the time (type timespan ) is actually not the nano seconds since midnight ; as per the table it is 10 days plus nanos since midnight
To select the data only on the basis of time :
q)select from t where (`time$(`date$0)+time) within (12:00;13:00)
date time
-------------------------------
2018.03.15 10D12:18:23.287989000
Try adding the date and time from the table , you would see the date forwarded by 10 days
q)select date+time from t
date
-----------------------------
2018.03.25D21:28:47.425287000
2018.03.25D12:18:23.287989000
The timespan is basically nDhh:mm:ss.sssssssss , where n is relative to midnight. If its 0 then it's current day otherwise its +/- n days (depending on whether n is positive or negative).
try running the following , it will return you the difference between the 2 timestamps as a timespan with n=10.
q)2018.03.25D10:12:00.000000000 - 2018.03.15D10:00:00.000000000
10D00:12:00.000000000
Although you should fix your timestamps (there shouldn't be a 10D) if you're in a situation where you can't fix the upstream data but you believe the timestamps to actually be correct, then you can strip away the 10D as follows:
q)update mod[;`long$10D]time from t
date time
-------------------------------
2018.03.16 0D21:28:47.425287000
2018.03.16 0D12:18:23.287989000

Get the last timestamps in a group by time query in Influxdb

I have a database with price and timestamps in nanoseconds measurement in InfluxDB. When I do a select grouped by time like this one:
select first(price),last(price) from priceseries where time>=1496815212834974866 and time<=1496865599580302882 group by time(1s)
I received a time column in which the timestamps is aligned to the second beginning the group. For example, the timestamp will be 08:00:00 and the next timestamps will be 08:00:01
How to
apply aggregation function on the record timestamps itself like last(time) or first(time) so that to have the real first and last timestamps of the group (I can have many prices within my group) ?
and how the time column in the response could be the closing second and not the opening second, that is if the group goes from 08:00:00 to 08:00:01, I want to see 08:00:01 in my time column instead of 08:00:00 which I see now ?
Not when using an aggregation function, which implies use of group by.
select first(price), last(price) where time >= <..> and time <= <..> will give you the first and last price within that time window.
When the query has a group by, the aggregation applies only to values within the intervals. The values themselves are the real values that fall in the 08:00:00 - 08:00:01 interval, it's just that the timestamp shown is for the interval itself, not the actual values.
Meaning that the query for between 08:00:00 and 08:00:01 without a group by and the query with a group by time(1s) for same period will give same result. Only difference is query without group by will have the value's actual timestamp and the group by query will have the interval's timestamp instead.
The timestamp when using group by indicates the starting time of the interval. From that, you can calculate end time is start time + interval. What timestamp to show is not configurable in the query language.

Query to select the highest and lowest values for a given day?

I would like to compose a query such that the highest (max) and lowest (min) values for each day are selected. Here is my current query:
SELECT
min("value"),
max("value")
FROM
"temperature"
WHERE
$timeFilter
GROUP BY
time(1d)
fill(null)
This query does select the minimum and maximum value for each 24-hour period but that period begins at 17:00 local time. My intent is to have each period begin at 0:00 local time.
How do I specify the "offset" for the query?
Grafana by default applies the timezone from your browser settings. I guess your timezone is UTC-7. You can set the timezone to UTC in the dashboard settings to get it to align.