
🧭 System Architecture
🗂️ Defect Module Categories
The SYIA Defect System handles different modules organized into three main categories:🗂️ Symbols & Sets
- = set of active vessels, indexed by IMO number.
- = defect record.
- = reporting year.
- = current run date at midnight.
- = ignored statuses.
🔄 Common Processing Logic
1. Data Retrieval & Cleaning
- Query MongoDB collections for inspection and report defects.
- Restrict to active vessels ().
- Normalize keys (case folding, unify field names).
- Parse dates:
reportedDate,targetDate,closingDate, nestedfindingsTargetDate,correctiveAction.targetDate. - Lowercase all statuses.
- Replace parse failures (
NaT) withNone. - Standardize nested JSON structures (
findings,correctiveAction).
2. Overdue Detection
For any candidate date with status : Applied at three levels:- Parent record (
targetDate). - Findings (
findingsTargetDate). - Corrective Actions (
CA.targetDate).
3. Parent Status Promotion
4. Status → Color Mapping
📊 Aggregations
Yearly Bar Data
For each year :- Total defects:
- Open defects:

Sample Defects Chart
Summary Counts
Let = count of rows by status. After filtering ignored:
Sample Output Table
🧮 Common Output Structures
- Tabular rows:
[Report Date, Reference No (link), Status, Description, Target Date, Closing Date, Closing Note, Source] - Charts: Yearly bar plots (Total vs Open).
- Summary JSON: status counts, pending counts, chart data.
- Lineitems: nested findings + corrective actions with overdue annotations.
⚙️ Algorithm
- Load environment + set .
- Fetch active vessels.
- Retrieve raw defect data (inspection/report collections).
- Normalize fields, lowercase status, parse datetimes.
- Build findings + corrective actions arrays.
- Apply overdue detection + parent status promotion.
- Aggregate yearly bars and status counts.
- Format outputs (tables, charts, summaries).
- Assign colors with
status_colors. - Upsert into MongoDB component collections.
✅ Robust Engineering
- Idempotent upserts (
update_one(..., upsert=True)). - Consistent normalization: statuses always lowercase, dates coerced.
- Error handling: try/except per vessel; log errors without halting pipeline.
- Deterministic sort order:
(reportedDate desc, status_color, targetDate, referenceNo). - Test mode override for fixed values.
📊 Worked Example
Suppose a record has:- Parent status =
open,targetDate=None. - One finding with
findingStatus=open,findingsTargetDate=t_0-1 day.
- Finding →
overdue. - Parent →
overdue. - Assigned color = red.

