Every view listing runs or approvals polled on a timer: the home page made four requests every five seconds per open tab, two of them the same run list, and the needs-attention card asked for 200 runs to render five. A run or an approval request moving is now published on the hub and framed on the dashboard stream, so those views re-read on the change. A reconnect still re-reads everything, and the hub drops a reader that falls behind, which ends its stream and reconnects it.
100 lines
3.2 KiB
Go
100 lines
3.2 KiB
Go
package dashboard
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/httpapi"
|
|
"github.com/samcharles93/archie-core/internal/platform/hub"
|
|
chatpkg "github.com/samcharles93/archie-core/modules/chat"
|
|
"github.com/samcharles93/archie-core/modules/governance"
|
|
)
|
|
|
|
// keepalive is how often an idle stream writes a comment, so proxies keep
|
|
// the connection open and the browser notices one that died.
|
|
const keepalive = 25 * time.Second
|
|
|
|
// workFrame tells the dashboard that a run or an approval request moved, so
|
|
// every view listing either re-reads it.
|
|
const workFrame = `data: {"topic":"work","data":{}}` + "\n\n"
|
|
|
|
// chatFrame is the frame the dashboard re-reads the open conversation on.
|
|
const chatFrame = `data: {"topic":"chat","data":{}}` + "\n\n"
|
|
|
|
// Register adds the dashboard's live stream: the one EventSource the SPA
|
|
// keeps open, framing each update as `data: {"topic":…,"data":…}`. A run or
|
|
// an approval request moving is what most views re-read on, and a settled
|
|
// chat action is what the open conversation re-reads on. Keepalives tell the
|
|
// connection indicator the server is up, and a reconnect makes every mounted
|
|
// page re-read.
|
|
func Register(api huma.API, h *hub.Hub) {
|
|
httpapi.Register(api, httpapi.Authorized(governance.ActionRead, governance.KindDashboard), huma.Operation{
|
|
OperationID: "stream-dashboard", Method: http.MethodGet, Path: "/api/stream",
|
|
Summary: "Stream live dashboard updates", Tags: []string{"Dashboard"},
|
|
Responses: map[string]*huma.Response{"200": {
|
|
Description: "Server-sent events: one data frame of {\"topic\", \"data\"} per update, and a keepalive comment while idle",
|
|
Content: map[string]*huma.MediaType{"text/event-stream": {Schema: &huma.Schema{Type: huma.TypeString}}},
|
|
}},
|
|
}, func(ctx context.Context, _ *struct{}) (*huma.StreamResponse, error) {
|
|
// A conversation is its owner's; nobody else hears that it changed.
|
|
var owner string
|
|
if p, ok := governance.PrincipalFromContext(ctx); ok {
|
|
owner = string(p.IdentityID)
|
|
}
|
|
return &huma.StreamResponse{Body: func(hc huma.Context) {
|
|
work, stopWork := h.Subscribe(hub.WorkTopic)
|
|
defer stopWork()
|
|
var chat <-chan hub.Note
|
|
if owner != "" {
|
|
var stopChat func()
|
|
chat, stopChat = h.Subscribe(chatpkg.OwnerTopic(owner))
|
|
defer stopChat()
|
|
}
|
|
serve(hc, work, chat)
|
|
}}, nil
|
|
})
|
|
}
|
|
|
|
func serve(hc huma.Context, work, chat <-chan hub.Note) {
|
|
w := hc.BodyWriter()
|
|
flusher, ok := w.(http.Flusher)
|
|
if !ok {
|
|
hc.SetStatus(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
hc.SetHeader("Content-Type", "text/event-stream")
|
|
hc.SetHeader("Cache-Control", "no-store")
|
|
hc.SetStatus(http.StatusOK)
|
|
flusher.Flush()
|
|
tick := time.NewTicker(keepalive)
|
|
defer tick.Stop()
|
|
for {
|
|
frame := ": keepalive\n\n"
|
|
select {
|
|
case <-hc.Context().Done():
|
|
return
|
|
case <-tick.C:
|
|
case _, ok := <-work:
|
|
if !ok {
|
|
// The hub dropped a reader that fell behind; it reconnects
|
|
// and re-reads.
|
|
return
|
|
}
|
|
frame = workFrame
|
|
case _, ok := <-chat:
|
|
if !ok {
|
|
return
|
|
}
|
|
frame = chatFrame
|
|
}
|
|
if _, err := fmt.Fprint(w, frame); err != nil {
|
|
return
|
|
}
|
|
flusher.Flush()
|
|
}
|
|
}
|