-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate-qa-maintenance-data.py
More file actions
1082 lines (985 loc) · 40.6 KB
/
Copy pathgenerate-qa-maintenance-data.py
File metadata and controls
1082 lines (985 loc) · 40.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Generate asset-level operations data for the operations portal.
The input is a generated city `design.toml` plus its expanded
`scenario.toml`. The output is a deterministic JSON bundle and CSV tables
that spreadsheet users can open directly.
"""
from __future__ import annotations
import argparse
import csv
import gzip
import hashlib
import json
import os
import re
import tempfile
import tomllib
from pathlib import Path
from typing import Any
REPO_ROOT = Path(__file__).resolve().parents[1]
DEFAULT_DESIGN = REPO_ROOT / "designs/west-asia/Iraq/Samawah/design.toml"
DEFAULT_SCENARIO = REPO_ROOT / "designs/west-asia/Iraq/Samawah/samawah.toml"
DEFAULT_BOM_DIR = REPO_ROOT / "build/bom"
DEFAULT_OUT_DIR = REPO_ROOT / "build/generated-operations/samawah"
def main() -> int:
parser = argparse.ArgumentParser(
description="Generate asset-level operations portal data."
)
parser.add_argument("--design", type=Path, default=DEFAULT_DESIGN)
parser.add_argument("--scenario", type=Path, default=DEFAULT_SCENARIO)
parser.add_argument(
"--out-dir",
type=Path,
default=None,
help="output folder (default: build/generated-operations/samawah)",
)
args = parser.parse_args()
design = _load_toml(args.design)
scenario = _load_toml(args.scenario)
qa_template = _load_toml(REPO_ROOT / "lib/templates/construction-qa.toml")
maint_template = _load_toml(REPO_ROOT / "lib/templates/maintenance-schedule.toml")
manufacturing_template = _load_toml(REPO_ROOT / "lib/templates/manufacturing-schedule.toml")
bom_catalog = _load_bom_catalog(DEFAULT_BOM_DIR)
bundle = build_bundle(
design=design,
scenario=scenario,
qa_template=qa_template,
maint_template=maint_template,
manufacturing_template=manufacturing_template,
bom_catalog=bom_catalog,
design_path=args.design,
scenario_path=args.scenario,
)
out_dir = args.out_dir or DEFAULT_OUT_DIR
out_dir.mkdir(parents=True, exist_ok=True)
slug = bundle["meta"]["city_slug"]
json_path = out_dir / f"{slug}-operations.json.gz"
manifest_path = out_dir / f"{slug}-operations-manifest.json"
assets_path = out_dir / f"{slug}-assets.csv"
manufacturing_path = out_dir / f"{slug}-manufacturing-schedule.csv"
manufacturing_materials_path = out_dir / f"{slug}-manufacturing-materials.csv"
manufacturing_verification_path = out_dir / f"{slug}-manufacturing-verification.csv"
maintenance_path = out_dir / f"{slug}-maintenance-schedule.csv"
qa_path = out_dir / f"{slug}-qa-register.csv"
payload = (json.dumps(bundle, indent=2, ensure_ascii=False) + "\n").encode("utf-8")
with tempfile.NamedTemporaryFile("wb", dir=out_dir, delete=False) as handle:
temporary = Path(handle.name)
with gzip.GzipFile(filename="", mode="wb", fileobj=handle, mtime=0) as compressed:
compressed.write(payload)
handle.flush()
os.fsync(handle.fileno())
os.replace(temporary, json_path)
compressed_bytes = json_path.read_bytes()
manifest_path.write_text(
json.dumps(
{
"city": slug,
"compression": "gzip",
"content_type": "application/json",
"file": json_path.name,
"compressed_bytes": len(compressed_bytes),
"compressed_sha256": hashlib.sha256(compressed_bytes).hexdigest(),
"uncompressed_bytes": len(payload),
"uncompressed_sha256": hashlib.sha256(payload).hexdigest(),
"totals": bundle.get("totals", {}),
},
indent=2,
sort_keys=True,
) + "\n"
)
_write_csv(assets_path, bundle["assets"])
_write_csv(manufacturing_path, bundle["manufacturing_tasks"])
_write_csv(manufacturing_materials_path, bundle["manufacturing_materials"])
_write_csv(manufacturing_verification_path, bundle["manufacturing_verifications"])
_write_csv(maintenance_path, bundle["maintenance_tasks"])
_write_csv(qa_path, bundle["qa_actions"])
print(f"wrote {json_path}")
print(f"wrote {manifest_path}")
print(f"wrote {assets_path}")
print(f"wrote {manufacturing_path}")
print(f"wrote {manufacturing_materials_path}")
print(f"wrote {manufacturing_verification_path}")
print(f"wrote {maintenance_path}")
print(f"wrote {qa_path}")
return 0
def build_bundle(
*,
design: dict[str, Any],
scenario: dict[str, Any],
qa_template: dict[str, Any],
maint_template: dict[str, Any],
manufacturing_template: dict[str, Any],
bom_catalog: dict[str, dict[str, dict[str, str]]],
design_path: Path,
scenario_path: Path,
) -> dict[str, Any]:
city = design.get("city", {})
slug = str(city.get("slug", design_path.parent.name.lower()))
prefix = _city_prefix(slug)
stations = list(design.get("stations", []))
lines = list(design.get("lines", []))
fleets = list(design.get("fleets", []))
depots = list(design.get("depots", []))
switches = list(design.get("switches", []))
for depot in depots:
switches.extend(depot.get("switches", []))
junctions = list(design.get("junctions", []))
sites = list(scenario.get("sites", []))
station_asset_by_source: dict[str, str] = {}
assets: list[dict[str, Any]] = []
counters: dict[str, int] = {}
def next_id(kind: str) -> str:
counters[kind] = counters.get(kind, 0) + 1
return f"{prefix}-{kind}-{counters[kind]:03d}"
project_asset_id = next_id("SYS")
assets.append({
"asset_id": project_asset_id,
"source_id": slug,
"asset_type": "system",
"subtype": "whole railway",
"name": f"{_title(slug)} railway system",
"line": "",
"parent_asset": "",
"station": "",
"km_start": "",
"km_end": "",
"location": _location_from_bbox(city.get("bbox", {})),
"criticality": "system",
})
for fleet in fleets:
line = str(fleet.get("line", "line"))
line_code = _line_code(line)
total = int(fleet.get("trainset_count", 0))
peak = int(fleet.get("peak_count", 0))
spare = int(fleet.get("spare_count", 0))
for idx in range(1, total + 1):
if idx <= peak:
subtype = "revenue"
elif idx <= peak + spare:
subtype = "spare"
else:
subtype = "cold-reserve"
assets.append({
"asset_id": f"{prefix}-RS-{line_code}-{idx:03d}",
"source_id": f"{line}-{idx:03d}",
"asset_type": "rolling-stock",
"subtype": subtype,
"name": f"{line} trainset {idx:03d}",
"line": line,
"parent_asset": project_asset_id,
"station": "",
"km_start": "",
"km_end": "",
"location": "fleet",
"criticality": "service",
})
for station in stations:
asset_id = next_id("ST")
station_asset_by_source[str(station.get("id"))] = asset_id
station_name = station.get("anchor_name") or station.get("name") or station.get("id")
assets.append({
"asset_id": asset_id,
"source_id": station.get("id", ""),
"asset_type": "station",
"subtype": station.get("archetype", "standard"),
"name": station_name,
"line": station.get("line", ""),
"parent_asset": project_asset_id,
"station": station.get("id", ""),
"km_start": _km(station.get("s_m")),
"km_end": _km(station.get("s_m")),
"location": _lat_lon(station),
"criticality": "public",
})
stations_by_line: dict[str, list[dict[str, Any]]] = {}
for station in stations:
stations_by_line.setdefault(str(station.get("line", "")), []).append(station)
for line, line_stations in stations_by_line.items():
ordered = sorted(line_stations, key=lambda s: float(s.get("s_m", 0.0)))
line_code = _line_code(line)
for idx, (a, b) in enumerate(zip(ordered, ordered[1:]), start=1):
a_s = float(a.get("s_m", 0.0))
b_s = float(b.get("s_m", a_s))
track_asset_id = f"{prefix}-TRK-{line_code}-{idx:03d}"
assets.append({
"asset_id": track_asset_id,
"source_id": f"{a.get('id')}--{b.get('id')}",
"asset_type": "track-section",
"subtype": "standard-urban",
"name": f"{line} section {idx:03d}",
"line": line,
"parent_asset": project_asset_id,
"station": f"{a.get('id')} to {b.get('id')}",
"km_start": _km(a_s),
"km_end": _km(b_s),
"location": f"{_display_station(a)} to {_display_station(b)}",
"criticality": "safety",
})
mid_s = (a_s + b_s) / 2.0
assets.append({
"asset_id": f"{prefix}-WPT-{line_code}-{idx:03d}",
"source_id": f"w-node-{a.get('id')}--{b.get('id')}",
"asset_type": "waypoint",
"subtype": "wayside-node",
"name": f"{line} waypoint {idx:03d}",
"line": line,
"parent_asset": track_asset_id,
"station": f"{a.get('id')} to {b.get('id')}",
"km_start": _km(mid_s),
"km_end": _km(mid_s),
"location": f"{_display_station(a)} to {_display_station(b)} midpoint W-Node",
"criticality": "safety",
})
for switch in switches:
station_id = str(switch.get("station", ""))
assets.append({
"asset_id": next_id("SW"),
"source_id": switch.get("id", ""),
"asset_type": "switch",
"subtype": switch.get("kit", ""),
"name": switch.get("id", ""),
"line": _line_from_station_id(station_id),
"parent_asset": station_asset_by_source.get(station_id, project_asset_id),
"station": station_id,
"km_start": "",
"km_end": "",
"location": f"{station_id} {switch.get('side', '')}".strip(),
"criticality": "safety",
})
for depot in depots:
station_id = str(depot.get("station", ""))
assets.append({
"asset_id": next_id("DEP"),
"source_id": f"depot-{station_id}",
"asset_type": "depot",
"subtype": depot.get("archetype", ""),
"name": f"{depot.get('archetype', 'depot')} at {station_id}",
"line": _line_from_station_id(station_id),
"parent_asset": station_asset_by_source.get(station_id, project_asset_id),
"station": station_id,
"km_start": "",
"km_end": "",
"location": f"{station_id}; {depot.get('fleet_stalls', '')} stalls",
"criticality": "service",
})
for idx, depot in enumerate(depots, start=1):
station_id = str(depot.get("station", ""))
assets.append({
"asset_id": f"{prefix}-PLANT-{idx:03d}",
"source_id": f"plant-{station_id}",
"asset_type": "depots-production",
"subtype": "tooling-fixtures",
"name": f"production/depot tooling at {station_id}",
"line": _line_from_station_id(station_id),
"parent_asset": station_asset_by_source.get(station_id, project_asset_id),
"station": station_id,
"km_start": "",
"km_end": "",
"location": station_id,
"criticality": "production",
})
for site in sites:
station_id = str(site.get("station", ""))
assets.append({
"asset_id": next_id("EN"),
"source_id": f"energy-{station_id}",
"asset_type": "energy",
"subtype": "pv-bess-charger",
"name": f"energy site at {station_id}",
"line": _line_from_station_id(station_id),
"parent_asset": station_asset_by_source.get(station_id, project_asset_id),
"station": station_id,
"km_start": "",
"km_end": "",
"location": (
f"{float(site.get('pv_nameplate_kw', 0.0)):,.0f} kW PV; "
f"{float(site.get('storage_capacity_kwh', 0.0)):,.0f} kWh storage"
),
"criticality": "energy",
})
for junction in junctions:
group = junction.get("group_id", len(assets))
assets.append({
"asset_id": next_id("STR"),
"source_id": f"junction-{group}",
"asset_type": "structure",
"subtype": "elevated-interchange",
"name": f"elevated interchange group {group}",
"line": junction.get("elevated_line", ""),
"parent_asset": project_asset_id,
"station": "",
"km_start": "",
"km_end": "",
"location": _lat_lon(junction),
"criticality": "safety",
})
for station in stations:
station_id = str(station.get("id", ""))
assets.append({
"asset_id": next_id("SIG"),
"source_id": f"systems-{station_id}",
"asset_type": "signalling-comms",
"subtype": "station-node",
"name": f"station systems node {station_id}",
"line": station.get("line", ""),
"parent_asset": station_asset_by_source.get(station_id, project_asset_id),
"station": station_id,
"km_start": _km(station.get("s_m")),
"km_end": _km(station.get("s_m")),
"location": _lat_lon(station),
"criticality": "safety",
})
maintenance_tasks = _expand_maintenance_tasks(
assets=assets,
intervals=list(maint_template.get("maintenance_interval", [])),
city_slug=slug,
)
qa_actions = _expand_qa_actions(
assets=assets,
gates=list(qa_template.get("construction_qa_gate", [])),
city_slug=slug,
)
manufacturing_tasks = _expand_manufacturing_tasks(
assets=assets,
packages=list(manufacturing_template.get("manufacturing_package", [])),
city_slug=slug,
)
_resolve_manufacturing_predecessors(manufacturing_tasks, assets)
manufacturing_materials = _expand_manufacturing_materials(
manufacturing_tasks=manufacturing_tasks,
bom_catalog=bom_catalog,
)
manufacturing_verifications = _expand_manufacturing_verifications(
manufacturing_tasks=manufacturing_tasks,
qa_actions=qa_actions,
)
_apply_manufacturing_control_summaries(
manufacturing_tasks=manufacturing_tasks,
manufacturing_materials=manufacturing_materials,
manufacturing_verifications=manufacturing_verifications,
)
apps = _portal_apps(design_path, scenario_path)
meta = {
"city_slug": slug,
"city_name": _title(slug),
"country": city.get("country", ""),
"population": city.get("population", ""),
"source_design": _rel(design_path),
"source_scenario": _rel(scenario_path),
"opening_date_placeholder": "opening_date",
}
totals = {
"assets": len(assets),
"qa_gates": len(qa_template.get("construction_qa_gate", [])),
"qa_actions": len(qa_actions),
"manufacturing_packages": len(manufacturing_template.get("manufacturing_package", [])),
"manufacturing_tasks": len(manufacturing_tasks),
"manufacturing_materials": len(manufacturing_materials),
"manufacturing_verifications": len(manufacturing_verifications),
"maintenance_tasks": len(maintenance_tasks),
"trainsets": sum(1 for a in assets if a["asset_type"] == "rolling-stock"),
"stations": sum(1 for a in assets if a["asset_type"] == "station"),
"track_sections": sum(1 for a in assets if a["asset_type"] == "track-section"),
"waypoints": sum(1 for a in assets if a["asset_type"] == "waypoint"),
"switches": sum(1 for a in assets if a["asset_type"] == "switch"),
"energy_sites": sum(1 for a in assets if a["asset_type"] == "energy"),
}
return {
"meta": meta,
"totals": totals,
"applications": apps,
"qa_gates": qa_template.get("construction_qa_gate", []),
"maintenance_intervals": maint_template.get("maintenance_interval", []),
"manufacturing_packages": manufacturing_template.get("manufacturing_package", []),
"assets": assets,
"manufacturing_tasks": manufacturing_tasks,
"manufacturing_materials": manufacturing_materials,
"manufacturing_verifications": manufacturing_verifications,
"maintenance_tasks": maintenance_tasks,
"qa_actions": qa_actions,
"policy": {
"maintenance": maint_template.get("policy", {}),
"manufacturing": manufacturing_template.get("policy", {}),
},
}
def _expand_maintenance_tasks(
*,
assets: list[dict[str, Any]],
intervals: list[dict[str, Any]],
city_slug: str,
) -> list[dict[str, Any]]:
rows: list[dict[str, Any]] = []
by_type: dict[str, list[dict[str, Any]]] = {}
for asset in assets:
by_type.setdefault(str(asset["asset_type"]), []).append(asset)
for interval in intervals:
targets = _maintenance_targets(str(interval.get("id", "")))
for target in targets:
for asset in by_type.get(target, []):
rows.append({
"task_uid": f"{city_slug}:{asset['asset_id']}:{interval.get('id')}",
"city": city_slug,
"asset_id": asset["asset_id"],
"asset_name": asset["name"],
"asset_type": asset["asset_type"],
"line": asset["line"],
"task_id": interval.get("id", ""),
"cadence": interval.get("cadence", ""),
"trigger": interval.get("trigger", ""),
"scope": interval.get("scope", ""),
"evidence_required": interval.get("evidence", ""),
"owner": interval.get("owner", ""),
"next_due_basis": _next_due_basis(str(interval.get("cadence", ""))),
"status": "scheduled",
"severity": _task_severity(str(interval.get("trigger", ""))),
})
return rows
def _expand_qa_actions(
*,
assets: list[dict[str, Any]],
gates: list[dict[str, Any]],
city_slug: str,
) -> list[dict[str, Any]]:
rows: list[dict[str, Any]] = []
for gate in gates:
for asset in assets:
if not _qa_applies(gate, asset):
continue
rows.append({
"qa_uid": f"{city_slug}:{asset['asset_id']}:{gate.get('id')}",
"city": city_slug,
"asset_id": asset["asset_id"],
"asset_name": asset["name"],
"asset_type": asset["asset_type"],
"line": asset["line"],
"gate_id": gate.get("id", ""),
"domain": gate.get("domain", ""),
"stage": gate.get("stage", ""),
"hold_point": gate.get("hold_point", ""),
"evidence_required": gate.get("evidence", ""),
"release_authority": gate.get("release_authority", ""),
"status": "planned",
})
return rows
def _expand_manufacturing_tasks(
*,
assets: list[dict[str, Any]],
packages: list[dict[str, Any]],
city_slug: str,
) -> list[dict[str, Any]]:
rows: list[dict[str, Any]] = []
indexed_assets = sorted(assets, key=lambda a: (str(a.get("asset_type", "")), str(a.get("asset_id", ""))))
sequence_by_type: dict[str, int] = {}
order_by_asset: dict[str, int] = {}
for asset in indexed_assets:
asset_type = str(asset.get("asset_type", ""))
sequence_by_type[asset_type] = sequence_by_type.get(asset_type, 0) + 1
order_by_asset[str(asset.get("asset_id", ""))] = sequence_by_type[asset_type]
for package in packages:
target_types = {str(t) for t in package.get("asset_types", [])}
sequence = int(package.get("sequence", 0) or 0)
duration_days = int(package.get("duration_days", 1) or 1)
for asset in indexed_assets:
asset_type = str(asset.get("asset_type", ""))
if asset_type not in target_types:
continue
asset_order = order_by_asset.get(str(asset.get("asset_id", "")), 1)
start_day = _manufacturing_start_day(asset_type, sequence, asset_order)
finish_day = start_day + max(duration_days, 1) - 1
rows.append({
"manufacturing_uid": f"{city_slug}:{asset['asset_id']}:{package.get('id')}",
"city": city_slug,
"asset_id": asset["asset_id"],
"asset_name": asset["name"],
"asset_type": asset["asset_type"],
"line": asset["line"],
"package_id": package.get("id", ""),
"phase": package.get("phase", ""),
"sequence": sequence,
"work_center": package.get("work_center", ""),
"duration_days": duration_days,
"planned_start_day": start_day,
"planned_finish_day": finish_day,
"planned_start_basis": f"project_day_{start_day}",
"planned_finish_basis": f"project_day_{finish_day}",
"predecessors": package.get("predecessors", ""),
"predecessor_uids": "",
"external_predecessors": "",
"work_order_title": package.get("work_order_title", ""),
"work_order_detail": package.get("work_order_detail", ""),
"staff_roles": "; ".join(str(role) for role in package.get("staff_roles", [])),
"staff_tasks": package.get("staff_tasks", ""),
"materials_or_inputs": package.get("materials_or_inputs", ""),
"bom_refs": "; ".join(str(ref) for ref in package.get("bom_refs", [])),
"material_count": 0,
"material_status": "not generated",
"deliverables": package.get("deliverables", ""),
"evidence_required": package.get("evidence_required", ""),
"release_authority": package.get("release_authority", ""),
"qa_gate_hint": package.get("qa_gate_hint", ""),
"qa_uid": "",
"verification_uid": "",
"verification_status": "not generated",
"blocks_successors": "yes",
"status": "planned",
"priority": package.get("priority", "routine"),
})
return sorted(rows, key=lambda row: (
int(row["planned_start_day"]),
str(row["asset_type"]),
str(row["asset_id"]),
int(row["sequence"]),
))
def _resolve_manufacturing_predecessors(
manufacturing_tasks: list[dict[str, Any]],
assets: list[dict[str, Any]],
) -> None:
asset_by_id = {str(asset["asset_id"]): asset for asset in assets}
tasks_by_asset_package = {
(str(task["asset_id"]), str(task["package_id"])): str(task["manufacturing_uid"])
for task in manufacturing_tasks
}
system_by_package = {
str(task["package_id"]): str(task["manufacturing_uid"])
for task in manufacturing_tasks
if task.get("asset_type") == "system"
}
track_assets = [asset for asset in assets if asset.get("asset_type") == "track-section"]
for task in manufacturing_tasks:
resolved: list[str] = []
external: list[str] = []
asset = asset_by_id.get(str(task["asset_id"]), {})
for predecessor in _split_refs(str(task.get("predecessors", ""))):
uid = _resolve_manufacturing_predecessor(
predecessor=predecessor,
task=task,
asset=asset,
tasks_by_asset_package=tasks_by_asset_package,
system_by_package=system_by_package,
track_assets=track_assets,
)
if uid:
resolved.append(uid)
else:
external.append(predecessor)
task["predecessor_uids"] = "; ".join(resolved)
task["external_predecessors"] = "; ".join(external)
def _resolve_manufacturing_predecessor(
*,
predecessor: str,
task: dict[str, Any],
asset: dict[str, Any],
tasks_by_asset_package: dict[tuple[str, str], str],
system_by_package: dict[str, str],
track_assets: list[dict[str, Any]],
) -> str:
asset_id = str(task["asset_id"])
same_asset = tasks_by_asset_package.get((asset_id, predecessor))
if same_asset:
return same_asset
if predecessor in system_by_package:
return system_by_package[predecessor]
parent_asset_id = str(asset.get("parent_asset", ""))
parent = tasks_by_asset_package.get((parent_asset_id, predecessor))
if parent:
return parent
if predecessor.startswith("trk-"):
track_asset = _nearest_track_asset(asset, track_assets)
if track_asset:
return tasks_by_asset_package.get((str(track_asset["asset_id"]), predecessor), "")
return ""
def _nearest_track_asset(
asset: dict[str, Any],
track_assets: list[dict[str, Any]],
) -> dict[str, Any] | None:
parent_asset = str(asset.get("parent_asset", ""))
for track in track_assets:
if str(track.get("asset_id")) == parent_asset:
return track
station = str(asset.get("station", ""))
line = str(asset.get("line", ""))
candidates = [
track for track in track_assets
if (not line or track.get("line") == line)
and (not station or station in str(track.get("station", "")))
]
if candidates:
return sorted(candidates, key=lambda row: str(row.get("asset_id", "")))[0]
line_candidates = [track for track in track_assets if not line or track.get("line") == line]
return sorted(line_candidates, key=lambda row: str(row.get("asset_id", "")))[0] if line_candidates else None
def _expand_manufacturing_materials(
*,
manufacturing_tasks: list[dict[str, Any]],
bom_catalog: dict[str, dict[str, dict[str, str]]],
) -> list[dict[str, Any]]:
rows: list[dict[str, Any]] = []
for task in manufacturing_tasks:
for position, ref in enumerate(_split_refs(str(task.get("bom_refs", ""))), start=1):
source, key = _split_bom_ref(ref)
bom_row = bom_catalog.get(source, {}).get(key, {})
material = _material_from_bom_ref(
task=task,
source=source,
key=key,
bom_row=bom_row,
position=position,
)
rows.append(material)
return rows
def _material_from_bom_ref(
*,
task: dict[str, Any],
source: str,
key: str,
bom_row: dict[str, str],
position: int,
) -> dict[str, Any]:
if source == "rolling_stock_bom" and bom_row:
description = bom_row.get("description", key)
quantity = bom_row.get("quantity", "")
make_buy_source = bom_row.get("source", "")
base_usd = bom_row.get("base_usd", "")
cost_basis = bom_row.get("cost_basis", "")
evidence = "supplier certificate, serial/lot traceability, receiving inspection"
elif source == "rolling_stock_cots_fitout" and bom_row:
description = bom_row.get("name", key)
quantity = bom_row.get("qty_per_consist", "")
make_buy_source = "COTS"
base_usd = bom_row.get("consist_cost_base_usd", "")
cost_basis = bom_row.get("cost_basis", "")
evidence = "supplier datasheet, fit check, fire/safety evidence where applicable"
else:
description = key.replace("-", " ")
quantity = "per asset package"
make_buy_source = "PROJECT_KIT"
base_usd = ""
cost_basis = "Controlled project-kit placeholder; replace with detailed BOM row when available."
evidence = "kit issue note, receiving inspection, lot/serial traceability where applicable"
material_uid = f"{task['manufacturing_uid']}:MAT-{position:03d}"
return {
"material_uid": material_uid,
"manufacturing_uid": task["manufacturing_uid"],
"city": task["city"],
"asset_id": task["asset_id"],
"asset_name": task["asset_name"],
"asset_type": task["asset_type"],
"package_id": task["package_id"],
"phase": task["phase"],
"bom_source": source,
"bom_ref": key,
"description": description,
"quantity_basis": quantity,
"make_buy_source": make_buy_source,
"base_usd": base_usd,
"cost_basis": cost_basis,
"traceability_required": "yes",
"evidence_required": evidence,
"material_status": "required",
}
def _expand_manufacturing_verifications(
*,
manufacturing_tasks: list[dict[str, Any]],
qa_actions: list[dict[str, Any]],
) -> list[dict[str, Any]]:
qa_by_asset_gate = {
(str(row["asset_id"]), str(row["gate_id"])): row
for row in qa_actions
}
rows: list[dict[str, Any]] = []
for task in manufacturing_tasks:
gate_id = str(task.get("qa_gate_hint", ""))
qa = qa_by_asset_gate.get((str(task["asset_id"]), gate_id), {})
verification_uid = f"{task['manufacturing_uid']}:VERIFY"
rows.append({
"verification_uid": verification_uid,
"manufacturing_uid": task["manufacturing_uid"],
"qa_uid": qa.get("qa_uid", ""),
"city": task["city"],
"asset_id": task["asset_id"],
"asset_name": task["asset_name"],
"asset_type": task["asset_type"],
"package_id": task["package_id"],
"phase": task["phase"],
"qa_gate_id": gate_id,
"qa_stage": qa.get("stage", task.get("phase", "")),
"hold_point": qa.get("hold_point", task.get("work_order_title", "")),
"evidence_required": qa.get("evidence_required", task.get("evidence_required", "")),
"release_authority": qa.get("release_authority", task.get("release_authority", "")),
"required_result": "pass",
"blocks_successors": "yes",
"verification_source": "qa_action" if qa else "template",
"status": "required",
})
return rows
def _apply_manufacturing_control_summaries(
*,
manufacturing_tasks: list[dict[str, Any]],
manufacturing_materials: list[dict[str, Any]],
manufacturing_verifications: list[dict[str, Any]],
) -> None:
material_counts: dict[str, int] = {}
for row in manufacturing_materials:
uid = str(row["manufacturing_uid"])
material_counts[uid] = material_counts.get(uid, 0) + 1
verifications = {
str(row["manufacturing_uid"]): row
for row in manufacturing_verifications
}
for task in manufacturing_tasks:
uid = str(task["manufacturing_uid"])
verification = verifications.get(uid, {})
material_count = material_counts.get(uid, 0)
task["material_count"] = material_count
task["material_status"] = "required" if material_count else "missing"
task["qa_uid"] = verification.get("qa_uid", "")
task["verification_uid"] = verification.get("verification_uid", "")
task["verification_status"] = verification.get("status", "missing")
task["qa_hold_point"] = verification.get("hold_point", "")
def _manufacturing_start_day(asset_type: str, sequence: int, asset_order: int) -> int:
base_by_type = {
"system": 0,
"depots-production": 8,
"depot": 12,
"energy": 28,
"station": 36,
"track-section": 42,
"switch": 52,
"waypoint": 62,
"signalling-comms": 66,
"rolling-stock": 72,
"structure": 44,
}
spacing_by_type = {
"system": 0,
"depots-production": 4,
"depot": 5,
"energy": 4,
"station": 4,
"track-section": 3,
"switch": 2,
"waypoint": 2,
"signalling-comms": 2,
"rolling-stock": 4,
"structure": 6,
}
sequence_offset = max(sequence // 10, 0) * 5
return (
base_by_type.get(asset_type, 40)
+ (max(asset_order, 1) - 1) * spacing_by_type.get(asset_type, 3)
+ sequence_offset
)
def _maintenance_targets(task_id: str) -> list[str]:
if task_id.startswith("rs-"):
return ["rolling-stock"]
if task_id.startswith("station-"):
return ["station"]
if task_id in {"track-weekly", "track-geometry"}:
return ["track-section"]
if task_id == "switch-monthly":
return ["switch"]
if task_id == "structures-annual":
return ["structure"]
if task_id.startswith("energy-"):
return ["energy"]
if task_id.startswith("systems-"):
return ["signalling-comms", "waypoint"]
if task_id == "depot-tooling":
return ["depots-production"]
return []
def _qa_applies(gate: dict[str, Any], asset: dict[str, Any]) -> bool:
gate_id = str(gate.get("id", ""))
asset_type = str(asset.get("asset_type", ""))
if gate.get("domain") == "system":
return asset_type == "system"
if gate.get("domain") == "rolling-stock":
return asset_type == "rolling-stock"
if gate_id == "qa-20-survey-geotech":
return asset_type in {"track-section", "station", "depot"}
if gate_id == "qa-21-earthworks-drainage":
return asset_type == "track-section"
if gate_id == "qa-22-trackform-rail":
return asset_type in {"track-section", "switch"}
if gate_id == "qa-23-structures":
return asset_type == "structure"
if gate_id == "qa-24-stations-depots-plant":
return asset_type in {"station", "depot", "depots-production"}
if gate_id == "qa-25-power-energy":
return asset_type in {"energy", "depot"}
if gate_id == "qa-26-wayside-comms-safety":
return asset_type in {"signalling-comms", "switch", "waypoint"}
return False
def _portal_apps(design_path: Path, scenario_path: Path) -> list[dict[str, str]]:
return [
{
"id": "occ",
"name": "OCC Console",
"category": "operator-gui",
"status": "available",
"summary": "Read-only operations-control console for dispatcher playback and action rehearsal.",
"native_command": 'cargo run --release -p osr-occ-gui -- --operator "dispatcher-alpha"',
"web_command": "cd crates/osr-occ-gui && trunk serve web/index.html --port 8081",
"docs": "crates/osr-occ-gui/README.md",
},
{
"id": "simulator",
"name": "Simulation GUI",
"category": "operator-gui",
"status": "available",
"summary": "Scenario playback and train/energy event timeline.",
"native_command": f"cargo run --release -p osr-sim-gui -- --scenario {_rel(scenario_path)}",
"web_command": "cd crates/osr-sim-gui && trunk serve web/index.html --port 8082",
"docs": "crates/osr-sim-gui/README.md",
},
{
"id": "cbm",
"name": "CBM Backend",
"category": "backend-library",
"status": "library",
"summary": "Depot-side condition-based maintenance analysis and work-order generation.",
"native_command": "cargo test -p osr-cbm-backend",
"web_command": "",
"docs": "crates/osr-cbm-backend/Cargo.toml",
},
{
"id": "afc",
"name": "AFC Backoffice",
"category": "backend-library",
"status": "library",
"summary": "Fare settlement, revenue reconciliation, and fraud detection library.",
"native_command": "cargo test -p osr-afc-backoffice",
"web_command": "",
"docs": "crates/osr-afc-backoffice/Cargo.toml",
},
{
"id": "historian",
"name": "Historian",
"category": "backend-library",
"status": "library",
"summary": "Time-series metric ring buffers and decimation for operations telemetry.",
"native_command": "cargo test -p osr-historian",
"web_command": "",
"docs": "crates/osr-historian/Cargo.toml",
},
{
"id": "qa-maintenance",
"name": "Ops Core Portal",
"category": "embedded",
"status": "active",
"summary": "Asset register, manufacturing schedule, construction QA gates, maintenance schedule, work orders, evidence, defects, and audit.",
"native_command": f"python3 scripts/generate-qa-maintenance-data.py --design {_rel(design_path)} --scenario {_rel(scenario_path)}",
"web_command": "python3 scripts/ops-core-server.py --port 8008",
"docs": "docs/operations-portal/README.md",
},
]
def _write_csv(path: Path, rows: list[dict[str, Any]]) -> None:
if not rows:
path.write_text("")
return
fieldnames = list(rows[0].keys())
with path.open("w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames, lineterminator="\n")
writer.writeheader()
writer.writerows(rows)
def _load_bom_catalog(path: Path) -> dict[str, dict[str, dict[str, str]]]:
return {
"rolling_stock_bom": _load_csv_index(path / "rolling_stock_bom.csv", "line_id"),
"rolling_stock_cots_fitout": _load_csv_index(
path / "rolling_stock_cots_fitout_bom.csv",
"category",
),
}
def _load_csv_index(path: Path, key: str) -> dict[str, dict[str, str]]:
if not path.exists():
return {}
with path.open(newline="", encoding="utf-8") as f:
return {
str(row.get(key, "")): row
for row in csv.DictReader(f)
if row.get(key)
}
def _load_toml(path: Path) -> dict[str, Any]:
with path.open("rb") as f:
return tomllib.load(f)
def _city_prefix(slug: str) -> str:
letters = re.sub(r"[^a-zA-Z0-9]", "", slug).upper()
return (letters[:3] or "OSR").ljust(3, "X")
def _line_code(line: str) -> str:
match = re.search(r"(\d+)", line)
if match:
return f"L{int(match.group(1))}"
return re.sub(r"[^A-Z0-9]", "", line.upper())[:4] or "LINE"
def _line_from_station_id(station_id: str) -> str:
parts = station_id.split("-")
if len(parts) >= 2 and parts[0] == "line":
return "-".join(parts[:2])
return ""
def _display_station(station: dict[str, Any]) -> str:
return str(station.get("anchor_name") or station.get("name") or station.get("id"))
def _lat_lon(row: dict[str, Any]) -> str:
lat = row.get("lat")
lon = row.get("lon")
if lat is None or lon is None: