Files
sam 61f576a25c feat(events): capture, event types, mappings and trigger approval on the binary (#2062, #2098)
Event types, field mappings and the stored-delivery inspector move into
modules/events: copied from internal/domain/eventtype and
internal/domain/mapping, which the old services still import. Each stored
event records how its delivery was verified; a source that checks nothing
refuses deliveries until approved; triggers gain approve, pause, resume,
event-type and mapping binding, dispatch history and delete. The captures,
mappings and bindings sections are reported as served.
2026-11-22 11:39:46 +11:00

96 lines
2.8 KiB
Go

package events
import (
"context"
"encoding/json/jsontext"
json "encoding/json/v2"
"fmt"
"time"
"github.com/google/uuid"
"github.com/samcharles93/archie-core/modules/events/internal/store"
)
// Group is a set of unmatched events that share a key, with the trigger it
// proposes.
type Group struct {
ID uuid.UUID
Source string
EventType string
Filter map[string]string
Count int64
FirstSeen time.Time
LastSeen time.Time
}
// Group returns the group id names, whether or not a trigger now covers it.
func (m *Module) Group(ctx context.Context, id uuid.UUID) (Group, bool, error) {
g, ok, err := m.store.GroupByID(ctx, m.orgID, id)
if err != nil || !ok {
return Group{}, false, err
}
var filter map[string]string
if err := json.Unmarshal(g.Filter, &filter); err != nil {
return Group{}, false, fmt.Errorf("group %s filter: %w", id, err)
}
return Group{
ID: g.ID, Source: g.Source, EventType: g.EventType, Filter: filter,
Count: g.Count, FirstSeen: g.FirstSeen, LastSeen: g.LastSeen,
}, true, nil
}
// Stored is an event as a run receives it: its id and the run input a trigger
// would have started a run with.
type Stored struct {
ID uuid.UUID
ReceivedAt time.Time
Input jsontext.Value
}
// GroupEvents returns up to limit stored events of a group, newest first.
func (m *Module) GroupEvents(ctx context.Context, id uuid.UUID, limit int) ([]Stored, error) {
rows, err := m.store.GroupEvents(ctx, m.orgID, id, limit)
if err != nil {
return nil, err
}
out := make([]Stored, 0, len(rows))
for _, e := range rows {
out = append(out, Stored{ID: e.ID, ReceivedAt: e.ReceivedAt, Input: runInput(e)})
}
return out, nil
}
// Replayed records that a dry run of a draft trigger replayed an event, so the
// run's trail names that event as its origin.
func (m *Module) Replayed(ctx context.Context, eventID uuid.UUID, trigger string, runID uuid.UUID) error {
return m.store.RecordReplay(ctx, eventID, "draft:"+trigger, runID)
}
// TriggerSpec is an event trigger to save.
type TriggerSpec struct {
Name string
Source string
EventType string
Filter map[string]string
Workflow string
}
// SaveTrigger creates or replaces an event trigger.
func (m *Module) SaveTrigger(ctx context.Context, t TriggerSpec) error {
if !validName.MatchString(t.Name) || t.Source == "" || t.EventType == "" || t.Workflow == "" {
return fmt.Errorf("a trigger needs a lowercase name, a source, an event type and a workflow")
}
if err := m.runs.CheckTrigger(ctx, m.orgID, t.Workflow); err != nil {
return err
}
filter, err := json.Marshal(t.Filter)
if err != nil {
return fmt.Errorf("encode filter: %w", err)
}
return m.store.UpsertTrigger(ctx, store.Trigger{
OrgID: m.orgID, Name: t.Name, Source: t.Source, EventType: t.EventType,
Filter: filter, Workflow: t.Workflow, Enabled: true, Approved: true,
})
}