-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix update tests, remove cagg test for time_weight, formatting fixes
- Loading branch information
1 parent
3b4e5b8
commit 4257d72
Showing
4 changed files
with
46 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,56 @@ | ||
|
||
# Time Weighted Average Test | ||
|
||
## Test aggregating data into a TimeWeightSummary and calculating the average | ||
# Time Weighted Average Tests | ||
|
||
## Test integral and interpolated integral | ||
|
||
```sql,creation,min-toolkit-version=1.15.0 | ||
CREATE TABLE freezer_temps ( | ||
freezer_id int, | ||
ts timestamptz, | ||
temperature float); | ||
INSERT INTO freezer_temps VALUES | ||
( 1, '2020-01-01 00:00:00+00', 4.0), | ||
( 1, '2020-01-01 00:05:00+00', 5.5), | ||
( 1, '2020-01-01 00:10:00+00', 3.0), | ||
( 1, '2020-01-01 00:15:00+00', 4.0), | ||
( 1, '2020-01-01 00:20:00+00', 3.5), | ||
( 1, '2020-01-01 00:25:00+00', 8.0), | ||
( 1, '2020-01-01 00:30:00+00', 9.0), | ||
( 1, '2020-01-01 00:31:00+00', 10.5), -- door opened! | ||
( 1, '2020-01-01 00:31:30+00', 11.0), | ||
( 1, '2020-01-01 00:32:00+00', 15.0), | ||
( 1, '2020-01-01 00:32:30+00', 20.0), -- door closed | ||
( 1, '2020-01-01 00:33:00+00', 18.5), | ||
( 1, '2020-01-01 00:33:30+00', 17.0), | ||
( 1, '2020-01-01 00:34:00+00', 15.5), | ||
( 1, '2020-01-01 00:34:30+00', 14.0), | ||
( 1, '2020-01-01 00:35:00+00', 12.5), | ||
( 1, '2020-01-01 00:35:30+00', 11.0), | ||
( 1, '2020-01-01 00:36:00+00', 10.0), -- temperature stabilized | ||
( 1, '2020-01-01 00:40:00+00', 7.0), | ||
( 1, '2020-01-01 00:45:00+00', 5.0); | ||
CREATE TABLE time_weight_test(time timestamptz, value double precision, bucket timestamptz); | ||
INSERT INTO time_weight_test VALUES | ||
('2020-1-1 8:00'::timestamptz, 10.0, '2020-1-1'::timestamptz), | ||
('2020-1-1 12:00'::timestamptz, 40.0, '2020-1-1'::timestamptz), | ||
('2020-1-1 16:00'::timestamptz, 20.0, '2020-1-1'::timestamptz), | ||
('2020-1-2 2:00'::timestamptz, 15.0, '2020-1-2'::timestamptz), | ||
('2020-1-2 12:00'::timestamptz, 50.0, '2020-1-2'::timestamptz), | ||
('2020-1-2 20:00'::timestamptz, 25.0, '2020-1-2'::timestamptz), | ||
('2020-1-3 10:00'::timestamptz, 30.0, '2020-1-3'::timestamptz), | ||
('2020-1-3 12:00'::timestamptz, 0.0, '2020-1-3'::timestamptz), | ||
('2020-1-3 16:00'::timestamptz, 35.0, '2020-1-3'::timestamptz); | ||
CREATE MATERIALIZED VIEW twa AS ( | ||
SELECT time_bucket('10 mins'::interval, ts) as bucket, | ||
freezer_id, | ||
time_weight('Linear', ts, temperature) | ||
FROM freezer_temps | ||
GROUP BY bucket, freezer_id | ||
SELECT bucket, time_weight('LOCF', time, value) as agg | ||
FROM time_weight_test | ||
GROUP BY bucket | ||
); | ||
``` | ||
|
||
```sql,validation,min-toolkit-version=1.15.0 | ||
SELECT average(rollup(time_weight)) as time_weighted_average | ||
FROM twa; | ||
SELECT | ||
interpolated_integral( | ||
agg, | ||
bucket, | ||
'1 day'::interval, | ||
LAG(agg) OVER (ORDER BY bucket), | ||
LEAD(agg) OVER (ORDER BY bucket), | ||
'hours') | ||
FROM twa | ||
ORDER BY bucket; | ||
``` | ||
|
||
```output | ||
time_weighted_average | ||
interpolated_integral | ||
----------------------- | ||
6.636111111111111 | ||
360 | ||
690 | ||
310 | ||
``` | ||
|
||
```sql,validation,min-toolkit-version=1.15.0 | ||
SELECT integral(agg, 'hrs') FROM twa; | ||
``` | ||
|
||
```output | ||
integral | ||
---------- | ||
200 | ||
60 | ||
550 | ||
``` |