feat(metrics): persist history in sqlite and add AMD memory validate tests

This commit is contained in:
2026-03-29 12:28:06 +03:00
parent 98f0cf0d52
commit e15bcc91c5
10 changed files with 539 additions and 29 deletions

View File

@@ -3,7 +3,6 @@ package webui
import (
"database/sql"
"encoding/csv"
"fmt"
"io"
"strconv"
"time"
@@ -13,7 +12,6 @@ import (
)
const metricsDBPath = "/appdata/bee/metrics.db"
const metricsKeepDuration = 24 * time.Hour
// MetricsDB persists live metric samples to SQLite.
type MetricsDB struct {
@@ -116,11 +114,18 @@ func (m *MetricsDB) Write(s platform.LiveMetricSample) error {
}
// LoadRecent returns up to n samples in chronological order (oldest first).
// It reconstructs LiveMetricSample from the normalized tables.
func (m *MetricsDB) LoadRecent(n int) ([]platform.LiveMetricSample, error) {
rows, err := m.db.Query(
`SELECT ts,cpu_load_pct,mem_load_pct,power_w FROM sys_metrics ORDER BY ts DESC LIMIT ?`, n,
)
return m.loadSamples(`SELECT ts,cpu_load_pct,mem_load_pct,power_w FROM sys_metrics ORDER BY ts DESC LIMIT ?`, n)
}
// LoadAll returns all persisted samples in chronological order (oldest first).
func (m *MetricsDB) LoadAll() ([]platform.LiveMetricSample, error) {
return m.loadSamples(`SELECT ts,cpu_load_pct,mem_load_pct,power_w FROM sys_metrics ORDER BY ts`, nil)
}
// loadSamples reconstructs LiveMetricSample rows from the normalized tables.
func (m *MetricsDB) loadSamples(query string, args ...any) ([]platform.LiveMetricSample, error) {
rows, err := m.db.Query(query, args...)
if err != nil {
return nil, err
}
@@ -257,14 +262,6 @@ func (m *MetricsDB) LoadRecent(n int) ([]platform.LiveMetricSample, error) {
return samples, nil
}
// Prune deletes samples older than keepDuration.
func (m *MetricsDB) Prune(keepDuration time.Duration) {
cutoff := time.Now().Add(-keepDuration).Unix()
for _, table := range []string{"sys_metrics", "gpu_metrics", "fan_metrics", "temp_metrics"} {
_, _ = m.db.Exec(fmt.Sprintf("DELETE FROM %s WHERE ts < ?", table), cutoff)
}
}
// ExportCSV writes all sys+gpu data as CSV to w.
func (m *MetricsDB) ExportCSV(w io.Writer) error {
rows, err := m.db.Query(`