Files
adamfandClaude Opus 5.5 f4ce04875c
Test and publish / test (push) Successful in 2m38s
Test and publish / publish (push) Failing after 2m30s
Add activity tracker server, Hyprland agent and Gitea image publishing
The Go API on PostgreSQL ingests desktop, browser, presence, manual and
Health Auto Export events and serves activities and daily goal summaries.
The Hyprland agent records window focus, idle and lock intervals.

The CI workflow publishes adam-french.co.uk/adamf/activity-tracker to the
Gitea registry and rolls the web_server stack onto it. The image probes
itself through a new `tracker healthcheck` command, because distroless
has no wget.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-14 20:38:12 +01:00

95 lines
2.3 KiB
Go

package hypr
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"strings"
"time"
"ccp/internal/ingest"
)
const uploadBatch = 500
// Uploader sends buffered events to the tracker server.
type Uploader struct {
URL string // server base URL, e.g. https://tracker.example.com
Key string
Buf *Buffer
Client *http.Client
Log *slog.Logger
}
// Flush uploads everything in the buffer, oldest first.
func (u *Uploader) Flush(ctx context.Context) error {
for {
batch := u.Buf.Peek(uploadBatch)
if len(batch) == 0 {
return nil
}
status, err := u.post(ctx, batch)
switch {
case err != nil:
return err
case status == http.StatusBadRequest || status == http.StatusUnprocessableEntity:
// The server will never accept this batch; drop it so it
// doesn't block everything behind it.
u.Log.Error("server rejected batch; dropping it", "status", status, "events", len(batch))
case status != http.StatusOK:
return fmt.Errorf("upload: server returned %d", status)
}
if err := u.Buf.Ack(len(batch)); err != nil {
return err
}
}
}
func (u *Uploader) post(ctx context.Context, events []ingest.Event) (int, error) {
body, err := json.Marshal(ingest.Batch{Events: events})
if err != nil {
return 0, err
}
url := strings.TrimRight(u.URL, "/") + "/api/v1/desktop/events"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
if err != nil {
return 0, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+u.Key)
resp, err := u.Client.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
msg, _ := io.ReadAll(io.LimitReader(resp.Body, 512))
u.Log.Warn("upload failed", "status", resp.StatusCode, "body", strings.TrimSpace(string(msg)))
}
return resp.StatusCode, nil
}
// Run flushes every interval until ctx is cancelled, backing off up to
// 10 minutes while the server is unreachable.
func (u *Uploader) Run(ctx context.Context, interval time.Duration) {
wait := interval
for {
select {
case <-ctx.Done():
return
case <-time.After(wait):
}
if err := u.Flush(ctx); err != nil {
wait = min(wait*2, 10*time.Minute)
u.Log.Warn("upload failed; will retry", "err", err, "pending", u.Buf.Len(), "retry_in", wait)
continue
}
wait = interval
}
}