ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m29s
ci / test (pull_request) Successful in 12m55s
ci / build (pull_request) Successful in 16m45s
ci / image (pull_request) Successful in 5m41s
Checkers and status page servers keep one WebSocket open to the API at /agent/v1/stream and send their requests over it. The API answers them through the agent API as if they had come over HTTP, each authenticated again, so the protocol stays the same and a revoked token ends the stream. While the stream is down agents fall back to plain HTTP, and an API without it is asked again every five minutes. Over the stream the API tells status page servers which of their pages changed, so they fetch them at once instead of on their next refresh: when a region of a monitor changes between up and down, an incident opens or resolves, or anything of the organization is edited. Changes are announced through Redis to every API instance and sent at most every five seconds per stream. A page that changes while it is fetched is fetched once more. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
75 lines
2.0 KiB
Go
75 lines
2.0 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"slices"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
|
)
|
|
|
|
// regionKey identifies a monitor in one region.
|
|
type regionKey struct {
|
|
monitor uuid.UUID
|
|
region string
|
|
}
|
|
|
|
// latestRow is a result as UpsertLatestResults reads it.
|
|
type latestRow struct {
|
|
MonitorID uuid.UUID `json:"monitor_id"`
|
|
Region string `json:"region"`
|
|
OrgID uuid.UUID `json:"org_id"`
|
|
Time time.Time `json:"time"`
|
|
OK bool `json:"ok"`
|
|
LatencyMS *int32 `json:"latency_ms"`
|
|
StatusCode *int32 `json:"status_code"`
|
|
Error *string `json:"error"`
|
|
}
|
|
|
|
// insertResults stores results and keeps the newest per monitor and
|
|
// region in latest_results, which the console reads instead of a day of
|
|
// results. It returns the organizations whose status changed somewhere,
|
|
// for pagesChanged once the transaction is committed.
|
|
func insertResults(ctx context.Context, q *store.Queries, rows []store.InsertCheckResultsParams) ([]uuid.UUID, error) {
|
|
if _, err := q.InsertCheckResults(ctx, rows); err != nil {
|
|
return nil, err
|
|
}
|
|
latest := make([]latestRow, len(rows))
|
|
for i, r := range rows {
|
|
latest[i] = latestRow{
|
|
MonitorID: r.MonitorID, Region: r.Region, OrgID: r.OrgID, Time: r.Time, OK: r.Ok,
|
|
LatencyMS: r.LatencyMs, StatusCode: r.StatusCode, Error: r.Error,
|
|
}
|
|
}
|
|
b, err := json.Marshal(latest)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ids := make([]uuid.UUID, len(rows))
|
|
for i, r := range rows {
|
|
ids[i] = r.MonitorID
|
|
}
|
|
before, err := q.LatestOK(ctx, ids)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
was := make(map[regionKey]bool, len(before))
|
|
for _, r := range before {
|
|
was[regionKey{r.MonitorID, r.Region}] = r.Ok
|
|
}
|
|
written, err := q.UpsertLatestResults(ctx, b)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var changed []uuid.UUID
|
|
for _, w := range written {
|
|
if ok, seen := was[regionKey{w.MonitorID, w.Region}]; (!seen || ok != w.Ok) && !slices.Contains(changed, w.OrgID) {
|
|
changed = append(changed, w.OrgID)
|
|
}
|
|
}
|
|
return changed, nil
|
|
}
|