Data Model
SampleRerun and SampleRerunValues models, sampleRedrawFlag states, ReportFormat meta fields, Redis cache structure, and activity log constants.
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
| Field | Type | Description |
|---|---|---|
lab_id | PositiveIntegerField | Lab the rerun belongs to |
type | CharField | "Manual" or "Auto" |
rerun_number | PositiveSmallIntegerField | Iteration number (1, 2, … or -1 for instrument-triggered) |
lab_report_id | PositiveBigIntegerField | FK to the LabReportRelation this rerun targets |
is_active | BooleanField | True while the rerun is in progress; False once fulfilled or cancelled |
requested_parameters | JSONField | Array of parameter indices requested for re-run (e.g., [1, 3, 5]) |
fulfilled_parameters | JSONField | Array of parameter indices that have received rerun values |
parameter_name | CharField | Display name of the parameter |
created_by | PositiveIntegerField | User who initiated the rerun (-1 for auto/internal) |
created_at | PositiveBigIntegerField | Unix timestamp |
updated_by | PositiveIntegerField | User who last updated |
updated_at | PositiveBigIntegerField | Unix timestamp |
Key methods
| Method | Purpose |
|---|---|
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
| Field | Type | Description |
|---|---|---|
lab_id | IntegerField | Lab identifier |
profile_test_id | IntegerField | Profile test identifier |
sample_id | CharField | Sample barcode / manual ID |
value | TextField | The rerun result value |
order | IntegerField | Parameter order in the report |
index | IntegerField | Parameter index in the report format |
highlight | IntegerField | Highlight flag (0 or 1) |
automated_value | IntegerField | Whether value was automated |
lab_report_id | IntegerField | FK to the report |
device_id | CharField | Source device identifier |
device_name | CharField | Source device display name |
rerun_type | CharField | "manual_rerun" or "instrument_triggered" |
rerun_number | IntegerField | Rerun iteration number |
parameter_name | CharField | Display name of the parameter |
created_at | DateTimeField | Auto-set on creation |
Key methods
| Method | Purpose |
|---|---|
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)| Value | Constant | State |
|---|---|---|
0 | — | No rerun active |
1 | — | Legacy partial redraw |
2 | — | Legacy full redraw |
3 | SampleRerun.REQUESTED_RERUN_STATE | Rerun requested, awaiting values |
4 | SampleRerun.COMPLETED_RERUN_STATE | Rerun 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 Context | Filter |
|---|---|
| Waiting list (normal) | sampleRedrawFlag = 0 |
| Waiting list (with reruns) | sampleRedrawFlag ∈ [0, 3] (Bool should query) |
| Completed tests | sampleRedrawFlag ∈ [0, 1, 2, 4] |
| Device results validation | sampleRedrawFlag = 0 for new records; ∈ [0, 3, 4] for tox |
| ES mappers (org, staff, doctor) | sampleRedrawFlag = 0 or must_not sampleRedrawFlag = 2 |
| Accession sample query | sampleRedrawFlag = 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
| Property | Returns |
|---|---|
auto_rerun_allowed | True if meta.auto_rerun == 1 |
auto_rerun_parameter_condition | The 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
ValueRangesifageRangeFlag = 1) - Critical: Critical reference ranges
- Custom: Custom bounds from meta fields
Redis cache structure
Key pattern: sample_rerun_{lab_id} (Redis hash)
| Hash field | Value | Description |
|---|---|---|
{lab_report_id} | {rerun_number} | Maps each report to its current active rerun number |
TTL: 86,400 seconds (1 day)
Cache operations
| Method | Operation |
|---|---|
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 = 985Log text format
| Event | Activity 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}'." |