Counting and grouping in Grafana - influxql

Grafana noob with simple query results formatted as table:
SELECT "locales" FROM "postgresql" WHERE ("environment" = 'stage') AND $timeFilter
Time
locales
2022-03-17 15:31:00
en
2022-03-17 15:31:01
en-us
2022-03-17 15:32:00
en
2022-03-17 15:32:01
en-us
2022-03-17 15:33:00
en-us
2022-03-17 15:34:00
en-us
I'd like to count occurrences and group by locales, like:
locale
count
en
2
en-us
4
I tried the following:
Using a transform, I can group by locales, but this does not give me count.
Adding a transform to count returns a single value of total rows, not individual locale counts.
Including count and group-by in the query: SELECT "locales", count("locales") FROM "postgresql" WHERE ("environment" = 'stage') GROUP BY "locales". This gives InfluxDB Error: mixing aggregate and non-aggregate queries is not supported.
I know Grafana is more time-based, but this should be easily doable, no? Thanks in advance for any tips!

Related

Add custom values in the query for timeFilter

I have below query and need to replace varibale with hard coded values.
Below query works fine when given the time range from the dashboard filter
SELECT mean("count") / $send_interval FROM "$measurement_name" WHERE ("transaction" = 'all' AND "application" =~ /^$application$/) AND $timeFilter GROUP BY time($__interval) fill(null)
I eneterd same time range (2022-05-05 12:46:00 - 2022-05-05 12:53:00) in millisecon as below, However can't see the data in the graph
SELECT mean("count") / $send_interval FROM "$measurement_name" WHERE ("transaction" = 'all' AND "application" =~ /^$application$/) AND time > 1651718760000 AND time < 1651719180000 GROUP BY time($__interval) fill(null)
My version:
Grafana v6.4.1
Influxdb 1.7.7
You should use timestamp format, specified here: https://docs.influxdata.com/influxdb/v1.7/query_language/spec/#dates--times
The date and time literal format is not specified in EBNF like the rest of this document. It is specified using Go’s date / time parsing format, which is a reference date written in the format required by InfluxQL. The reference date time is:
InfluxQL reference date time: January 2nd, 2006 at 3:04:05 PM
time_lit = "2006-01-02 15:04:05.999999" | "2006-01-02"
Alternatively you would likely need to use epoch time in nanoseconds, since influx is stored in ns.

InfluxQL/Grafana: Get maximum per day

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.

Postgres Date Compare with ISO timestamp

When I perform a > than query on a timestampz field it seems to include dates that are equal to the date I'm querying with. At least when I'm comparing to an ISO date string?
select
created,
to_char(created, 'MI:SS:MS')
from
private.event
where
created > '2020-03-24T05:14:08.082Z'
Results
created |to_char |
-------------------|---------|
2020-03-24 18:14:08|14:08:082|
2020-03-24 18:14:08|14:08:180|
I'm not expecting the first row in that result.
FYI if I adjust the query so that I compare with '2020-03-24T05:14:08.083Z' it goes away.
Does anyone know whats going on here ?
Postgres timestamps have microsecond resolution even if they're displayed with millisecond resolution. So you're effectively searching for >'2020-03-24T05:14:08.082000Z' while that first result is probably non-zero in one or more of those last three hidden digits.

Time between two timestamp (postgresql)

Is there a way to have the time (HH:mm) between two timestamps in postgresql ?
The difference between two timestamps can be calculated using -, e.g.:
timestamp_one - timestamp_two
However, the result is not a "time", but an "interval".
You can display that in the form HH:mm using to_char():
to_char(timestamp_one - timestamp_two, 'HH:mi')
but what do you expect for differences larger then 24 hours?

Using generate_series() to produce 30 day date ranges for each day?

My goal is to create a table that looks something like this using PostgreSQL:
date date_start date_end
12/16/2015 11/17/2015 12/16/2015
12/17/2015 11/18/2015 12/17/2015
etc.
So that I can then join to a different table to get the aggregations for each date on a rolling 30 day window. I've been doing some research and I think generate_series() is what I want to use, but I am unsure.
Something like this:
SELECT '2015-12-16'::date + g - 30 AS date_start
, '2015-12-16'::date + g AS date_end
FROM generate_series (0, 25) g; -- number of rows
I skipped the redundant date column. (You shouldn't use a basic type name as identifier anyways.)
There is a variant of generate_series() that works with timestamps, but the simple version generating integer numbers is just as good for dates. Maybe even better because you avoid possible confusion with time zones.
Always use the ISO 8601 format for date literals, which is unambiguous with any datestyle or locale settings.
Related:
Rolling sum / count / average over date interval