Product EngineeringFeaturesSample RerunBackend

Data Model

SampleRerun and SampleRerunValues models, sampleRedrawFlag states, ReportFormat meta fields, Redis cache structure, and activity log constants.

👤 Aakash Pawar📅 Updated: May 25, 2026🏷️ feature🏷️ backend🏷️ data-model

Data Model


SampleRerun (DocumentDB)

File: report/models/sample_rerun.py Collection: SampleRerun

Tracks the transactional state of each rerun — which parameters were requested, which have been fulfilled, and whether the rerun is still active.

class SampleRerun(BaseModel, DocumentDBModelBase):
    document_db_table_name = "SampleRerun"

    REQUESTED_RERUN_STATE = 3
    COMPLETED_RERUN_STATE = 4
    INSTRUMENT_TRIGGERED_RERUN_NUMBER = -1
    AUTO_RERUN_DEFAULT_RERUN_NUMBER = "1"
    CUSTOM_RANGE_PARAMETER = "custom"

Fields

FieldTypeDescription
lab_idPositiveIntegerFieldLab the rerun belongs to
typeCharField"Manual" or "Auto"
rerun_numberPositiveSmallIntegerFieldIteration number (1, 2, … or -1 for instrument-triggered)
lab_report_idPositiveBigIntegerFieldFK to the LabReportRelation this rerun targets
is_activeBooleanFieldTrue while the rerun is in progress; False once fulfilled or cancelled
requested_parametersJSONFieldArray of parameter indices requested for re-run (e.g., [1, 3, 5])
fulfilled_parametersJSONFieldArray of parameter indices that have received rerun values
parameter_nameCharFieldDisplay name of the parameter
created_byPositiveIntegerFieldUser who initiated the rerun (-1 for auto/internal)
created_atPositiveBigIntegerFieldUnix timestamp
updated_byPositiveIntegerFieldUser who last updated
updated_atPositiveBigIntegerFieldUnix timestamp

Key methods

MethodPurpose
get_valid_requested_parameters()Filters incoming device data to only include parameters that were actually requested
add_received_parameters_to_fulfilled_parameters()Merges received indices into fulfilled_parameters
is_rerun_fulfilled()Returns True when all requested_parameters are in fulfilled_parameters
fulfill_rerun()Marks is_active = False in DocumentDB
auto_rerun_condition_met()Evaluates a value against the configured auto-rerun range
get_auto_rerun_qualified_indices()Returns indices that qualify for auto rerun from a batch of device values
auto_rerun_record_exists()Guard: prevents duplicate auto reruns for the same report
get_reports_rerun_details()Bulk fetch of active rerun details for a list of report IDs

SampleRerunValues (DocumentDB)

File: report/models/sample_rerun_values.py Collection: SampleRerunValues

Stores the actual parameter values received during a rerun — one record per parameter per rerun iteration.

Fields

FieldTypeDescription
lab_idIntegerFieldLab identifier
profile_test_idIntegerFieldProfile test identifier
sample_idCharFieldSample barcode / manual ID
valueTextFieldThe rerun result value
orderIntegerFieldParameter order in the report
indexIntegerFieldParameter index in the report format
highlightIntegerFieldHighlight flag (0 or 1)
automated_valueIntegerFieldWhether value was automated
lab_report_idIntegerFieldFK to the report
device_idCharFieldSource device identifier
device_nameCharFieldSource device display name
rerun_typeCharField"manual_rerun" or "instrument_triggered"
rerun_numberIntegerFieldRerun iteration number
parameter_nameCharFieldDisplay name of the parameter
created_atDateTimeFieldAuto-set on creation

Key methods

MethodPurpose
serialize_records()Converts DocumentDB records (with ObjectId) to JSON-safe format
delete_values_and_transactions_for_report()Deletes all SampleRerunValues and SampleRerun records for a report, clears Redis cache
close_rerun()Full cleanup — resets sampleRedrawFlag = 0, syncs ES, deletes DocDB records and cache

