Skip to content

Commit

Permalink
Support getting entire state timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
syvb committed Nov 23, 2022
1 parent 0eca871 commit fb8da95
Show file tree
Hide file tree
Showing 2 changed files with 734 additions and 21 deletions.
222 changes: 221 additions & 1 deletion docs/state_agg.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Test table

Examples below are tested against the following table:
Examples below are tested against the following tables:

```SQL ,non-transactional
SET TIME ZONE 'UTC';
Expand All @@ -13,6 +13,16 @@ INSERT INTO states_test VALUES
('2020-01-01 00:01:00+00', 'ERROR'),
('2020-01-01 00:01:03+00', 'OK'),
('2020-01-01 00:02:00+00', 'STOP');
CREATE TABLE states_test_2(ts TIMESTAMPTZ, state TEXT);
INSERT INTO states_test_2 VALUES
('2019-12-31 00:00:00+00', 'START'),
('2019-12-31 00:00:11+00', 'OK'),
('2019-12-31 00:02:00+00', 'STOP'),
('2019-12-31 00:01:03+00', 'OK');
CREATE TABLE states_test_3(ts TIMESTAMPTZ, state TEXT);
INSERT INTO states_test_3 VALUES
('2019-12-31 00:00:11+00', 'UNUSED'),
('2019-12-31 00:01:00+00', 'START');
```

