Skip to content

OpenConceptLab/ocl_issues#3065 | repair_reference_systems fills in system on legacy concept and mapping version references - #1043

Open
snyaggarwal wants to merge 3 commits into
masterfrom
ocl_issues-3065-repair-reference-systems
Open

snyaggarwal wants to merge 3 commits into
masterfrom
ocl_issues-3065-repair-reference-systems

Conversation

@snyaggarwal

Copy link
Copy Markdown
Contributor

For OpenConceptLab/ocl_issues#3065 (epic OpenConceptLab/ocl_issues#2942), as Jon decided on Nov 23. Some legacy references, created Oct 2023 to May 2024, have an expression that names a concept or mapping version (/orgs/<org>/sources/<source>/concepts/<code>/<version>/) but no system. OCL can't resolve them, so they bring in nothing.

python manage.py repair_reference_systems --output report.jsonl          # dry run
python manage.py repair_reference_systems --apply --output applied.jsonl

What it does

  • Which references: the command looks at references with no system and no value set whose expression contains /concepts/ or /mappings/, in every collection version. It parses each expression with the current parser (CollectionReferenceExpressionStringParser), then saves:
    • system;
    • code and resource_version, where they're missing. Most on collection HEADs already have them, and some in older collection versions have neither;
    • reference_type, when its default (concepts) disagrees with a mapping expression.
  • Left alone and listed, with the reason:
    • an expression that doesn't name a concept or mapping version;
    • one that names a source version too, which is for a person to judge;
    • a reference whose own code or resource_version disagrees with its expression.
  • Only reference fields change: each reference is saved with a queryset update(), so nothing is re-evaluated, no expansion is rebuilt, and no member is added or removed. The update applies only while the reference still has no system, so one changed meanwhile is left as it is.
  • Dry run by default: each reference it would change is listed with its old and new fields, and each one it leaves with why. --output writes the same as JSON lines, as the before-and-after record. Totals come at the end.

Afterwards

Tests

core/collections/tests/tests_repair_reference_systems.py has 8 tests:

  • a concept version reference gets its system;
  • code and resource_version are filled in where missing;
  • a mapping version reference gets the mappings type;
  • the dry run lists the change and saves nothing;
  • the three kinds of references left alone;
  • references with a system or a value set aren't touched;
  • no member or expansion changes;
  • the JSON report.

The local Elasticsearch container is down, so CI is the first run of these tests; pylint is clean on the new files.

🤖 Generated with Claude Code

…stem on legacy concept and mapping version references, dry run by default

@snyaggarwal snyaggarwal left a comment

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.

Round 1 verdict: ready to merge once CI passes. Four optional nits on 25af836. Fixes follow in this PR.

What checks out:

  • The candidates: references with no system and no value set, in every collection version.
  • The parsing: it uses the same parser that adding references uses.
  • What's saved: system, plus code, resource_version and reference_type only where they're missing or wrong.
  • The update: it's guarded on system still being empty, so a reference changed meanwhile is left as it is.
  • What doesn't change: there's no re-evaluation, no expansion is rebuilt, and no member is touched. References aren't in the search index, so update() leaves nothing stale there.
  • What's left alone: source-version expressions and disagreeing fields are listed with their reasons.

return None, 'its expression names a source version too'
for field in ('code', 'resource_version'):
if getattr(reference, field) and getattr(reference, field) != getattr(parser, field):
return None, f'its {field} disagrees with its expression'

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.

Nit (optional): the code check can mistake encoding for disagreement. The parser reads code from the URL as written, percent-encoded, while an older save may hold the decoded form, so A%2FB and A/B look different. Comparing decode_string of both sides would keep a valid reference from being left as 'its code disagrees with its expression'.

if not saved:
self.totals['left: changed since it was read'] += 1
return {**report, 'left': 'changed since it was read'}
self.totals['repaired' if self.options['apply'] else 'would repair'] += 1

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.

