analytics

package
v0.0.0-...-a78c924 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitDB

func InitDB(dsn string) (*gorm.DB, error)

InitDB opens a separate SQLite database for analytics data. Uses WAL mode and busy timeout for concurrent access, mirroring the main database configuration in models.InitDB.

Types

type Collector

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

Collector buffers analytics events and batch-writes them to the database. It is safe for concurrent use — Record never blocks the HTTP handler.

func NewCollector

func NewCollector(db *gorm.DB) *Collector

NewCollector creates a Collector and starts its background flush goroutine.

func (*Collector) Record

func (c *Collector) Record(ev Event)

Record enqueues an event for writing. If the internal buffer is full the event is silently dropped — this ensures analytics never slows down request serving.

func (*Collector) Stop

func (c *Collector) Stop()

Stop signals the background goroutine to drain remaining events and exit.

type Event

type Event struct {
	SiteID     string
	Timestamp  time.Time
	Method     string
	Path       string
	Status     int
	BytesSent  int64
	DurationMs int64
	ClientIP   string
	UserAgent  string
	Referer    string
	ServeType  string
}

Event holds all fields needed to record a single request.

type HourlyStat

type HourlyStat struct {
	ID             int64     `gorm:"primaryKey;autoIncrement"`
	SiteID         string    `gorm:"uniqueIndex:idx_hourly_site_bucket,priority:1;size:12"`
	Bucket         time.Time `gorm:"uniqueIndex:idx_hourly_site_bucket,priority:2"` // hour-truncated UTC
	Requests       int64
	UniqueVisitors int64
	BytesSent      int64
	Status2xx      int64
	Status3xx      int64
	Status4xx      int64
	Status5xx      int64
	TopPaths       string `gorm:"type:text"` // JSON array of TopEntry
	TopReferrers   string `gorm:"type:text"` // JSON array of TopEntry
}

HourlyStat stores pre-aggregated hourly rollups for long-term dashboards.

type PeriodFilter

type PeriodFilter struct {
	From   time.Time
	To     time.Time
	Bucket string // "hour" or "day"
}

PeriodFilter defines the time range and bucket granularity for queries.

func ParsePeriod

func ParsePeriod(period string) PeriodFilter

ParsePeriod converts a period string ("24h", "7d", "30d") into a PeriodFilter. Defaults to "24h" for unrecognized values.

type RequestLog

type RequestLog struct {
	ID         int64     `gorm:"primaryKey;autoIncrement"`
	SiteID     string    `gorm:"index:idx_request_log_site_ts,priority:1;size:12"`
	Timestamp  time.Time `gorm:"index:idx_request_log_site_ts,priority:2"`
	Method     string    `gorm:"size:10"`
	Path       string    `gorm:"size:2048"`
	Status     int
	BytesSent  int64
	DurationMs int64
	ClientIP   string `gorm:"size:45"` // IPv6 max length
	UserAgent  string `gorm:"size:512"`
	Referer    string `gorm:"size:2048"`
	ServeType  string `gorm:"size:20"` // static, worker, redirect, rewrite, spa, 404, error
}

RequestLog stores raw per-request data for short-term drill-down. Uses integer auto-increment PK (not nanoid) for write throughput.

type RetentionRunner

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

RetentionRunner periodically deletes expired analytics data. Mirrors the pattern of workeradapter.LogRetentionRunner.

func NewRetentionRunner

func NewRetentionRunner(db *gorm.DB, rawDays, rollDays int) *RetentionRunner

NewRetentionRunner creates and starts a retention runner that prunes request logs older than rawDays and hourly stats older than rollDays.

func (*RetentionRunner) Stop

func (r *RetentionRunner) Stop()

Stop stops the retention runner and waits for it to finish.

type RollupRunner

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

RollupRunner periodically aggregates raw request logs into hourly stats.

func NewRollupRunner

func NewRollupRunner(db *gorm.DB) *RollupRunner

NewRollupRunner creates and starts a rollup runner that aggregates raw request logs into HourlyStat rows. Runs once per hour, aligned to the hour boundary plus a 2-minute grace period for late-arriving events.

func (*RollupRunner) RollupHour

func (r *RollupRunner) RollupHour(bucket time.Time) error

RollupHour aggregates all RequestLog rows within [bucket, bucket+1h) into HourlyStat rows, one per site. Uses upsert so re-running is idempotent.

func (*RollupRunner) Stop

func (r *RollupRunner) Stop()

Stop stops the rollup runner and waits for it to finish.

type SummaryResult

type SummaryResult struct {
	Requests       int64 `json:"requests"`
	UniqueVisitors int64 `json:"unique_visitors"`
	BytesSent      int64 `json:"bytes_sent"`
	Status2xx      int64 `json:"status_2xx"`
	Status3xx      int64 `json:"status_3xx"`
	Status4xx      int64 `json:"status_4xx"`
	Status5xx      int64 `json:"status_5xx"`
}

SummaryResult holds aggregate totals for a site over a time period.

func GetSummary

func GetSummary(db *gorm.DB, siteID string, pf PeriodFilter) (SummaryResult, error)

GetSummary returns aggregate totals from hourly_stats for a site and period.

type TimeseriesPoint

type TimeseriesPoint struct {
	Bucket         time.Time `json:"bucket"`
	Requests       int64     `json:"requests"`
	UniqueVisitors int64     `json:"unique_visitors"`
	BytesSent      int64     `json:"bytes_sent"`
}

TimeseriesPoint is a single data point in a time series.

func GetTimeseries

func GetTimeseries(db *gorm.DB, siteID string, pf PeriodFilter) ([]TimeseriesPoint, error)

GetTimeseries returns per-bucket data points for charting. Uses DATE(bucket) grouping for day buckets, raw bucket for hour buckets.

type TopEntry

type TopEntry struct {
	Value    string `json:"value"`
	Requests int64  `json:"requests"`
}

TopEntry represents a single entry in top-N rankings (paths, referrers).

func GetTopPages

func GetTopPages(db *gorm.DB, siteID string, pf PeriodFilter, limit int) ([]TopEntry, error)

GetTopPages merges top_paths JSON from each HourlyStat in the period, re-aggregates totals in Go, and returns the top N.

func GetTopReferrers

func GetTopReferrers(db *gorm.DB, siteID string, pf PeriodFilter, limit int) ([]TopEntry, error)

GetTopReferrers merges top_referrers JSON from each HourlyStat in the period, re-aggregates totals in Go, and returns the top N.

Jump to

Keyboard shortcuts

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