stats

package
v0.0.0-...-5aaefcd Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KB = 1024
	MB = 1024 * KB
	GB = 1024 * MB
)
View Source
const DBFileName = "stats.db"

DBFileName is the name of the SQLite database file used to persist statistics. It lives in the cache directory next to cache_index.db.

Variables

This section is empty.

Functions

func FormatBytes

func FormatBytes(bytes uint64) string

FormatBytes converts bytes to human-readable format with 1 decimal place. - < 1 MB: "X.X KB" - >= 1 MB and < 1 GB: "X.X MB" - >= 1 GB: "X.X GB"

func FormatNumber

func FormatNumber(n uint64) string

FormatNumber formats a number with thousand separators.

func PrintAllStats

func PrintAllStats(allStats *AllStats)

PrintAllStats prints statistics from AllStats struct.

func PrintSummary

func PrintSummary(statsFile string) error

PrintSummary reads stats from file and prints ASCII table.

Types

type AllStats

type AllStats struct {
	Total    Stats             `json:"total"`
	ByDomain map[string]*Stats `json:"by_domain"`
}

AllStats represents statistics for all domains.

func NewAllStats

func NewAllStats() *AllStats

NewAllStats creates a new AllStats instance.

func (*AllStats) GetOrCreateDomainStats

func (a *AllStats) GetOrCreateDomainStats(domain string) *Stats

GetOrCreateDomainStats returns stats for a domain, creating if needed.

type Collector

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

Collector collects and manages statistics. Statistics are kept in memory for fast recording and periodically persisted to a SQLite database via a Store (see store.go), instead of being written into the config file.

func NewCollector

func NewCollector() *Collector

NewCollector creates a new statistics collector.

func (*Collector) AttachStore

func (c *Collector) AttachStore(store *Store, interval time.Duration) error

AttachStore attaches a persistent store and loads any previously saved statistics into memory so counters continue to accumulate across restarts. It also starts a goroutine that periodically flushes statistics to the database. Call Stop to flush and stop the goroutine.

func (*Collector) GetSnapshot

func (c *Collector) GetSnapshot() AllStats

GetSnapshot returns a copy of current statistics.

func (*Collector) RecordCacheHit

func (c *Collector) RecordCacheHit(domain string, size uint64)

RecordCacheHit records a cache hit and bytes saved for a domain.

func (*Collector) RecordCacheMiss

func (c *Collector) RecordCacheMiss(domain string)

RecordCacheMiss records a cache miss for a domain.

func (*Collector) RecordResponse

func (c *Collector) RecordResponse(domain string, statusCode int)

RecordResponse records a response status code for a domain.

func (*Collector) Stop

func (c *Collector) Stop()

Stop stops the periodic writer, flushes statistics to the store and closes the store.

func (*Collector) UpdateCacheSize

func (c *Collector) UpdateCacheSize(domain string, size uint64)

UpdateCacheSize updates the cache size for a domain.

type DomainStatRow

type DomainStatRow struct {
	Domain         string    `json:"domain" gorm:"primaryKey;column:domain"`
	CacheHits      uint64    `json:"cache_hits" gorm:"column:cache_hits"`
	CacheMisses    uint64    `json:"cache_misses" gorm:"column:cache_misses"`
	BytesSaved     uint64    `json:"bytes_saved" gorm:"column:bytes_saved"`
	CacheSizeBytes uint64    `json:"cache_size_bytes" gorm:"column:cache_size_bytes"`
	ErrorResponses uint64    `json:"error_responses" gorm:"column:error_responses"`
	LastUpdated    time.Time `json:"last_updated" gorm:"column:last_updated"`
}

DomainStatRow is the persistence model for per-domain statistics.

func (DomainStatRow) TableName

func (DomainStatRow) TableName() string

TableName overrides the default GORM table name.

type Stats

type Stats struct {
	CacheHits      uint64    `json:"cache_hits"`
	CacheMisses    uint64    `json:"cache_misses"`
	BytesSaved     uint64    `json:"bytes_saved"`
	CacheSizeBytes uint64    `json:"cache_size_bytes"`
	ErrorResponses uint64    `json:"error_responses"`
	LastUpdated    time.Time `json:"last_updated"`
}

Stats represents cache statistics for a single domain or overall.

type Store

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

Store persists statistics in a SQLite database via GORM.

func OpenStore

func OpenStore(dbPath string) (*Store, error)

OpenStore opens (creating if necessary) the statistics database at dbPath.

func (*Store) Close

func (s *Store) Close() error

Close closes the underlying database.

func (*Store) Load

func (s *Store) Load() (*AllStats, error)

Load reads all per-domain statistics from the database and derives totals.

func (*Store) Save

func (s *Store) Save(all *AllStats) error

Save persists a snapshot of all per-domain statistics, replacing any previously stored rows in a single transaction.

Jump to

Keyboard shortcuts

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