Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
69 lines
2.3 KiB
Go
69 lines
2.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"pageswiki/internal/seheweit"
|
|
)
|
|
|
|
const ResourceEventID = seheweit.ResourceEventID
|
|
const ResourceEventType = seheweit.ResourceEventType
|
|
|
|
func (s *Store) EnsureResourceHistorySchema() error {
|
|
return s.ensureSchemaComponent("resource-history")
|
|
}
|
|
|
|
func (s *Store) EnsureBuiltinResourceEvent() error {
|
|
now := time.Now().UnixMilli()
|
|
_, err := s.DB.Exec(`INSERT OR IGNORE INTO event_definitions
|
|
(uuid,name,event_type,event_key,payload,schedule_kind,interval_seconds,next_run_at_ms,enabled,priority,max_attempts,created_at_ms,updated_at_ms)
|
|
VALUES(?,?,?,?,?,'interval',60,?,1,100,1,?,?)`, ResourceEventID, "Resource monitoring", ResourceEventType, "local", "{}", now, now, now)
|
|
return err
|
|
}
|
|
|
|
func (s *Store) SaveResourceSample(job string, at int64, payload []byte) error {
|
|
return s.SaveResourceSampleContext(context.Background(), job, at, payload)
|
|
}
|
|
|
|
func (s *Store) SaveResourceSampleContext(ctx context.Context, job string, at int64, payload []byte) error {
|
|
if s.ReadOnly {
|
|
return ErrReadOnly
|
|
}
|
|
tx, err := s.DB.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
if _, err = tx.ExecContext(ctx, `INSERT OR IGNORE INTO resource_samples(job_uuid,timestamp_ms,payload) VALUES(?,?,?)`, job, at, string(payload)); err != nil {
|
|
return err
|
|
}
|
|
if _, err = tx.ExecContext(ctx, `DELETE FROM resource_samples WHERE timestamp_ms < ?`, seheweit.RetentionCutoff(at)); err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
// Return the last inserted sample in each time bucket, not averages or peaks.
|
|
// At most 361 points and an indexed seven-day scan; no request-side collection.
|
|
func (s *Store) ResourceHistory(ctx context.Context, hours int, now time.Time) ([]json.RawMessage, error) {
|
|
window := seheweit.Window(hours, now)
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT payload FROM resource_samples WHERE id IN
|
|
(SELECT MAX(id) FROM resource_samples WHERE timestamp_ms>=? AND timestamp_ms<=? GROUP BY timestamp_ms / ?)
|
|
ORDER BY timestamp_ms LIMIT ?`, window.FromMS, window.ToMS, window.BucketMS, seheweit.MaxHistoryPoints)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []json.RawMessage{}
|
|
for rows.Next() {
|
|
var raw string
|
|
if err := rows.Scan(&raw); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, json.RawMessage(raw))
|
|
}
|
|
return out, rows.Err()
|
|
}
|