-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-interval.sql
87 lines (85 loc) · 2.7 KB
/
custom-interval.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
--explain analyze verbose
SELECT
agent.name AS agent,
stat.name AS stat,
upload_info.uploaded_at AS uploaded_at,
-- baseline_history.upload_id AS baseline_upload_id,
baseline_history.num_value AS baseline,
current_history.num_value AS current,
one_week_ago_upload.uploaded_at AS one_week_ago,
one_week_ago_history.num_value AS one_week_ago_value,
current_history.num_value - baseline_history.num_value AS gained,
ROUND((current_history.num_value - baseline_history.num_value) / (NULLIF(extract(epoch from (current_upload.uploaded_at - baseline_upload.uploaded_at)), 0) / 86400)::numeric, 2) AS rate_per_day,
current_history.num_value - one_week_ago_history.num_value AS gained_last_week,
ROUND((current_history.num_value - one_week_ago_history.num_value) / (NULLIF(extract(epoch from (current_upload.uploaded_at - one_week_ago_upload.uploaded_at)), 0) / 86400)::numeric, 2) AS rate_last_week
FROM
(
SELECT
agent.agent_id AS agent_id,
upload.upload_id,
upload.uploaded_at,
FIRST_VALUE(upload_id) OVER upload_window AS baseline_upload_id,
LAST_VALUE(upload_id) OVER upload_window AS last_upload_id,
FIRST_VALUE(upload_id) OVER (
PARTITION BY upload.agent_id
ORDER BY upload.uploaded_at
RANGE BETWEEN
'7 days'::interval PRECEDING
AND
UNBOUNDED FOLLOWING
) AS one_week_ago_upload_id
FROM
agent JOIN upload USING (agent_id)
WHERE
upload.uploaded_at BETWEEN '2024-08-01' AND '2024-08-23'
WINDOW
upload_window AS (
PARTITION BY upload.agent_id
ORDER BY upload.uploaded_at
RANGE BETWEEN
UNBOUNDED PRECEDING
AND
UNBOUNDED FOLLOWING
)
) upload_info
JOIN stat ON (true)
JOIN agent ON (
upload_info.agent_id = agent.agent_id
)
JOIN upload AS baseline_upload ON (
upload_info.baseline_upload_id = baseline_upload.upload_id
)
JOIN history AS baseline_history ON (
upload_info.baseline_upload_id = baseline_history.upload_id
AND
stat.stat_id = baseline_history.stat_id
)
JOIN upload AS one_week_ago_upload ON (
upload_info.one_week_ago_upload_id = one_week_ago_upload.upload_id
)
JOIN history AS one_week_ago_history ON (
upload_info.one_week_ago_upload_id = one_week_ago_history.upload_id
AND
stat.stat_id = one_week_ago_history.stat_id
)
JOIN upload AS current_upload ON (
upload_info.upload_id = current_upload.upload_id
)
JOIN history AS current_history ON (
upload_info.upload_id = current_history.upload_id
AND
stat.stat_id = current_history.stat_id
)
--WHERE
-- agent.name IN ('eigood', 'Shooters42', 'peagmj', 'IISrPitufoII')
-- agent.name IN ('eigood', 'CurtisEFlush')
-- AND
-- stat.name = 'lifetime_ap'
WHERE
stat.name != 'ap'
AND
agent.name = 'eigood'
ORDER BY
agent.name,
stat.name,
upload_info.uploaded_at