Add per-resource history and guest network groups
This commit is contained in:
56
store.go
56
store.go
@@ -117,6 +117,8 @@ func openStore(path string) (*Store, error) {
|
||||
FOREIGN KEY(service_id) REFERENCES services(id) ON DELETE CASCADE
|
||||
)`,
|
||||
`CREATE INDEX IF NOT EXISTS service_checks_service_ts_idx ON service_checks(service_id,ts)`,
|
||||
`CREATE TABLE IF NOT EXISTS entity_history (kind TEXT NOT NULL,name TEXT NOT NULL,ts INTEGER NOT NULL,value1 REAL,value2 REAL,PRIMARY KEY(kind,name,ts))`,
|
||||
`CREATE INDEX IF NOT EXISTS entity_history_kind_ts_idx ON entity_history(kind,ts)`,
|
||||
}
|
||||
for _, statement := range statements {
|
||||
if _, err := db.Exec(statement); err != nil {
|
||||
@@ -131,6 +133,60 @@ func openStore(path string) (*Store, error) {
|
||||
return &Store{db: db}, nil
|
||||
}
|
||||
|
||||
type EntityHistoryPoint struct {
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
Value1 *float64 `json:"value1,omitempty"`
|
||||
Value2 *float64 `json:"value2,omitempty"`
|
||||
}
|
||||
type EntityHistorySeries struct {
|
||||
Name string `json:"name"`
|
||||
Points []EntityHistoryPoint `json:"points"`
|
||||
}
|
||||
|
||||
func (s *Store) AddEntityHistory(kind, name string, ts int64, v1, v2 *float64) {
|
||||
_, _ = s.db.Exec(`INSERT OR REPLACE INTO entity_history(kind,name,ts,value1,value2) VALUES(?,?,?,?,?)`, kind, name, ts, v1, v2)
|
||||
}
|
||||
func (s *Store) EntityHistory(kind string, hours int) ([]EntityHistorySeries, error) {
|
||||
if hours < 1 {
|
||||
hours = 24
|
||||
}
|
||||
if hours > 168 {
|
||||
hours = 168
|
||||
}
|
||||
rows, err := s.db.Query(`SELECT name,ts,value1,value2 FROM entity_history WHERE kind=? AND ts>=? ORDER BY name,ts`, kind, time.Now().Add(-time.Duration(hours)*time.Hour).Unix())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []EntityHistorySeries
|
||||
index := map[string]int{}
|
||||
for rows.Next() {
|
||||
var name string
|
||||
var p EntityHistoryPoint
|
||||
var v1, v2 sql.NullFloat64
|
||||
if err = rows.Scan(&name, &p.Timestamp, &v1, &v2); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if v1.Valid {
|
||||
p.Value1 = &v1.Float64
|
||||
}
|
||||
if v2.Valid {
|
||||
p.Value2 = &v2.Float64
|
||||
}
|
||||
i, ok := index[name]
|
||||
if !ok {
|
||||
i = len(out)
|
||||
index[name] = i
|
||||
out = append(out, EntityHistorySeries{Name: name})
|
||||
}
|
||||
out[i].Points = append(out[i].Points, p)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
func (s *Store) PruneEntityHistory() {
|
||||
_, _ = s.db.Exec(`DELETE FROM entity_history WHERE ts<?`, time.Now().Add(-historyRetention).Unix())
|
||||
}
|
||||
|
||||
func (s *Store) Close() error { return s.db.Close() }
|
||||
|
||||
func (s *Store) Thresholds() AlertThresholds {
|
||||
|
||||
Reference in New Issue
Block a user