-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monthly-report.sql
209 lines (200 loc) · 6.82 KB
/
monthly-report.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
-- Reporting Script intended to be run from something like cron
-- on a monthly basis to provide stats on the previous month.
\set QUIET 1
\timing off
\pset footer off
\x on
\set QUIET 0
-- Monthly Stats
SELECT
date_trunc('month', (data ->> 'time')::timestamptz)::date AS "Outdoor",
MIN(((data -> 'fields') -> 'temperature_F')::numeric) AS "Monthly Low",
MAX(((data -> 'fields') -> 'temperature_F')::numeric) AS "Monthly High",
AVG(((data -> 'fields') -> 'temperature_F')::numeric)::numeric(7,4) AS "Average",
MAX(((data -> 'fields') -> 'wind_speed_mph')::numeric) AS "Max Wind"
FROM weather
WHERE
(data ->> 'measurement') LIKE '%3n1%'
AND
(data ->> 'time')::timestamptz BETWEEN date_trunc('month', current_date - interval '1 month') and date_trunc('month', current_date)
GROUP BY 1
ORDER BY 1 ASC
LIMIT 1
;
-- Daily stats
\set QUIET 1
\x auto
\set QUIET 0
SELECT
date_trunc('day', (data ->> 'time')::timestamptz)::date AS "Date",
MIN(((data -> 'fields') -> 'temperature_F')::numeric) AS "Daily Low",
MAX(((data -> 'fields') -> 'temperature_F')::numeric) AS "Daily High",
AVG(((data -> 'fields') -> 'temperature_F')::numeric)::numeric(7,4) AS "Average Temp",
MAX(((data -> 'fields') -> 'wind_speed_mph')::numeric) AS "Max Wind",
AVG(((data -> 'fields') -> 'wind_speed_mph')::numeric)::numeric(7,4) AS "Avg Wind"
FROM weather
WHERE
(data ->> 'measurement') LIKE '%3n1%'
AND
(data ->> 'time')::timestamptz BETWEEN date_trunc('month', CURRENT_DATE - interval '1 month') and date_trunc('month', CURRENT_DATE)
GROUP BY 1
ORDER BY 1 ASC
;
-- Highs and Lows
\set QUIET 1
\x on
\set QUIET 0
WITH stats AS (
SELECT
date_trunc('day', (data ->> 'time')::timestamptz)::date AS "Outdoor",
MAX(((data -> 'fields') -> 'temperature_F')::numeric) AS "High"
FROM weather
WHERE
(data ->> 'measurement') LIKE '%3n1%'
AND
(data ->> 'time')::timestamptz BETWEEN date_trunc('month', current_date - interval '1 month') and date_trunc('month', current_date)
GROUP BY 1
HAVING MAX(((data -> 'fields') -> 'temperature_F')::numeric) > 89
ORDER BY 1 ASC
)
SELECT
COUNT(*) AS "Days above 90"
FROM stats
;
\set QUIET 1
\x off
\set QUIET 0
SELECT
date_trunc('day', (data ->> 'time')::timestamptz)::date AS "Outdoor",
MAX(((data -> 'fields') -> 'temperature_F')::numeric) AS "High"
FROM weather
WHERE
(data ->> 'measurement') LIKE '%3n1%'
AND
(data ->> 'time')::timestamptz BETWEEN date_trunc('month', current_date - interval '1 month') and date_trunc('month', current_date)
GROUP BY 1
HAVING MAX(((data -> 'fields') -> 'temperature_F')::numeric) > 89
ORDER BY 1 ASC
;
\set QUIET 1
\x on
\set QUIET 0
WITH stats AS (
SELECT
date_trunc('day', (data ->> 'time')::timestamptz)::date AS "Outdoor",
MIN(((data -> 'fields') -> 'temperature_F')::numeric) AS "Low"
FROM weather
WHERE
(data ->> 'measurement') LIKE '%3n1%'
AND
(data ->> 'time')::timestamptz BETWEEN date_trunc('month', current_date - interval '1 month') and date_trunc('month', current_date)
GROUP BY 1
HAVING MIN(((data -> 'fields') -> 'temperature_F')::numeric) < 33
ORDER BY 1 ASC
)
SELECT
COUNT(*) AS "Days below freezing"
FROM stats
;
\set QUIET 1
\x off
\set QUIET 0
SELECT
date_trunc('day', (data ->> 'time')::timestamptz)::date AS "Outdoor",
MIN(((data -> 'fields') -> 'temperature_F')::numeric) AS "Low"
FROM weather
WHERE
(data ->> 'measurement') LIKE '%3n1%'
AND
(data ->> 'time')::timestamptz BETWEEN date_trunc('month', current_date - interval '1 month') and date_trunc('month', current_date)
GROUP BY 1
HAVING MIN(((data -> 'fields') -> 'temperature_F')::numeric) < 33
ORDER BY 1 ASC
;
-- Living Room stats
\set QUIET 1
\x on
\set QUIET 0
SELECT
date_trunc('month', (data ->> 'time')::timestamptz)::date AS "Living Room",
MIN(((data -> 'fields') -> 'temperature_F')::numeric) AS "Monthly Low",
MAX(((data -> 'fields') -> 'temperature_F')::numeric) AS "Monthly High",
AVG(((data -> 'fields') -> 'temperature_F')::numeric)::numeric(7,4) AS "Average Temp",
MIN(((data -> 'fields') -> 'humidity')::numeric) AS "Min Humidity",
MAX(((data -> 'fields') -> 'humidity')::numeric) AS "Max Humidity",
AVG(((data -> 'fields') -> 'humidity')::numeric)::numeric(7,4) AS "Avg Humidity"
FROM weather
WHERE
(data -> 'tags' ->> 'id') like '11855'
AND
(data ->> 'time')::timestamptz BETWEEN date_trunc('month', CURRENT_DATE - interval '1 month') and date_trunc('month', CURRENT_DATE)
GROUP BY 1
ORDER BY 1 ASC
;
-- Server Room stats
\set QUIET 1
\x on
\set QUIET 0
SELECT
date_trunc('month', (data ->> 'time')::timestamptz)::date AS "Server Room",
MIN(((data -> 'fields') -> 'temperature_F')::numeric) AS "Monthly Low",
MAX(((data -> 'fields') -> 'temperature_F')::numeric) AS "Monthly High",
AVG(((data -> 'fields') -> 'temperature_F')::numeric)::numeric(7,4) AS "Average Temp",
MIN(((data -> 'fields') -> 'humidity')::numeric) AS "Min Humidity",
MAX(((data -> 'fields') -> 'humidity')::numeric) AS "Max Humidity",
AVG(((data -> 'fields') -> 'humidity')::numeric)::numeric(7,4) AS "Avg Humidity"
FROM weather
WHERE
(data -> 'tags' ->> 'id') like '13064'
AND
(data ->> 'time')::timestamptz BETWEEN date_trunc('month', CURRENT_DATE - interval '1 month') and date_trunc('month', CURRENT_DATE)
GROUP BY 1
ORDER BY 1 ASC
;
-- Garage stats
SELECT
date_trunc('month', (data ->> 'time')::timestamptz)::date AS "Garage",
MIN(((data -> 'fields') -> 'temperature_F')::numeric) AS "Monthly Low",
MAX(((data -> 'fields') -> 'temperature_F')::numeric) AS "Monthly High",
AVG(((data -> 'fields') -> 'temperature_F')::numeric)::numeric(7,4) AS "Average Temp"
FROM weather
WHERE
(data -> 'tags' ->> 'id') like '9359'
AND
(data ->> 'time')::timestamptz BETWEEN date_trunc('month', CURRENT_DATE - interval '1 month') and date_trunc('month', CURRENT_DATE)
GROUP BY 1
ORDER BY 1 ASC
;
-- Shed stats
SELECT
date_trunc('month', (data ->> 'time')::timestamptz)::date AS "Shed",
MIN(((data -> 'fields') -> 'temperature_F')::numeric) AS "Monthly Low",
MAX(((data -> 'fields') -> 'temperature_F')::numeric) AS "Monthly High",
AVG(((data -> 'fields') -> 'temperature_F')::numeric)::numeric(7,4) AS "Average Temp",
MIN(((data -> 'fields') -> 'humidity')::numeric) AS "Min Humidity",
MAX(((data -> 'fields') -> 'humidity')::numeric) AS "Max Humidity",
AVG(((data -> 'fields') -> 'humidity')::numeric)::numeric(7,4) AS "Avg Humidity"
FROM weather
WHERE
(data -> 'tags' ->> 'id') like '3377'
AND
(data ->> 'time')::timestamptz BETWEEN date_trunc('month', CURRENT_DATE - interval '1 month') and date_trunc('month', CURRENT_DATE)
GROUP BY 1
ORDER BY 1 ASC
;
-- ADS/B Stats
\set QUIET 1
\x off
\c adsb
\set QUIET 0
SELECT
date_trunc('day', parsed_time::timestamptz)::date AS "ADS/B Stats",
COUNT("transmission_type") as "Messages"
FROM adsb.adsb_messages
WHERE
parsed_time::timestamptz between date_trunc('month', CURRENT_DATE - interval '1 month') and date_trunc('month', CURRENT_DATE)
GROUP BY
1
ORDER BY
1 ASC
;