feat(events): sources, triggers and filtered events are listed for the dashboard (#2026)
This commit is contained in:
2 files changed
+194
-4
No files matched your search
+132
-1
@@ -87,8 +87,11 @@ func (m *Module) Routes(mux *http.ServeMux, guard func(http.Handler) http.Handle
|
||||
mux.Handle("PUT /api/sources/{name}", guard(http.HandlerFunc(m.putSource)))
|
||||
mux.Handle("PUT /api/sources/{name}/key-fields", guard(http.HandlerFunc(m.putKeyFields)))
|
||||
mux.Handle("PUT /api/triggers/{name}", guard(http.HandlerFunc(m.putTrigger)))
|
||||
mux.Handle("GET /api/sources", guard(http.HandlerFunc(m.sources)))
|
||||
mux.Handle("GET /api/triggers", guard(http.HandlerFunc(m.triggers)))
|
||||
mux.Handle("GET /api/groups", guard(http.HandlerFunc(m.groups)))
|
||||
mux.Handle("GET /api/events", guard(http.HandlerFunc(m.recent)))
|
||||
mux.Handle("GET /api/events/{id}", guard(http.HandlerFunc(m.event)))
|
||||
}
|
||||
|
||||
// intake verifies the source's token before reading the body, has the
|
||||
@@ -477,12 +480,92 @@ func (m *Module) groups(w http.ResponseWriter, r *http.Request) {
|
||||
httpjson.Write(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
// sourceView is a source as the API returns it. The token is returned only
|
||||
// when it is created or rotated, never here.
|
||||
type sourceView struct {
|
||||
Name string `json:"name"`
|
||||
Connector string `json:"connector"`
|
||||
KeyFields []string `json:"key_fields"`
|
||||
TokenSet bool `json:"token_set"`
|
||||
}
|
||||
|
||||
func (m *Module) sources(w http.ResponseWriter, r *http.Request) {
|
||||
sources, err := m.store.Sources(r.Context(), m.orgID)
|
||||
if err != nil {
|
||||
m.log.ErrorContext(r.Context(), "list sources", "error", err)
|
||||
httpjson.Error(w, http.StatusInternalServerError, "couldn't list sources")
|
||||
return
|
||||
}
|
||||
out := make([]sourceView, 0, len(sources))
|
||||
for _, src := range sources {
|
||||
view := sourceView{Name: src.Name, Connector: src.Connector, TokenSet: src.TokenSHA256 != "", KeyFields: []string{}}
|
||||
if len(src.KeyFields) > 0 {
|
||||
if err := json.Unmarshal(src.KeyFields, &view.KeyFields); err != nil {
|
||||
m.log.ErrorContext(r.Context(), "read source key fields", "source", src.Name, "error", err)
|
||||
httpjson.Error(w, http.StatusInternalServerError, "couldn't read the key fields of "+src.Name)
|
||||
return
|
||||
}
|
||||
}
|
||||
out = append(out, view)
|
||||
}
|
||||
httpjson.Write(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
// triggerView is a trigger as the API returns it.
|
||||
type triggerView struct {
|
||||
Name string `json:"name"`
|
||||
Source string `json:"source"`
|
||||
EventType string `json:"event_type"`
|
||||
Filter map[string]string `json:"filter"`
|
||||
Workflow string `json:"workflow"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Every string `json:"every,omitempty"`
|
||||
}
|
||||
|
||||
func (m *Module) triggers(w http.ResponseWriter, r *http.Request) {
|
||||
triggers, err := m.store.AllTriggers(r.Context(), m.orgID)
|
||||
if err != nil {
|
||||
m.log.ErrorContext(r.Context(), "list triggers", "error", err)
|
||||
httpjson.Error(w, http.StatusInternalServerError, "couldn't list triggers")
|
||||
return
|
||||
}
|
||||
out := make([]triggerView, 0, len(triggers))
|
||||
for _, t := range triggers {
|
||||
view := triggerView{Name: t.Name, Source: t.Source, EventType: t.EventType, Workflow: t.Workflow, Enabled: t.Enabled, Filter: map[string]string{}}
|
||||
if len(t.Filter) > 0 {
|
||||
if err := json.Unmarshal(t.Filter, &view.Filter); err != nil {
|
||||
m.log.ErrorContext(r.Context(), "read trigger filter", "trigger", t.Name, "error", err)
|
||||
httpjson.Error(w, http.StatusInternalServerError, "couldn't read the filter of "+t.Name)
|
||||
return
|
||||
}
|
||||
}
|
||||
if t.ScheduleEverySeconds.Valid {
|
||||
view.Every = (time.Duration(t.ScheduleEverySeconds.Int64) * time.Second).String()
|
||||
}
|
||||
out = append(out, view)
|
||||
}
|
||||
httpjson.Write(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
func (m *Module) recent(w http.ResponseWriter, r *http.Request) {
|
||||
limit := 50
|
||||
if n, err := strconv.Atoi(r.URL.Query().Get("limit")); err == nil && n > 0 && n <= 500 {
|
||||
limit = n
|
||||
}
|
||||
events, err := m.store.Recent(r.Context(), m.orgID, limit)
|
||||
filter := store.EventFilter{
|
||||
Source: r.URL.Query().Get("source"),
|
||||
Type: r.URL.Query().Get("type"),
|
||||
Limit: limit,
|
||||
}
|
||||
if q := r.URL.Query().Get("matched"); q != "" {
|
||||
matched, err := strconv.ParseBool(q)
|
||||
if err != nil {
|
||||
httpjson.Error(w, http.StatusBadRequest, "matched is true or false")
|
||||
return
|
||||
}
|
||||
filter.Matched = &matched
|
||||
}
|
||||
events, err := m.store.Recent(r.Context(), m.orgID, filter)
|
||||
if err != nil {
|
||||
httpjson.Error(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
@@ -494,6 +577,54 @@ func (m *Module) recent(w http.ResponseWriter, r *http.Request) {
|
||||
httpjson.Write(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
// eventDetail is one event with the payload it arrived in and the runs it
|
||||
// started.
|
||||
type eventDetail struct {
|
||||
eventView
|
||||
Body jsontext.Value `json:"body"`
|
||||
Runs []triggerRun `json:"runs"`
|
||||
}
|
||||
|
||||
type triggerRun struct {
|
||||
Trigger string `json:"trigger"`
|
||||
RunID uuid.UUID `json:"run_id"`
|
||||
}
|
||||
|
||||
func (m *Module) event(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := uuid.Parse(r.PathValue("id"))
|
||||
if err != nil {
|
||||
httpjson.Error(w, http.StatusBadRequest, "an event id is a UUID")
|
||||
return
|
||||
}
|
||||
e, err := m.store.Event(r.Context(), m.orgID, id)
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
httpjson.Error(w, http.StatusNotFound, "no such event")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
m.log.ErrorContext(r.Context(), "read event", "event", id, "error", err)
|
||||
httpjson.Error(w, http.StatusInternalServerError, "couldn't read the event")
|
||||
return
|
||||
}
|
||||
runs, err := m.store.RunsForEvent(r.Context(), m.orgID, id)
|
||||
if err != nil {
|
||||
m.log.ErrorContext(r.Context(), "read event runs", "event", id, "error", err)
|
||||
httpjson.Error(w, http.StatusInternalServerError, "couldn't read the runs of the event")
|
||||
return
|
||||
}
|
||||
view := eventDetail{eventView: viewOf(e), Body: jsontext.Value(e.Body), Runs: make([]triggerRun, 0, len(runs))}
|
||||
if len(view.Body) == 0 {
|
||||
view.Body = jsontext.Value("null")
|
||||
}
|
||||
if !view.Body.IsValid() {
|
||||
view.Body = jsontext.Value("null")
|
||||
}
|
||||
for _, run := range runs {
|
||||
view.Runs = append(view.Runs, triggerRun{Trigger: run.Trigger, RunID: run.RunID})
|
||||
}
|
||||
httpjson.Write(w, http.StatusOK, view)
|
||||
}
|
||||
|
||||
// eventView is an event as the API returns it.
|
||||
type eventView struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
|
||||
@@ -261,18 +261,77 @@ func (s *Store) ScheduledTriggers(ctx context.Context, orgID string) ([]Trigger,
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Recent returns an organisation's newest events.
|
||||
func (s *Store) Recent(ctx context.Context, orgID string, limit int) ([]Event, error) {
|
||||
// EventFilter narrows the events a list returns. A zero field doesn't filter,
|
||||
// and Matched distinguishes events a trigger started a run for from the rest.
|
||||
type EventFilter struct {
|
||||
Source string
|
||||
Type string
|
||||
Matched *bool
|
||||
Limit int
|
||||
}
|
||||
|
||||
// Recent returns an organisation's newest events that match filter.
|
||||
func (s *Store) Recent(ctx context.Context, orgID string, f EventFilter) ([]Event, error) {
|
||||
var out []Event
|
||||
if err := s.db.SelectContext(ctx, &out, `
|
||||
SELECT id, org_id, source, type, source_event_id, link_key, linked_to, fields, occurred_at,
|
||||
received_at, matched, dispatched_at, own, origin_run_id, group_key, group_filter
|
||||
FROM events.events WHERE org_id = $1 ORDER BY received_at DESC LIMIT $2`, orgID, limit); err != nil {
|
||||
FROM events.events
|
||||
WHERE org_id = $1
|
||||
AND ($2 = '' OR source = $2)
|
||||
AND ($3 = '' OR type = $3)
|
||||
AND ($4::boolean IS NULL OR matched = $4)
|
||||
ORDER BY received_at DESC LIMIT $5`, orgID, f.Source, f.Type, f.Matched, f.Limit); err != nil {
|
||||
return nil, fmt.Errorf("list events: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Event returns one event with the payload it arrived in.
|
||||
func (s *Store) Event(ctx context.Context, orgID string, id uuid.UUID) (Event, error) {
|
||||
var e Event
|
||||
err := s.db.GetContext(ctx, &e, `
|
||||
SELECT id, org_id, source, type, source_event_id, link_key, linked_to, fields, body, occurred_at,
|
||||
received_at, matched, dispatched_at, own, origin_run_id, group_key, group_filter
|
||||
FROM events.events WHERE org_id = $1 AND id = $2`, orgID, id)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return Event{}, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return Event{}, fmt.Errorf("read event: %w", err)
|
||||
}
|
||||
return e, nil
|
||||
}
|
||||
|
||||
// TriggerRun is a run a trigger started for an event.
|
||||
type TriggerRun struct {
|
||||
Trigger string `db:"trigger"`
|
||||
RunID uuid.UUID `db:"run_id"`
|
||||
}
|
||||
|
||||
// RunsForEvent returns the runs triggers started for an event.
|
||||
func (s *Store) RunsForEvent(ctx context.Context, orgID string, id uuid.UUID) ([]TriggerRun, error) {
|
||||
var out []TriggerRun
|
||||
if err := s.db.SelectContext(ctx, &out, `
|
||||
SELECT d.trigger, d.run_id FROM events.dispatches d
|
||||
JOIN events.events e ON e.id = d.event_id
|
||||
WHERE e.org_id = $1 AND d.event_id = $2 ORDER BY d.trigger`, orgID, id); err != nil {
|
||||
return nil, fmt.Errorf("read event runs: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// AllTriggers returns every trigger of the organisation, enabled or not.
|
||||
func (s *Store) AllTriggers(ctx context.Context, orgID string) ([]Trigger, error) {
|
||||
var out []Trigger
|
||||
if err := s.db.SelectContext(ctx, &out, `
|
||||
SELECT org_id, name, source, event_type, filter, workflow, enabled, schedule_every_s, schedule_since
|
||||
FROM events.triggers WHERE org_id = $1 ORDER BY name`, orgID); err != nil {
|
||||
return nil, fmt.Errorf("list triggers: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Origin is the event and trigger that started a run.
|
||||
type Origin struct {
|
||||
EventID uuid.UUID `db:"event_id"`
|
||||
|
||||
Reference in new issue
Block a user