/
rustwizard
/
pgtrace
Обзор
Документация
Войти
/
rustwizard
/
pgtrace
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
internal/store/store.go
84 строки
2 KB
Rust Wizard
feat(offcpu): aggregate wait types per query and expose in console/agent
08 авг 2026, 09:51
Верифицирован
08 авг 2026, 09:51
73d29f7
Код
Авторство
О чём код?
// Package store keeps recent aggregation windows in memory. package store import ( "encoding/json" "net/http" "strconv" "sync" "time" "github.com/rustwizard/pgtrace/internal/agg" ) // Window is a snapshot of per-syscall and per-wait-type stats for one // aggregation window. type Window struct { Start time.Time End time.Time Syscalls map[uint32]agg.Stats Waits map[string]agg.WaitStat } // Store is a ring buffer of the most recent windows, safe for concurrent use. type Store struct { mu sync.RWMutex max int wins []Window } // New returns a Store keeping at most n windows. func New(n int) *Store { if n < 1 { n = 1 } return &Store{max: n} } // Add appends w, dropping the oldest window when the buffer is full. func (s *Store) Add(w Window) { s.mu.Lock() defer s.mu.Unlock() if len(s.wins) == s.max { copy(s.wins, s.wins[1:]) s.wins[s.max-1] = w return } s.wins = append(s.wins, w) } // Last returns up to n most recent windows, oldest first. func (s *Store) Last(n int) []Window { s.mu.RLock() defer s.mu.RUnlock() if n < 1 || n > len(s.wins) { n = len(s.wins) } out := make([]Window, n) copy(out, s.wins[len(s.wins)-n:]) return out } // HandleWindows serves GET /api/v1/windows?limit=N as JSON. func (s *Store) HandleWindows(w http.ResponseWriter, r *http.Request) { n := 10 if v := r.URL.Query().Get("limit"); v != "" { if parsed, err := strconv.Atoi(v); err == nil { n = parsed } } w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(s.Last(n)); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }