Extend shift reporting and downtime analytics
This commit is contained in:
1 parent
41a5ae7a63
commit
6e8eb6f50f
74 files changed
+5397
-238
No files matched your search
@@ -104,6 +104,18 @@ def _parser() -> argparse.ArgumentParser:
|
||||
default=Path("secrets/enlyze.env"),
|
||||
help="Local dotenv-style secret file (default: secrets/enlyze.env)",
|
||||
)
|
||||
historical = namespaces.add_parser("historical-reconcile", help="Repair historical attribution")
|
||||
historical.add_argument("--lookback-days", type=int, default=3)
|
||||
historical.add_argument("--dry-run", action="store_true")
|
||||
historical.add_argument("--verbose", action="store_true",
|
||||
help="Show individual record diagnostics as well as summaries")
|
||||
historical.add_argument("--config", type=Path,
|
||||
default=Path("config/shift-report-machines.yaml"))
|
||||
historical.add_argument("--order-formats", type=Path,
|
||||
default=Path("config/historical-order-formats.yaml"))
|
||||
historical.add_argument("--secrets-file", type=Path, default=Path("secrets/enlyze.env"))
|
||||
historical.add_argument("--erp-secrets-file", type=Path, default=Path("secrets/erp.env"))
|
||||
historical.add_argument("--db-secrets-file", type=Path, default=Path("secrets/postgres.env"))
|
||||
run = namespaces.add_parser("run", help="Foreground production runners")
|
||||
runners = run.add_subparsers(dest="command", required=True)
|
||||
material = runners.add_parser("material-poll", help="Continuously poll material consumption")
|
||||
@@ -113,7 +125,9 @@ def _parser() -> argparse.ArgumentParser:
|
||||
material.add_argument("--state-directory", type=Path, default=Path("data/state/material"))
|
||||
material.add_argument("--secrets-file", type=Path, default=Path("secrets/enlyze.env"))
|
||||
material.add_argument("--erp-secrets-file", type=Path, default=Path("secrets/erp.env"))
|
||||
power = runners.add_parser("power-meter", help="Collect configured ENLYZE power-meter telemetry")
|
||||
power = runners.add_parser(
|
||||
"power-meter", help="Collect configured ENLYZE power-meter telemetry",
|
||||
)
|
||||
power.add_argument("--config", type=Path, required=True)
|
||||
power.add_argument("--meter")
|
||||
power.add_argument("--start", help="ISO timestamp; defaults to one polling window ago")
|
||||
@@ -129,6 +143,16 @@ def _parser() -> argparse.ArgumentParser:
|
||||
monthly.add_argument("--month", required=True, help="YYYY-MM")
|
||||
monthly.add_argument("--db-secrets-file", type=Path, default=Path("secrets/postgres.env"))
|
||||
|
||||
shift = report_commands.add_parser("shift", help="Render one explicit shift report PDF")
|
||||
shift.add_argument("--config", type=Path, default=Path("config/shift-report-machines.yaml"))
|
||||
shift.add_argument("--machine", required=True)
|
||||
shift.add_argument("--start", required=True, help="ISO 8601 datetime with timezone")
|
||||
shift.add_argument("--end", required=True, help="ISO 8601 datetime with timezone")
|
||||
shift.add_argument("--output", type=Path, required=True)
|
||||
shift.add_argument("--secrets-file", type=Path, default=Path("secrets/enlyze.env"))
|
||||
shift.add_argument("--erp-secrets-file", type=Path, default=Path("secrets/erp.env"))
|
||||
shift.add_argument("--db-secrets-file", type=Path, default=Path("secrets/postgres.env"))
|
||||
|
||||
channel_material = runners.add_parser(
|
||||
"channel-material-poll", help="Continuously poll channel-level material consumption"
|
||||
)
|
||||
@@ -258,8 +282,9 @@ def _parse_cli_timestamp(value: str) -> object:
|
||||
|
||||
|
||||
def _run_power_meter(args: argparse.Namespace) -> int:
|
||||
from datetime import UTC, datetime, timedelta
|
||||
import time
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
from production_analytics.calculations.config import CalculationConfigError
|
||||
from production_analytics.enlyze.exploration import ConfigurationError
|
||||
from production_analytics.power_meter.config import load_power_meter_config
|
||||
@@ -292,6 +317,7 @@ def _run_power_meter(args: argparse.Namespace) -> int:
|
||||
|
||||
|
||||
def _run_monthly_report(args: argparse.Namespace) -> int:
|
||||
from production_analytics.calculations.config import CalculationConfigError
|
||||
from production_analytics.enlyze.exploration import ConfigurationError
|
||||
from production_analytics.power_meter.config import load_power_meter_config
|
||||
from production_analytics.power_meter.repository import PostgresPowerMeterRepository
|
||||
@@ -300,7 +326,8 @@ def _run_monthly_report(args: argparse.Namespace) -> int:
|
||||
config = load_power_meter_config(args.config, args.meter)
|
||||
environment = _runtime_environment(args.db_secrets_file)
|
||||
from production_analytics.service.postgres_material import PostgresSettings
|
||||
path = monthly_report(config, PostgresPowerMeterRepository(PostgresSettings.from_environment(environment)), args.month)
|
||||
repository = PostgresPowerMeterRepository(PostgresSettings.from_environment(environment))
|
||||
path = monthly_report(config, repository, args.month)
|
||||
print(path)
|
||||
return 0
|
||||
except (CalculationConfigError, ConfigurationError, ValueError, RuntimeError) as exc:
|
||||
@@ -308,6 +335,36 @@ def _run_monthly_report(args: argparse.Namespace) -> int:
|
||||
return 2
|
||||
|
||||
|
||||
def _run_shift_report(args: argparse.Namespace) -> int:
|
||||
from production_analytics.calculations.shift_speed import validate_speed_window
|
||||
from production_analytics.enlyze.exploration import ConfigurationError
|
||||
from production_analytics.presentation.shift_report import write_pdf
|
||||
from production_analytics.service.shift_report_config import load_shift_report_machines
|
||||
from production_analytics.service.shift_report_runtime import build_shift_report_service
|
||||
|
||||
try:
|
||||
start = _parse_cli_timestamp(args.start)
|
||||
end = _parse_cli_timestamp(args.end)
|
||||
validate_speed_window(start, end)
|
||||
machines = load_shift_report_machines(args.config)
|
||||
if args.machine not in machines:
|
||||
raise ValueError(f"Unknown shift-report machine: {args.machine}")
|
||||
environment = _runtime_environment(
|
||||
args.secrets_file, args.erp_secrets_file, args.db_secrets_file,
|
||||
)
|
||||
service = build_shift_report_service(machines, environment)
|
||||
report = service.build(machine_name=args.machine, start=start, end=end)
|
||||
write_pdf(report, args.output)
|
||||
except (ValueError, ConfigurationError, ExplorationError) as exc:
|
||||
print(f"Report error: {exc}", file=sys.stderr)
|
||||
return 2
|
||||
except Exception as exc:
|
||||
print(f"Report failed: {type(exc).__name__}", file=sys.stderr)
|
||||
return 1
|
||||
print(args.output)
|
||||
return 0
|
||||
|
||||
|
||||
def _run_downtime(args: argparse.Namespace) -> int:
|
||||
from production_analytics.service.downtime_runtime import build_downtime_runner
|
||||
|
||||
@@ -325,6 +382,17 @@ def _run_downtime(args: argparse.Namespace) -> int:
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
args = _parser().parse_args(argv)
|
||||
if args.namespace == "historical-reconcile":
|
||||
from production_analytics.service.historical_runtime import run
|
||||
|
||||
try:
|
||||
return run(args, _runtime_environment(
|
||||
args.secrets_file, args.erp_secrets_file, args.db_secrets_file,
|
||||
))
|
||||
except Exception as exc:
|
||||
print(f"Historical reconciliation failed: {type(exc).__name__}: {exc}",
|
||||
file=sys.stderr)
|
||||
return 1
|
||||
if args.namespace == "run":
|
||||
if args.command == "channel-material-poll":
|
||||
return _run_channel_material(args)
|
||||
@@ -336,6 +404,8 @@ def main(argv: Sequence[str] | None = None) -> int:
|
||||
return _run_downtime(args)
|
||||
return _run_material(args)
|
||||
if args.namespace == "report":
|
||||
if args.command == "shift":
|
||||
return _run_shift_report(args)
|
||||
return _run_monthly_report(args)
|
||||
if args.namespace != "enlyze" or args.command not in {"raw", "timeseries"}:
|
||||
return 2
|
||||
|
||||
Reference in new issue
Block a user