The flag engine lives in
lib/flagEngine.ts. It’s invoked after case validation, during demo data generation, and can be manually triggered from the data quality page.Core tables
| Table | Description |
|---|---|
flag_rules | Rule definitions per facility (metric, threshold, operator, severity) |
case_flags | Individual flag instances detected on cases |
Threshold types
| Type | How it works |
|---|---|
absolute | Direct threshold value (e.g., turnover > 45 min) |
median_plus_sd | Median ± (N × standard deviation) |
median_plus_offset | Median ± fixed offset |
percentage_of_median | Median × (1 ± percent) |
percentile | Nth percentile of the cohort |
between | Value falls within range [min, max] |
Comparison scopes
| Scope | Description |
|---|---|
facility | Compare against facility-wide baselines |
personal | Compare against the surgeon’s own historical performance |
Metric categories
Timing
total_case_time, surgical_time, pre_op_time, anesthesia_time, closing_time, emergence_time, prep_to_incision, surgeon_readiness_gap
Efficiency
turnover_time (cross-case), fcots_delay (first-case only), room_idle_gap
Financial
case_profit, case_margin, profit_per_minute, total_case_cost, reimbursement_variance, or_time_cost, excess_time_cost
Quality
missing_milestones, milestone_out_of_order
Evaluation pipeline
Per-case evaluation
For each case and each enabled rule:- Extract the metric value from case data
- Look up the baseline (personal or facility scope)
- Resolve the threshold based on type + baseline
- Compare value against threshold using the operator
- If triggered, create a
CaseFlagwith severity, rule name, and metric details
Standard vs. custom rules
| Type | Description |
|---|---|
| Standard | Seeded from global templates on facility creation. Cannot be deleted, only toggled. |
| Custom | Created by facility admins. Can use any metric including dynamic cost-category metrics. |
When flags run
- When a case is validated (Completed status)
- During demo data generation (batch mode)
- Manually triggered from the data quality page
Key file
lib/flagEngine.ts
Adding a new flag metric
Create a flag rule
Add a rule in the
flag_rules table (or create a migration to seed it as a built-in rule) with the threshold type, operator, and severity.Next steps
Flag rules
Configure rules in facility settings.
Flag analytics
See flag patterns across your facility.