|
| 1 | +--- |
| 2 | +api_name: interpolated_state_timeline() |
| 3 | +excerpt: Get time periods for a state from a timeline aggregate |
| 4 | +topics: [hyperfunctions] |
| 5 | +keywords: [duration, states, hyperfunctions, Toolkit] |
| 6 | +api: |
| 7 | + license: community |
| 8 | + type: function |
| 9 | + experimental: true |
| 10 | + toolkit: true |
| 11 | + version: |
| 12 | + experimental: 1.13.0 |
| 13 | +hyperfunction: |
| 14 | + family: state aggregates |
| 15 | + type: accessor |
| 16 | + aggregates: |
| 17 | + - state_agg() | timeline_agg() |
| 18 | +--- |
| 19 | + |
| 20 | +import Experimental from 'versionContent/_partials/_experimental.mdx'; |
| 21 | + |
| 22 | +# interpolated_state_timeline() <tag type="toolkit">Toolkit</tag><tag type="experimental-toolkit">Experimental</tag> |
| 23 | + |
| 24 | +Returns the interpolated timeline of states in a [timeline aggregate][state_agg]. |
| 25 | + |
| 26 | +Unlike [`state_timeline`][state_timeline], you can use this function across multiple state |
| 27 | +aggregates that cover different time buckets. Any missing values at the time bucket |
| 28 | +boundaries are interpolated from adjacent TimelineAggs. |
| 29 | + |
| 30 | +```sql |
| 31 | +interpolated_state_timeline( |
| 32 | + tws [StateAgg | TimelineAgg], |
| 33 | + start TIMESTAMPTZ, |
| 34 | + interval INTERVAL, |
| 35 | + prev [StateAgg | TimelineAgg], |
| 36 | + next [StateAgg | TimelineAgg] |
| 37 | +) RETURNS (TIMESTAMPTZ, TIMESTAMPTZ) |
| 38 | +``` |
| 39 | + |
| 40 | +<Experimental /> |
| 41 | + |
| 42 | +## Required arguments |
| 43 | + |
| 44 | +|Name|Type|Description| |
| 45 | +|-|-|-| |
| 46 | +|`aggregate`|`TimelineAgg`|Previously created state_agg aggregate| |
| 47 | +|`start`|`TIMESTAMPTZ`|The start of the interval which this function should cover (if there is a preceeding point)| |
| 48 | +|`interval`|`INTERVAL`|The length of the interval| |
| 49 | + |
| 50 | +### Optional arguments |
| 51 | + |
| 52 | +|Name|Type|Description| |
| 53 | +|-|-|-| |
| 54 | +|`prev`|`TimelineAgg`|The `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.| |
| 55 | +|`next`|`TimelineAgg`|The `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.| |
| 56 | + |
| 57 | +## Returns |
| 58 | + |
| 59 | +|Column|Type|Description| |
| 60 | +|-|-|-| |
| 61 | +|`state`|`TEXT` or `BIGINT`|A state found in the `TimelineAgg`| |
| 62 | +|`start_time`|`TIMESTAMPTZ`|The time the state started at (inclusive)| |
| 63 | +|`end_time`|`TIMESTAMPTZ`|The time the state ended at (exclusive)| |
| 64 | + |
| 65 | +## Sample usage |
| 66 | + |
| 67 | +Getting the interpolated history of states in a timeline aggregate: |
| 68 | + |
| 69 | +```sql |
| 70 | +SELECT |
| 71 | + bucket, |
| 72 | + (toolkit_experimental.interpolated_state_timeline( |
| 73 | + summary, |
| 74 | + bucket, |
| 75 | + '15 min', |
| 76 | + LAG(summary) OVER (ORDER by bucket), |
| 77 | + LEAD(summary) OVER (ORDER by bucket) |
| 78 | + )).* |
| 79 | +FROM ( |
| 80 | + SELECT |
| 81 | + time_bucket('1 min'::interval, ts) AS bucket, |
| 82 | + toolkit_experimental.timeline_agg(ts, state) AS summary |
| 83 | + FROM states_test |
| 84 | + GROUP BY time_bucket('1 min'::interval, ts) |
| 85 | +) t; |
| 86 | +``` |
| 87 | + |
| 88 | +Example output: |
| 89 | + |
| 90 | +``` |
| 91 | + bucket | state | start_time | end_time |
| 92 | +------------------------+-------+------------------------+------------------------ |
| 93 | + 2020-01-01 00:00:00+00 | START | 2020-01-01 00:00:00+00 | 2020-01-01 00:00:11+00 |
| 94 | + 2020-01-01 00:00:00+00 | OK | 2020-01-01 00:00:11+00 | 2020-01-01 00:15:00+00 |
| 95 | + 2020-01-01 00:01:00+00 | ERROR | 2020-01-01 00:01:00+00 | 2020-01-01 00:01:03+00 |
| 96 | + 2020-01-01 00:01:00+00 | OK | 2020-01-01 00:01:03+00 | 2020-01-01 00:16:00+00 |
| 97 | + 2020-01-01 00:02:00+00 | STOP | 2020-01-01 00:02:00+00 | 2020-01-01 00:02:00+00 |
| 98 | +``` |
| 99 | + |
| 100 | +[state_agg]: /api/:currentVersion:/hyperfunctions/state-aggregates/state_agg/ |
| 101 | +[state_timeline]: /api/:currentVersion:/hyperfunctions/state-aggregates/state_timeline/ |
0 commit comments