diff --git a/internal/aw/aw.go b/internal/aw/aw.go index e322e16..920ea7f 100644 --- a/internal/aw/aw.go +++ b/internal/aw/aw.go @@ -68,3 +68,57 @@ func hostname() string { } return "keel" } + +type eventJSON struct { + Timestamp time.Time `json:"timestamp"` + Duration float64 `json:"duration"` + Data map[string]any `json:"data"` +} + +// Insert posts a single event to the bucket. The AW events endpoint takes a +// JSON array, so the event is wrapped in a one-element list. +func (c *Client) Insert(ctx context.Context, bucketID string, e Event) error { + body, _ := json.Marshal([]eventJSON{{Timestamp: e.Timestamp, Duration: e.Duration, Data: e.Data}}) + req, err := http.NewRequestWithContext(ctx, http.MethodPost, + c.baseURL+"/api/0/buckets/"+bucketID+"/events", bytes.NewReader(body)) + if err != nil { + return err + } + req.Header.Set("Content-Type", "application/json") + resp, err := c.hc.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return fmt.Errorf("aw: insert into %s: status %d", bucketID, resp.StatusCode) + } + return nil +} + +// Recent returns up to limit events from the bucket, newest first (the AW +// default ordering for the events endpoint). +func (c *Client) Recent(ctx context.Context, bucketID string, limit int) ([]Event, error) { + endpoint := fmt.Sprintf("%s/api/0/buckets/%s/events?limit=%d", c.baseURL, bucketID, limit) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) + if err != nil { + return nil, err + } + resp, err := c.hc.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return nil, fmt.Errorf("aw: read %s: status %d", bucketID, resp.StatusCode) + } + var raw []eventJSON + if err := json.NewDecoder(resp.Body).Decode(&raw); err != nil { + return nil, err + } + out := make([]Event, len(raw)) + for i, e := range raw { + out[i] = Event{Timestamp: e.Timestamp, Duration: e.Duration, Data: e.Data} + } + return out, nil +} diff --git a/internal/aw/aw_test.go b/internal/aw/aw_test.go index a95c7e7..359ef8f 100644 --- a/internal/aw/aw_test.go +++ b/internal/aw/aw_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "testing" + "time" ) func TestEnsureBucketPostsCreate(t *testing.T) { @@ -54,3 +55,54 @@ func TestEnsureBucketErrorsOnServerError(t *testing.T) { t.Fatal("expected error on 500") } } + +func TestInsertPostsEventArray(t *testing.T) { + var gotPath string + var gotEvents []map[string]any + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + gotPath = r.URL.Path + if r.Method != http.MethodPost { + t.Errorf("method = %q, want POST", r.Method) + } + b, _ := io.ReadAll(r.Body) + _ = json.Unmarshal(b, &gotEvents) + w.WriteHeader(http.StatusOK) + })) + defer srv.Close() + + c := New(srv.URL) + ev := Event{Timestamp: time.Unix(1000, 0).UTC(), Data: map[string]any{"_kind": "proposal_made"}} + if err := c.Insert(context.Background(), "keel.events", ev); err != nil { + t.Fatalf("Insert: %v", err) + } + if gotPath != "/api/0/buckets/keel.events/events" { + t.Fatalf("path = %q", gotPath) + } + if len(gotEvents) != 1 || gotEvents[0]["data"].(map[string]any)["_kind"] != "proposal_made" { + t.Fatalf("events = %+v", gotEvents) + } +} + +func TestRecentDecodesNewestFirst(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("method = %q, want GET", r.Method) + } + if got := r.URL.Query().Get("limit"); got != "5" { + t.Errorf("limit = %q, want 5", got) + } + _, _ = io.WriteString(w, `[ + {"id":2,"timestamp":"2026-06-05T10:00:00+00:00","duration":0,"data":{"_kind":"proposal_made"}}, + {"id":1,"timestamp":"2026-06-04T10:00:00+00:00","duration":0,"data":{"_kind":"action_taken"}} + ]`) + })) + defer srv.Close() + + got, err := New(srv.URL).Recent(context.Background(), "keel.events", 5) + if err != nil { + t.Fatalf("Recent: %v", err) + } + if len(got) != 2 || got[0].Data["_kind"] != "proposal_made" { + t.Fatalf("got = %+v", got) + } +}