package cmd import ( "encoding/json" "fmt" "time" "github.com/david/knox/internal/db" "github.com/spf13/cobra" ) // NewSyncStatusCmd reports per-source ingest sync health: the watcher's last // sweep per source (success/error/not-configured), how stale it is relative to // its interval, and the derived entry counts. func NewSyncStatusCmd(kdb *db.KnoxDB) *cobra.Command { var jsonOut bool cmd := &cobra.Command{ Use: "sync-status", Short: "Show per-source ingest sync health", RunE: func(c *cobra.Command, args []string) error { rows, err := kdb.SourceSync() if err != nil { return err } if jsonOut { b, _ := json.MarshalIndent(rows, "", " ") fmt.Println(string(b)) return nil } now := time.Now().UTC() fmt.Printf("%-18s %-13s %8s %-10s %-9s %s\n", "SOURCE", "STATUS", "ENTRIES", "LAST OK", "INTERVAL", "LAST ERROR") for _, r := range rows { lastOK := "never" if r.LastSuccess != "" { lastOK = db.RelTime(now, db.ParseRFC3339(r.LastSuccess)) + " ago" } interval := "-" if r.IntervalSeconds > 0 { interval = (time.Duration(r.IntervalSeconds) * time.Second).String() } lastErr := r.LastErrorDetail if len(lastErr) > 50 { lastErr = lastErr[:50] + "…" } fmt.Printf("%-18s %-13s %8d %-10s %-9s %s\n", r.ID, r.Status, r.Entries, lastOK, interval, lastErr) } fmt.Println() fmt.Println("ok = swept on schedule; stale = last success overdue; never = no sweep recorded;") fmt.Println("unconfigured = source env missing, sweep is a no-op.") return nil }, } cmd.Flags().BoolVar(&jsonOut, "json", false, "Output as JSON") return cmd }