255 lines
14 KiB
SQL
255 lines
14 KiB
SQL
-- EDA queries: captures, mappings, bindings, dispatch ledgers, tool_calls.
|
|
|
|
-- name: InsertCapture :exec
|
|
INSERT INTO captures (id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type, delivery, org_id, workspace_id)
|
|
SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11,
|
|
COALESCE(s.org_id, @default_org::text), COALESCE(s.workspace_id, @default_workspace::text)
|
|
FROM (SELECT 1) intake LEFT JOIN sources s ON s.path = $2;
|
|
|
|
-- name: ListCaptures :many
|
|
SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type, org_id, workspace_id, delivery
|
|
FROM captures
|
|
WHERE org_id = @org_id
|
|
ORDER BY received_at DESC
|
|
LIMIT $1;
|
|
|
|
-- name: ListUndispatchedCaptures :many
|
|
-- Oldest first, so sustained intake cannot starve accepted work. Only
|
|
-- dispatchable captures (signed, or from an approved unsigned source) that one
|
|
-- of the named bindings still owes a dispatch are listed: no other capture
|
|
-- would ever be dispatched, so it would hold the batch.
|
|
SELECT c.id, c.source, c.remote_addr, c.content_type, c.headers, c.body, c.authenticated, c.received_at, c.unsigned, c.event_type, c.org_id, c.workspace_id, c.delivery
|
|
FROM captures c
|
|
WHERE c.org_id = @org_id
|
|
AND c.event_type <> ''
|
|
AND (c.authenticated OR c.unsigned)
|
|
AND EXISTS (
|
|
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
|
|
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
|
|
AND b.id = ANY(@bindings::text[])
|
|
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
|
|
)
|
|
ORDER BY c.received_at, c.id
|
|
LIMIT @entry_limit;
|
|
|
|
-- name: DeleteCapturesOlderThan :exec
|
|
-- Age retention spares what DeleteCapturesBeyondCount spares: a capture
|
|
-- ListUndispatchedCaptures would still offer is accepted work.
|
|
DELETE FROM captures AS c
|
|
WHERE c.received_at < $1
|
|
AND NOT (
|
|
c.event_type <> '' AND (c.authenticated OR c.unsigned)
|
|
AND EXISTS (
|
|
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
|
|
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
|
|
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
|
|
)
|
|
);
|
|
|
|
-- name: DeleteCapturesBeyondCount :exec
|
|
-- Count retention stays within the just-inserted capture's org: one org's
|
|
-- volume must never evict another org's recent captures. It never evicts a
|
|
-- capture ListUndispatchedCaptures would still offer: that is accepted work.
|
|
DELETE FROM captures AS c
|
|
WHERE c.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = sqlc.arg(capture_id))
|
|
AND c.id NOT IN (
|
|
SELECT k.id FROM captures AS k
|
|
WHERE k.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = sqlc.arg(capture_id))
|
|
ORDER BY k.received_at DESC
|
|
LIMIT sqlc.arg(max_events)
|
|
)
|
|
AND NOT (
|
|
c.event_type <> '' AND (c.authenticated OR c.unsigned)
|
|
AND EXISTS (
|
|
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
|
|
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
|
|
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
|
|
)
|
|
);
|
|
|
|
-- name: InsertMapping :exec
|
|
-- A mapping lives in its event type's workspace; one with no event type in
|
|
-- the org's default workspace.
|
|
INSERT INTO mappings (id, name, source_hint, event_type, fields, org_id, workspace_id)
|
|
VALUES (sqlc.arg(id), sqlc.arg(name), sqlc.arg(source_hint), sqlc.arg(event_type), sqlc.arg(fields), sqlc.arg(org_id),
|
|
COALESCE((SELECT y.workspace_id FROM event_types y WHERE y.id = sqlc.arg(event_type) AND y.org_id = sqlc.arg(org_id)), 'default'));
|
|
|
|
-- name: GetMapping :one
|
|
SELECT sqlc.embed(mappings),
|
|
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
|
|
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id), 'epoch')::timestamptz AS last_matched_at
|
|
FROM mappings WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
|
|
|
-- name: ListMappings :many
|
|
SELECT sqlc.embed(mappings),
|
|
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
|
|
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id), 'epoch')::timestamptz AS last_matched_at
|
|
FROM mappings WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY created_at DESC;
|
|
|
|
-- name: UpdateMapping :execrows
|
|
UPDATE mappings
|
|
SET name = sqlc.arg(name), source_hint = sqlc.arg(source_hint), event_type = sqlc.arg(event_type), fields = sqlc.arg(fields), updated_at = now(),
|
|
workspace_id = COALESCE((SELECT y.workspace_id FROM event_types y WHERE y.id = sqlc.arg(event_type) AND y.org_id = mappings.org_id), 'default')
|
|
WHERE mappings.id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR mappings.org_id = sqlc.narg(scope_org));
|
|
|
|
-- name: InsertMappingMatch :exec
|
|
INSERT INTO mapping_matches (mapping, capture) VALUES ($1, $2)
|
|
ON CONFLICT DO NOTHING;
|
|
|
|
-- name: DeleteMapping :execrows
|
|
DELETE FROM mappings WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
|
|
|
-- name: InsertBinding :exec
|
|
-- A binding lives in its source's workspace.
|
|
INSERT INTO bindings (id, name, source, mapping, filter, workflow, owner, repo, version, status, inputs, repo_param, org_id, workspace_id)
|
|
VALUES (sqlc.arg(id), sqlc.arg(name), sqlc.arg(source), sqlc.arg(mapping), sqlc.arg(filter), sqlc.arg(workflow), sqlc.arg(owner), sqlc.arg(repo), 1,
|
|
sqlc.arg(status), sqlc.arg(inputs), sqlc.arg(repo_param), sqlc.arg(org_id),
|
|
COALESCE((SELECT s.workspace_id FROM sources s WHERE s.path = sqlc.arg(source) AND s.org_id = sqlc.arg(org_id)), 'default'));
|
|
|
|
-- name: GetBinding :one
|
|
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id, workspace_id
|
|
FROM bindings WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
|
|
|
-- name: ListBindings :many
|
|
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id, workspace_id
|
|
FROM bindings WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY created_at DESC;
|
|
|
|
-- name: ArmedBindingsForSource :many
|
|
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id, workspace_id
|
|
FROM bindings WHERE source = sqlc.arg(source) AND status = 'armed' AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY created_at DESC;
|
|
|
|
-- name: UpdateBinding :execrows
|
|
UPDATE bindings
|
|
SET name = sqlc.arg(name), source = sqlc.arg(source), mapping = sqlc.arg(mapping), filter = sqlc.arg(filter),
|
|
workflow = sqlc.arg(workflow), owner = sqlc.arg(owner), repo = sqlc.arg(repo),
|
|
version = version + 1, status = sqlc.arg(status), inputs = sqlc.arg(inputs), repo_param = sqlc.arg(repo_param), updated_at = now(),
|
|
workspace_id = COALESCE((SELECT s.workspace_id FROM sources s WHERE s.path = sqlc.arg(source) AND s.org_id = bindings.org_id), 'default')
|
|
WHERE bindings.id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR bindings.org_id = sqlc.narg(scope_org));
|
|
|
|
-- name: SetBindingArmed :execrows
|
|
UPDATE bindings SET status = 'armed', updated_at = now() WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
|
|
|
-- name: TransitionBindingStatus :execrows
|
|
UPDATE bindings SET status = @to_status, updated_at = now()
|
|
WHERE id = @id AND status = @from_status AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
|
|
|
-- name: DeleteBinding :execrows
|
|
DELETE FROM bindings WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
|
|
|
-- name: InsertBindingDispatch :execrows
|
|
INSERT INTO binding_dispatches (binding, binding_version, capture, task_id, reason, delivery)
|
|
SELECT @binding, @binding_version, c.id, @task_id, @reason, c.delivery
|
|
FROM captures c WHERE c.id = @capture;
|
|
|
|
-- name: SetBindingDispatchTask :exec
|
|
UPDATE binding_dispatches SET task_id = $3 WHERE binding = $1 AND capture = $2;
|
|
|
|
-- name: ListBindingDispatches :many
|
|
SELECT d.binding, d.binding_version, d.capture, d.task_id, d.reason, d.dispatched_at
|
|
FROM binding_dispatches d JOIN bindings b ON b.id = d.binding
|
|
WHERE (sqlc.narg(scope_org)::text IS NULL OR b.org_id = sqlc.narg(scope_org))
|
|
AND (sqlc.narg(binding)::text IS NULL OR d.binding = sqlc.narg(binding))
|
|
AND (sqlc.narg(capture)::text IS NULL OR d.capture = sqlc.narg(capture))
|
|
AND (sqlc.narg(task_id)::bigint IS NULL OR d.task_id = sqlc.narg(task_id))
|
|
ORDER BY d.dispatched_at DESC
|
|
LIMIT @entry_limit;
|
|
|
|
-- name: InsertPlaybookDispatch :exec
|
|
INSERT INTO playbook_dispatches (playbook_id, playbook_version, event_id, action_id)
|
|
VALUES ($1, $2, $3, $4);
|
|
|
|
-- name: DeletePlaybookDispatches :exec
|
|
DELETE FROM playbook_dispatches WHERE playbook_id = $1;
|
|
|
|
-- name: InsertToolCall :exec
|
|
INSERT INTO tool_calls (id, task_id, attempt, tool, result, error, called_at, org_id, workspace_id)
|
|
SELECT sqlc.arg(id), t.id, sqlc.arg(attempt), sqlc.arg(tool), sqlc.arg(result), sqlc.arg(error), sqlc.arg(called_at), t.org_id, t.workspace_id
|
|
FROM tasks t WHERE t.id = sqlc.arg(task_id);
|
|
|
|
-- name: TaskToolCalls :many
|
|
SELECT id, task_id, attempt, tool, result, error, called_at
|
|
FROM tool_calls WHERE task_id = sqlc.arg(task_id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY called_at ASC;
|
|
|
|
-- name: InsertEventType :exec
|
|
-- An event type lives in its source's workspace.
|
|
INSERT INTO event_types (id, source, name, rule, schema, org_id, workspace_id)
|
|
VALUES (sqlc.arg(id), sqlc.arg(source), sqlc.arg(name), sqlc.arg(rule), sqlc.arg(schema), sqlc.arg(org_id),
|
|
COALESCE((SELECT s.workspace_id FROM sources s WHERE s.path = sqlc.arg(source) AND s.org_id = sqlc.arg(org_id)), 'default'));
|
|
|
|
-- name: GetEventType :one
|
|
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
|
|
FROM event_types WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
|
|
|
-- name: ListEventTypes :many
|
|
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
|
|
FROM event_types WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY source, name;
|
|
|
|
-- name: EventTypesForSource :many
|
|
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
|
|
FROM event_types WHERE source = $1 ORDER BY name;
|
|
|
|
-- name: UpdateEventType :execrows
|
|
UPDATE event_types SET name = sqlc.arg(name), rule = sqlc.arg(rule), updated_at = now()
|
|
WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
|
|
|
-- name: DeleteEventType :execrows
|
|
DELETE FROM event_types WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
|
|
|
-- name: LockEventTypeSource :exec
|
|
-- Serialises saves per source so two concurrent saves cannot each pass the
|
|
-- overlap check against a set that excludes the other.
|
|
SELECT pg_advisory_xact_lock(hashtext('event_types:' || sqlc.arg(source)::text));
|
|
-- name: InsertSource :execrows
|
|
-- Inserts nothing when the workspace is not one of the org's.
|
|
INSERT INTO sources (path, signing, secret, org_id, workspace_id)
|
|
SELECT sqlc.arg(path), sqlc.arg(signing), sqlc.arg(secret), w.org_id, w.id
|
|
FROM workspaces w WHERE w.org_id = sqlc.arg(org_id) AND w.id = sqlc.arg(workspace_id);
|
|
|
|
-- name: GetSource :one
|
|
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
|
|
|
-- name: ListSources :many
|
|
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY created_at DESC, path;
|
|
|
|
-- name: SetSourceSigning :execrows
|
|
UPDATE sources SET signing = sqlc.arg(to_signing), updated_at = now()
|
|
WHERE path = sqlc.arg(path) AND signing = sqlc.arg(from_signing) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
|
|
|
-- name: SetSourceSecret :execrows
|
|
UPDATE sources SET secret = sqlc.arg(secret), updated_at = now() WHERE path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
|
|
|
-- name: SetSourceName :execrows
|
|
UPDATE sources SET name = sqlc.arg(name), updated_at = now() WHERE path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
|
|
|
-- name: SetSourceDeliveryHeader :execrows
|
|
UPDATE sources SET delivery_header = sqlc.arg(delivery_header), updated_at = now() WHERE path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
|
|
|
-- name: DeleteUnboundSource :execrows
|
|
-- A source an armed binding fires on is not deleted: the binding would
|
|
-- silently stop receiving events.
|
|
DELETE FROM sources s WHERE s.path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR s.org_id = sqlc.narg(scope_org))
|
|
AND NOT EXISTS (SELECT 1 FROM bindings b WHERE b.source = s.path AND b.status = 'armed');
|
|
|
|
-- name: SourceExists :one
|
|
SELECT EXISTS (SELECT 1 FROM sources WHERE path = $1);
|
|
|
|
-- name: InsertCaptureRefusal :exec
|
|
INSERT INTO capture_refusals (source, addr, refused_at) VALUES ($1, $2, $3);
|
|
|
|
-- name: DeleteCaptureRefusalsBefore :exec
|
|
DELETE FROM capture_refusals WHERE refused_at < $1;
|
|
|
|
-- name: CaptureRefusalSummary :many
|
|
WITH r AS (
|
|
SELECT source, count(*) AS refused, array_agg(DISTINCT addr ORDER BY addr)::text[] AS addrs
|
|
FROM capture_refusals cr WHERE refused_at >= sqlc.arg(since)
|
|
AND (sqlc.narg(scope_org)::text IS NULL
|
|
OR EXISTS (SELECT 1 FROM sources s WHERE s.path = cr.source AND s.org_id = sqlc.narg(scope_org)))
|
|
GROUP BY source
|
|
), a AS (
|
|
SELECT source, count(*) AS accepted FROM captures WHERE received_at >= sqlc.arg(since) GROUP BY source
|
|
)
|
|
SELECT r.source, r.refused, COALESCE(a.accepted, 0)::bigint AS accepted, r.addrs
|
|
FROM r LEFT JOIN a ON a.source = r.source
|
|
ORDER BY r.source;
|