stats

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: AGPL-3.0 Imports: 21 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 stats serves the /api/v1/stats endpoints: aggregates over the derived tables of the signed-in user.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MergeSeconds

func MergeSeconds(in []Interval) int64

MergeSeconds returns the length of the union of in in whole seconds; intervals with End <= Start are ignored.

func Streak

func Streak(days []time.Time, today time.Time) int

Streak counts consecutive days ending today, or yesterday when today has none yet.

Types

type ActivityDay

type ActivityDay struct {
	Day     string `json:"day"`
	AgentMs int64  `json:"agent_ms"`
}

ActivityDay is the main-agent time of the sessions started on one local day.

type AgentItem

type AgentItem struct {
	AgentID           string           `json:"agent_id"`
	AgentType         string           `json:"agent_type"`
	Description       string           `json:"description"`
	ParentAgentID     *string          `json:"parent_agent_id"`
	SpawnToolUseID    *string          `json:"spawn_tool_use_id"`
	SpawnDepth        int              `json:"spawn_depth"`
	StartedAt         *time.Time       `json:"started_at"`
	EndedAt           *time.Time       `json:"ended_at"`
	Finished          *bool            `json:"finished"`
	DurationMs        int64            `json:"duration_ms"`
	DurationEstimated bool             `json:"duration_estimated"`
	SpawnedAt         *time.Time       `json:"spawned_at"` // the spawning tool call's start
	MergedAt          *time.Time       `json:"merged_at"`  // its result's time (a background agent's: its task notification); null while open
	Background        bool             `json:"background"` // spawned with run_in_background: its tool result is only the launch ack
	ToolCalls         map[string]int64 `json:"tool_calls"`
	ToolErrors        int64            `json:"tool_errors"`
	Usage             AgentUsage       `json:"usage"`
}

AgentItem is one agent of a session; the tree is encoded by ParentAgentID.

type AgentUsage

type AgentUsage struct {
	CostUSD    float64 `json:"cost_usd"`
	Tokens     int64   `json:"tokens"`
	Input      int64   `json:"input"`
	Output     int64   `json:"output"`
	CacheRead  int64   `json:"cache_read"`
	CacheWrite int64   `json:"cache_write"`
	Messages   int64   `json:"messages"`
}

AgentUsage is the usage of one agent.

type ChatLine added in v0.2.0

type ChatLine struct {
	LineItem
	AgentID string     `json:"agent_id"`
	Ts      *time.Time `json:"ts"`
}

ChatLine is one line of any agent of a session; Ts is null while its file has no timestamp yet.

type ChatPage added in v0.2.0

type ChatPage struct {
	Lines []ChatLine `json:"lines"`
	Next  *string    `json:"next"`
	Prev  *string    `json:"prev"`
}

ChatPage is one page of the session's lines in time order; Next and Prev are cursors for after and before.

type CurrentTool

type CurrentTool struct {
	Name      string    `json:"name"`
	Summary   string    `json:"summary"` // tool_calls.input_summary, <= 200 chars
	StartedAt time.Time `json:"started_at"`
}

CurrentTool is the newest unfinished tool call of an agent.

type DayPoint

type DayPoint struct {
	Day      string  `json:"day"`
	CostUSD  float64 `json:"cost_usd"`
	Tokens   int64   `json:"tokens"`
	Sessions int64   `json:"sessions"`
	AgentMs  int64   `json:"agent_ms"`
}

DayPoint is one local day.

type FailureAgg

type FailureAgg struct {
	Key       string `json:"key"`
	Count     int64  `json:"count"`
	TimeLostS int64  `json:"time_lost_s"`
}

FailureAgg is one aggregation bucket; TimeLostS is the union of the lost intervals within the key.

type FailureItem

type FailureItem struct {
	Category   string     `json:"category"`
	Detail     string     `json:"detail"`
	OccurredAt time.Time  `json:"occurred_at"`
	SessionID  string     `json:"session_id"`
	AgentID    string     `json:"agent_id"`
	LimitType  *string    `json:"limit_type"`
	ResetsAt   *time.Time `json:"resets_at"`
	TimeLostS  *int64     `json:"time_lost_s"`
	LineID     int64      `json:"line_id"` // raw_lines.id of the failing line (highest id at its offset)
}

FailureItem is one failure; TimeLostS is set for usage-limit failures only.

type FailurePage

type FailurePage struct {
	Items       []FailureItem `json:"items"`
	Next        *string       `json:"next"`
	ByCategory  []FailureAgg  `json:"by_category"`
	ByLimitType []FailureAgg  `json:"by_limit_type"`
	ByDay       []FailureAgg  `json:"by_day"`
	TimeLostS   int64         `json:"time_lost_s"`
}

FailurePage is the response of GET /api/v1/stats/failures.

type Filter

type Filter struct {
	From, To                      time.Time
	Device, Project, Model, Skill string
}

Filter is parsed from the query string.

type FilterOptions

type FilterOptions struct {
	Projects []ProjectOption `json:"projects"`
	Models   []string        `json:"models"`
	Skills   []string        `json:"skills"`
}

FilterOptions is the response of GET /api/v1/stats/filters.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler serves /api/v1/stats.

func New

func New(d *db.DB) *Handler

New returns a Handler.

func (*Handler) Register

func (h *Handler) Register(mux *http.ServeMux, auth *webauth.Auth, devLim *httpx.Limiter)

Register mounts the web routes behind auth; devLim is used by the device routes.

type HeatCell

type HeatCell struct {
	Weekday int   `json:"weekday"`
	Hour    int   `json:"hour"`
	Count   int64 `json:"count"`
}

HeatCell is the number of assistant messages in one weekday/hour slot (weekday 0 = Sunday, user time zone).

type Interval

type Interval struct{ Start, End time.Time }

Interval is a half-open time span.

type KPI

type KPI struct {
	Sessions          int64    `json:"sessions"`
	AgentMs           int64    `json:"agent_ms"`
	TotalAgentMs      int64    `json:"total_agent_ms"`
	AgentMsEstimated  bool     `json:"agent_ms_estimated"`
	CostUSD           float64  `json:"cost_usd"`
	CostUnknownModels []string `json:"cost_unknown_models"`
	Tokens            int64    `json:"tokens"`
	StreakDays        int      `json:"streak_days"`
	LongestAgentMs    int64    `json:"longest_agent_ms"`
	Failures          int64    `json:"failures"`
	TimeLostS         int64    `json:"time_lost_s"`
}

KPI is the headline figures of the selected sessions.

type LineItem

type LineItem struct {
	ID     int64           `json:"id"`
	Offset int64           `json:"offset"`
	Line   json.RawMessage `json:"line"`
}

LineItem is one stored transcript line.

type LinePage

type LinePage struct {
	Lines []LineItem `json:"lines"`
	Next  *string    `json:"next"`
	Prev  *string    `json:"prev"` // id of the first line when an earlier line of the agent exists
}

LinePage is one page of lines; Next is the last id when more exist.

type LiveAgent

type LiveAgent struct {
	AgentID       string       `json:"agent_id"`
	AgentType     string       `json:"agent_type"`
	Description   string       `json:"description"`
	ParentAgentID *string      `json:"parent_agent_id"`
	StartedAt     *time.Time   `json:"started_at"`
	CurrentTool   *CurrentTool `json:"current_tool"` // newest tool call of the agent without ended_at, started < 15 min ago
}

LiveAgent is an agent active within the last two minutes.

type LivePage

type LivePage struct {
	Items []LiveSession `json:"items"`
}

LivePage is the response of GET /api/v1/stats/live.

type LiveSession

type LiveSession struct {
	Session SessionItem `json:"session"`
	Agents  []LiveAgent `json:"agents"`
}

LiveSession is a live session with its active agents, main first.

type ModelPoint

