add barometric pressure
This commit is contained in:
@@ -11,18 +11,19 @@ import (
|
||||
)
|
||||
|
||||
type ObservationPoint struct {
|
||||
TS time.Time `json:"ts"`
|
||||
TempC *float64 `json:"temp_c,omitempty"`
|
||||
RH *float64 `json:"rh,omitempty"`
|
||||
WindMS *float64 `json:"wind_m_s,omitempty"`
|
||||
WindGustMS *float64 `json:"wind_gust_m_s,omitempty"`
|
||||
WindDirDeg *float64 `json:"wind_dir_deg,omitempty"`
|
||||
UVI *float64 `json:"uvi,omitempty"`
|
||||
LightLux *float64 `json:"light_lux,omitempty"`
|
||||
BatteryMV *float64 `json:"battery_mv,omitempty"`
|
||||
SupercapV *float64 `json:"supercap_v,omitempty"`
|
||||
RainMM *float64 `json:"rain_mm,omitempty"`
|
||||
RainStart *int64 `json:"rain_start,omitempty"`
|
||||
TS time.Time `json:"ts"`
|
||||
TempC *float64 `json:"temp_c,omitempty"`
|
||||
RH *float64 `json:"rh,omitempty"`
|
||||
PressureHPA *float64 `json:"pressure_hpa,omitempty"`
|
||||
WindMS *float64 `json:"wind_m_s,omitempty"`
|
||||
WindGustMS *float64 `json:"wind_gust_m_s,omitempty"`
|
||||
WindDirDeg *float64 `json:"wind_dir_deg,omitempty"`
|
||||
UVI *float64 `json:"uvi,omitempty"`
|
||||
LightLux *float64 `json:"light_lux,omitempty"`
|
||||
BatteryMV *float64 `json:"battery_mv,omitempty"`
|
||||
SupercapV *float64 `json:"supercap_v,omitempty"`
|
||||
RainMM *float64 `json:"rain_mm,omitempty"`
|
||||
RainStart *int64 `json:"rain_start,omitempty"`
|
||||
}
|
||||
|
||||
type ForecastPoint struct {
|
||||
@@ -59,26 +60,54 @@ func (d *DB) ObservationSeries(ctx context.Context, site, bucket string, start,
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
WITH ws AS (
|
||||
SELECT
|
||||
time_bucket(INTERVAL '%s', ts) AS bucket,
|
||||
avg(temperature_c) AS temp_c_avg,
|
||||
avg(humidity) AS rh_avg,
|
||||
avg(wind_avg_m_s) AS wind_avg_ms_avg,
|
||||
max(wind_max_m_s) AS wind_gust_ms_max,
|
||||
avg(wind_dir_deg) AS wind_dir_deg_avg,
|
||||
max(uvi) AS uvi_max,
|
||||
max(light_lux) AS light_lux_max,
|
||||
avg(battery_mv) AS battery_mv_avg,
|
||||
avg(supercap_v) AS supercap_v_avg,
|
||||
avg(rain_mm) AS rain_mm_avg,
|
||||
max(rain_start) AS rain_start_max
|
||||
FROM observations_ws90
|
||||
WHERE site = $1
|
||||
AND ts >= $2
|
||||
AND ts <= $3
|
||||
GROUP BY bucket
|
||||
),
|
||||
baro AS (
|
||||
SELECT
|
||||
time_bucket(INTERVAL '%s', ts) AS bucket,
|
||||
avg(pressure_hpa) AS pressure_hpa_avg
|
||||
FROM observations_baro
|
||||
WHERE site = $1
|
||||
AND ts >= $2
|
||||
AND ts <= $3
|
||||
GROUP BY bucket
|
||||
)
|
||||
SELECT
|
||||
time_bucket(INTERVAL '%s', ts) AS bucket,
|
||||
avg(temperature_c) AS temp_c_avg,
|
||||
avg(humidity) AS rh_avg,
|
||||
avg(wind_avg_m_s) AS wind_avg_ms_avg,
|
||||
max(wind_max_m_s) AS wind_gust_ms_max,
|
||||
avg(wind_dir_deg) AS wind_dir_deg_avg,
|
||||
max(uvi) AS uvi_max,
|
||||
max(light_lux) AS light_lux_max,
|
||||
avg(battery_mv) AS battery_mv_avg,
|
||||
avg(supercap_v) AS supercap_v_avg,
|
||||
avg(rain_mm) AS rain_mm_avg,
|
||||
max(rain_start) AS rain_start_max
|
||||
FROM observations_ws90
|
||||
WHERE site = $1
|
||||
AND ts >= $2
|
||||
AND ts <= $3
|
||||
GROUP BY bucket
|
||||
COALESCE(ws.bucket, baro.bucket) AS bucket,
|
||||
ws.temp_c_avg,
|
||||
ws.rh_avg,
|
||||
baro.pressure_hpa_avg,
|
||||
ws.wind_avg_ms_avg,
|
||||
ws.wind_gust_ms_max,
|
||||
ws.wind_dir_deg_avg,
|
||||
ws.uvi_max,
|
||||
ws.light_lux_max,
|
||||
ws.battery_mv_avg,
|
||||
ws.supercap_v_avg,
|
||||
ws.rain_mm_avg,
|
||||
ws.rain_start_max
|
||||
FROM ws
|
||||
FULL OUTER JOIN baro ON ws.bucket = baro.bucket
|
||||
ORDER BY bucket ASC
|
||||
`, interval)
|
||||
`, interval, interval)
|
||||
|
||||
rows, err := d.Pool.Query(ctx, query, site, start, end)
|
||||
if err != nil {
|
||||
@@ -90,27 +119,28 @@ func (d *DB) ObservationSeries(ctx context.Context, site, bucket string, start,
|
||||
for rows.Next() {
|
||||
var (
|
||||
ts time.Time
|
||||
temp, rh, wind, gust sql.NullFloat64
|
||||
temp, rh, pressure, wind, gust sql.NullFloat64
|
||||
dir, uvi, light, battery, supercap sql.NullFloat64
|
||||
rainMM sql.NullFloat64
|
||||
rainStart sql.NullInt64
|
||||
)
|
||||
if err := rows.Scan(&ts, &temp, &rh, &wind, &gust, &dir, &uvi, &light, &battery, &supercap, &rainMM, &rainStart); err != nil {
|
||||
if err := rows.Scan(&ts, &temp, &rh, &pressure, &wind, &gust, &dir, &uvi, &light, &battery, &supercap, &rainMM, &rainStart); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
points = append(points, ObservationPoint{
|
||||
TS: ts,
|
||||
TempC: nullFloatPtr(temp),
|
||||
RH: nullFloatPtr(rh),
|
||||
WindMS: nullFloatPtr(wind),
|
||||
WindGustMS: nullFloatPtr(gust),
|
||||
WindDirDeg: nullFloatPtr(dir),
|
||||
UVI: nullFloatPtr(uvi),
|
||||
LightLux: nullFloatPtr(light),
|
||||
BatteryMV: nullFloatPtr(battery),
|
||||
SupercapV: nullFloatPtr(supercap),
|
||||
RainMM: nullFloatPtr(rainMM),
|
||||
RainStart: nullIntPtr(rainStart),
|
||||
TS: ts,
|
||||
TempC: nullFloatPtr(temp),
|
||||
RH: nullFloatPtr(rh),
|
||||
PressureHPA: nullFloatPtr(pressure),
|
||||
WindMS: nullFloatPtr(wind),
|
||||
WindGustMS: nullFloatPtr(gust),
|
||||
WindDirDeg: nullFloatPtr(dir),
|
||||
UVI: nullFloatPtr(uvi),
|
||||
LightLux: nullFloatPtr(light),
|
||||
BatteryMV: nullFloatPtr(battery),
|
||||
SupercapV: nullFloatPtr(supercap),
|
||||
RainMM: nullFloatPtr(rainMM),
|
||||
RainStart: nullIntPtr(rainStart),
|
||||
})
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
@@ -126,6 +156,7 @@ func (d *DB) LatestObservation(ctx context.Context, site string) (*ObservationPo
|
||||
ts,
|
||||
temperature_c,
|
||||
humidity,
|
||||
(SELECT pressure_hpa FROM observations_baro WHERE site = $1 ORDER BY ts DESC LIMIT 1) AS pressure_hpa,
|
||||
wind_avg_m_s,
|
||||
wind_max_m_s,
|
||||
wind_dir_deg,
|
||||
@@ -143,12 +174,12 @@ func (d *DB) LatestObservation(ctx context.Context, site string) (*ObservationPo
|
||||
|
||||
var (
|
||||
ts time.Time
|
||||
temp, rh, wind, gust sql.NullFloat64
|
||||
temp, rh, pressure, wind, gust sql.NullFloat64
|
||||
dir, uvi, light, battery, supercap sql.NullFloat64
|
||||
rainMM sql.NullFloat64
|
||||
rainStart sql.NullInt64
|
||||
)
|
||||
err := d.Pool.QueryRow(ctx, query, site).Scan(&ts, &temp, &rh, &wind, &gust, &dir, &uvi, &light, &battery, &supercap, &rainMM, &rainStart)
|
||||
err := d.Pool.QueryRow(ctx, query, site).Scan(&ts, &temp, &rh, &pressure, &wind, &gust, &dir, &uvi, &light, &battery, &supercap, &rainMM, &rainStart)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
@@ -157,18 +188,19 @@ func (d *DB) LatestObservation(ctx context.Context, site string) (*ObservationPo
|
||||
}
|
||||
|
||||
return &ObservationPoint{
|
||||
TS: ts,
|
||||
TempC: nullFloatPtr(temp),
|
||||
RH: nullFloatPtr(rh),
|
||||
WindMS: nullFloatPtr(wind),
|
||||
WindGustMS: nullFloatPtr(gust),
|
||||
WindDirDeg: nullFloatPtr(dir),
|
||||
UVI: nullFloatPtr(uvi),
|
||||
LightLux: nullFloatPtr(light),
|
||||
BatteryMV: nullFloatPtr(battery),
|
||||
SupercapV: nullFloatPtr(supercap),
|
||||
RainMM: nullFloatPtr(rainMM),
|
||||
RainStart: nullIntPtr(rainStart),
|
||||
TS: ts,
|
||||
TempC: nullFloatPtr(temp),
|
||||
RH: nullFloatPtr(rh),
|
||||
PressureHPA: nullFloatPtr(pressure),
|
||||
WindMS: nullFloatPtr(wind),
|
||||
WindGustMS: nullFloatPtr(gust),
|
||||
WindDirDeg: nullFloatPtr(dir),
|
||||
UVI: nullFloatPtr(uvi),
|
||||
LightLux: nullFloatPtr(light),
|
||||
BatteryMV: nullFloatPtr(battery),
|
||||
SupercapV: nullFloatPtr(supercap),
|
||||
RainMM: nullFloatPtr(rainMM),
|
||||
RainStart: nullIntPtr(rainStart),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user