proxy

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateMockArchives added in v1.5.0

func GenerateMockArchives(archivePath string, monthsCount int) error

GenerateMockArchives generates mock archive data for testing

func GenerateMockArchivesForUser added in v1.5.0

func GenerateMockArchivesForUser(monthsCount int) error

GenerateMockArchivesForUser generates mock archives in the user's archive directory

func GetStatsPath added in v0.5.2

func GetStatsPath() (string, error)

GetStatsPath returns the stats file path

Types

type APIResponse

type APIResponse struct {
	Usage Usage `json:"usage"`
}

APIResponse represents the structure of API responses to extract usage

type ArchiveManager added in v1.5.0

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

ArchiveManager manages monthly data archives

func NewArchiveManager added in v1.5.0

func NewArchiveManager() (*ArchiveManager, error)

NewArchiveManager creates a new archive manager

func (*ArchiveManager) ArchiveCurrentMonth added in v1.5.0

func (am *ArchiveManager) ArchiveCurrentMonth(stats *Stats) error

ArchiveCurrentMonth archives current month's data up to yesterday (T+1 mode) Unlike archiveMonth, this method does NOT remove data from stats

func (*ArchiveManager) CheckAndArchive added in v1.5.0

func (am *ArchiveManager) CheckAndArchive(stats *Stats) error

CheckAndArchive checks if there are complete months to archive and archives them Also archives current month data in T+1 mode (without removing data)

func (*ArchiveManager) GetArchivePath added in v1.5.0

func (am *ArchiveManager) GetArchivePath() string

GetArchivePath returns the archive directory path

func (*ArchiveManager) GetArchiveSummary added in v1.5.0

func (am *ArchiveManager) GetArchiveSummary(month string) (*ArchiveSummary, error)

GetArchiveSummary returns summary statistics for an archived month

func (*ArchiveManager) ListArchives added in v1.5.0

func (am *ArchiveManager) ListArchives() ([]string, error)

ListArchives returns a list of all available archive months

func (*ArchiveManager) LoadArchive added in v1.5.0

func (am *ArchiveManager) LoadArchive(month string) (*MonthlyArchive, error)

LoadArchive loads an archived month's data

type ArchiveSummary added in v1.5.0

type ArchiveSummary struct {
	TotalRequests     int `json:"totalRequests"`
	TotalErrors       int `json:"totalErrors"`
	TotalInputTokens  int `json:"totalInputTokens"`
	TotalOutputTokens int `json:"totalOutputTokens"`
}

ArchiveSummary represents summary statistics for a month

type DailyStats added in v1.4.0

type DailyStats struct {
	Date         string `json:"date"` // Format: "2006-01-02"
	Requests     int    `json:"requests"`
	Errors       int    `json:"errors"`
	InputTokens  int    `json:"inputTokens"`
	OutputTokens int    `json:"outputTokens"`
}

DailyStats represents statistics for a single day

type EndpointStats

type EndpointStats struct {
	Requests     int                    `json:"requests"`     // Computed from DailyHistory
	Errors       int                    `json:"errors"`       // Computed from DailyHistory
	InputTokens  int                    `json:"inputTokens"`  // Computed from DailyHistory
	OutputTokens int                    `json:"outputTokens"` // Computed from DailyHistory
	LastUsed     time.Time              `json:"lastUsed"`
	DailyHistory map[string]*DailyStats `json:"dailyHistory"` // Key: date string (source of truth)
}

EndpointStats represents statistics for a single endpoint

type MonthlyArchive added in v1.5.0

type MonthlyArchive struct {
	Month      string                    `json:"month"`      // Format: "YYYY-MM"
	ArchivedAt time.Time                 `json:"archivedAt"` // When this archive was created
	Summary    ArchiveSummary            `json:"summary"`    // Month summary statistics
	Endpoints  map[string]*EndpointStats `json:"endpoints"`  // Endpoint statistics with daily history
}

MonthlyArchive represents archived statistics for a single month

type Proxy

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

Proxy represents the proxy server

func New

func New(cfg *config.Config) *Proxy

New creates a new Proxy instance

func (*Proxy) GetCurrentEndpointName added in v1.1.1

func (p *Proxy) GetCurrentEndpointName() string

GetCurrentEndpointName returns the current endpoint name (thread-safe)

func (*Proxy) GetStats

func (p *Proxy) GetStats() *Stats

GetStats returns current statistics

func (*Proxy) SetCurrentEndpoint added in v1.1.1

func (p *Proxy) SetCurrentEndpoint(targetName string) error

SetCurrentEndpoint manually switches to a specific endpoint by name Returns error if endpoint not found or not enabled Thread-safe and won't affect ongoing requests

func (*Proxy) Start

func (p *Proxy) Start() error

Start starts the proxy server

func (*Proxy) Stop

func (p *Proxy) Stop() error

Stop stops the proxy server

func (*Proxy) UpdateConfig

func (p *Proxy) UpdateConfig(cfg *config.Config) error

UpdateConfig updates the proxy configuration

type SSEEvent

type SSEEvent struct {
	Event string
	Data  string
}

SSEEvent represents a Server-Sent Event

type Stats

type Stats struct {
	TotalRequests int                       `json:"totalRequests"` // Computed from EndpointStats
	EndpointStats map[string]*EndpointStats `json:"endpointStats"`
	// contains filtered or unexported fields
}

Stats represents overall proxy statistics

func NewStats

func NewStats() *Stats

NewStats creates a new Stats instance

func (*Stats) FlushSave added in v1.5.0

func (s *Stats) FlushSave() error

FlushSave forces an immediate save, canceling any pending debounced save

func (*Stats) GetDailyStats added in v1.4.0

func (s *Stats) GetDailyStats(date string) map[string]*DailyStats

GetDailyStats returns statistics for a specific date Only returns endpoints that have data on the specified date

func (*Stats) GetLastSaveError added in v1.5.0

func (s *Stats) GetLastSaveError() error

GetLastSaveError returns the last save error if any

func (*Stats) GetPeriodStats added in v1.4.0

func (s *Stats) GetPeriodStats(startDate, endDate string) map[string]*DailyStats

GetPeriodStats returns aggregated statistics for a time period Only returns endpoints that have data in the specified period

func (*Stats) GetStats

func (s *Stats) GetStats() (int, map[string]*EndpointStats)

GetStats returns a copy of current statistics (thread-safe) Computes aggregated values from DailyHistory

func (*Stats) Load added in v0.5.2

func (s *Stats) Load() error

Load loads statistics from file

func (*Stats) RecordError

func (s *Stats) RecordError(endpointName string)

RecordError records an error for an endpoint

func (*Stats) RecordRequest

func (s *Stats) RecordRequest(endpointName string)

RecordRequest records a request for an endpoint

func (*Stats) RecordTokens

func (s *Stats) RecordTokens(endpointName string, inputTokens, outputTokens int)

RecordTokens records token usage for an endpoint

func (*Stats) Reset

func (s *Stats) Reset()

Reset resets all statistics

func (*Stats) Save added in v0.5.2

func (s *Stats) Save() error

Save saves statistics to file

func (*Stats) SetStatsPath added in v0.5.2

func (s *Stats) SetStatsPath(path string)

SetStatsPath sets the path for stats persistence

type Usage

type Usage struct {
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
}

Usage represents token usage information from API response

Jump to

Keyboard shortcuts

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