db

package
v0.260224.1130 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MPL-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AggregatedStat

type AggregatedStat struct {
	Key             string  `json:"key"`
	ProviderUUID    string  `json:"provider_uuid,omitempty"`
	ProviderName    string  `json:"provider_name,omitempty"`
	Model           string  `json:"model,omitempty"`
	Scenario        string  `json:"scenario,omitempty"`
	RequestCount    int64   `json:"request_count"`
	TotalTokens     int64   `json:"total_tokens"`
	InputTokens     int64   `json:"total_input_tokens"`
	OutputTokens    int64   `json:"total_output_tokens"`
	AvgInputTokens  float64 `json:"avg_input_tokens"`
	AvgOutputTokens float64 `json:"avg_output_tokens"`
	AvgLatencyMs    float64 `json:"avg_latency_ms"`
	ErrorCount      int64   `json:"error_count"`
	ErrorRate       float64 `json:"error_rate"`
	StreamedCount   int64   `json:"streamed_count"`
	StreamedRate    float64 `json:"streamed_rate"`
}

AggregatedStat represents aggregated usage statistics

type EndpointType

type EndpointType string

EndpointType defines the type of API endpoint

const (
	EndpointTypeChat      EndpointType = "chat"
	EndpointTypeResponses EndpointType = "responses"
)

type ModelCapability

type ModelCapability struct {
	ProviderUUID string       `gorm:"primaryKey;column:provider_uuid"`
	ModelID      string       `gorm:"primaryKey;column:model_id"`
	EndpointType EndpointType `gorm:"primaryKey;column:endpoint_type"`
	Available    bool         `gorm:"column:available"`
	LatencyMs    int          `gorm:"column:latency_ms"`
	LastChecked  time.Time    `gorm:"column:last_checked"`
	ErrorMessage string       `gorm:"column:error_message"`
	CreatedAt    time.Time    `gorm:"column:created_at"`
	UpdatedAt    time.Time    `gorm:"column:updated_at"`
}

ModelCapability stores endpoint capability information for each model

func (ModelCapability) TableName

func (ModelCapability) TableName() string

TableName specifies the table name for ModelCapability

type ModelCapabilityStore

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

ModelCapabilityStore persists model endpoint capability information in SQLite using GORM.

func NewModelCapabilityStore

func NewModelCapabilityStore(baseDir string) (*ModelCapabilityStore, error)

NewModelCapabilityStore creates or loads a model capability store using SQLite database.

func (*ModelCapabilityStore) GetCapability

func (mcs *ModelCapabilityStore) GetCapability(providerUUID, modelID string, endpointType EndpointType) (ModelCapability, bool)

GetCapability retrieves a single endpoint capability for a model

func (*ModelCapabilityStore) GetModelCapability

func (mcs *ModelCapabilityStore) GetModelCapability(providerUUID, modelID string) (ModelEndpointCapability, bool)

GetModelCapability retrieves aggregated capabilities for a model

func (*ModelCapabilityStore) GetProviderCapabilities

func (mcs *ModelCapabilityStore) GetProviderCapabilities(providerUUID string) map[string]ModelEndpointCapability

GetProviderCapabilities retrieves all capabilities for a provider

func (*ModelCapabilityStore) IsStale

func (mcs *ModelCapabilityStore) IsStale(providerUUID, modelID string, maxAge time.Duration) bool

IsStale checks if capabilities are stale (older than specified duration)

func (*ModelCapabilityStore) RemoveModel

func (mcs *ModelCapabilityStore) RemoveModel(providerUUID, modelID string) error

RemoveModel removes capabilities for a specific model

func (*ModelCapabilityStore) RemoveProvider

func (mcs *ModelCapabilityStore) RemoveProvider(providerUUID string) error

RemoveProvider removes all capabilities for a provider

func (*ModelCapabilityStore) SaveCapability

func (mcs *ModelCapabilityStore) SaveCapability(providerUUID, modelID string, endpointType EndpointType, available bool, latencyMs int, errorMsg string) error

SaveCapability saves a single endpoint capability for a model

type ModelEndpointCapability

type ModelEndpointCapability struct {
	ProviderUUID       string
	ModelID            string
	SupportsChat       bool
	ChatLatencyMs      int
	ChatError          string
	SupportsResponses  bool
	ResponsesLatencyMs int
	ResponsesError     string
	PreferredEndpoint  string // "chat", "responses", or ""
	LastVerified       time.Time
}

ModelEndpointCapability represents aggregated endpoint capabilities for a model

type ModelStore

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

ModelStore persists provider model information in SQLite using GORM.

func NewModelStore

func NewModelStore(baseDir string) (*ModelStore, error)

NewModelStore creates or loads a model store using SQLite database.

func (*ModelStore) GetAllModelRecords

func (ms *ModelStore) GetAllModelRecords() []ProviderModelRecord

GetAllModelRecords returns all provider records (with metadata)

func (*ModelStore) GetAllProviders

func (ms *ModelStore) GetAllProviders() []string

GetAllProviders returns all provider UUIDs that have models

func (*ModelStore) GetModelCount

func (ms *ModelStore) GetModelCount(providerUUID string) int

GetModelCount returns the number of models for a provider

func (*ModelStore) GetModels

func (ms *ModelStore) GetModels(providerUUID string) []string

GetModels returns models for a provider by UUID

func (*ModelStore) GetProviderInfo

func (ms *ModelStore) GetProviderInfo(providerUUID string) (apiBase string, lastUpdated string, exists bool)

GetProviderInfo returns basic info about a provider (apiBase, lastUpdated, exists)

func (*ModelStore) HasModels

func (ms *ModelStore) HasModels(providerUUID string) bool

HasModels checks if a provider has models

func (*ModelStore) RemoveProvider

func (ms *ModelStore) RemoveProvider(providerUUID string) error

RemoveProvider removes all models for a provider by UUID

func (*ModelStore) SaveModels

func (ms *ModelStore) SaveModels(provider *typ.Provider, apiBase string, models []string) error

SaveModels saves models for a provider by UUID

type ProviderModelRecord

type ProviderModelRecord struct {
	ProviderUUID string    `gorm:"primaryKey;column:provider_uuid"`
	ProviderName string    `gorm:"column:provider_name;index"`
	APIBase      string    `gorm:"column:api_base"`
	Models       string    `gorm:"column:models;type:text"` // JSON array of model names
	LastUpdated  time.Time `gorm:"column:last_updated"`
	CreatedAt    time.Time `gorm:"column:created_at"`
	UpdatedAt    time.Time `gorm:"column:updated_at"`
}

ProviderModelRecord is the GORM model for persisting provider models

func (ProviderModelRecord) TableName

func (ProviderModelRecord) TableName() string

TableName specifies the table name for GORM

type RuleServiceRecord

type RuleServiceRecord struct {
	RuleUUID         string `gorm:"primaryKey;column:rule_uuid"`
	CurrentServiceID string `gorm:"column:current_service_id"` // provider:model format
}

RuleServiceRecord stores the current service for each rule using provider+model as ID This is persisted to SQLite to avoid frequent config.json writes

func (RuleServiceRecord) TableName

func (RuleServiceRecord) TableName() string

TableName specifies the table name for GORM

type RuleStateStore

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

RuleStateStore manages rule state persistence separately from config

func NewRuleStateStore

func NewRuleStateStore(baseDir string) (*RuleStateStore, error)

NewRuleStateStore creates or loads a rule state store using SQLite database.

func (*RuleStateStore) ClearAll

func (rs *RuleStateStore) ClearAll() error

ClearAll removes all persisted rule state

func (*RuleStateStore) GetServiceID

func (rs *RuleStateStore) GetServiceID(ruleUUID string) (string, error)

GetServiceID retrieves the current service ID for a rule

func (*RuleStateStore) HydrateRules

func (rs *RuleStateStore) HydrateRules(rules []typ.Rule) error

HydrateRules loads service IDs from SQLite into the provided rules

func (*RuleStateStore) SetServiceID

func (rs *RuleStateStore) SetServiceID(ruleUUID string, serviceID string) error

SetServiceID sets the current service ID for a rule

type ServiceStatsRecord

type ServiceStatsRecord struct {
	// Composite primary key: provider + model (stats are global, not per-rule)
	Provider             string    `gorm:"primaryKey;column:provider"`
	Model                string    `gorm:"primaryKey;column:model"`
	ServiceID            string    `gorm:"column:service_id"`
	RequestCount         int64     `gorm:"column:request_count"`
	LastUsed             time.Time `gorm:"column:last_used"`
	WindowStart          time.Time `gorm:"column:window_start"`
	WindowRequestCount   int64     `gorm:"column:window_request_count"`
	WindowTokensConsumed int64     `gorm:"column:window_tokens_consumed"`
	WindowInputTokens    int64     `gorm:"column:window_input_tokens"`
	WindowOutputTokens   int64     `gorm:"column:window_output_tokens"`
	TimeWindow           int       `gorm:"column:time_window"`
}

ServiceStatsRecord is the GORM model for persisting service statistics

func (ServiceStatsRecord) TableName

func (ServiceStatsRecord) TableName() string

TableName specifies the table name for GORM

type StatsStore

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

StatsStore persists service usage statistics in SQLite using GORM.

func NewStatsStore

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

NewStatsStore creates or loads a stats store using SQLite database.

func (*StatsStore) ClearAll

func (ss *StatsStore) ClearAll() error

ClearAll removes all persisted stats.

func (*StatsStore) Get

func (ss *StatsStore) Get(provider, model string) (loadbalance.ServiceStats, bool)

Get returns stats for a specific provider/model combination.

func (*StatsStore) HydrateRules

func (ss *StatsStore) HydrateRules(rules []typ.Rule) error

HydrateRules injects stored stats into the provided rules and initializes missing entries.

func (*StatsStore) RecordUsage

func (ss *StatsStore) RecordUsage(service *loadbalance.Service, inputTokens, outputTokens int) (loadbalance.ServiceStats, error)

RecordUsage records usage for a service and persists the updated stats.

func (*StatsStore) ServiceKey

func (ss *StatsStore) ServiceKey(provider, model string) string

ServiceKey builds a unique key for a provider/model combination.

func (*StatsStore) Snapshot

func (ss *StatsStore) Snapshot() map[string]loadbalance.ServiceStats

Snapshot returns a copy of all stats keyed by provider:model.

func (*StatsStore) UpdateFromService

func (ss *StatsStore) UpdateFromService(service *loadbalance.Service) error

UpdateFromService stores the current stats from a service into the store.

type TimeSeriesData

type TimeSeriesData struct {
	Timestamp    string  `json:"timestamp"`
	RequestCount int64   `json:"request_count"`
	TotalTokens  int64   `json:"total_tokens"`
	InputTokens  int64   `json:"input_tokens"`
	OutputTokens int64   `json:"output_tokens"`
	ErrorCount   int64   `json:"error_count"`
	AvgLatencyMs float64 `json:"avg_latency_ms"`
}

TimeSeriesData represents a single time bucket in time series data

type UsageDailyRecord

type UsageDailyRecord struct {
	ID           uint      `gorm:"primaryKey;autoIncrement;column:id"`
	Date         time.Time `gorm:"column:date;index:idx_date;index:idx_date_provider_model;not null"`
	ProviderUUID string    `gorm:"column:provider_uuid;index:idx_date_provider_model;not null"`
	ProviderName string    `gorm:"column:provider_name;not null"`
	Model        string    `gorm:"column:model;index:idx_date_provider_model;not null"`
	RequestCount int64     `gorm:"column:request_count;not null"`
	TotalTokens  int64     `gorm:"column:total_tokens;not null"`
	InputTokens  int64     `gorm:"column:input_tokens;not null"`
	OutputTokens int64     `gorm:"column:output_tokens;not null"`
	ErrorCount   int64     `gorm:"column:error_count;default:0"`
}

UsageDailyRecord is the GORM model for daily aggregated usage statistics

func (UsageDailyRecord) TableName

func (UsageDailyRecord) TableName() string

TableName specifies the table name for GORM

type UsageMonthlyRecord

type UsageMonthlyRecord struct {
	ID           uint   `gorm:"primaryKey;autoIncrement;column:id"`
	Year         int    `gorm:"column:year;not null"`
	Month        int    `gorm:"column:month;not null"`
	ProviderUUID string `gorm:"column:provider_uuid;not null"`
	ProviderName string `gorm:"column:provider_name;not null"`
	Model        string `gorm:"column:model;not null"`
	RequestCount int64  `gorm:"column:request_count;not null"`
	TotalTokens  int64  `gorm:"column:total_tokens;not null"`
	InputTokens  int64  `gorm:"column:input_tokens;not null"`
	OutputTokens int64  `gorm:"column:output_tokens;not null"`
	ErrorCount   int64  `gorm:"column:error_count;default:0"`
}

UsageMonthlyRecord is the GORM model for monthly aggregated usage statistics

func (UsageMonthlyRecord) TableName

func (UsageMonthlyRecord) TableName() string

TableName specifies the table name for GORM

type UsageRecord

type UsageRecord struct {
	ID           uint      `gorm:"primaryKey;autoIncrement;column:id"`
	ProviderUUID string    `gorm:"column:provider_uuid;index:idx_provider_model;not null"`
	ProviderName string    `gorm:"column:provider_name;not null"`
	Model        string    `gorm:"column:model;index:idx_provider_model;not null"`
	Scenario     string    `gorm:"column:scenario;index:idx_scenario;not null"`
	RuleUUID     string    `gorm:"column:rule_uuid;index:idx_rule"`
	RequestModel string    `gorm:"column:request_model"`
	Timestamp    time.Time `gorm:"column:timestamp;index:idx_timestamp;index:idx_timestamp_scenario;not null"`
	InputTokens  int       `gorm:"column:input_tokens;not null"`
	OutputTokens int       `gorm:"column:output_tokens;not null"`
	TotalTokens  int       `gorm:"column:total_tokens;index;not null"`
	Status       string    `gorm:"column:status;index;not null"` // success, error, partial
	ErrorCode    string    `gorm:"column:error_code"`
	LatencyMs    int       `gorm:"column:latency_ms"`
	Streamed     bool      `gorm:"column:streamed;type:integer"`
}

UsageRecord is the GORM model for persisting individual usage records

func (UsageRecord) TableName

func (UsageRecord) TableName() string

TableName specifies the table name for GORM

type UsageStatsQuery

type UsageStatsQuery struct {
	GroupBy   string // model, provider, scenario, rule, daily, hourly
	StartTime time.Time
	EndTime   time.Time
	Provider  string
	Model     string
	Scenario  string
	RuleUUID  string
	Status    string
	Limit     int
	SortBy    string // total_tokens, request_count, avg_latency
	SortOrder string // asc, desc
}

GetAggregatedStats returns aggregated usage statistics based on query parameters

type UsageStore

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

UsageStore persists usage records in SQLite using GORM.

func NewUsageStore

func NewUsageStore(baseDir string) (*UsageStore, error)

NewUsageStore creates or loads a usage store using SQLite database.

func (*UsageStore) AggregateToDaily

func (us *UsageStore) AggregateToDaily(date time.Time) (int64, error)

AggregateToDaily aggregates records to daily summaries

func (*UsageStore) DeleteOlderThan

func (us *UsageStore) DeleteOlderThan(cutoffDate time.Time) (int64, error)

DeleteOlderThan deletes records older than the specified date

func (*UsageStore) GetAggregatedStats

func (us *UsageStore) GetAggregatedStats(query UsageStatsQuery) ([]AggregatedStat, error)

GetAggregatedStats returns aggregated statistics

func (*UsageStore) GetRecords

func (us *UsageStore) GetRecords(startTime, endTime time.Time, filters map[string]string, limit, offset int) ([]UsageRecord, int64, error)

GetRecords returns individual usage records (for debugging/audit)

func (*UsageStore) GetTimeSeries

func (us *UsageStore) GetTimeSeries(interval string, startTime, endTime time.Time, filters map[string]string) ([]TimeSeriesData, error)

GetTimeSeries returns time-series data for usage

func (*UsageStore) RecordUsage

func (us *UsageStore) RecordUsage(record *UsageRecord) error

RecordUsage records a single usage event

Jump to

Keyboard shortcuts

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