statsweb

package
v0.10.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const OtherSlot = -1

OtherSlot is the Slot of every client past the fifth.

Variables

View Source
var ErrRange = errors.New("range must be one of 24h, 7d, 30d, all")

ErrRange is a range the API doesn't know.

View Source
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

func FriendlyName(id string) string

FriendlyName is what the page calls a client.

func Listen

func Listen(port int) (net.Listener, error)

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

func NewHandler(cfg Config, port int) http.Handler

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.

func NewToken

func NewToken() (string, error)

NewToken is a random 128-bit token in hex, one per run of the server.

func Serve

func Serve(ctx context.Context, ln net.Listener, cfg Config) error

Serve answers on ln until ctx is done, then shuts down gracefully.

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 Info

type Info struct {
	Title   string
	Account string
	Project string
}

Info is what the local index says about a Session. The log never holds it (ADR 0013).

type KPIs

type KPIs struct {
	Window
	Prev *Window `json:"prev"`
}

KPIs is the range's Window plus the same-length window just before it (null for "all").

type Lookup

type Lookup func(id string) (Info, bool)

Lookup finds a Session in the local index by its full id.

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 Query

type Query struct {
	Query string `json:"query"`
	Count int    `json:"count"`
}

Query is a search_sessions query and how often it was sent.

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

func BuildSession(raw []mcpserver.Entry, id string, now time.Time, lookup Lookup) (Session, bool)

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.

func BuildStats

func BuildStats(raw []mcpserver.Entry, rng string, now time.Time, loc *time.Location, lookup Lookup) (Stats, error)

BuildStats answers GET /api/stats for rng at now. Bucket boundaries are local to loc.

type Tool

type Tool struct {
	Name  string `json:"name"`
	Count int    `json:"count"`
}

Tool is a tool and how often it was called.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL