package web import ( "context" "log/slog" "net/http" "time" "gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi" "gitea.mynodes.xyz/ilker/llmgw/internal/store" ) // DashboardData is the whole page. type DashboardData struct { Nodes []adminapi.NodeView Models []adminapi.ModelView Replicas []adminapi.ReplicaView Unplaced []adminapi.ExplainView Usage []store.KeyUsage NodeCount int ModelCount int ReadyReplicas int TotalReplicas int AuthEnabled bool RefreshSec int Version string Now string } // Source is what the dashboard reads. An interface so the page can be rendered // in a test without a control plane. type Source interface { Nodes() []adminapi.NodeView Models() []adminapi.ModelView Replicas(model string) []adminapi.ReplicaView Explain(model string) adminapi.ExplainView } // UsageSource is the accounting half, absent when authentication is off. type UsageSource interface { UsageSince(ctx context.Context, since time.Time) ([]store.KeyUsage, error) } // Dashboard serves the read-only cluster view. // // It belongs on the admin listener and nowhere else. That listener is bound to // loopback by default and also serves pprof, which can dump the process heap — // where API keys live in plaintext. Exposing this page publicly would mean // exposing that, so reaching it from elsewhere is an SSH tunnel's job: // // ssh -L 9090:127.0.0.1:9090 llmgw-ctl // // Read-only on purpose. Anything that mints a credential stays in `llmgw keys`, // where it needs a shell on the machine rather than a browser session this // project has no way to authenticate. func Dashboard(src Source, usage UsageSource, version string, refresh time.Duration, log *slog.Logger, ) http.HandlerFunc { if log == nil { log = slog.Default() } if refresh <= 0 { refresh = 5 * time.Second } return func(w http.ResponseWriter, r *http.Request) { d := DashboardData{ RefreshSec: int(refresh.Seconds()), Version: version, Now: time.Now().UTC().Format(time.RFC3339), AuthEnabled: usage != nil, } if src != nil { d.Nodes = src.Nodes() d.Models = src.Models() d.Replicas = src.Replicas("") d.NodeCount = len(d.Nodes) d.ModelCount = len(d.Models) d.TotalReplicas = len(d.Replicas) for _, rep := range d.Replicas { if rep.State == "ready" { d.ReadyReplicas++ } } d.Unplaced = unplaced(src, d.Models) } if usage != nil { rows, err := usage.UsageSince(r.Context(), time.Now().Add(-24*time.Hour)) if err != nil { // A reporting query failing must not take the page with it: the // cluster state above is what someone is most likely here for. log.Warn("could not read usage for the dashboard", "err", err) } d.Usage = rows } render(w, "dashboard", d) } } // unplaced collects the placement explanations for models short of replicas. // // Only for those: asking why a satisfied model was not placed produces an empty // answer, and a page full of empty answers hides the one that matters. func unplaced(src Source, models []adminapi.ModelView) []adminapi.ExplainView { var out []adminapi.ExplainView for _, m := range models { if m.Ready >= m.Min { continue } if e := src.Explain(m.Name); len(e.Rejections) > 0 { out = append(out, e) } } return out }