|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + |
| 11 | + "health-receiver/internal/health" |
| 12 | + "health-receiver/internal/storage" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + schema := flag.String("schema", "health", "tenant schema") |
| 17 | + lang := flag.String("lang", "en", "briefing language") |
| 18 | + date := flag.String("date", "", "specific local date YYYY-MM-DD; defaults to latest briefing") |
| 19 | + days := flag.Int("days", 0, "retrospective sweep over recent daily_scores dates") |
| 20 | + noCheckin := flag.Bool("no-checkin", false, "ignore subjective check-in evidence") |
| 21 | + flag.Parse() |
| 22 | + |
| 23 | + db, err := storage.NewWithSchema(context.Background(), os.Getenv("DATABASE_URL"), *schema) |
| 24 | + if err != nil { |
| 25 | + log.Fatal(err) |
| 26 | + } |
| 27 | + defer db.Close() |
| 28 | + |
| 29 | + if *days > 0 { |
| 30 | + runSweep(db, *schema, *days, !*noCheckin) |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + if *date != "" { |
| 35 | + printDate(db, *schema, *date, !*noCheckin) |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + briefing, err := db.GetHealthBriefing(*lang) |
| 40 | + if err != nil { |
| 41 | + log.Fatal(err) |
| 42 | + } |
| 43 | + out := map[string]any{ |
| 44 | + "schema": *schema, |
| 45 | + "date": briefing.Date, |
| 46 | + "readiness_band": briefing.ReadinessBand, |
| 47 | + "energy_verdict": nil, |
| 48 | + "illness_suspicion": briefing.IllnessSuspicion, |
| 49 | + "subjective_checkin": briefing.SubjectiveCheckin, |
| 50 | + } |
| 51 | + if briefing.EnergyBank != nil { |
| 52 | + out["energy_verdict"] = briefing.EnergyBank.ActionVerdict |
| 53 | + } |
| 54 | + printJSON(out) |
| 55 | +} |
| 56 | + |
| 57 | +func printDate(db *storage.DB, schema, date string, includeCheckin bool) { |
| 58 | + var checkin *health.SubjectiveCheckinSummary |
| 59 | + if includeCheckin { |
| 60 | + checkin = checkinForDate(db, date) |
| 61 | + } |
| 62 | + ill := db.BuildIllnessEvidenceInput(date, checkin) |
| 63 | + out := map[string]any{ |
| 64 | + "schema": schema, |
| 65 | + "date": date, |
| 66 | + "illness_suspicion": health.ComputeIllnessSuspicion(ill), |
| 67 | + "subjective_checkin": checkin, |
| 68 | + } |
| 69 | + printJSON(out) |
| 70 | +} |
| 71 | + |
| 72 | +func runSweep(db *storage.DB, schema string, days int, includeCheckin bool) { |
| 73 | + if days < 1 { |
| 74 | + days = 1 |
| 75 | + } |
| 76 | + if days > 3650 { |
| 77 | + days = 3650 |
| 78 | + } |
| 79 | + rows, err := db.QueryReadOnly(fmt.Sprintf("SELECT date FROM daily_scores ORDER BY date DESC LIMIT %d", days)) |
| 80 | + if err != nil { |
| 81 | + log.Fatal(err) |
| 82 | + } |
| 83 | + counts := map[string]int{"none": 0, "low": 0, "moderate": 0, "high": 0} |
| 84 | + type hit struct { |
| 85 | + Date string `json:"date"` |
| 86 | + Confidence string `json:"confidence"` |
| 87 | + Signals []string `json:"signals"` |
| 88 | + } |
| 89 | + var hits []hit |
| 90 | + for _, row := range rows { |
| 91 | + date, _ := row["date"].(string) |
| 92 | + if date == "" { |
| 93 | + continue |
| 94 | + } |
| 95 | + var checkin *health.SubjectiveCheckinSummary |
| 96 | + if includeCheckin { |
| 97 | + checkin = checkinForDate(db, date) |
| 98 | + } |
| 99 | + ill := health.ComputeIllnessSuspicion(db.BuildIllnessEvidenceInput(date, checkin)) |
| 100 | + counts[ill.Confidence]++ |
| 101 | + if ill.Confidence == "moderate" || ill.Confidence == "high" { |
| 102 | + h := hit{Date: date, Confidence: ill.Confidence} |
| 103 | + for _, sig := range ill.Signals { |
| 104 | + if sig.Status == "ok" && sig.Strength != "weak" { |
| 105 | + h.Signals = append(h.Signals, sig.Metric+":"+sig.Strength) |
| 106 | + } |
| 107 | + } |
| 108 | + hits = append(hits, h) |
| 109 | + } |
| 110 | + } |
| 111 | + printJSON(map[string]any{"schema": schema, "days": days, "counts": counts, "top_hits": hits}) |
| 112 | +} |
| 113 | + |
| 114 | +func checkinForDate(db *storage.DB, date string) *health.SubjectiveCheckinSummary { |
| 115 | + row, err := db.GetTodayCheckin(date, storage.CheckinSourceTelegram) |
| 116 | + if err != nil || row == nil { |
| 117 | + return nil |
| 118 | + } |
| 119 | + return &health.SubjectiveCheckinSummary{Status: row.Status, Answer: row.Answer} |
| 120 | +} |
| 121 | + |
| 122 | +func printJSON(v any) { |
| 123 | + data, err := json.MarshalIndent(v, "", " ") |
| 124 | + if err != nil { |
| 125 | + log.Fatal(err) |
| 126 | + } |
| 127 | + fmt.Println(string(data)) |
| 128 | +} |
0 commit comments