feat(webui): bindings pick an event type and filter, mappings show their type and match count
This commit is contained in:
1 parent
b9d3acd31c
commit
d508f2d9b5
14 files changed
+220
-42
No files matched your search
@@ -7,6 +7,7 @@ import { Button } from "@/components/ui/button";
|
||||
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
||||
import { Field, FieldDescription, FieldGroup, FieldLabel } from "@/components/ui/field";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { eventTypeLabel, type EventType } from "@/captures/event-types";
|
||||
import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from "@/components/ui/input-group";
|
||||
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
@@ -15,6 +16,7 @@ import {
|
||||
draftFromBinding,
|
||||
emptyDraft,
|
||||
generateSecret,
|
||||
mappingsForEventType,
|
||||
type Binding,
|
||||
type BindingDraft,
|
||||
type MappingOption,
|
||||
@@ -22,15 +24,16 @@ import {
|
||||
} from "./binding-draft";
|
||||
|
||||
/**
|
||||
* The binding editor: name, the matcher source senders POST to, the field
|
||||
* mapping, the workflow, an optional repo pin, and the shared secret their
|
||||
* signature is checked against.
|
||||
* The binding editor: name, the event type it applies to, one of that type's
|
||||
* mappings, an optional filter over the mapping's parameters, the workflow, an
|
||||
* optional repo pin, and the shared secret senders sign with.
|
||||
*/
|
||||
|
||||
const props = defineProps<{
|
||||
/** null is a new binding; a binding is an edit. */
|
||||
binding: Binding | null;
|
||||
mappings: MappingOption[];
|
||||
eventTypes: EventType[];
|
||||
workflows: WorkflowOption[];
|
||||
saving: boolean;
|
||||
error: string | null;
|
||||
@@ -45,11 +48,22 @@ const draft = ref<BindingDraft>(emptyDraft());
|
||||
watch(
|
||||
open,
|
||||
(isOpen) => {
|
||||
if (isOpen) draft.value = props.binding ? draftFromBinding(props.binding) : emptyDraft();
|
||||
if (isOpen) draft.value = props.binding ? draftFromBinding(props.binding, props.mappings) : emptyDraft();
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
const typeMappings = computed(() => mappingsForEventType(props.mappings, draft.value.eventTypeId));
|
||||
|
||||
// A mapping belongs to one event type, so changing the type clears a mapping
|
||||
// that no longer fits it.
|
||||
watch(
|
||||
() => draft.value.eventTypeId,
|
||||
() => {
|
||||
if (!typeMappings.value.some((m) => m.id === draft.value.mappingId)) draft.value.mappingId = "";
|
||||
},
|
||||
);
|
||||
|
||||
const title = computed(() => (props.binding ? "Edit binding" : "New binding"));
|
||||
const copied = ref(false);
|
||||
async function copySecret(): Promise<void> {
|
||||
@@ -79,20 +93,30 @@ async function copySecret(): Promise<void> {
|
||||
</Field>
|
||||
|
||||
<Field>
|
||||
<FieldLabel for="binding-source">Source</FieldLabel>
|
||||
<Input id="binding-source" v-model="draft.source" />
|
||||
<FieldDescription>The path segment senders POST to, for example "sentry".</FieldDescription>
|
||||
<FieldLabel for="binding-event-type">Event type</FieldLabel>
|
||||
<Select v-model="draft.eventTypeId">
|
||||
<SelectTrigger id="binding-event-type" class="w-full">
|
||||
<SelectValue placeholder="Pick an event type" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectGroup>
|
||||
<SelectItem v-for="type in props.eventTypes" :key="type.id" :value="type.id">
|
||||
{{ eventTypeLabel(type.id, props.eventTypes) }}
|
||||
</SelectItem>
|
||||
</SelectGroup>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</Field>
|
||||
|
||||
<Field>
|
||||
<FieldLabel for="binding-mapping">Field mapping</FieldLabel>
|
||||
<Select v-model="draft.mappingId">
|
||||
<Select v-model="draft.mappingId" :disabled="!typeMappings.length">
|
||||
<SelectTrigger id="binding-mapping" class="w-full">
|
||||
<SelectValue placeholder="Pick a saved mapping" />
|
||||
<SelectValue :placeholder="draft.eventTypeId && !typeMappings.length ? 'No mappings for this event type' : 'Pick a mapping'" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectGroup>
|
||||
<SelectItem v-for="mapping in props.mappings" :key="mapping.id" :value="String(mapping.id)">
|
||||
<SelectItem v-for="mapping in typeMappings" :key="mapping.id" :value="String(mapping.id)">
|
||||
{{ mapping.name }}
|
||||
</SelectItem>
|
||||
</SelectGroup>
|
||||
@@ -100,6 +124,12 @@ async function copySecret(): Promise<void> {
|
||||
</Select>
|
||||
</Field>
|
||||
|
||||
<Field>
|
||||
<FieldLabel for="binding-filter">Filter</FieldLabel>
|
||||
<Input id="binding-filter" v-model="draft.filter" class="font-mono text-xs" placeholder='severity in ["high", "critical"]' />
|
||||
<FieldDescription>Optional CEL over the mapping's parameters.</FieldDescription>
|
||||
</Field>
|
||||
|
||||
<Field>
|
||||
<FieldLabel for="binding-workflow">Workflow</FieldLabel>
|
||||
<Select v-model="draft.workflow">
|
||||
|
||||
@@ -4,10 +4,11 @@ import { Check, Pencil, Trash2 } from "@lucide/vue";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { TableCell, TableRow } from "@/components/ui/table";
|
||||
import BindingStatusBadge from "./BindingStatusBadge.vue";
|
||||
import { matcherSource, repoPin, type Binding } from "./binding-draft";
|
||||
import { eventTypeLabel, type EventType } from "@/captures/event-types";
|
||||
import { bindingEventType, repoPin, type Binding, type MappingOption } from "./binding-draft";
|
||||
|
||||
/** One saved binding's summary row. */
|
||||
const props = defineProps<{ binding: Binding }>();
|
||||
const props = defineProps<{ binding: Binding; mappings: MappingOption[]; eventTypes: EventType[] }>();
|
||||
const emit = defineEmits<{
|
||||
edit: [binding: Binding];
|
||||
approve: [binding: Binding];
|
||||
@@ -18,7 +19,12 @@ const emit = defineEmits<{
|
||||
<template>
|
||||
<TableRow>
|
||||
<TableCell class="font-medium">{{ props.binding.name }}</TableCell>
|
||||
<TableCell class="font-mono text-fg-muted">{{ matcherSource(props.binding) }}</TableCell>
|
||||
<TableCell class="font-mono text-fg-muted">
|
||||
{{ eventTypeLabel(bindingEventType(props.binding, props.mappings), props.eventTypes) }}
|
||||
</TableCell>
|
||||
<TableCell class="max-w-56 truncate font-mono text-xs text-fg-muted" :title="props.binding.filter">
|
||||
{{ props.binding.filter || "—" }}
|
||||
</TableCell>
|
||||
<TableCell>{{ props.binding.workflow || "—" }}</TableCell>
|
||||
<TableCell class="font-mono text-fg-muted">{{ repoPin(props.binding) }}</TableCell>
|
||||
<TableCell><BindingStatusBadge :status="props.binding.status" /></TableCell>
|
||||
|
||||
@@ -12,8 +12,8 @@ import type { Binding, BindingDraft } from "./binding-draft";
|
||||
import { useBindings } from "./use-bindings";
|
||||
|
||||
/**
|
||||
* Playbook bindings (t2db.8): tie a matcher + payload mapping + workflow
|
||||
* together, with a draft -> pending_approval -> armed state machine so nothing
|
||||
* Playbook bindings: tie an event type's mapping, an optional filter and a
|
||||
* workflow together, with a draft -> pending_approval -> armed state machine so nothing
|
||||
* self-arms. See docs/architecture/bindings.md and
|
||||
* docs/prds/payload-field-mapping.md. Owner/Repo optionally pin a multi-repo
|
||||
* deployment's dispatch target (archie-core-t2db.8's backend fix, commit
|
||||
@@ -23,6 +23,7 @@ import { useBindings } from "./use-bindings";
|
||||
const {
|
||||
bindings,
|
||||
mappings,
|
||||
eventTypes,
|
||||
workflows,
|
||||
failure,
|
||||
actionFailure,
|
||||
@@ -87,6 +88,8 @@ async function handleDelete(binding: Binding): Promise<void> {
|
||||
|
||||
<BindingsTable
|
||||
:bindings="bindings"
|
||||
:mappings="mappings"
|
||||
:event-types="eventTypes"
|
||||
:failure="failure"
|
||||
@edit="startEdit"
|
||||
@approve="approve"
|
||||
@@ -97,6 +100,7 @@ async function handleDelete(binding: Binding): Promise<void> {
|
||||
v-model:open="editorOpen"
|
||||
:binding="editing"
|
||||
:mappings="mappings"
|
||||
:event-types="eventTypes"
|
||||
:workflows="workflows"
|
||||
:saving="saving"
|
||||
:error="saveFailure"
|
||||
|
||||
@@ -6,12 +6,15 @@ import { Empty, EmptyDescription, EmptyHeader, EmptyTitle } from "@/components/u
|
||||
import { Skeleton } from "@/components/ui/skeleton";
|
||||
import { Table, TableBody, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
||||
import BindingRow from "./BindingRow.vue";
|
||||
import type { Binding } from "./binding-draft";
|
||||
import type { EventType } from "@/captures/event-types";
|
||||
import type { Binding, MappingOption } from "./binding-draft";
|
||||
import type { LoadFailure } from "./use-bindings";
|
||||
|
||||
const props = defineProps<{
|
||||
/** null means loading, [] means loaded and empty. */
|
||||
bindings: Binding[] | null;
|
||||
mappings: MappingOption[];
|
||||
eventTypes: EventType[];
|
||||
failure: LoadFailure | null;
|
||||
}>();
|
||||
|
||||
@@ -56,7 +59,8 @@ const failureTitle = computed(() =>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Name</TableHead>
|
||||
<TableHead>Source</TableHead>
|
||||
<TableHead>Event type</TableHead>
|
||||
<TableHead>Filter</TableHead>
|
||||
<TableHead>Workflow</TableHead>
|
||||
<TableHead>Repo pin</TableHead>
|
||||
<TableHead>Status</TableHead>
|
||||
@@ -66,7 +70,7 @@ const failureTitle = computed(() =>
|
||||
<TableBody>
|
||||
<template v-if="loading">
|
||||
<TableRow v-for="row in 3" :key="row">
|
||||
<TableCell v-for="col in 6" :key="col"><Skeleton class="h-4 w-full" /></TableCell>
|
||||
<TableCell v-for="col in 7" :key="col"><Skeleton class="h-4 w-full" /></TableCell>
|
||||
</TableRow>
|
||||
</template>
|
||||
<template v-else>
|
||||
@@ -74,6 +78,8 @@ const failureTitle = computed(() =>
|
||||
v-for="binding in rows"
|
||||
:key="binding.id"
|
||||
:binding="binding"
|
||||
:mappings="props.mappings"
|
||||
:event-types="props.eventTypes"
|
||||
@edit="emit('edit', $event)"
|
||||
@approve="emit('approve', $event)"
|
||||
@delete="emit('delete', $event)"
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
* binding-editor.jsx had from bindings.jsx in the Preact build.
|
||||
*/
|
||||
|
||||
import type { EventType } from "../captures/event-types";
|
||||
|
||||
/**
|
||||
* A binding as GET /api/bindings returns it. Secret is never returned: the
|
||||
* daemon strips it from every read, and a blank secret on update is its
|
||||
@@ -15,6 +17,8 @@ export interface Binding {
|
||||
name: string;
|
||||
matcher?: { source?: string };
|
||||
mapping_id?: string;
|
||||
/** Optional CEL over the mapping's parameters; an excluded event is not dispatched. */
|
||||
filter?: string;
|
||||
workflow?: string;
|
||||
/** A pin is a complete owner/repo pair or not a pin at all. */
|
||||
owner?: string;
|
||||
@@ -27,6 +31,14 @@ export interface Binding {
|
||||
export interface MappingOption {
|
||||
id: string;
|
||||
name: string;
|
||||
event_type_id?: string;
|
||||
}
|
||||
|
||||
/** The mappings that belong to one event type, which are the ones a binding
|
||||
* on that type can use. */
|
||||
export function mappingsForEventType(mappings: MappingOption[], eventTypeId: string): MappingOption[] {
|
||||
if (!eventTypeId) return [];
|
||||
return mappings.filter((m) => m.event_type_id === eventTypeId);
|
||||
}
|
||||
|
||||
/** One entry of GET /api/workflows' `definitions`. */
|
||||
@@ -66,8 +78,10 @@ export function statusKind(status?: string): StatusKind {
|
||||
export interface BindingDraft {
|
||||
id: string | null;
|
||||
name: string;
|
||||
source: string;
|
||||
/** The event type the binding applies to; its source is the binding's. */
|
||||
eventTypeId: string;
|
||||
mappingId: string;
|
||||
filter: string;
|
||||
workflow: string;
|
||||
owner: string;
|
||||
repo: string;
|
||||
@@ -78,8 +92,9 @@ export function emptyDraft(): BindingDraft {
|
||||
return {
|
||||
id: null,
|
||||
name: "",
|
||||
source: "",
|
||||
eventTypeId: "",
|
||||
mappingId: "",
|
||||
filter: "",
|
||||
workflow: "",
|
||||
owner: "",
|
||||
repo: "",
|
||||
@@ -87,12 +102,14 @@ export function emptyDraft(): BindingDraft {
|
||||
};
|
||||
}
|
||||
|
||||
export function draftFromBinding(binding: Binding): BindingDraft {
|
||||
export function draftFromBinding(binding: Binding, mappings: MappingOption[]): BindingDraft {
|
||||
const mapping = mappings.find((m) => m.id === binding.mapping_id);
|
||||
return {
|
||||
id: binding.id,
|
||||
name: binding.name,
|
||||
source: binding.matcher?.source || "",
|
||||
eventTypeId: mapping?.event_type_id || "",
|
||||
mappingId: binding.mapping_id || "",
|
||||
filter: binding.filter || "",
|
||||
workflow: binding.workflow || "",
|
||||
owner: binding.owner || "",
|
||||
repo: binding.repo || "",
|
||||
@@ -103,11 +120,13 @@ export function draftFromBinding(binding: Binding): BindingDraft {
|
||||
}
|
||||
|
||||
/** The body POST /api/bindings and PATCH /api/bindings/{id} both accept. */
|
||||
export function bindingPayload(draft: BindingDraft): Record<string, unknown> {
|
||||
export function bindingPayload(draft: BindingDraft, eventTypes: EventType[]): Record<string, unknown> {
|
||||
const eventType = eventTypes.find((t) => t.id === draft.eventTypeId);
|
||||
return {
|
||||
name: draft.name,
|
||||
matcher: { source: draft.source },
|
||||
matcher: { source: eventType?.source || "" },
|
||||
mapping_id: draft.mappingId,
|
||||
filter: draft.filter.trim(),
|
||||
workflow: draft.workflow,
|
||||
owner: draft.owner,
|
||||
repo: draft.repo,
|
||||
@@ -127,7 +146,7 @@ export function repoPin(binding: Binding): string {
|
||||
return binding.owner && binding.repo ? `${binding.owner}/${binding.repo}` : "—";
|
||||
}
|
||||
|
||||
/** The matcher source a row shows: the path segment senders POST to. */
|
||||
export function matcherSource(binding: Binding): string {
|
||||
return binding.matcher?.source || "—";
|
||||
/** The event type a row shows: its mapping's. */
|
||||
export function bindingEventType(binding: Binding, mappings: MappingOption[]): string | undefined {
|
||||
return mappings.find((m) => m.id === binding.mapping_id)?.event_type_id;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { ref } from "vue";
|
||||
|
||||
import { api, ApiError } from "@/lib/api";
|
||||
import type { EventType } from "@/captures/event-types";
|
||||
import {
|
||||
bindingPayload,
|
||||
type Binding,
|
||||
@@ -37,6 +38,7 @@ export function useBindings() {
|
||||
// null means loading, [] means loaded and empty.
|
||||
const bindings = ref<Binding[] | null>(null);
|
||||
const mappings = ref<MappingOption[]>([]);
|
||||
const eventTypes = ref<EventType[]>([]);
|
||||
const workflows = ref<WorkflowOption[]>([]);
|
||||
const failure = ref<LoadFailure | null>(null);
|
||||
// A failed mutation is not a failed load: the list on screen is still
|
||||
@@ -48,13 +50,15 @@ export function useBindings() {
|
||||
async function load(): Promise<void> {
|
||||
failure.value = null;
|
||||
try {
|
||||
const [b, m, w] = await Promise.all([
|
||||
const [b, m, w, t] = await Promise.all([
|
||||
api.bindings<{ bindings?: Binding[] }>(),
|
||||
api.mappings<{ mappings?: MappingOption[] }>(),
|
||||
api.workflows<{ definitions?: WorkflowOption[] }>(),
|
||||
api.eventTypes<{ event_types?: EventType[] }>(),
|
||||
]);
|
||||
bindings.value = b.bindings || [];
|
||||
mappings.value = m.mappings || [];
|
||||
eventTypes.value = t.event_types || [];
|
||||
// Only enabled definitions are offerable: binding to a disabled one
|
||||
// would persist a binding that can never dispatch.
|
||||
workflows.value = (w.definitions || []).filter((d) => d.enabled);
|
||||
@@ -73,7 +77,7 @@ export function useBindings() {
|
||||
saving.value = true;
|
||||
saveFailure.value = null;
|
||||
try {
|
||||
const body = bindingPayload(draft);
|
||||
const body = bindingPayload(draft, eventTypes.value);
|
||||
if (draft.id) {
|
||||
await api.bindingUpdate(String(draft.id), body);
|
||||
} else {
|
||||
@@ -112,6 +116,7 @@ export function useBindings() {
|
||||
return {
|
||||
bindings,
|
||||
mappings,
|
||||
eventTypes,
|
||||
workflows,
|
||||
failure,
|
||||
actionFailure,
|
||||
|
||||
@@ -59,6 +59,13 @@ export function captureIdentity(
|
||||
return { label: found ? found.name : "Deleted type", identified: true };
|
||||
}
|
||||
|
||||
/** eventTypeLabel names an event type by id as "source / name". */
|
||||
export function eventTypeLabel(id: string | undefined, types: EventType[]): string {
|
||||
if (!id) return "No event type";
|
||||
const found = types.find((t) => t.id === id);
|
||||
return found ? `${found.source} / ${found.name}` : "Deleted type";
|
||||
}
|
||||
|
||||
/** ruleSummary renders a rule as one line of its conditions. */
|
||||
export function ruleSummary(rule: Rule): string {
|
||||
const parts = [
|
||||
|
||||
@@ -5,7 +5,8 @@ import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { ago } from "@/lib/format";
|
||||
import { captures, capturesEnabled, draft, selectCapture, type Capture } from "./state";
|
||||
import { eventTypeLabel } from "@/captures/event-types";
|
||||
import { captures, capturesEnabled, draft, eventTypes, selectCapture, type Capture } from "./state";
|
||||
|
||||
/**
|
||||
* The mapping's own fields: the name it is saved under, the organisational
|
||||
@@ -49,6 +50,22 @@ const captureHint = computed(() => {
|
||||
<Input id="mapping-name" v-model="draft.name" />
|
||||
</div>
|
||||
|
||||
<div class="flex flex-col gap-2">
|
||||
<Label for="mapping-event-type">Event type</Label>
|
||||
<Select v-model="draft.eventTypeId">
|
||||
<SelectTrigger id="mapping-event-type" class="w-full">
|
||||
<SelectValue placeholder="Pick an event type" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectGroup>
|
||||
<SelectItem v-for="type in eventTypes" :key="type.id" :value="type.id">
|
||||
{{ eventTypeLabel(type.id, eventTypes) }}
|
||||
</SelectItem>
|
||||
</SelectGroup>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-col gap-2">
|
||||
<Label for="mapping-source-hint">Source hint (organisational only)</Label>
|
||||
<Input id="mapping-source-hint" v-model="draft.sourceHint" />
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
<script setup lang="ts">
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { TableCell, TableRow } from "@/components/ui/table";
|
||||
import type { Mapping } from "./state";
|
||||
import { eventTypeLabel } from "@/captures/event-types";
|
||||
import { ago } from "@/lib/format";
|
||||
import { matchCountLabel } from "./match-count";
|
||||
import { eventTypes, type Mapping } from "./state";
|
||||
|
||||
/** One saved mapping's summary row. Both of its actions are the page's, not the
|
||||
* row's: editing opens the editor overlay, deleting opens the confirmation. */
|
||||
@@ -12,8 +15,10 @@ const emit = defineEmits<{ edit: [mapping: Mapping]; delete: [mapping: Mapping]
|
||||
<template>
|
||||
<TableRow>
|
||||
<TableCell class="font-medium">{{ mapping.name }}</TableCell>
|
||||
<TableCell class="font-mono">{{ mapping.source_hint || "—" }}</TableCell>
|
||||
<TableCell>{{ mapping.fields?.length ?? 0 }}</TableCell>
|
||||
<TableCell class="font-mono text-fg-muted">{{ eventTypeLabel(mapping.event_type_id, eventTypes) }}</TableCell>
|
||||
<TableCell class="tabular-nums">{{ mapping.fields?.length ?? 0 }}</TableCell>
|
||||
<TableCell class="tabular-nums">{{ matchCountLabel(mapping) }}</TableCell>
|
||||
<TableCell class="text-fg-muted">{{ mapping.match_count ? ago(mapping.last_matched_at) : "—" }}</TableCell>
|
||||
<TableCell>
|
||||
<div class="flex justify-end gap-2">
|
||||
<Button variant="outline" size="xs" @click="emit('edit', mapping)">Edit</Button>
|
||||
|
||||
@@ -40,8 +40,10 @@ import { listLoading, loadError, mappings, requestDelete, startEdit } from "./st
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Name</TableHead>
|
||||
<TableHead>Source hint</TableHead>
|
||||
<TableHead>Event type</TableHead>
|
||||
<TableHead>Fields</TableHead>
|
||||
<TableHead>Matched</TableHead>
|
||||
<TableHead>Last match</TableHead>
|
||||
<TableHead><span class="sr-only">Actions</span></TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
/** How many events a mapping has resolved, as the mappings list shows it. */
|
||||
export function matchCountLabel(mapping: { match_count?: number }): string {
|
||||
const count = mapping.match_count ?? 0;
|
||||
if (count === 0) return "Never matched";
|
||||
return `${count} ${count === 1 ? "event" : "events"}`;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { ref } from "vue";
|
||||
|
||||
import { api, classifyActionError, type ActionErrorKind } from "@/lib/api";
|
||||
import type { EventType } from "@/captures/event-types";
|
||||
import { uniqueFieldName, type FieldPick, type FieldType } from "./mapping-fields";
|
||||
|
||||
/**
|
||||
@@ -21,16 +22,18 @@ export interface MappingField {
|
||||
}
|
||||
|
||||
/**
|
||||
* A named, reusable set of bindings (mapping.Mapping). The source hint is
|
||||
* organisational only: a mapping is authored by clicking through one example
|
||||
* captured event but is not pinned to that event or its source, because
|
||||
* t2db.4's matcher decides which events a mapping applies to.
|
||||
* A named set of bound fields belonging to one event type (mapping.Mapping).
|
||||
* The daemon checks the fields against the type's schema on save, and counts
|
||||
* each event the mapping resolves.
|
||||
*/
|
||||
export interface Mapping {
|
||||
id: number;
|
||||
name: string;
|
||||
source_hint?: string;
|
||||
event_type_id?: string;
|
||||
fields?: MappingField[];
|
||||
match_count?: number;
|
||||
last_matched_at?: string;
|
||||
}
|
||||
|
||||
/** A captured inbound event. Its body is redacted before it is stored, and is
|
||||
@@ -63,6 +66,8 @@ export interface MappingDraft {
|
||||
id: number | null;
|
||||
name: string;
|
||||
sourceHint: string;
|
||||
/** The event type the mapping belongs to; its schema checks the fields. */
|
||||
eventTypeId: string;
|
||||
fields: MappingField[];
|
||||
/** The capture the payload and the preview resolve against, or null. */
|
||||
captureId: number | null;
|
||||
@@ -82,6 +87,7 @@ export interface ActionFailure {
|
||||
}
|
||||
|
||||
export const mappings = ref<Mapping[]>([]);
|
||||
export const eventTypes = ref<EventType[]>([]);
|
||||
export const captures = ref<Capture[]>([]);
|
||||
/** Whether this deployment has capture storage at all. GET /api/captures
|
||||
* answers {"enabled": false} rather than failing, which is what lets an empty
|
||||
@@ -112,6 +118,7 @@ function blankDraft(): MappingDraft {
|
||||
id: null,
|
||||
name: "",
|
||||
sourceHint: "",
|
||||
eventTypeId: "",
|
||||
fields: [],
|
||||
captureId: null,
|
||||
payload: null,
|
||||
@@ -126,11 +133,13 @@ export async function loadMappings(): Promise<void> {
|
||||
// Capture storage is optional infrastructure -- a deployment can serve
|
||||
// mappings without it -- so a failing capture leg degrades to an empty
|
||||
// picker instead of taking the mappings list down with it.
|
||||
const [listed, captured] = await Promise.all([
|
||||
const [listed, captured, types] = await Promise.all([
|
||||
api.mappings<{ mappings?: Mapping[] }>(),
|
||||
api.captures<{ captures?: Capture[]; enabled?: boolean }>(100).catch(() => null),
|
||||
api.eventTypes<{ event_types?: EventType[] }>().catch(() => null),
|
||||
]);
|
||||
mappings.value = listed.mappings || [];
|
||||
eventTypes.value = types?.event_types || [];
|
||||
captures.value = captured?.captures || [];
|
||||
capturesEnabled.value = captured?.enabled !== false;
|
||||
loadError.value = null;
|
||||
@@ -153,6 +162,7 @@ export function startEdit(mapping: Mapping): void {
|
||||
id: mapping.id,
|
||||
name: mapping.name,
|
||||
sourceHint: mapping.source_hint || "",
|
||||
eventTypeId: mapping.event_type_id || "",
|
||||
// Copied, not aliased: the editor must not write through to the loaded list.
|
||||
fields: (mapping.fields || []).map((field) => ({ ...field })),
|
||||
};
|
||||
@@ -229,7 +239,12 @@ export async function runPreview(): Promise<void> {
|
||||
export async function saveMapping(): Promise<void> {
|
||||
if (saving.value) return;
|
||||
const current = draft.value;
|
||||
const body = { name: current.name, source_hint: current.sourceHint, fields: current.fields };
|
||||
const body = {
|
||||
name: current.name,
|
||||
source_hint: current.sourceHint,
|
||||
event_type_id: current.eventTypeId,
|
||||
fields: current.fields,
|
||||
};
|
||||
saving.value = true;
|
||||
try {
|
||||
if (current.id !== null) await api.mappingUpdate(String(current.id), body);
|
||||
|
||||
Reference in new issue
Block a user