Skip to content

refactor(edr): create and update backing views (BDMS-1599) - #1062

Open
jacob-a-brown wants to merge 45 commits into
BDMS-1602-pygeoapi-edr-configuration-provider-refactorfrom
BDMS-1599-backing-view-for-manual-water-levels
Open

jacob-a-brown wants to merge 45 commits into
BDMS-1602-pygeoapi-edr-configuration-provider-refactorfrom
BDMS-1599-backing-view-for-manual-water-levels

Conversation

@jacob-a-brown

@jacob-a-brown jacob-a-brown commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Why

This PR addresses the following problem / context:

  • A backing view for the ogc_manual_waterlevels EDR collection needed to be created
  • With the station_name -> name refactor for EDR collections a column name needed to be added to the current ogc_water_chemistry materialized view. This enables backwards compatibility and is not the full update for water chemistry. That'll happen in a future ticket.
  • Some updates needed to be made to the EDR provider. The necessity of these updates were discovered after the backing view was created and tested. A number of these were identified in the PR for the EDR refactor. The most important being:
    • Using SELECT DISTINCT ON (thing_id) ... to get unique locations for /locations
    • Building a parameter's categories using the lexicon tables
    • Defaulting the parameter's description to its name
    • Enabling parameters to not have units

How

Implementation summary: the following was changed / added / removed:

Views

  • Created two backing views for manual water levels: ogc_manual_waterlevels and ogc_internal_manual_waterlevels
  • Added name to ogc_waterchemistry to make it compatible with the EDR refactor. I did not remove station_name here. 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

  • Create categories from the lexicon for parameters that are restricted to different lexicon terms as defined by categories, such as the groundwater level reason qualifier
    • This means that every lexicon term associated with a category gets written to the categories. If we want to proceed with categories (which I think is a good way to proceed), I think that we should create categories for EDR and just apply them. That way we can enforce restrictions/sets if need-be. That's on a case-by-case basis, though, so no need to do it yet.
  • Added bespoke get_parameters so that units are not required. The OGC standard says that units don't need to exist, but pygeoapi enforces them. This is why get_parameters needed to be overwritten
  • Defaulted a parameter's description to the parameter name. This is because all parameters are now "discovered" and not hardcoded in a YAML file. If we want to have verbose descriptions I think that we can leverage the lexicon table since it has term/description pairs. Thoughts?

Author Checklist

  • Changes are limited to the intended scope. <- I needed to update the EDR provider since I discovered omissions and bugs after implementing the view
  • Tests were added or updated where appropriate.
  • Relevant tests and lint checks pass.
  • No secrets, credentials, or sensitive data were publicly exposed.
  • Documentation was updated where necessary.

Reviewer Checklist

  • Use checkboxes here

Notes

Any special considerations, known limitations, risks, workarounds, or follow-up work to note?

  • @likithabommasani21 and @ksmuczynski the new ogc_manual_waterlevels uses release_status = 'public' for observation, thing, and alternate_id
  • This does not drop the ogc_waterlevels and ogc_internal_waterlevels views. Those are just views, not materialized views, and so take up no memory besides storing their queries.
    • On that note my plan is to make ogc_waterchemistry and ogc_transducer_waterlevels views 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.)
  • 🚨 Do you think these should be views or materialized views?

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.

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.
… 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
@jacob-a-brown

Copy link
Copy Markdown
Contributor Author

@ksmuczynski Should the stack be in the opposite order because this is the one where all of the tests pass? So this goes into staging and #1060 goes into this? Even though this has work from #1060 that is not in staging

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

❌ 43.40% total — gate is 75%.

Coverage for the Python files changed in this PR
Name Stmts Miss Cover Missing
core/edr_provider.py 250 162 35% 105, 116-123, 158, 175-177, 185, 200-210, 226, 244-245, 255-280, 291, 302-332, 347-354, 358, 377-413, 435, 441-447, 453-459, 465-471, 491-524, 535-559, 570-579, 582-706, 717-722, 731-740, 743
core/pygeoapi.py 181 22 88% 368, 372, 381, 386, 405, 412, 437-440, 587, 592, 680, 734, 737, 780, 798, 805, 807, 827, 829, 841
TOTAL 431 184 57%

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 name to the chemistry materialized views while retaining station_name for 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

Comment thread core/edr_provider.py Resolved
@jacob-a-brown

Copy link
Copy Markdown
Contributor Author

@jirhiker I can add the two parameters to core/ogc-field-descriptions.yml, but then I imagine I should do the same with chemistry parameters when I work on that. This is certainly doable, but means that every time a new parameter is added to the chemistry table that the file would need to be updated to keep pace with the DB (and therefore we'd need to redeploy). This is a fine way to proceed since we can get more information in, but I think it's something that will need to be documented as part of the API workflow.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

✅ 82.32% total — gate is 75%.

Coverage for the Python files changed in this PR
Name Stmts Miss Cover Missing
core/edr_provider.py 264 42 84% 105, 158, 178-180, 203-213, 370-375, 462-468, 474-480, 486-492, 515-521, 539-547, 549-550, 552-553, 555-560, 636, 789-794, 815
core/pygeoapi.py 198 20 90% 371, 375, 384, 389, 408, 415, 440-443, 590, 595, 683, 820, 838, 845, 847, 867, 869, 881
TOTAL 462 62 87%

@jacob-a-brown

Copy link
Copy Markdown
Contributor Author

@jirhiker and @ksmuczynski

Parameters are now hard-coded and described in core/ogc-field-descriptions.yml. If a parameter does not exist there the EDR provider defaults to the parameter name for the description and title. It then logs that this occurs at a warning level.

@ksmuczynski and @likithabommasani21

I pulled the latest updates from staging so that the alembic revisions have only one head. The alembic version 7ae4753c9b3e, which creates the backing ogc_manual_waterlevels view, now downgrades to 73d27e78571a, which adds the release_level field to many tables.

… into BDMS-1599-backing-view-for-manual-water-levels
Comment on lines +124 to +130
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.id

Because 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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ksmuczynski I pushed that update to this PR. Would you run and time it again?

@ksmuczynski ksmuczynski Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Times are down.

@jacob-a-brown

Copy link
Copy Markdown
Contributor Author

@ksmuczynski I added some more documentation to the README. Think that this should go in its own document in docs/? Something like docs/edr-collections.md?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants