stats

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const RetentionDays = 30

RetentionDays is the rolling window for event retention.

Variables

This section is empty.

Functions

func DefaultDBPath

func DefaultDBPath() string

DefaultDBPath returns the path to the stats database (~/.human/stats.db), creating the directory if needed.

Types

type EventNameCount

type EventNameCount struct {
	EventName string `json:"event_name"`
	Count     int    `json:"count"`
}

EventNameCount holds an event name and its count.

type StatsStore

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

StatsStore persists tool call events in SQLite for historical trend queries.

func NewStatsStore

func NewStatsStore(dbPath string) (*StatsStore, error)

NewStatsStore opens (or creates) a SQLite database at dbPath and ensures the schema is up to date. Use ":memory:" for in-memory databases in tests.

func (*StatsStore) BuildToolStats

func (s *StatsStore) BuildToolStats(ctx context.Context, since, until time.Time) (*ToolStats, error)

BuildToolStats runs all aggregation queries for a time range and returns a ready-to-render ToolStats.

func (*StatsStore) Close

func (s *StatsStore) Close() error

Close closes the database connection.

func (*StatsStore) InsertEvent

func (s *StatsStore) InsertEvent(ctx context.Context, sessionID, eventName, toolName, cwd, errorType string, ts time.Time) error

InsertEvent persists a single tool call event.

func (*StatsStore) Prune

func (s *StatsStore) Prune(ctx context.Context) (int64, error)

Prune deletes events older than RetentionDays.

func (*StatsStore) QueryByEventName

func (s *StatsStore) QueryByEventName(ctx context.Context, since, until time.Time) ([]EventNameCount, error)

QueryByEventName returns event counts grouped by event_name for the given time range.

func (*StatsStore) QueryByHour

func (s *StatsStore) QueryByHour(ctx context.Context, since, until time.Time) ([]TimeBucket, error)

QueryByHour returns event counts grouped by hour for the given time range.

func (*StatsStore) QueryByTool

func (s *StatsStore) QueryByTool(ctx context.Context, since, until time.Time) ([]ToolCount, error)

QueryByTool returns tool call counts grouped by tool_name for the given time range.

func (*StatsStore) QueryTotal

func (s *StatsStore) QueryTotal(ctx context.Context, since, until time.Time) (int, error)

QueryTotal returns the total event count for the given time range.

type TimeBucket

type TimeBucket struct {
	Bucket string `json:"bucket"`
	Count  int    `json:"count"`
}

TimeBucket holds a time bucket label and its event count.

type ToolCount

type ToolCount struct {
	ToolName string `json:"tool_name"`
	Count    int    `json:"count"`
}

ToolCount holds a tool name and its event count.

type ToolStats

type ToolStats struct {
	ByTool      []ToolCount      `json:"by_tool"`
	ByHour      []TimeBucket     `json:"by_hour"`
	ByEventName []EventNameCount `json:"by_event_name"`
	TotalEvents int              `json:"total_events"`
	Since       time.Time        `json:"since"`
	Until       time.Time        `json:"until"`
}

ToolStats is the pre-aggregated stats payload sent to the TUI.

type Writer

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

Writer accepts hook events on a channel and inserts them into a StatsStore in a single background goroutine. Call Close to drain remaining events and shut down.

func NewWriter

func NewWriter(ctx context.Context, store *StatsStore, logger zerolog.Logger) *Writer

NewWriter creates a Writer and starts the background drain goroutine. The goroutine runs until ctx is cancelled or Close is called.

func (*Writer) Close

func (w *Writer) Close()

Close signals the writer to stop and waits for the background goroutine to finish draining. It is idempotent and safe to call after ctx cancellation has already stopped the goroutine.

func (*Writer) Send

func (w *Writer) Send(evt hookevents.Event)

Send enqueues an event for async persistence. If the channel is full the event is dropped silently (trends tolerate small gaps). The data channel is never closed, so Send is safe to call concurrently with — and after — Close without panicking; the quit case just discards events once shut down.

Jump to

Keyboard shortcuts

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