Skip to content

Commit

Permalink
sensors and latest model fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
russbiggs committed Sep 13, 2024
1 parent 14136b6 commit 10b75e1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
2 changes: 0 additions & 2 deletions openaq_api/openaq_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
providers,
sensors,
tiles,
trends,
licenses,
latest,
)
Expand Down Expand Up @@ -253,7 +252,6 @@ def favico():
app.include_router(manufacturers.router)
app.include_router(measurements.router)
app.include_router(owners.router)
app.include_router(trends.router)
app.include_router(providers.router)
app.include_router(sensors.router)
app.include_router(latest.router)
Expand Down
11 changes: 3 additions & 8 deletions openaq_api/openaq_api/v3/models/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ class Manufacturer(ManufacturerBase):
class Sensor(SensorBase):
datetime_first: DatetimeObject | None = None
datetime_last: DatetimeObject | None = None
coverage: Coverage
latest: LatestBase
summary: Summary
coverage: Coverage | None = None
latest: LatestBase | None = None
summary: Summary | None = None


class Latest(LatestBase):
Expand Down Expand Up @@ -243,7 +243,6 @@ class Location(JsonBase):


class Measurement(JsonBase):
# datetime: DatetimeObject
value: float
parameter: ParameterBase
period: Period | None = None
Expand All @@ -253,7 +252,6 @@ class Measurement(JsonBase):


class HourlyData(JsonBase):
# datetime: DatetimeObject
value: float | None = None # Nullable to deal with errors
parameter: ParameterBase
period: Period | None = None
Expand All @@ -263,7 +261,6 @@ class HourlyData(JsonBase):


class DailyData(JsonBase):
# datetime: DatetimeObject
value: float
parameter: ParameterBase
period: Period | None = None
Expand All @@ -273,7 +270,6 @@ class DailyData(JsonBase):


class AnnualData(JsonBase):
# datetime: DatetimeObject
value: float
parameter: ParameterBase
period: Period | None = None
Expand All @@ -287,7 +283,6 @@ class Trend(JsonBase):
factor: Factor
value: float
parameter: ParameterBase
# coordinates: Coordinates | None
summary: Summary
coverage: Coverage

Expand Down
2 changes: 1 addition & 1 deletion openaq_api/openaq_api/v3/routers/latest.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ async def fetch_latest(query, db):
timezones t ON (n.timezones_id = t.timezones_id)
JOIN
measurands m ON (s.measurands_id = m.measurands_id)
LEFT JOIN
INNER JOIN
sensors_rollup r ON (s.sensors_id = r.sensors_id)
{query_builder.where()}
{query_builder.pagination()}
Expand Down
37 changes: 25 additions & 12 deletions openaq_api/openaq_api/v3/routers/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async def fetch_sensors(q, db):

logger.debug(query.params())
sql = f"""
SELECT s.sensors_id as id
SELECT s.sensors_id as id
, m.measurand||' '||m.units as name
, json_build_object(
'id', m.measurands_id
Expand All @@ -82,28 +82,41 @@ async def fetch_sensors(q, db):
, 'display_name', m.display
) as parameter
, s.sensors_id
, json_build_object(
, CASE
WHEN r.value_latest IS NOT NULL THEN
json_build_object(
'min', r.value_min
, 'max', r.value_max
, 'avg', r.value_avg
, 'sd', r.value_sd
) as summary
, calculate_coverage(
r.value_count
, s.data_averaging_period_seconds
, s.data_logging_period_seconds
, r.datetime_first
, r.datetime_last
) as coverage
)
ELSE NULL
END as summary
, CASE
WHEN r.value_latest IS NOT NULL THEN
jsonb_build_object(
'datetime_from', get_datetime_object(r.datetime_first, t.tzid),
'datetime_to', get_datetime_object(r.datetime_last, t.tzid)
) || calculate_coverage(
r.value_count,
s.data_averaging_period_seconds,
s.data_logging_period_seconds
)::jsonb
ELSE NULL
END as coverage
, get_datetime_object(r.datetime_first, t.tzid) as datetime_first
, get_datetime_object(r.datetime_last, t.tzid) as datetime_last
, json_build_object(
,CASE
WHEN r.value_latest IS NOT NULL THEN
json_build_object(
'datetime', get_datetime_object(r.datetime_last, t.tzid)
, 'value', r.value_latest
, 'coordinates', json_build_object(
'latitude', st_y(COALESCE(r.geom_latest, n.geom))
,'longitude', st_x(COALESCE(r.geom_latest, n.geom))
)) as latest
))
ELSE NULL
END as latest
FROM sensors s
JOIN sensor_systems sy ON (s.sensor_systems_id = sy.sensor_systems_id)
JOIN sensor_nodes n ON (sy.sensor_nodes_id = n.sensor_nodes_id)
Expand Down

0 comments on commit 10b75e1

Please sign in to comment.