Documentation
¶
Overview ¶
Package statsweb serves `unpause mcp stats --web`: the stats page from the embedded export and a small JSON API over the Access log (ADR 0014). The API is built from the log's Entries plus a title lookup in the local index, so it is tested without a network.
Index ¶
- Constants
- Variables
- func FriendlyName(id string) string
- func Listen(port int) (net.Listener, error)
- func NewHandler(cfg Config, port int) http.Handler
- func NewToken() (string, error)
- func Serve(ctx context.Context, ln net.Listener, cfg Config) error
- type Call
- type Client
- type Config
- type Info
- type KPIs
- type Lookup
- type Point
- type Query
- type Read
- type Series
- type Session
- type SessionCall
- type Stats
- type Tool
- type Window
Constants ¶
const OtherSlot = -1
OtherSlot is the Slot of every client past the fifth.
Variables ¶
var ErrRange = errors.New("range must be one of 24h, 7d, 30d, all")
ErrRange is a range the API doesn't know.
var ErrSlowShutdown = errors.New("connections still open on stop; closed them")
ErrSlowShutdown is what Serve returns when connections were still open after the grace period and had to be closed.
Functions ¶
func FriendlyName ¶
FriendlyName is what the page calls a client.
func Listen ¶
Listen binds 127.0.0.1:port, or a free port the OS picks when that one is taken. Port 0 always lets the OS pick.
func NewHandler ¶
NewHandler serves the page and the API for a server bound to 127.0.0.1:port. It answers only requests addressed to that host and port, so a page on another site can't read the log through DNS rebinding, sends no CORS headers, and answers GET and HEAD only. The API also wants the run's token, so another local user or process can't read titles without the URL the command printed.
Types ¶
type Call ¶
type Call struct {
At string `json:"at"`
Client string `json:"client"`
ClientName string `json:"client_name"`
Tool string `json:"tool"`
Input json.RawMessage `json:"input"`
SessionIDs []string `json:"session_ids"`
Count int `json:"count"`
Total int `json:"total"`
DurationMS int64 `json:"duration_ms"`
Error string `json:"error"`
}
Call is one row of the recent calls: the Entry as logged plus the client's friendly name.
type Client ¶
type Client struct {
ID string `json:"id"`
Name string `json:"name"`
// Slot is the client's color, 0 to 4, by first appearance in the whole log, so it
// doesn't change with the range. -1 means the sixth client or later: draw it as "Other".
Slot int `json:"slot"`
Calls int `json:"calls"`
// SessionsRead counts the distinct Sessions this client read with a successful get_session.
SessionsRead int `json:"sessions_read"`
LastSeen string `json:"last_seen"`
}
Client is one MCP client with calls in the range.
type Config ¶
type Config struct {
ReadLog func() ([]mcpserver.Entry, error)
Lookup func() Lookup // loads the index; at most once per titleTTL
Now func() time.Time
Loc *time.Location // bucket boundaries; time.Local in the command
Static fs.FS // the export: index.html, _next/...
// Token is this run's secret. Every /api/ request has to carry it as ?t=; without
// one, every API request is refused. The page gets it from the URL the command prints.
Token string
}
Config is what the server reads. Every API request calls ReadLog again, so the counts are as fresh as `unpause mcp stats`. Lookup loads the index for titles only when a response names Sessions, and its result is kept for titleTTL.
type Point ¶
type Point struct {
T string `json:"t"`
Covered bool `json:"covered"` // false before the first logged call: a gap, not a zero
ByClient map[string]int `json:"by_client"`
}
Point is one bucket of the calls-over-time series. T is the bucket's start in UTC; the bucket boundaries are local hours or local midnights of the machine running unpause.
type Read ¶
type Read struct {
ID string `json:"id"`
Title string `json:"title"` // the short id when the Session is no longer indexed
Account string `json:"account"`
Project string `json:"project"`
Reads int `json:"reads"`
LastRead string `json:"last_read"`
}
Read is one of the Sessions read most through get_session.
type Series ¶
type Series struct {
Bucket string `json:"bucket"` // "hour" or "day"
Points []Point `json:"points"`
}
Series is calls over time, by client id.
type Session ¶
type Session struct {
ID string `json:"id"`
Title string `json:"title"`
Account string `json:"account"`
Project string `json:"project"`
Calls []SessionCall `json:"calls"`
}
Session is the body of GET /api/session.
func BuildSession ¶
BuildSession answers GET /api/session: every logged call that returned id (an exact id), newest first, with the same sane times as BuildStats at now. ok is false when no call did, so the API never describes a Session the log doesn't name.
type SessionCall ¶
type SessionCall struct {
At string `json:"at"`
ClientName string `json:"client_name"`
Tool string `json:"tool"`
Count int `json:"count"`
}
SessionCall is one logged call that returned a Session.
type Stats ¶
type Stats struct {
Range string `json:"range"`
GeneratedAt string `json:"generated_at"`
Empty bool `json:"empty"` // no Entries in the log at all
KPIs KPIs `json:"kpis"`
Clients []Client `json:"clients"`
Series Series `json:"series"`
MostRead []Read `json:"most_read"`
TopQueries []Query `json:"top_queries"`
Tools []Tool `json:"tools"`
Recent []Call `json:"recent"`
}
Stats is the body of GET /api/stats.
type Window ¶
type Window struct {
Calls int `json:"calls"`
SessionsRead int `json:"sessions_read"` // distinct Sessions from successful get_session calls
Clients int `json:"clients"`
Errors int `json:"errors"`
P50MS *int64 `json:"p50_ms"` // lower median duration of successful calls; null when none
}
Window is the KPI row for one time window.