a8ec7b80de
---ci--- project: inboxos phase: 1 milestone: v1.4 status: execute wave: 2 tasks: [T3, T4, T5, T6, T7, T8] ---/ci---
76 lines
2.2 KiB
Python
Executable File
76 lines
2.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Daily digest: one-line-per-module summary of yesterday's runs.
|
|
|
|
v1.4 REQ-INB-47.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
import smtplib
|
|
import sys
|
|
from datetime import date, datetime, timedelta
|
|
from email.message import EmailMessage
|
|
|
|
|
|
ARTIFACTS_DIR = os.environ.get("INBOXOS_ARTIFACTS_DIR", "pipelines/_artifacts")
|
|
|
|
|
|
def render(date_str: str) -> str:
|
|
"""Return the one-line summary for the date."""
|
|
path = os.path.join(ARTIFACTS_DIR, f"{date_str}.jsonl")
|
|
if not os.path.exists(path):
|
|
return f"{date_str}: no runs"
|
|
finance = crm = intel = promo = errors = 0
|
|
with open(path) as f:
|
|
for line in f:
|
|
try:
|
|
ev = json.loads(line)
|
|
except json.JSONDecodeError:
|
|
continue
|
|
mod = ev.get("module", "")
|
|
if mod in ("finance", "crm", "intel", "promotions"):
|
|
locals()[mod] += 1
|
|
if ev.get("event", "").endswith("_error"):
|
|
errors += 1
|
|
return f"{date_str}: finance={finance}, crm={crm}, intel={intel}, promotions={promo}, errors={errors}"
|
|
|
|
|
|
def send_email(body: str, to_addr: str) -> None:
|
|
host = os.environ.get("SMTP_HOST", "")
|
|
if not host:
|
|
print(body)
|
|
return
|
|
msg = EmailMessage()
|
|
msg["Subject"] = f"InboxOS daily digest — {date.today().isoformat()}"
|
|
msg["From"] = os.environ.get("SMTP_FROM", "inboxos@local")
|
|
msg["To"] = to_addr
|
|
msg.set_content(body)
|
|
with smtplib.SMTP(host, int(os.environ.get("SMTP_PORT", "25"))) as s:
|
|
if os.environ.get("SMTP_USER"):
|
|
s.starttls()
|
|
s.login(os.environ["SMTP_USER"], os.environ["SMTP_PASS"])
|
|
s.send_message(msg)
|
|
|
|
|
|
def main() -> int:
|
|
p = argparse.ArgumentParser(description="InboxOS daily digest")
|
|
p.add_argument("--date", help="YYYY-MM-DD (default: yesterday)")
|
|
p.add_argument("--email", help="Send to this address via SMTP")
|
|
args = p.parse_args()
|
|
|
|
if not args.date:
|
|
args.date = (date.today() - timedelta(days=1)).isoformat()
|
|
|
|
body = render(args.date)
|
|
if args.email:
|
|
send_email(body, args.email)
|
|
else:
|
|
print(body)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|