storage

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProbeModeICMP  = "MODE_ICMP"
	ProbeModeHTTP  = "MODE_HTTP"
	ProbeModeSSH   = "MODE_SSH"
	ProbeModeIPERF = "MODE_IPERF"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

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

func NewDB

func NewDB(dbPath string) (*DB, error)

NewDB initializes the SQLite database

func (*DB) CleanOldRecords added in v1.4.0

func (d *DB) CleanOldRecords(days int) (int64, error)

CleanOldRecords deletes records older than the specified number of days

func (*DB) ClearTargetError added in v1.2.0

func (d *DB) ClearTargetError(address string) error

ClearTargetError clears the error fields for a target (on successful probe)

func (*DB) Close

func (d *DB) Close() error

Close closes the underlying db connection (optional, GORM manages pool)

func (*DB) CreateTarget added in v1.3.2

func (d *DB) CreateTarget(t *Target) error

CreateTarget inserts a new target. Returns error if address already exists.

func (*DB) DeleteTarget

func (d *DB) DeleteTarget(id uint) error

func (*DB) GetDatabaseStats added in v1.4.0

func (d *DB) GetDatabaseStats(dbPath string, retentionDays int) (*DatabaseStats, error)

GetDatabaseStats returns statistics about the database

func (*DB) GetFirstUser added in v1.3.2

func (d *DB) GetFirstUser() (*User, error)

GetFirstUser returns the first user in the database (single-user system)

func (*DB) GetHistory

func (d *DB) GetHistory(target string, start, end time.Time) ([]MonitorRecord, error)

GetHistory fetches records for a specific target within a time range. Optimization: We exclude TraceJson to reduce I/O for general charts.

func (*DB) GetLatestRecord

func (d *DB) GetLatestRecord(target string) (*MonitorRecord, error)

GetLatestRecord fetches the most recent record for a target

func (*DB) GetLatestTrace

func (d *DB) GetLatestTrace(target string) (*MonitorRecord, error)

GetLatestTrace fetches the most recent record that includes traceroute data

func (*DB) GetRecordDetail

func (d *DB) GetRecordDetail(id uint) (*MonitorRecord, error)

GetRecordDetail fetches the full record including TraceJson by ID

func (*DB) GetTargetByID added in v1.3.2

func (d *DB) GetTargetByID(id uint) (*Target, error)

GetTargetByID retrieves a target by its ID

func (*DB) GetTargets

func (d *DB) GetTargets(onlyEnabled bool) ([]Target, error)

func (*DB) GetUser

func (d *DB) GetUser(username string) (*User, error)

func (*DB) HasAnyUser

func (d *DB) HasAnyUser() bool

func (*DB) PruneOldData

func (d *DB) PruneOldData(retentionDays int) error

PruneOldData deletes records older than the specified retention days

func (*DB) SaveRecord

func (d *DB) SaveRecord(r *MonitorRecord) error

SaveRecord persists a monitoring record

func (*DB) SaveTarget

func (d *DB) SaveTarget(t *Target) error

SaveTarget creates or updates a target based on whether ID is set. WARNING: For updates, prefer UpdateTarget to avoid GORM Save() pitfalls.

func (*DB) SaveUser

func (d *DB) SaveUser(u *User) error

func (*DB) UpdateTarget added in v1.3.2

func (d *DB) UpdateTarget(t *Target) error

UpdateTarget updates an existing target by ID. Only updates non-zero fields to avoid overwriting with defaults.

func (*DB) UpdateTargetError added in v1.2.0

func (d *DB) UpdateTargetError(address string, errMsg string) error

UpdateTargetError updates the last_error and last_error_at fields for a target

func (*DB) UpdateUserPassword added in v1.3.2

func (d *DB) UpdateUserPassword(userID uint, hashedPassword string) error

UpdateUserPassword updates the password for a specific user ID This avoids GORM's Save() which can INSERT if ID is missing

func (*DB) VacuumDatabase added in v1.4.0

func (d *DB) VacuumDatabase() error

VacuumDatabase runs VACUUM to reclaim space

type DatabaseStats added in v1.4.0

type DatabaseStats struct {
	SizeBytes     int64  `json:"size_bytes"`
	SizeHuman     string `json:"size_human"`
	RecordCount   int64  `json:"record_count"`
	TargetCount   int64  `json:"target_count"`
	OldestRecord  string `json:"oldest_record,omitempty"`
	NewestRecord  string `json:"newest_record,omitempty"`
	RetentionDays int    `json:"retention_days"`
}

DatabaseStats returns database statistics

type MonitorRecord

type MonitorRecord struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	CreatedAt time.Time `gorm:"index;not null" json:"created_at"` // Time-series index
	Target    string    `gorm:"index;type:varchar(128);not null" json:"target"`

	// Ping Metrics (Always present)
	LatencyMs  float64 `gorm:"not null" json:"latency_ms"`  // Average RTT in milliseconds
	PacketLoss float64 `gorm:"not null" json:"packet_loss"` // Loss Percentage (0.0 - 100.0)

	// Traceroute Data (JSON Blob)
	TraceJson []byte `gorm:"type:text" json:"trace_json,omitempty"`

	// Speed Test Metrics
	SpeedUp   float64 `gorm:"default:0" json:"speed_up"`   // Mbps
	SpeedDown float64 `gorm:"default:0" json:"speed_down"` // Mbps
}

MonitorRecord represents a single monitoring data point (snapshot)

type Target

type Target struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	CreatedAt time.Time `gorm:"not null" json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
	Name      string    `gorm:"type:varchar(64);not null" json:"name"`
	Address   string    `gorm:"type:varchar(128);uniqueIndex;not null" json:"address"` // IP or Domain
	Desc      string    `gorm:"type:text" json:"desc"`
	Enabled   bool      `gorm:"default:true" json:"enabled"`

	// --- Probing Configuration (Phase 13) ---
	// ProbeMode: ICMP, SSH, HTTP, IPERF3
	ProbeType string `gorm:"column:probe_type;type:varchar(20);default:'MODE_ICMP'" json:"probe_type"`

	// ProbeConfig (JSON stored as text for flexibility)
	// Includes URL for HTTP, Port for Iperf, Credentials for SSH
	ProbeConfig string `gorm:"column:probe_config;type:text" json:"probe_config"`

	// --- Error Tracking (Phase Polish) ---
	// LastError stores the most recent probe error message
	LastError   string     `gorm:"column:last_error;type:text" json:"last_error"`
	LastErrorAt *time.Time `gorm:"column:last_error_at" json:"last_error_at"`
}

Target represents a monitoring destination

type User

type User struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	CreatedAt time.Time `gorm:"not null" json:"created_at"`
	Username  string    `gorm:"type:varchar(64);uniqueIndex;not null" json:"username"`
	Password  string    `gorm:"type:varchar(128);not null" json:"-"` // Hashed
}

User represents a system administrator

Jump to

Keyboard shortcuts

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