Repository navigation
refactor(edr): create and update backing views (BDMS-1599) - #1062
jacob-a-brown wants to merge 45 commits into
Conversation
The parameters in an EDR are discovered from the database and not hardcoded in yaml. This commit ensures that the field description defaults to the parameter name when discovered from the database.
…ion_name for backwards compatability
… into BDMS-1599-backing-view-for-manual-water-levels
… into BDMS-1599-backing-view-for-manual-water-levels
… into BDMS-1599-backing-view-for-manual-water-levels
|
@ksmuczynski Should the stack be in the opposite order because this is the one where all of the tests pass? So this goes into |
Coverage❌ 43.40% total — gate is 75%. Coverage for the Python files changed in this PR
|
There was a problem hiding this comment.
🟡 Changes recommended
Active findings identify incorrect unit conversion, duplicate location associations that can produce inconsistent coordinates, inclusion of non-well observations, and duplicate category IDs.
4 open issues
What changed in this PR
This PR adds backing views for OcotilloAPI’s refactored OGC API–EDR collections and updates provider metadata handling.
Changes:
- Adds public and internal manual-waterlevel views with station properties and publication filters.
- Adds
nameto the chemistry materialized views while retainingstation_namefor compatibility. - Updates location deduplication, lexicon categories, optional units, and parameter descriptions, with corresponding tests and documentation.
| File | Description |
|---|---|
tests/test_ogc_water_well_field_operations.py |
Explicitly clears the formation code for the null-description test. |
tests/test_ogc_manual_waterlevels.py |
Updates view assertions and tests parameter metadata and schema enums. |
tests/test_edr_provider.py |
Tests lexicon category construction and categorical metadata. |
tests/conftest.py |
Initializes category metadata in the database-free provider fixture. |
docs/ogc-waterlevels-data-contract.md |
Clarifies reason-only queries and null reason values. |
core/pygeoapi.py |
Adds lexicon-category configuration to EDR parameters. |
core/edr_provider.py |
Updates parameter discovery, optional units, categories, and unique location selection. |
alembic/versions/7ae4753c9b3e_create_ogc_manual_waterlevels_views.py |
Creates public and internal manual-waterlevel views. |
alembic/versions/469c72f7c334_add_name_to_water_chemistry_views.py |
Rebuilds chemistry materialized views with the compatible name column. |
Review effort: Balanced
… into BDMS-1599-backing-view-for-manual-water-levels
|
@jirhiker I can add the two parameters to |
… into BDMS-1599-backing-view-for-manual-water-levels
… into BDMS-1599-backing-view-for-manual-water-levels
… into BDMS-1599-backing-view-for-manual-water-levels
… into BDMS-1599-backing-view-for-manual-water-levels
… into BDMS-1599-backing-view-for-manual-water-levels
… into BDMS-1599-backing-view-for-manual-water-levels
… into BDMS-1599-backing-view-for-manual-water-levels
… into BDMS-1599-backing-view-for-manual-water-levels
Coverage✅ 82.32% total — gate is 75%. Coverage for the Python files changed in this PR
|
|
Parameters are now hard-coded and described in @ksmuczynski and @likithabommasani21 I pulled the latest updates from |
… into BDMS-1599-backing-view-for-manual-water-levels
| JOIN LATERAL ( | ||
| SELECT location_id | ||
| FROM location_thing_association | ||
| WHERE thing_id = t.id AND effective_end IS NULL | ||
| ORDER BY effective_start DESC, location_id DESC | ||
| LIMIT 1 | ||
| ) lta ON TRUE |
There was a problem hiding this comment.
I ran the /locations listing query (SELECT DISTINCT ON (thing_id) ... ORDER BY thing_id) over this view's SELECT, inline and read-only, against ocotillo-staging. It returned one row for each of 2,205 wells, built from 12,760 observations, and took 12.2 to 12.6 s over three runs.
Nearly all of that time is spent in this JOIN LATERAL. Postgres evaluates the subquery once per observation rather than once per well, 12,760 times in total. Because the only index on location_thing_association is its primary key (location_id, thing_id, id) and thing_id is not the leading column, Postgres scans the entire table (~11,300 rows) on every iteration. EXPLAIN ANALYZE shows the subquery averaging 0.97 ms across 12,760 executions, accounting for 12.4 s of the 12.6 s total.
ADR3 defines /locations as the primary query pattern for this collection, and the data contract lists WeaverAPI as a consumer, so a 12-second listing query is a problem. Large /area or /cube queries read the same view rows and should be as slow, though I did not time them. The single-well query for /locations/{id} took ~2 ms for the well with the most observations (43), since the subquery only runs for that well's observations.
We might consider resolving each well's current location once in a derived table and joining on thing_id:
JOIN (
SELECT DISTINCT ON (thing_id) thing_id, location_id
FROM location_thing_association
WHERE effective_end IS NULL
ORDER BY thing_id, effective_start DESC, location_id DESC
) lta ON lta.thing_id = t.idBecause the ORDER BY (effective_start DESC, location_id DESC) matches the existing subquery, it selects the same location for every well. I verified this on staging with EXCEPT queries in both directions, which returned zero differences. With this change, the listing query dropped to 0.17 s on staging while /locations/{id} stayed at ~2 ms. Placing it in _create_view fixes both ogc_manual_waterlevels and ogc_internal_manual_waterlevels without requiring a schema migration to add an index on location_thing_association (thing_id).
There was a problem hiding this comment.
Good catch! I just got this to work in a logical manner, but didn't consider how this would severely slow down the queries. I'll use your suggested subquery and join
There was a problem hiding this comment.
@ksmuczynski I pushed that update to this PR. Would you run and time it again?
There was a problem hiding this comment.
Looks good! Times are down.
This reverts commit 80c9d13.
… into BDMS-1599-backing-view-for-manual-water-levels
|
@ksmuczynski I added some more documentation to the README. Think that this should go in its own document in |