Nit (optional): LEFT lines for non-version references will dominate a production dry run. Most candidates are probably references whose expression doesn't name a version at all. Counting those, and keeping them in --output, while printing a line only for changes and the other reasons, would keep the 1,743 repairs readable.

reference.refresh_from_db()
return reference.system, reference.code, reference.resource_version, reference.reference_type

def test_a_concept_version_reference_gets_its_system(self):

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.

Nit (optional): no test that a second run changes nothing. The production run relies on being safe to stop and start again. A test that a second --apply repairs nothing would pin that.



class Command(BaseCommand):
help = ('Fills in system on legacy references that have none and no value set, whose expression names a concept '

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.

Nit (optional): the help text doesn't say how this fits with the health backfill. The repair changes reference fields that health reads. Saying to run it before backfill_expansion_health (#1031) avoids stored health that doesn't count the repaired references.

…ists only references worth reading, says to run before the health backfill, and is tested for reruns
@snyaggarwal

Copy link
Copy Markdown
Contributor Author

Round 1's four nits are fixed in 8eb8001.

  1. Codes: the code and resource version checks compare decode_string of both sides, so A%2FB agrees with A/B. A new test covers it.
  2. Output: references whose expression isn't a concept or mapping version are counted in the totals and kept in --output, but get no line each. Repairs and the other reasons are still listed.
  3. Reruns: a new test checks that a second --apply repairs and lists nothing.
  4. Health backfill: the help text now says to run this before backfill_expansion_health.

Local Elasticsearch is down, so CI is the first run of the tests; pylint is clean.

@snyaggarwal snyaggarwal left a comment

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.

Round 2 verdict: ready to merge once CI passes. No findings on 8eb8001.

Round 1's four fixes check out:

  • Codes: they're compared decoded, so the URL's form and an older save's agree, and a test covers A%2FB against A/B.
  • Output: references that name no version are counted and kept in --output, but don't get a line each.
  • Reruns: a second --apply repairs and lists nothing.
  • Help text: it says to run before backfill_expansion_health.

Nothing else turned up on a second read of the whole PR. A benchmark on local data is running separately, and its numbers will be added here. CI is the first run of the tests, because local Elasticsearch is down.

…nly the fields it uses, not every collection column
@snyaggarwal

Copy link
Copy Markdown
Contributor Author

Benchmark on a local database (2.22M references, 518 MB), run at 25af836, then again with 04cc86e's narrower query.

Local data: 1,485 references have no system, and 30 match the candidate filter. All 30 are junk, like /orgs/CIEL/sources/CIEL/concepts//, and the repair leaves them. So per-reference cost was measured on 2,000 synthetic legacy references, inside a transaction that was rolled back. Local data was confirmed unchanged afterwards.

Run Wall time Queries
Dry run, real data 7.4 s cold, 1.2 s warm 1: the scan
Dry run, real plus 2,000 synthetic 1.19 s, then 0.87 s after 04cc86e 1
--apply, real plus 2,000 synthetic 1.30 s 2,001: the scan plus one UPDATE per repaired reference
  • Per reference: about 0.42 ms to parse, with no extra queries; the collection URI comes from the join. Python's peak memory was 21.8 MB.
  • Results: all 2,000 synthetic references were repaired correctly, including 200 mapping references set to reference_type = mappings.
  • The plan: a parallel sequential scan of collection_references, since no index serves system IS NULL together with LIKE, then a primary-key lookup on collections per match. Locally that's 500 ms cold and 125 ms warm. 04cc86e reads only the fields used, cutting rows from 2,427 to 247 bytes, and the warm scan to about 95 ms.
  • Production estimate: the 1,743 known HEAD references, plus copies in older collection versions, which clone HEAD's references: roughly 2k–35k candidates. The dry run and --apply should each take a few seconds up to about 25 s, with memory in the tens of MB. Batching the UPDATEs would only matter at hundreds of thousands.

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.

1 participant