type ModelPoint struct {
	Model        string  `json:"model"`
	CostUSD      float64 `json:"cost_usd"`
	Tokens       int64   `json:"tokens"`
	Messages     int64   `json:"messages"`
	PriceUnknown bool    `json:"price_unknown"`
}

ModelPoint is one model.

type Overview

type Overview struct {
	From       time.Time      `json:"from"`
	To         time.Time      `json:"to"`
	TimeZone   string         `json:"time_zone"`
	Reindexing bool           `json:"reindexing"`
	KPI        KPI            `json:"kpi"`
	PerDay     []DayPoint     `json:"per_day"`
	PerModel   []ModelPoint   `json:"per_model"`
	PerProject []ProjectPoint `json:"per_project"`
	Heatmap    []HeatCell     `json:"heatmap"`
	Previous   Totals         `json:"previous"`
	Activity   []ActivityDay  `json:"activity"`

	PerDayByProject ProjectSplit `json:"per_day_by_project"`
}

Overview is the response of GET /api/v1/stats/overview.

type PhaseItem

type PhaseItem struct {
	AgentID   string     `json:"agent_id"`
	Seq       int        `json:"seq"`
	Skill     string     `json:"skill"`
	StartedAt *time.Time `json:"started_at"`
	EndedAt   *time.Time `json:"ended_at"`
}

PhaseItem is one skill phase.

type ProjectDay

type ProjectDay struct {
	Day    string         `json:"day"`
	Values []ProjectValue `json:"values"`
}

ProjectDay is one local day; Values[i] belongs to ProjectSplit.Projects[i].

type ProjectKey

type ProjectKey struct {
	Key  string `json:"key"`
	Name string `json:"name"`
}

ProjectKey names one series of ProjectSplit; the trailing "_other" key collects every project beyond the top eight.

type ProjectOption

type ProjectOption struct {
	Path     string `json:"path"`
	Name     string `json:"name"`
	Sessions int64  `json:"sessions"`
	// LastActive is the newest session end (start if none ended) of the project in range.
	LastActive time.Time `json:"last_active"`
}

ProjectOption is one project in the filter dropdown.

type ProjectPoint

type ProjectPoint struct {
	Path     string  `json:"path"`
	Name     string  `json:"name"`
	CostUSD  float64 `json:"cost_usd"`
	Tokens   int64   `json:"tokens"`
	Sessions int64   `json:"sessions"`
	AgentMs  int64   `json:"agent_ms"`
}

ProjectPoint is one project.

type ProjectSplit

type ProjectSplit struct {
	Projects []ProjectKey `json:"projects"`
	Days     []ProjectDay `json:"days"`
}

ProjectSplit is the daily cost, tokens, sessions and agent time stacked by project.

type ProjectValue

type ProjectValue struct {
	CostUSD  float64 `json:"cost_usd"`
	Tokens   int64   `json:"tokens"`
	Sessions int64   `json:"sessions"`
	AgentMs  int64   `json:"agent_ms"`
}

ProjectValue is one project's share of a day: usage by its time, sessions and agent time by the session's start.

type SearchHit

type SearchHit struct {
	SessionID string     `json:"session_id"`
	AgentID   string     `json:"agent_id"`
	Role      string     `json:"role"`
	At        *time.Time `json:"at"`
	Title     string     `json:"title"`
	Snippet   string     `json:"snippet"`
	Rank      float64    `json:"rank"`
	LineID    int64      `json:"line_id"` // raw_lines.id of the hit line (highest id at its offset)
}

SearchHit is one matching prompt or reply; Snippet is cut from the raw line at query time.

type SearchResult

type SearchResult struct {
	Hits []SearchHit `json:"hits"`
}

SearchResult is the response of GET /api/v1/stats/search.

type Self

type Self struct {
	Today   SelfToday    `json:"today"`
	Session *SelfSession `json:"session"`
	Limit   *SelfLimit   `json:"limit"`
}

Self is the response of GET /v1/stats/self.

type SelfLimit

type SelfLimit struct {
	Type     string    `json:"type"`
	ResetsAt time.Time `json:"resets_at"`
}

