This commit is contained in:
QWQLwToo
2026-07-12 09:12:12 +08:00
parent b22914b5de
commit ef25f8875c
43 changed files with 2797 additions and 709 deletions
@@ -3,6 +3,8 @@ package db
import (
"database/sql"
"errors"
"fmt"
"strings"
)
func (s *Store) UpsertSource(item Source) (Source, error) {
@@ -165,6 +167,65 @@ func (s *Store) RecordSourceCheck(sourceDBID int64, status string, latency int,
return err
}
func (s *Store) SourceHealthHistory(sourceDBIDs []int64, limit int) (map[int64][]map[string]any, error) {
if len(sourceDBIDs) == 0 {
return map[int64][]map[string]any{}, nil
}
if limit <= 0 || limit > 48 {
limit = 16
}
placeholders := make([]string, 0, len(sourceDBIDs))
args := make([]any, 0, len(sourceDBIDs)+1)
for _, id := range sourceDBIDs {
if id <= 0 {
continue
}
placeholders = append(placeholders, "?")
args = append(args, id)
}
if len(placeholders) == 0 {
return map[int64][]map[string]any{}, nil
}
args = append(args, limit*len(placeholders))
rows, err := s.query(fmt.Sprintf(`SELECT source_db_id, status, latency_ms, checked_at
FROM endpoint_health_checks
WHERE source_db_id IN (%s)
ORDER BY checked_at DESC, id DESC LIMIT ?`, strings.Join(placeholders, ",")), args...)
if err != nil {
return nil, err
}
defer rows.Close()
out := map[int64][]map[string]any{}
for rows.Next() {
var sourceDBID int64
var status, checkedAt string
var latency int
if err := rows.Scan(&sourceDBID, &status, &latency, &checkedAt); err != nil {
return nil, err
}
if len(out[sourceDBID]) >= limit {
continue
}
out[sourceDBID] = append(out[sourceDBID], map[string]any{
"status": status,
"latencyMs": latency,
"latency_ms": latency,
"checkedAt": checkedAt,
"checked_at": checkedAt,
})
}
if err := rows.Err(); err != nil {
return nil, err
}
for id, items := range out {
for left, right := 0, len(items)-1; left < right; left, right = left+1, right-1 {
items[left], items[right] = items[right], items[left]
}
out[id] = items
}
return out, nil
}
func (s *Store) RecordSourceCall(call SourceCall) error {
if call.CreatedAt == "" {
call.CreatedAt = Now()