Repository navigation
OpenConceptLab/ocl_issues#3003 | expansion health in the API (PR 3 of 4, stacked on #1026) - #1029
snyaggarwal wants to merge 8 commits into
Conversation
…nsions, the default expansion's state on collection versions, includeHealth on member lists
snyaggarwal
left a comment
There was a problem hiding this comment.
Review (8beb0d9). Verdict: changes needed before merge. One important finding and two nits; I'm fixing all three next.
The API part is right:
- Expansions: list and detail return the stored
healthandhealth_is_current, read-only. - Versions list: it gets each default expansion's state through subqueries in the same query, so there's no query per row. Nested version lists (expansions, references) leave the field out.
- Member lists:
?includeHealth=trueclassifies only the page's ids, and only while the toggle is on. - No stale cache: the cached first-page path in
ListWithHeadersMixinapplies only to source children, so these expansion lists aren't cached and can't serve stale health.
- Important: collection version exports now include
expansion_health(inline). It's a snapshot that goes stale on the next recompute. - Nit:
includeHealthisn't in the Swagger docs. - Nit: two paths have no test: the verbose versions list, and mappings with
includeHealth.
CI: Snyk passed; Pylint and the test run are pending.
| 'immutable', 'revision_date', 'summary', 'text', 'experimental', 'locked_date', | ||
| 'autoexpand', 'expansion_url', 'checksums', 'states', 'tasks', 'external_exports', 'verification' | ||
| 'autoexpand', 'expansion_url', 'checksums', 'states', 'tasks', 'external_exports', 'verification', | ||
| 'expansion_health' |
There was a problem hiding this comment.
Important: this also reaches every collection version export. CollectionVersionExportSerializer exports all of this serializer's fields except VERSION_EXPORT_EXCLUDED_FIELDS ({'verification'}).
So exports now carry a health snapshot that goes stale on the next recompute, two exports of an unchanged version can differ by it alone, and each export runs one more query. Exclude it the way verification is excluded:
VERSION_EXPORT_EXCLUDED_FIELDS = {'verification', 'expansion_health'}Also add a test that the export leaves it out.
| self.expansion = expansion # pylint: disable=attribute-defined-outside-init | ||
| return expansion | ||
|
|
||
| def serialize_list(self, results, paginator=None): |
There was a problem hiding this comment.
Nit (optional): includeHealth isn't documented. Other include* parameters are declared through swagger_auto_schema(manual_parameters=[...]), so /swagger/ shows no way to get member classes. Add an include_health_param and attach it to the concepts and mappings list views' get.
| url = self.expansion.uri + 'concepts/' | ||
|
|
||
| plain = self.client.get(url) | ||
| with_health = self.client.get(url + '?includeHealth=true') |
There was a problem hiding this comment.
Nit (optional): two paths this PR serves have no test:
- The verbose versions list:
CollectionVersionDetailSerializeron the annotated queryset. - Mappings with
includeHealth:health_resource_type = 'mappings'.
A one-assertion test each would cover them.
… expansion_health; includeHealth documented; verbose versions and mapping health tested
|
All three review points are addressed in the latest commit. The description is updated.
|
snyaggarwal
left a comment
There was a problem hiding this comment.
Second review (c824c17). Verdict: approved, ready to merge once CI is green and the stack below it has merged. No open findings.
The first review's three points are resolved:
- Exports:
expansion_healthis inVERSION_EXPORT_EXCLUDED_FIELDS. Source exports use the same set and don't have the field, so they're unaffected. - Swagger:
include_health_paramis onCollectionVersionExpansionChildrenView.get, which both the concepts and mappings lists inherit. - Tests: the verbose versions list and the mappings
includeHealthpath are tested.
The PR as a whole:
- Expansions: stored health is read-only on the list and detail.
- Versions list: the default expansion's state comes from subqueries in the list query, so there's no query per row, and nested version lists and exports leave it out.
- Member lists:
includeHealthclassifies only the page's ids, and only while the toggle is on. - No writes: nothing changes for production while
EXPANSION_HEALTH_TOGGLEis off. - Local run:
core.collections.tests.tests_healthpasses, 45 tests.
Merge order: #1025, then #1026 (retargeted to master), then this one (retargeted to master).
CI: Snyk passed; Pylint and the test run are pending.
paynejd
left a comment
There was a problem hiding this comment.
Review at b3c0caf. An independent defect pass (Codex, high effort), with every finding checked against the code. Points from today's earlier read that you've already fixed aren't repeated.
Verdict: request changes, for point 1.
Must fix before merge
/versions/?csv=truereturns a 500.get_querysetnow annotates every versions-list queryset (views.py~819).get_csvfalls back toqueryset.values()(common/mixins.py~376). djqscsv'swrite_csvthen starts fromquery.values_select, a tuple on Django 5.2, and doesfield_names += list(annotation_select): TypeError. This doesn't depend on the toggle, so it breaks for any API user as soon as it's deployed. Suggestion inline: annotate only when the request isn't a CSV export. Please add a regression test for/versions/?csv=truewithwrite_csv_to_s3mocked.
Before merge, no code change
2. Merge master first. Master's new viewable-versions filter on the expansions list (from today) covers private versions' expansions there. git merge-tree with master is clean.
Worth fixing
3. Nested serializers do a lookup per row and throw it away. to_representation calls super() first, which runs get_expansion_health (serializers.py ~133-142), so every embedded collection version with an expansion_uri costs a query before the field is popped. That hits a concept's or mapping's collection-versions list, expansion version lists and reference resolution, whatever the toggle. Suggestion inline: return None unless the row was annotated.
4. The default-expansion lookup matches by URI only (models.py ~412, ~423). Collection.expansion scopes to self.expansions. Versions created before #950 could point at another collection's expansion, and would show its health. Suggestion inline: add collection_version_id.
5. ?includeHealth=true on one page does the whole collection's work. classify resolves every include reference (health.py ~53, 68, 104) before looking at the page, and an empty page does the same work. That's fine until TBv3 calls it, but due before #3004 uses it. Return early on an empty page, read the page's attributions first, and resolve only the references attributed to it.
Minor
6. The row-alignment test gives both rows the same class, so swapped rows would pass. Use two members of different classes.
| default_expansion = Expansion.objects.filter(uri=models.OuterRef('expansion_uri')) | ||
| return queryset.annotate( | ||
| expansion_health_state=models.Subquery(default_expansion.values('health__state')[:1]), | ||
| expansion_health_is_current=models.Subquery(default_expansion.values('health_is_current')[:1])) | ||
|
|
||
| @staticmethod | ||
| def get_expansion_health(version): | ||
| """{state, is_current} of the version's default expansion; None until it has been computed""" | ||
| if hasattr(version, 'expansion_health_state'): | ||
| state, is_current = version.expansion_health_state, version.expansion_health_is_current | ||
| elif version.expansion_uri: | ||
| state, is_current = Expansion.objects.filter(uri=version.expansion_uri).values_list( | ||
| 'health__state', 'health_is_current').first() or (None, None) |
There was a problem hiding this comment.
Point 4: scopes both lookups to this version's own expansions (collection_version_id), as Collection.expansion does.
| default_expansion = Expansion.objects.filter(uri=models.OuterRef('expansion_uri')) | |
| return queryset.annotate( | |
| expansion_health_state=models.Subquery(default_expansion.values('health__state')[:1]), | |
| expansion_health_is_current=models.Subquery(default_expansion.values('health_is_current')[:1])) | |
| @staticmethod | |
| def get_expansion_health(version): | |
| """{state, is_current} of the version's default expansion; None until it has been computed""" | |
| if hasattr(version, 'expansion_health_state'): | |
| state, is_current = version.expansion_health_state, version.expansion_health_is_current | |
| elif version.expansion_uri: | |
| state, is_current = Expansion.objects.filter(uri=version.expansion_uri).values_list( | |
| 'health__state', 'health_is_current').first() or (None, None) | |
| default_expansion = Expansion.objects.filter( | |
| uri=models.OuterRef('expansion_uri'), collection_version_id=models.OuterRef('id')) | |
| return queryset.annotate( | |
| expansion_health_state=models.Subquery(default_expansion.values('health__state')[:1]), | |
| expansion_health_is_current=models.Subquery(default_expansion.values('health_is_current')[:1])) | |
| @staticmethod | |
| def get_expansion_health(version): | |
| """{state, is_current} of the version's default expansion; None until it has been computed""" | |
| if hasattr(version, 'expansion_health_state'): | |
| state, is_current = version.expansion_health_state, version.expansion_health_is_current | |
| elif version.expansion_uri: | |
| state, is_current = version.expansions.filter(uri=version.expansion_uri).values_list( | |
| 'health__state', 'health_is_current').first() or (None, None) |
|
|
||
| @staticmethod | ||
| def get_expansion_health(obj): | ||
| return Collection.get_expansion_health(obj) |
There was a problem hiding this comment.
Point 3: looks up health only on rows the versions list annotated; every other row drops the field anyway.
| return Collection.get_expansion_health(obj) | |
| return Collection.get_expansion_health(obj) if hasattr(obj, 'expansion_health_state') else None |
| if self.released_filter is not None: | ||
| queryset = queryset.filter(released=self.released_filter) | ||
| return queryset.order_by('-created_at') | ||
| return Collection.annotate_expansion_health(queryset).order_by('-created_at') |
There was a problem hiding this comment.
Point 1: annotates only when the request isn't a CSV export, so get_csv's queryset.values() stays plain for djqscsv.
| return Collection.annotate_expansion_health(queryset).order_by('-created_at') | |
| if not self.request.query_params.get('csv'): # djqscsv can't write values() that carry annotations | |
| queryset = Collection.annotate_expansion_health(queryset) | |
| return queryset.order_by('-created_at') |
# Conflicts: # core/collections/tests/tests_health.py
… nested version lists look up no health, and the default expansion lookup is scoped to its version
|
Thanks, Jon. All six points are addressed. The fixes are in the latest commit, on a merge of the storage branch (70d0cfe), which carries master and the classifier and storage fixes.
Also from #1025, point 1: health is kept for default expansions only, so an expansion that isn't a version's default now reads The local Elasticsearch container is down, so CI is the first test run; pylint is clean. |
paynejd
left a comment
There was a problem hiding this comment.
Thanks, Sunny. All six points are met:
- The CSV export works, and the regression test runs the real writer.
- Master is merged in.
- Nested version lists look up no health.
- Both default-expansion lookups are scoped to the version.
- A page resolves only its own references (#1025).
- The row-alignment test now fails on swapped rows.
This approval covers this PR's own changes. Its CI failures come from #1025 and #1026 below it, and clear with their fixes.
|
CI: the failing health tests came from the classifier and storage branches. Their fixes are merged in (a58e8ef). No change on this branch. |
PR 3 of 4 for OpenConceptLab/ocl_issues#3003, stacked on #1026, which is itself stacked on #1025. Its base is #1026's branch, so this diff shows only PR 3. Merge in order and retarget each PR to
masteras the one below it merges.It exposes the stored health. It adds no writes and no new computation, apart from
?includeHealth=true, which classifies one page on request.Expansions
health(the stored summary) andhealth_is_current, read-only.health_is_currentistruewhen current,falsewhen a recompute is queued, and null when nothing is queued.Collection versions (for TBv3's versions table, OpenConceptLab/ocl_issues#3004)
expansion_health:{"state": ..., "is_current": ...}, or null until its default expansion has been computed.Collection.annotate_expansion_health).CollectionVersionListSerializerincludes it only when the view annotated it, so those lists don't gain a query per row.VERSION_EXPORT_EXCLUDED_FIELDS, next toverification. An export doesn't carry a health snapshot that goes stale.Member lists
?includeHealth=trueon an expansion's concepts or mappings list addshealth: {"class": ..., "references": [reference URLs]}to each member on the page.ExpansionHealth.classify(resource_type, page ids)), and paginated like the list.uuidwhere it has one, and by position otherwise (DRF serializes the page in order).EXPANSION_HEALTH_TOGGLEis off.include_health_paramon both lists.Tests
In
core/collections/tests/tests_health.py:expansion_healthexpansion_healthoutincludeHealthadds the class and reference URLs only when asked, for concepts and mappingsincludeHealthis ignored with the toggle offpylint -j0 core/exits 0.core.collections.tests.tests_healthpasses locally, together withTasksTest(100 tests), run in a throwaway container from the API image against a fresh test database.🤖 Generated with Claude Code