SelfLimit is the active usage limit; an enum string and a time only.

type SelfSession

type SelfSession struct {
	SessionID    string     `json:"session_id"`
	AgentMs      int64      `json:"agent_ms"`
	TotalAgentMs int64      `json:"total_agent_ms"`
	CostUSD      float64    `json:"cost_usd"`
	Tokens       int64      `json:"tokens"`
	StartedAt    *time.Time `json:"started_at"`
	EndedAt      *time.Time `json:"ended_at"`
	Failures     int64      `json:"failures"`
}

SelfSession is the whole-session figures of one session; numbers and times only.

type SelfToday

type SelfToday struct {
	From      time.Time `json:"from"`
	To        time.Time `json:"to"`
	TimeZone  string    `json:"time_zone"`
	Sessions  int64     `json:"sessions"`
	AgentMs   int64     `json:"agent_ms"`
	CostUSD   float64   `json:"cost_usd"`
	Tokens    int64     `json:"tokens"`
	Failures  int64     `json:"failures"`
	TimeLostS int64     `json:"time_lost_s"`
}

SelfToday is today's aggregates; numbers only.

type SessionDetail

type SessionDetail struct {
	Session     SessionItem   `json:"session"`
	Agents      []AgentItem   `json:"agents"`
	SkillPhases []PhaseItem   `json:"skill_phases"`
	Failures    []FailureItem `json:"failures"`
}

SessionDetail is the response of GET /api/v1/stats/sessions/{id}.

type SessionItem

type SessionItem struct {
	SessionID        string    `json:"session_id"`
	Title            string    `json:"title"`
	Project          string    `json:"project"`
	ProjectName      string    `json:"project_name"`
	DeviceID         *string   `json:"device_id"`
	DeviceIDs        []string  `json:"device_ids"`
	Models           []string  `json:"models"`
	StartedAt        time.Time `json:"started_at"`
	EndedAt          time.Time `json:"ended_at"`
	IsLive           bool      `json:"is_live"`
	AgentMs          int64     `json:"agent_ms"`
	TotalAgentMs     int64     `json:"total_agent_ms"`
	AgentMsEstimated bool      `json:"agent_ms_estimated"`
	CostUSD          float64   `json:"cost_usd"`
	Tokens           int64     `json:"tokens"`
	Failures         int64     `json:"failures"`
	Skills           []string  `json:"skills"`
	LineCount        int64     `json:"line_count"`
	GitBranch        string    `json:"git_branch"`
	Turns            int64     `json:"turns"`
}

SessionItem is one session with whole-session figures.

type SessionPage

type SessionPage struct {
	Items []SessionItem `json:"items"`
	Next  *string       `json:"next"`
}

SessionPage is one page of sessions; Next is the opaque cursor of the following page.

type TimelineEvent added in v0.2.0

type TimelineEvent struct {
	ID          string    `json:"id"` // opaque and unique: the event's cursor key
	SessionID   string    `json:"session_id"`
	Kind        string    `json:"kind"`
	At          time.Time `json:"at"`
	DeviceID    *string   `json:"device_id"`
	Model       *string   `json:"model"`
	Project     string    `json:"project"`
	ProjectName string    `json:"project_name"`
	Title       string    `json:"title"`
	IsLive      bool      `json:"is_live"`
	Failures    int64     `json:"failures"`
}

TimelineEvent is one start or resume of a session.

type TimelinePage added in v0.2.0

type TimelinePage struct {
	Events []TimelineEvent `json:"events"`
	Next   *string         `json:"next"`
}

TimelinePage is one page of events, newest first.

type Totals

type Totals struct {
	Sessions int64   `json:"sessions"`
	AgentMs  int64   `json:"agent_ms"`
	CostUSD  float64 `json:"cost_usd"`
	Tokens   int64   `json:"tokens"`
}

Totals are the headline sums of a period; Overview.Previous holds them for the period of the same length before From.

Jump to

Keyboard shortcuts

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