-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
“Giems”
committed
Feb 22, 2024
1 parent
cf93a57
commit 42b6c83
Showing
7 changed files
with
197 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
----------------- Daily Average session duration ----------------- | ||
--- View | ||
CREATE MATERIALIZED VIEW avg_session_duration_per_app_daily | ||
WITH (timescaledb.continuous) AS | ||
SELECT | ||
app_id, | ||
time_bucket('1 day'::interval, session_open_timestamp) AS daily_bucket, | ||
stats_agg(EXTRACT(EPOCH FROM (session_close_timestamp - session_open_timestamp))) AS daily_session_stats | ||
FROM | ||
sessions | ||
WHERE | ||
session_close_timestamp IS NOT NULL | ||
GROUP BY | ||
app_id, daily_bucket | ||
WITH NO DATA; | ||
|
||
--- Refresh policy | ||
SELECT add_continuous_aggregate_policy('avg_session_duration_per_app_daily', | ||
start_offset => INTERVAL '14 days', | ||
end_offset => INTERVAL '1 hour', | ||
schedule_interval => INTERVAL '1 hour'); | ||
|
||
--- Real time aggregation | ||
ALTER MATERIALIZED VIEW avg_session_duration_per_app_daily set (timescaledb.materialized_only = false); | ||
|
||
----------------- Monthly Average session duration ----------------- | ||
--- View | ||
CREATE MATERIALIZED VIEW avg_session_duration_per_app_monthly | ||
WITH (timescaledb.continuous) AS | ||
SELECT | ||
app_id, | ||
time_bucket('1 month'::interval, daily_bucket) AS monthly_bucket, | ||
average(rollup(daily_session_stats)) AS avg_monthly_session_duration_seconds | ||
FROM | ||
avg_session_duration_per_app_daily | ||
GROUP BY | ||
app_id, monthly_bucket | ||
WITH NO DATA; | ||
|
||
--- Refresh policy | ||
SELECT add_continuous_aggregate_policy('avg_session_duration_per_app_monthly', | ||
start_offset => INTERVAL '3 months', | ||
end_offset => INTERVAL '1 day', | ||
schedule_interval => INTERVAL '1 day'); | ||
|
||
--- Real time aggregation | ||
ALTER MATERIALIZED VIEW avg_session_duration_per_app_monthly set (timescaledb.materialized_only = false); |
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
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