File size: 3,217 Bytes
ba70497
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Calendar check on saved runs: no incident window may start after it ends.

    python tests/check_dates.py                    # the live feed (runs/run1) and every saved test run
    python tests/check_dates.py runs/run1          # one folder

For each incident in each traces.json it checks, with the company calendar:
  supplier delay   blocked from <= blocked until
  plant outage     outage start <= outage end
  quality hold     build start  <= build end
and flags a supplier delay whose dates fall on a weekend (the tower moves those to the next working day),
or that blocks more than 15 working days: the usual sign of a day read in the wrong month
("arriving March 31st instead of the 29th" read as April 29).
Exit code 1 when anything is out of order.
"""

from __future__ import annotations

import glob
import json
import os
import sys
from datetime import date

HITACHI = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, HITACHI)

from tower.calendar_util import is_working_day  # noqa: E402

MAX_DELAY_DAYS = 15
WINDOWS = {"supplier_delay": ("blocked_from", "blocked_until"), "plant_outage": ("outage_start", "outage_end"),
           "quality_hold": ("build_start", "build_end")}


def check(path: str) -> list[str]:
    problems = []
    with open(path, encoding="utf-8") as handle:
        traces = json.load(handle)
    for trace in traces:
        for inc in trace.get("incidents", []):
            keys = WINDOWS.get(inc.get("kind"))
            if not keys or not inc.get(keys[0]) or not inc.get(keys[1]):
                continue
            start, end = date.fromisoformat(inc[keys[0]]), date.fromisoformat(inc[keys[1]])
            where = f"{os.path.relpath(path, HITACHI)} {trace.get('event_id')} {inc['kind']}"
            if start > end:
                problems.append(f"{where}: {keys[0]} {start} is after {keys[1]} {end}")
            if inc["kind"] == "supplier_delay":
                for key, day in zip(keys, (start, end)):
                    if not is_working_day(day):
                        problems.append(f"{where}: {key} {day} is a weekend")
                working = sum(is_working_day(date.fromordinal(o)) for o in range(start.toordinal(), end.toordinal()))
                if working > MAX_DELAY_DAYS:
                    problems.append(f"{where}: blocks {working} working days ({start} to {end}); check the dates")
    return problems


def main() -> int:
    folders = sys.argv[1:] or ["runs/run1", *sorted(glob.glob("runs/*/*/")), "runs/submission"]
    files = []
    for folder in folders:
        path = os.path.join(HITACHI, folder, "traces.json")
        if os.path.isfile(path):
            files.append(path)
    problems, incidents = [], 0
    for path in files:
        problems += check(path)
        with open(path, encoding="utf-8") as handle:
            incidents += sum(len(t.get("incidents", [])) for t in json.load(handle))
    for p in problems:
        print("✗", p)
    print(f"{len(files)} runs, {incidents} incidents checked: "
          + ("every window is in order." if not problems else f"{len(problems)} problems."))
    return 1 if problems else 0


if __name__ == "__main__":
    raise SystemExit(main())