68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
// conditions_queries.go contains SQL text for current-conditions reads.
|
|
// Layer: adapters/outbound/postgres conditions feature.
|
|
package postgres
|
|
|
|
const (
|
|
queryCurrentConditions = `
|
|
WITH windowed AS (
|
|
SELECT
|
|
temperature_c,
|
|
apparent_temperature_c,
|
|
dewpoint_c,
|
|
relative_humidity_percent,
|
|
wind_speed_kmh,
|
|
wind_direction_degrees,
|
|
is_day,
|
|
observed_at
|
|
FROM observations
|
|
WHERE observed_at > CURRENT_TIMESTAMP - make_interval(mins => $1)
|
|
)
|
|
SELECT
|
|
COUNT(*) AS sample_count,
|
|
AVG(temperature_c) AS temperature_c,
|
|
AVG(apparent_temperature_c) AS apparent_temperature_c,
|
|
AVG(dewpoint_c) AS dewpoint_c,
|
|
AVG(relative_humidity_percent) AS relative_humidity_percent,
|
|
AVG(wind_speed_kmh) AS wind_speed_kmh,
|
|
CASE
|
|
WHEN atan2d(
|
|
AVG(sind(wind_direction_degrees)),
|
|
AVG(cosd(wind_direction_degrees))
|
|
) < 0
|
|
THEN atan2d(
|
|
AVG(sind(wind_direction_degrees)),
|
|
AVG(cosd(wind_direction_degrees))
|
|
) + 360.0
|
|
ELSE atan2d(
|
|
AVG(sind(wind_direction_degrees)),
|
|
AVG(cosd(wind_direction_degrees))
|
|
)
|
|
END AS wind_direction_degrees,
|
|
(
|
|
SELECT is_day
|
|
FROM windowed
|
|
ORDER BY observed_at DESC
|
|
LIMIT 1
|
|
) AS is_day
|
|
FROM windowed`
|
|
|
|
queryCurrentConditionsConditionCodeCandidates = `
|
|
WITH ranked AS (
|
|
SELECT
|
|
event_source,
|
|
condition_code,
|
|
ROW_NUMBER() OVER (
|
|
PARTITION BY event_source
|
|
ORDER BY observed_at DESC, event_emitted_at DESC
|
|
) AS source_rank
|
|
FROM observations
|
|
WHERE observed_at > CURRENT_TIMESTAMP - make_interval(mins => $1)
|
|
)
|
|
SELECT
|
|
event_source,
|
|
condition_code
|
|
FROM ranked
|
|
WHERE source_rank = 1
|
|
ORDER BY event_source`
|
|
)
|