Why
This PR addresses the following problem / context:
ogc_manual_waterlevelsEDR collection needed to be createdstation_name->namerefactor for EDR collections a columnnameneeded to be added to the currentogc_water_chemistrymaterialized view. This enables backwards compatibility and is not the full update for water chemistry. That'll happen in a future ticket.SELECT DISTINCT ON (thing_id) ...to get unique locations for/locationsHow
Implementation summary: the following was changed / added / removed:
Views
ogc_manual_waterlevelsandogc_internal_manual_waterlevelsnametoogc_waterchemistryto make it compatible with the EDR refactor. I did not removestation_namehere. When I update water chemistry EDR in a future ticket I'll remake it to have all of the requirements in the contract (I assume the FeatureCollection will parallel this, so for example it'll have alternate ids and geologic formation information)EDR Provider
get_parametersso that units are not required. The OGC standard says that units don't need to exist, but pygeoapi enforces them. This is whyget_parametersneeded to be overwrittenAuthor Checklist
Reviewer Checklist
Notes
Any special considerations, known limitations, risks, workarounds, or follow-up work to note?
ogc_manual_waterlevelsusesrelease_status = 'public'forobservation,thing, andalternate_idogc_waterlevelsandogc_internal_waterlevelsviews. Those are just views, not materialized views, and so take up no memory besides storing their queries.ogc_waterchemistryandogc_transducer_waterlevelsviews and not materialized views in the future so we don't have to worry about refreshes. (Or perhaps transducer should be materialized because of the sheer amount of data? That's a problem for future us, though, and not pertinent to this PR.)Deploy
Existing databases only get the new term when someone runs
oco initialize-lexicon, the same as the reason term added in #944. Until that runs on staging/prod, the view can emit this reason but it won't appear in the EDR parameter's lexicon-built categories. Worth adding to the PR notes so it isn't missed after merge.