## Functions
Expand Down Expand Up @@ -60,3 +70,213 @@ SELECT state, duration FROM toolkit_experimental.into_values(
START | 11000000
STOP | 0
```

### state_timeline

```SQL
SELECT state, start_time, end_time FROM toolkit_experimental.state_timeline(
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test))
ORDER BY start_time;
```
```output
state | start_time | end_time
------+------------------------+-----------------------
START | 2020-01-01 00:00:00+00 | 2020-01-01 00:00:11+00
OK | 2020-01-01 00:00:11+00 | 2020-01-01 00:01:00+00
ERROR | 2020-01-01 00:01:00+00 | 2020-01-01 00:01:03+00
OK | 2020-01-01 00:01:03+00 | 2020-01-01 00:02:00+00
STOP | 2020-01-01 00:02:00+00 | 2020-01-01 00:02:00+00
```

```SQL
SELECT state, start_time, end_time FROM toolkit_experimental.state_timeline(
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_2))
ORDER BY start_time;
```
```output
state | start_time | end_time
------+------------------------+-----------------------
START | 2019-12-31 00:00:00+00 | 2019-12-31 00:00:11+00
OK | 2019-12-31 00:00:11+00 | 2019-12-31 00:02:00+00
STOP | 2019-12-31 00:02:00+00 | 2019-12-31 00:02:00+00
```

## state_periods

```SQL
SELECT start_time, end_time
FROM toolkit_experimental.state_periods(
'OK',
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test)
)
ORDER BY start_time;
```
```output
start_time | end_time
-----------------------+-----------------------
2020-01-01 00:00:11+00 | 2020-01-01 00:01:00+00
2020-01-01 00:01:03+00 | 2020-01-01 00:02:00+00
```

```SQL
SELECT start_time, end_time
FROM toolkit_experimental.state_periods(
'ANYTHING',
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test)
)
ORDER BY start_time;
```
```output
start_time | end_time
-----------------------+-----------------------
```

## interpolated_state_timeline

```SQL
SELECT state, start_time, end_time FROM toolkit_experimental.interpolated_state_timeline(
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test),
'2019-12-31', '1 days',
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_3),
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_3)
)
ORDER BY start_time;
```
```output
state | start_time | end_time
------+------------------------+-----------------------
START | 2019-12-31 00:00:00+00 | 2020-01-01 00:00:11+00
OK | 2020-01-01 00:00:11+00 | 2020-01-01 00:01:00+00
ERROR | 2020-01-01 00:01:00+00 | 2020-01-01 00:01:03+00
OK | 2020-01-01 00:01:03+00 | 2020-01-01 00:02:00+00
STOP | 2020-01-01 00:02:00+00 | 2020-01-01 00:02:00+00
```

```SQL
SELECT state, start_time, end_time FROM toolkit_experimental.interpolated_state_timeline(
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test),
'2019-12-31', '5 days',
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_3),
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_3)
)
ORDER BY start_time;
```
```output
state | start_time | end_time
------+------------------------+-----------------------
START | 2019-12-31 00:00:00+00 | 2020-01-01 00:00:11+00
OK | 2020-01-01 00:00:11+00 | 2020-01-01 00:01:00+00
ERROR | 2020-01-01 00:01:00+00 | 2020-01-01 00:01:03+00
OK | 2020-01-01 00:01:03+00 | 2020-01-01 00:02:00+00
STOP | 2020-01-01 00:02:00+00 | 2020-01-05 00:00:00+00
```

```SQL
SELECT state, start_time, end_time FROM toolkit_experimental.interpolated_state_timeline(
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test),
'2019-12-31', '1 days',
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_2),
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_2)
)
ORDER BY start_time;
```
```output
state | start_time | end_time
------+------------------------+-----------------------
STOP | 2019-12-31 00:00:00+00 | 2020-01-01 00:00:00+00
START | 2020-01-01 00:00:00+00 | 2020-01-01 00:00:11+00
OK | 2020-01-01 00:00:11+00 | 2020-01-01 00:01:00+00
ERROR | 2020-01-01 00:01:00+00 | 2020-01-01 00:01:03+00
OK | 2020-01-01 00:01:03+00 | 2020-01-01 00:02:00+00
STOP | 2020-01-01 00:02:00+00 | 2020-01-01 00:02:00+00
```

```SQL
SELECT state, start_time, end_time FROM toolkit_experimental.interpolated_state_timeline(
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test),
'2019-12-31', '5 days',
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_2),
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_2)
)
ORDER BY start_time;
```
```output
state | start_time | end_time
------+------------------------+-----------------------
STOP | 2019-12-31 00:00:00+00 | 2020-01-01 00:00:00+00
START | 2020-01-01 00:00:00+00 | 2020-01-01 00:00:11+00
OK | 2020-01-01 00:00:11+00 | 2020-01-01 00:01:00+00
ERROR | 2020-01-01 00:01:00+00 | 2020-01-01 00:01:03+00
OK | 2020-01-01 00:01:03+00 | 2020-01-01 00:02:00+00
STOP | 2020-01-01 00:02:00+00 | 2020-01-05 00:00:00+00
```


## interpolated_state_periods

```SQL
SELECT start_time, end_time FROM toolkit_experimental.interpolated_state_periods(
'OK',
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test),
'2019-12-31', '1 days',
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_3),
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_3)
)
ORDER BY start_time;
```
```output
start_time | end_time
-----------------------+-----------------------
2020-01-01 00:00:11+00 | 2020-01-01 00:01:00+00
2020-01-01 00:01:03+00 | 2020-01-01 00:02:00+00
```

```SQL
SELECT start_time, end_time FROM toolkit_experimental.interpolated_state_periods(
'START',
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test),
'2019-12-31', '5 days',
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_3),
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_3)
)
ORDER BY start_time;
```
```output
start_time | end_time
-----------------------+-----------------------
2019-12-31 00:00:00+00 | 2020-01-01 00:00:11+00
```

```SQL
SELECT start_time, end_time FROM toolkit_experimental.interpolated_state_periods(
'STOP',
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test),
'2019-12-31', '1 days',
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_2),
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_2)
)
ORDER BY start_time;
```
```output
start_time | end_time
-----------------------+-----------------------
2019-12-31 00:00:00+00 | 2020-01-01 00:00:00+00
2020-01-01 00:02:00+00 | 2020-01-01 00:02:00+00
```

```SQL
SELECT start_time, end_time FROM toolkit_experimental.interpolated_state_periods(
'STOP',
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test),
'2019-12-31', '5 days',
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_2),
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_2)
)
ORDER BY start_time;
```
```output
start_time | end_time
-----------------------+-----------------------
2019-12-31 00:00:00+00 | 2020-01-01 00:00:00+00
2020-01-01 00:02:00+00 | 2020-01-05 00:00:00+00
```
Loading

0 comments on commit fb8da95

Please sign in to comment.