Add custom values in the query for timeFilter - grafana

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.

Related

Condition not applied properly in quarterly lookup in postgresql

SELECT (outbound.data_bas_year||outbound.data_bas_month) as year,
EXTRACT(QUARTER from to_date(outbound.data_bas_year||outbound.data_bas_month, 'YYYYMM')) AS quarter,
count(outbound.call_time) as col_1_0_
FROM cfk_dashboard.if_outbnd_call_dtl outbound
WHERE outbound.data_bas_year||outbound.data_bas_month between '20210101' and '20211231'
AND outbound.conn_call_number = 1
GROUP BY year,quarter
I wrote a query to look up January through December quarterly, but no data for January is aggregated.
In other words, only February and March are counted except for January in the first quarter.
However, if I change the condition start date from 20210101 to 20201231, I get the result I want.
Why?
Function to_date generates date for you, if you have not day value then default value of day will be -01.
But in your query after command WHERE you are used outbound.data_bas_year||outbound.data_bas_month, this is gets only 202101 and you compare this with 20211231 or with 20210101, this will not work correctly. For example, you can use outbound.data_bas_year||outbound.data_bas_month||'01' or to_date(outbound.data_bas_year||outbound.data_bas_month, 'YYYYMM') between '20210101'::date and '20211231'::date

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.

Can the as400 timestamp (yyyy-MM-dd-HH.mm.ssssss) be used for querying irrespective of the device time format?

Date format of the machine is yyyy/mm/dd HH:mm:ss
(yyyy-MM-dd-HH.mm.ssssss) 1988-12-25-17.12.30.000000 this is my time format input, Can this time format be used to query logs from historic_log_info table? Irrespective of the date format set in the machine.
example query - SELECT * FROM TABLE(HISTORY_LOG_INFO( START_TIME => '2021-02-22-09.35.16.508075'))WHERE MESSAGE_ID IS NOT NULL
Based on the description for end-time, that formating looks correct:
start-time
A timestamp expression that indicates the starting timestamp to use when returning history log information.
If this parameter is omitted, the default of CURRENT DATE - 1 DAY is used.
end-time
A timestamp expression that indicates the ending timestamp to use when returning history log information.
If this parameter is omitted, the default of '9999-12-30-00.00.00.000000' is used.
https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzajq/rzajqudfhistoryloginfo.htm

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.

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.