stats

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 18 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 stores derived request statistics in minute buckets.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StartFeed

func StartFeed(ctx context.Context, s *Store, src bifrost.LogSource, tail *bifrost.Tail, pace time.Duration, rawDays int, costs CostSink, log *slog.Logger) func()

StartFeed subscribes before the tail starts. Cancel ctx before waiting for the feed to finish.

Types

type CostSink added in v0.9.0

type CostSink interface {
	Price(bifrost.Log) store.RequestCost
	RequestCosts(ctx context.Context, ids []string) (map[string]store.RequestCost, error)
	RecordCosts(ctx context.Context, rows []store.RequestCost) error
	PruneRequestCost(ctx context.Context, before time.Time) error
}

CostSink prices terminal request log rows and persists them in the main store.

type Cursor

type Cursor struct {
	Timestamp time.Time
	ID        string
}

Cursor is the last point committed by a log feed. The zero value means it has not started.

type Dimensions

type Dimensions struct {
	Provider, Model                            string
	VirtualKeyID, RoutingRuleID, SelectedKeyID string
	FallbackIndex                              int
}

Dimensions are the untranslated request-log dimensions. Unset IDs are empty.

type KeyUse added in v0.12.0

type KeyUse struct {
	Served24h int64
	LastUsed  time.Time
}

KeyUse holds a provider key's 24-hour served request count and newest usage bucket.

type Mix added in v0.9.0

type Mix struct {
	Requests, Input, Output, CacheRead int64
}

Mix holds request and token-class totals over a range. Input excludes cache tokens.

type ModelPair

type ModelPair struct{ Provider, Model string }

ModelPair identifies an untranslated provider and model.

type Point

type Point struct {
	Time time.Time
	Summary
}

Point is one UTC series bucket.

type Result

type Result struct {
	Summary Summary
	Series  []Point
	Groups  []Summary
}

Result contains the totals, the zero-filled series, and one summary per group predicate in input order.

type Row added in v0.9.0

type Row struct {
	bifrost.Log
	Cost store.RequestCost
}

Row is one engine log row paired with its computed request cost.

type Store

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

Store holds the derived rollup, with a single SQLite connection serializing writes.

func Open

func Open(ctx context.Context, path string) (*Store, error)

Open opens the dedicated stats database at path. An existing database with a different schema version is discarded, so only one Store may own this path.

func (*Store) Close

func (s *Store) Close() error

Close closes the SQLite connection.

func (*Store) Coverage added in v0.13.1

func (s *Store) Coverage() (from, through time.Time, active []string, ok bool)

Coverage returns the time range [from, through] over which the request log feed has complete terminal-row coverage in this store, plus OAuth models currently in flight or recently completed on the tail. It returns ok=false until startup backfill completes or if the live tail feed drops events.

func (*Store) Cursor

func (s *Store) Cursor(ctx context.Context, name string) (Cursor, error)

Cursor returns the named feed's persisted position, or the zero value if absent.

func (*Store) Ingest

func (s *Store) Ingest(ctx context.Context, rows []Row) error

Ingest records terminal attempts once within the replay window. In-flight rows stay eligible when they are delivered again as terminal attempts.

func (*Store) IngestPage

func (s *Store) IngestPage(ctx context.Context, rows []Row, name string, pos Cursor) error

IngestPage records a page and advances the named cursor in one transaction. On restart, resume strictly after pos using timestamp and ID to break ties.

func (*Store) KeyUsage added in v0.12.0

func (s *Store) KeyUsage(ctx context.Context, now time.Time) (map[string]KeyUse, error)

KeyUsage summarizes per-key request volume in the 24 hours ending at now and the newest rollup bucket per key.

func (*Store) ModelPairs

func (s *Store) ModelPairs(ctx context.Context, from, to time.Time, filter func(Dimensions) bool) ([]ModelPair, error)

ModelPairs returns distinct nonempty models that match the range and filter, busiest first, breaking ties by model and then provider as in the log rankings. The predicate has the same restrictions as in Query.

func (*Store) Prune

func (s *Store) Prune(ctx context.Context, now time.Time, rawRetentionDays int) error

Prune removes expired minute buckets and replay IDs. The feed calls it hourly with the raw log retention; stats keep at least eight days.

func (*Store) Query

func (s *Store) Query(ctx context.Context, from, to time.Time, bucketSec int64, filter func(Dimensions) bool, groups ...func(Dimensions) bool) (Result, error)

Query includes both boundary minutes; a minute cannot be split. bucketSec must be a positive multiple of 60, or zero for no series. Predicates are evaluated while reading the store and must not call it; nil matches all dimensions.

func (*Store) SettleActualCost added in v0.10.0

func (s *Store) SettleActualCost(ctx context.Context, logID string, actualUSD float64) error

SettleActualCost applies a late actual charge to the minute bucket recorded when logID was ingested as unpriced. If logID is absent or already settled, it is a no-op.

func (*Store) TokenMix added in v0.9.0

func (s *Store) TokenMix(ctx context.Context, from, to time.Time, filter func(Dimensions) bool) (Mix, error)

TokenMix sums request counts and token classes matching the range and filter. Input excludes cache read and cache write tokens.

func (*Store) TokensByRule added in v0.11.0

func (s *Store) TokensByRule(ctx context.Context, since time.Time) (map[string]Mix, error)

TokensByRule sums request counts and token classes since the minute bucket of since, grouped by non-empty routing rule ID. Input excludes cache read and cache write tokens.

type Summary

type Summary struct {
	Requests, Errors                            int64
	PromptTokens, CompletionTokens, TotalTokens int64
	CacheReadTokens, CacheWriteTokens           int64
	ActualUSD, EquivalentUSD, SavingUSD         float64
	UnpricedActual, UnpricedEquivalent          int64
	LatencyP50Ms, LatencyP95Ms                  *float64
}

Summary holds attempt totals and approximate percentiles of successful attempts.

Jump to

Keyboard shortcuts

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