117 lines
3.8 KiB
Go
117 lines
3.8 KiB
Go
package events
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json/jsontext"
|
|
json "encoding/json/v2"
|
|
"errors"
|
|
"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
|
|
DeclaredEventType 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, DeclaredEventType: g.DeclaredEventType, 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 run for a proposed trigger replayed an event, so the
|
|
// run's trail names that event as its origin. label is the trigger's name.
|
|
func (m *Module) Replayed(ctx context.Context, eventID uuid.UUID, label string, runID uuid.UUID) error {
|
|
return m.store.RecordReplay(ctx, eventID, label, runID)
|
|
}
|
|
|
|
// TriggerNamed returns the id of the live trigger with the name, if there is
|
|
// one.
|
|
func (m *Module) TriggerNamed(ctx context.Context, name string) (uuid.UUID, bool, error) {
|
|
id, err := m.store.TriggerNamed(ctx, m.orgID, name)
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
return uuid.Nil, false, nil
|
|
}
|
|
return id, err == nil, err
|
|
}
|
|
|
|
// TriggerSpec is an event trigger to save. The caller chooses its ID, so
|
|
// saving it again replaces it instead of adding a second trigger.
|
|
type TriggerSpec struct {
|
|
ID uuid.UUID
|
|
Name string
|
|
SourceID uuid.UUID
|
|
DeclaredEventType 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 t.ID == uuid.Nil || !validName.MatchString(t.Name) || t.SourceID == uuid.Nil || t.DeclaredEventType == "" || t.WorkflowID == uuid.Nil {
|
|
return fmt.Errorf("a trigger needs an id, a lowercase name, a source, a declared 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{
|
|
ID: t.ID, OrgID: m.orgID, Name: t.Name, SourceID: uuid.NullUUID{UUID: t.SourceID, Valid: true}, DeclaredEventType: t.DeclaredEventType,
|
|
Filter: filter, WorkflowID: t.WorkflowID, Enabled: true, Approved: true,
|
|
})
|
|
}
|