interpolated_state_timeline()

Returns the interpolated timeline of states in a timeline aggregate.

Unlike state_timeline, 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 TimelineAggs.

  1. interpolated_state_timeline(
  2. tws [StateAgg | TimelineAgg],
  3. start TIMESTAMPTZ,
  4. interval INTERVAL,
  5. prev [StateAgg | TimelineAgg],
  6. next [StateAgg | TimelineAgg]
  7. ) RETURNS (TIMESTAMPTZ, TIMESTAMPTZ)
warning

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.

Required arguments

NameTypeDescription
aggregateTimelineAggPreviously created state_agg aggregate
startTIMESTAMPTZThe start of the interval which this function should cover (if there is a preceeding point)
intervalINTERVALThe length of the interval

Optional arguments

NameTypeDescription
prevTimelineAggThe TimelineAgg from the prior interval, used to interpolate the value at start. If NULL, the first timestamp in aggregate is used as the start of the interval.
nextTimelineAggThe TimelineAgg from the following interval, used to interpolate the value at start + interval. If NULL, the last timestamp in aggregate is used as the end of the interval.

Returns

ColumnTypeDescription
stateTEXT or BIGINTA state found in the TimelineAgg
start_timeTIMESTAMPTZThe time the state started at (inclusive)
end_timeTIMESTAMPTZThe time the state ended at (exclusive)

Sample usage

Getting the interpolated history of states in a timeline aggregate:

  1. SELECT
  2. bucket,
  3. (toolkit_experimental.interpolated_state_timeline(
  4. summary,
  5. bucket,
  6. '15 min',
  7. LAG(summary) OVER (ORDER by bucket),
  8. LEAD(summary) OVER (ORDER by bucket)
  9. )).*
  10. FROM (
  11. SELECT
  12. time_bucket('1 min'::interval, ts) AS bucket,
  13. toolkit_experimental.timeline_agg(ts, state) AS summary
  14. FROM states_test
  15. GROUP BY time_bucket('1 min'::interval, ts)
  16. ) t;

Example output:

  1. bucket | state | start_time | end_time
  2. ------------------------+-------+------------------------+------------------------
  3. 2020-01-01 00:00:00+00 | START | 2020-01-01 00:00:00+00 | 2020-01-01 00:00:11+00
  4. 2020-01-01 00:00:00+00 | OK | 2020-01-01 00:00:11+00 | 2020-01-01 00:15:00+00
  5. 2020-01-01 00:01:00+00 | ERROR | 2020-01-01 00:01:00+00 | 2020-01-01 00:01:03+00
  6. 2020-01-01 00:01:00+00 | OK | 2020-01-01 00:01:03+00 | 2020-01-01 00:16:00+00
  7. 2020-01-01 00:02:00+00 | STOP | 2020-01-01 00:02:00+00 | 2020-01-01 00:02:00+00