Confirmation report keys

When confirming rerun values, only these fields are copied back to ReportValue:

CONFIRMATION_REPORT_KEYS = [
    "profileTestId", "value", "order", "index",
    "highlight", "automatedValue", "reportForId_id",
]

sampleRedrawFlag (MySQL)

File: report/models/lab_report_relation.py

An integer field on every LabReportRelation row:

sampleRedrawFlag = models.IntegerField(default=0)
ValueConstantState
0No rerun active
1Legacy partial redraw
2Legacy full redraw
3SampleRerun.REQUESTED_RERUN_STATERerun requested, awaiting values
4SampleRerun.COMPLETED_RERUN_STATERerun values received, awaiting confirmation

Where sampleRedrawFlag is filtered

The flag is used extensively in queries to exclude rerun-in-progress reports from normal workflows:

Query ContextFilter
Waiting list (normal)sampleRedrawFlag = 0
Waiting list (with reruns)sampleRedrawFlag ∈ [0, 3] (Bool should query)
Completed testssampleRedrawFlag ∈ [0, 1, 2, 4]
Device results validationsampleRedrawFlag = 0 for new records; ∈ [0, 3, 4] for tox
ES mappers (org, staff, doctor)sampleRedrawFlag = 0 or must_not sampleRedrawFlag = 2
Accession sample querysampleRedrawFlag = 0

ReportFormat meta — rerun configuration

File: report/models/report_format.py

Rerun configuration is stored in the meta JSON field of each ReportFormat row:

{
  "auto_rerun": 1,
  "manual_rerun": 1,
  "parameter_result_value": "ABNORMAL",
  "customLowerMale": "10",
  "customUpperMale": "50",
  "customLowerFemale": "8",
  "customUpperFemale": "45"
}

Key properties

PropertyReturns
auto_rerun_allowedTrue if meta.auto_rerun == 1
auto_rerun_parameter_conditionThe condition string: "normal", "abnormal", "critical", "custom"
get_auto_rerun_custom_ranges(patient_sex)(lower, upper) tuple from custom meta fields

Range evaluation

SampleRerun.auto_rerun_condition_met() delegates to ReportFormat.value_meets_parameter_condition() which compares the value against:

  • Normal/Abnormal: Standard reference ranges (or ValueRanges if ageRangeFlag = 1)
  • Critical: Critical reference ranges
  • Custom: Custom bounds from meta fields

Redis cache structure

Key pattern: sample_rerun_{lab_id} (Redis hash)

Hash fieldValueDescription
{lab_report_id}{rerun_number}Maps each report to its current active rerun number

TTL: 86,400 seconds (1 day)

Cache operations

MethodOperation
set_rerun_cache()HSET a single lab_report_id → rerun_number
get_rerun_cache()HGET for a single lab_report_id
get_rerun_cache_bulk()HMGET for multiple lab_report_ids
delete_rerun_cache()HDEL for a single lab_report_id
fill_rerun_cache()Backfill from DocumentDB on cache miss

Activity log constants

File: core/utils/activity_log/__init__.py

RERUN_VALUES_RECEIVED = 979
RERUN_VALUES_CONFIRMED = 980
RERUN_REQUESTED = 981
RERUN_FAILED = 984
RERUN_CANCELLED = 985

Log text format

EventActivity text pattern
Request"Manual Sample Rerun requested for Sample ID: '{id}' on parameters: (...) of Test: '{name}'."
Values received"Sample Rerun values received for parameters: {name} ({value}) of Test: '{name}' with Sample ID: '{id}'."
Confirmed"Sample Rerun values confirmed for parameters: {name} (Value: {value}) of Test: '{name}' with Sample ID: '{id}'."
Failed"Sample rerun request failed for Test: '{name}' with Sample ID: '{id}'."
Cancelled"Sample rerun cancelled successfully for Test: '{name}' with Sample ID: '{id}'."

On this page