A source is keyed by a uuid, owned by the organisation or one person, and labelled within that scope. The webhook path is a separate unique column, so renaming either leaves stored events, event types, triggers, groups, mappings and drafts alone. Delivery policies, sealed secrets and group keys use the id.
53 lines
1.9 KiB
Go
53 lines
1.9 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// GroupByID returns one group, listed or not.
|
|
func (s *Store) GroupByID(ctx context.Context, orgID string, id uuid.UUID) (Group, bool, error) {
|
|
var g Group
|
|
err := s.db.GetContext(ctx, &g, `
|
|
SELECT id, source_id, event_type, filter, count, first_seen, last_seen
|
|
FROM events.groups WHERE org_id = $1 AND id = $2`, orgID, id)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return Group{}, false, nil
|
|
}
|
|
if err != nil {
|
|
return Group{}, false, fmt.Errorf("read group: %w", err)
|
|
}
|
|
return g, true, nil
|
|
}
|
|
|
|
// GroupEvents returns the stored events of a group, newest first.
|
|
func (s *Store) GroupEvents(ctx context.Context, orgID string, id uuid.UUID, limit int) ([]Event, error) {
|
|
var out []Event
|
|
if err := s.db.SelectContext(ctx, &out, `
|
|
SELECT e.id, e.org_id, e.source_id, e.type, e.source_event_id, e.link_key, e.linked_to, e.fields, e.occurred_at,
|
|
e.received_at, e.matched, e.dispatched_at, e.own, e.origin_run_id, e.group_key, e.group_filter
|
|
FROM events.events e
|
|
JOIN events.groups g ON g.org_id = e.org_id AND g.key = e.group_key
|
|
WHERE g.org_id = $1 AND g.id = $2 ORDER BY e.received_at DESC LIMIT $3`, orgID, id, limit); err != nil {
|
|
return nil, fmt.Errorf("list group events: %w", err)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// RecordReplay notes that a dry run replayed an event for a draft trigger, so
|
|
// the run's origin reads as that event. The latest replay for a trigger wins.
|
|
func (s *Store) RecordReplay(ctx context.Context, eventID uuid.UUID, trigger string, runID uuid.UUID) error {
|
|
if _, err := s.db.ExecContext(ctx, `
|
|
INSERT INTO events.dispatches (event_id, trigger, run_id) VALUES ($1, $2, $3)
|
|
ON CONFLICT (event_id, trigger) DO UPDATE
|
|
SET run_id = EXCLUDED.run_id, outcome = 'started', reason = NULL, created_at = now()`,
|
|
eventID, trigger, runID); err != nil {
|
|
return fmt.Errorf("record replay: %w", err)
|
|
}
|
|
return nil
|
|
}
|