A step is id/type/settings with when and on_failure; the five kinds become
the types agent.run (inline mission), connector.action, approval.wait,
run.notify and workflow.sleep. Documents declare inputs, repository,
instructions, identity and profile; references are {{ task|inputs|steps }}
checked at save. Any number of approval steps, each asking only about what
was held since; actions still held at the end are put to people before the
run finishes. Triggers are refused for a workflow they can't start, and the
workflows API lists, reads and saves documents beside a step-type catalog.
Copied from internal/domain/workflow: definition.go, step_refs.go,
step_flow.go's checks, control_steps.go's when parser, vocabulary.go and
task/interface.go. stableid moves to internal/platform.
96 lines
2.8 KiB
Go
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,
|
|
})
|
|
}
|