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 // SourceID is the source whose events the group collects, and Source its // label. SourceID 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 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) } src, err := m.store.Source(ctx, m.orgID, g.SourceID) if err != nil { return Group{}, false, fmt.Errorf("group %s source: %w", id, err) } return Group{ ID: g.ID, SourceID: g.SourceID, Source: src.Label, 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 SourceID uuid.UUID EventType string Filter map[string]string // WorkflowID is the workflow the trigger starts. WorkflowID uuid.UUID } // SaveTrigger creates or replaces an event trigger. func (m *Module) SaveTrigger(ctx context.Context, t TriggerSpec) error { if !validName.MatchString(t.Name) || t.SourceID == uuid.Nil || t.EventType == "" || t.WorkflowID == uuid.Nil { 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.WorkflowID); 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, SourceID: uuid.NullUUID{UUID: t.SourceID, Valid: true}, EventType: t.EventType, Filter: filter, WorkflowID: t.WorkflowID, Enabled: true, Approved: true, }) }