Changeset 0.22.0.1 (#94)
This commit is contained in:
125
server/store/sqlite/health.go
Normal file
125
server/store/sqlite/health.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/models"
|
||||
"git.gobha.me/xcaliber/chat-switchboard/store"
|
||||
)
|
||||
|
||||
type HealthStore struct{}
|
||||
|
||||
func NewHealthStore() *HealthStore { return &HealthStore{} }
|
||||
|
||||
func (s *HealthStore) UpsertWindow(ctx context.Context, w *models.ProviderHealthWindow) error {
|
||||
id := store.NewID()
|
||||
windowStr := w.WindowStart.Format(timeFmt)
|
||||
_, err := DB.ExecContext(ctx, `
|
||||
INSERT INTO provider_health (id, provider_config_id, window_start,
|
||||
request_count, error_count, timeout_count,
|
||||
total_latency_ms, max_latency_ms, last_error, last_error_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, datetime('now'))
|
||||
ON CONFLICT (provider_config_id, window_start) DO UPDATE SET
|
||||
request_count = provider_health.request_count + excluded.request_count,
|
||||
error_count = provider_health.error_count + excluded.error_count,
|
||||
timeout_count = provider_health.timeout_count + excluded.timeout_count,
|
||||
total_latency_ms = provider_health.total_latency_ms + excluded.total_latency_ms,
|
||||
max_latency_ms = MAX(provider_health.max_latency_ms, excluded.max_latency_ms),
|
||||
last_error = COALESCE(excluded.last_error, provider_health.last_error),
|
||||
last_error_at = COALESCE(excluded.last_error_at, provider_health.last_error_at),
|
||||
updated_at = datetime('now')
|
||||
`, id, w.ProviderConfigID, windowStr,
|
||||
w.RequestCount, w.ErrorCount, w.TimeoutCount,
|
||||
w.TotalLatencyMs, w.MaxLatencyMs, w.LastError, w.LastErrorAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *HealthStore) GetCurrentWindow(ctx context.Context, providerConfigID string) (*models.ProviderHealthWindow, error) {
|
||||
windowStr := time.Now().UTC().Truncate(time.Hour).Format(timeFmt)
|
||||
var w models.ProviderHealthWindow
|
||||
var windowStartStr string
|
||||
err := DB.QueryRowContext(ctx, `
|
||||
SELECT id, provider_config_id, window_start,
|
||||
request_count, error_count, timeout_count,
|
||||
total_latency_ms, max_latency_ms, last_error, last_error_at
|
||||
FROM provider_health
|
||||
WHERE provider_config_id = ? AND window_start = ?
|
||||
`, providerConfigID, windowStr).Scan(
|
||||
&w.ID, &w.ProviderConfigID, &windowStartStr,
|
||||
&w.RequestCount, &w.ErrorCount, &w.TimeoutCount,
|
||||
&w.TotalLatencyMs, &w.MaxLatencyMs, &w.LastError, &w.LastErrorAt,
|
||||
)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
w.WindowStart, _ = time.Parse(timeFmt, windowStartStr)
|
||||
return &w, nil
|
||||
}
|
||||
|
||||
func (s *HealthStore) ListWindows(ctx context.Context, providerConfigID string, hours int) ([]models.ProviderHealthWindow, error) {
|
||||
rows, err := DB.QueryContext(ctx, `
|
||||
SELECT id, provider_config_id, window_start,
|
||||
request_count, error_count, timeout_count,
|
||||
total_latency_ms, max_latency_ms, last_error, last_error_at
|
||||
FROM provider_health
|
||||
WHERE provider_config_id = ?
|
||||
ORDER BY window_start DESC
|
||||
LIMIT ?
|
||||
`, providerConfigID, hours)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return scanHealthRows(rows)
|
||||
}
|
||||
|
||||
func (s *HealthStore) ListAllCurrent(ctx context.Context) ([]models.ProviderHealthWindow, error) {
|
||||
windowStr := time.Now().UTC().Truncate(time.Hour).Format(timeFmt)
|
||||
rows, err := DB.QueryContext(ctx, `
|
||||
SELECT id, provider_config_id, window_start,
|
||||
request_count, error_count, timeout_count,
|
||||
total_latency_ms, max_latency_ms, last_error, last_error_at
|
||||
FROM provider_health
|
||||
WHERE window_start = ?
|
||||
ORDER BY provider_config_id
|
||||
`, windowStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return scanHealthRows(rows)
|
||||
}
|
||||
|
||||
func (s *HealthStore) Prune(ctx context.Context, before time.Time) (int64, error) {
|
||||
result, err := DB.ExecContext(ctx, `
|
||||
DELETE FROM provider_health WHERE window_start < ?
|
||||
`, before.Format(timeFmt))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected()
|
||||
}
|
||||
|
||||
func scanHealthRows(rows *sql.Rows) ([]models.ProviderHealthWindow, error) {
|
||||
var result []models.ProviderHealthWindow
|
||||
for rows.Next() {
|
||||
var w models.ProviderHealthWindow
|
||||
var windowStartStr string
|
||||
if err := rows.Scan(
|
||||
&w.ID, &w.ProviderConfigID, &windowStartStr,
|
||||
&w.RequestCount, &w.ErrorCount, &w.TimeoutCount,
|
||||
&w.TotalLatencyMs, &w.MaxLatencyMs, &w.LastError, &w.LastErrorAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
w.WindowStart, _ = time.Parse(timeFmt, windowStartStr)
|
||||
result = append(result, w)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user