Skip to content

Add lab tests documentation with activity definition, observations and reference ranges - #160

Open
sonzsara wants to merge 1 commit into
mainfrom
ENG-1017
Open

sonzsara wants to merge 1 commit into
mainfrom
ENG-1017

Conversation

@sonzsara

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI balanced review requested due to automatic review settings September 30, 2026 10:34
@sonzsara sonzsara changed the title Add lab tests documentation with activity definition, observations and refernce ranges Add lab tests documentation with activity definition, observations and reference ranges Sep 30, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The observation status filter unintentionally excludes laboratory tests without active observation definitions, and the filename contains a typo.

Review effort: Balanced
Findings: 1 Medium severity · 1 Low severity

Open (2)
What changed in this PR

Adds an SSMM laboratory-test query documenting linked clinical definitions and reference ranges.

Changes:

  • Adds a static SQL report for laboratory service requests.
  • Includes charge items, specimens, activity definitions, observations, and qualified ranges.
File Description
Care/​Clinical/​lab_test_with_activity_definition_reference_ranges_and_observation_defintion_ssmm.md Documents the laboratory-test reference-range query.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +41 to +46
LEFT JOIN emr_observationdefinition
ON emr_observationdefinition.id = emr_observation.observation_definition_id
WHERE emr_servicerequest.status != 'entered_in_error'
AND emr_chargeitem.status != 'entered_in_error'
AND emr_servicerequest.category = 'laboratory'
AND emr_observationdefinition.status = 'active'
@@ -0,0 +1,56 @@
# Lab Tests with Activity Definition, Reference Ranges and Observation Definition - SSMM

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Analytics SQL Review — SSMM lab test / activity definition / reference range query

Ticket: none could be retrieved for branch ENG-1017 (JIRA returned 404/401 for the review bot's token). I can't verify requirement fidelity; please confirm the ticket is accessible and the branch name matches it. Assumed ask: list lab tests with charge item, specimen, activity and observation definitions and reference ranges for SSMM.

Verdict: not yet safe to publish. The parse is clean (lint: no findings), but:

  • No facility scope despite the _ssmm suffix, and no deleted = FALSE (High).
  • The WHERE emr_observationdefinition.status = 'active' negates the LEFT JOIN (High).
  • DISTINCT over patient-level observation joins hides fan-out and is costly (Medium).
  • Low: filename typo defintion → definition. The doc is otherwise template-conformant and Last updated is set; Notes should document the facility ID once added.

Generated by Analytics SQL Reviewer for #160 · auto · 14.1 AIC · ⊞ 14.3K

WHERE emr_servicerequest.status != 'entered_in_error'
AND emr_chargeitem.status != 'entered_in_error'
AND emr_servicerequest.category = 'laboratory'
AND emr_observationdefinition.status = 'active'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

High – this WHERE predicate turns the LEFT JOIN emr_observationdefinition into an inner join, so tests with no observations/active definition silently disappear (same point as the Copilot review). If tests without an active definition should still be listed, move it into the join:

LEFT JOIN emr_observationdefinition
    ON emr_observationdefinition.id = emr_observation.observation_definition_id
   AND emr_observationdefinition.status = 'active'

If dropping them is intended, use JOIN and say so in Notes. Same applies to emr_chargeitem.status != 'entered_in_error' — fine as an inner join, but consider whitelisting valid statuses.

## Query

```sql
SELECT DISTINCT

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Medium – SELECT DISTINCT over a join through emr_observation → emr_diagnosticreport → emr_specimen builds one row per patient observation before de-duplicating, which masks fan-out and will be slow on production volumes. Since only definition-level attributes are returned, consider joining emr_activitydefinition → its observation definitions directly (if the model links them) instead of via patient results. Also, emr_chargeitem.service_resource_id = external_id::text is a cast on the indexed side, so it can't use the external_id index; confirm this is the only link. Note: the Purpose says "each distinct test", but the observation path only surfaces definitions that have actually been used.

ON emr_observation.diagnostic_report_id = emr_diagnosticreport.id
LEFT JOIN emr_observationdefinition
ON emr_observationdefinition.id = emr_observation.observation_definition_id
WHERE emr_servicerequest.status != 'entered_in_error'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

High – no facility scope (and no deleted filter). The _ssmm suffix says this is SSMM-specific, but nothing restricts the query to that facility, so on a shared DB it returns lab tests from every facility. Also add deleted = FALSE on the tables the ORM would filter.

Suggested (verify column names against care/emr/models):

WHERE emr_servicerequest.facility_id = <ssmm facility id>  -- document in Notes
  AND emr_servicerequest.deleted = FALSE
  AND emr_chargeitem.deleted = FALSE

If the chargeitem facility is the intended scope, use that instead. Whichever ID you use needs a "what it is / when to update" line in ## Notes.

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.

2 participants