Calculate the total duration in a given state from a state aggregate.
duration_in, you can use this function across multiple state
aggregates that cover different time buckets. Any missing values at the time bucket
boundaries are interpolated from adjacent StateAggs.
interpolated_duration_in( state TEXT, tws StateAgg, start TIMESTAMPTZ, interval INTERVAL, prev StateAgg, next StateAgg ) RETURNS DOUBLE PRECISION
Experimental features could have bugs. They might not be backwards compatible, and could be removed in future releases. Use these features at your own risk, and do not use any experimental features in production.
|State to query|
|Previously created state_agg aggregate|
|The start of the interval which this function should cover (if there is a preceeding point)|
|The length of the interval|
|The total time spent in the target state. Displayed as |
This example creates a simple test table:
SET timezone TO 'UTC'; CREATE TABLE states(time TIMESTAMPTZ, state TEXT); INSERT INTO states VALUES ('1-1-2020 10:00', 'starting'), ('1-1-2020 10:30', 'running'), ('1-2-2020 16:00', 'error'), ('1-3-2020 18:30', 'starting'), ('1-3-2020 19:30', 'running'), ('1-4-2020 12:00', 'stopping');
You can query this table for the time spent in the running state, like this:
SELECT time, toolkit_experimental.interpolated_duration_in( 'running', agg, time, '1 day', LAG(agg) OVER (ORDER BY time), LEAD(agg) OVER (ORDER BY time) ) FROM ( SELECT time_bucket('1 day', time) as time, toolkit_experimental.state_agg(time, state) as agg FROM states GROUP BY time_bucket('1 day', time) ) s;
Which gives the result:
time | interpolated_duration_in ------------------------+-------------------------- 2020-01-01 00:00:00+00 | 13:30:00 2020-01-02 00:00:00+00 | 16:00:00 2020-01-03 00:00:00+00 | 04:30:00 2020-01-04 00:00:00+00 | 12:00:00
If you prefer to see the result in seconds,
EXTRACT the epoch from
the returned result.
Found an issue on this page?Report an issue!