Skip to content

The only tool you need to know what is happening and how to fix it.

33.5 KB
Raw Blame
<script lang="ts">
	import * as AlertDialog from '$lib/components/ui/alert-dialog';
	import { Button } from '$lib/components/ui/button';
	import { Input } from '$lib/components/ui/input';
	import { Label } from '$lib/components/ui/label';
	import { ErrorAlert } from '$lib/components/ui/error-alert';
	import * as Select from '$lib/components/ui/select';
	import { Plus, Check, TriangleAlert } from '@lucide/svelte';
	import * as Alert from '$lib/components/ui/alert';
	import { toast } from 'svelte-sonner';
	import { api } from '$lib/api';
	import { projectsState, isFrontendFramework } from '$lib/state/projects.svelte';
	import { ruleTypeOptions } from './rule-types';
	import type { NotificationChannelConfig, NotificationRuleConfig } from '$lib/types/notifications';
	import { BURN_RATE_PRESETS } from '$lib/utils/slo-math';
	import { SLI_KIND_LABELS, type SLO } from '$lib/state/slos.svelte';
	import { WarningCallout } from '$lib/components/ui/warning-callout';
	import { resolve } from '$app/paths';

	interface NotificationChannel {
		id: number;
		projectId: string;
		name: string;
		channelType: string;
		config: NotificationChannelConfig;
		enabled: boolean;
		createdAt: string;
	}

	interface NotificationRule {
		id: number;
		projectId: string;
		channelId: number;
		name: string;
		ruleType: string;
		config: NotificationRuleConfig;
		enabled: boolean;
		cooldownMinutes: number;
		severity: string;
		snoozedUntil: string | null;
		channelName: string;
		channelType: string;
		createdAt: string;
	}

	interface Props {
		open: boolean;
		rule: NotificationRule | null;
		channels: NotificationChannel[];
		onSaved: () => void;
	}

	let { open = $bindable(), rule, channels, onSaved }: Props = $props();

	let name = $state('');
	let channelId = $state('');
	let ruleType = $state('new_error');
	let cooldownMinutes = $state(15);
	let severity = $state('');
	let loading = $state(false);
	let error = $state('');

	const severityOptions = [
		{ value: '', label: 'Auto (default)' },
		{ value: 'critical', label: 'Critical' },
		{ value: 'warning', label: 'Warning' },
		{ value: 'info', label: 'Info' }
	];

	let thresholdPercent = $state(5);
	let lookbackMinutes = $state(5);
	let minRequests = $state(10);
	let endpoint = $state('*');
	let thresholdMs = $state(500);
	let thresholdApdex = $state(0.85);
	let metricName = $state('');
	let operator = $state('gt');
	let thresholdValue = $state(90);
	let aggregation = $state('avg');
	let metricTags = $state('');
	let metricGroupBy = $state('');
	let dataType = $state('any');
	let silenceMinutes = $state(10);
	let thresholdCount = $state(100);
	let taskName = $state('*');
	let minExecutions = $state(5);
	let dropPercent = $state(50);
	let baselineWindowMinutes = $state(60);
	let ignorePatterns = $state('');
	let traceName = $state('*');
	let thresholdCost = $state(0.5);
	let conversationThresholdCost = $state(5);
	let flaggedTermsFilter = $state('');
	let sloId = $state('');
	let longWindowMinutes = $state(60);
	let shortWindowMinutes = $state(5);
	let burnRateThreshold = $state(14.4);
	let consumedPercent = $state(80);
	let slos = $state<SLO[]>([]);
	let slosLoaded = $state(false);
	let slosStatus = $state<'loading' | 'loaded' | 'error'>('loading');

	const isEditing = $derived(rule !== null);

	const isSloRule = $derived(ruleType === 'slo_burn_rate' || ruleType === 'slo_budget_consumed');
	const burnPreset = $derived(
		BURN_RATE_PRESETS.find(
			(p) =>
				p.longWindowMinutes === longWindowMinutes &&
				p.shortWindowMinutes === shortWindowMinutes &&
				p.burnRateThreshold === burnRateThreshold
		)?.id ?? 'custom'
	);

	const selectedSlo = $derived(slos.find((s) => String(s.id) === sloId));
	const sloSliceMinutes = $derived(selectedSlo?.bucketMinutes ?? 1);
	const roundsBurnWindows = $derived(
		sloSliceMinutes > 1 &&
			(shortWindowMinutes % sloSliceMinutes !== 0 || longWindowMinutes % sloSliceMinutes !== 0)
	);

	function roundUpToSlice(minutes: number): number {
		return Math.ceil(minutes / sloSliceMinutes) * sloSliceMinutes;
	}

	async function loadSlos() {
		slosStatus = 'loading';
		try {
			const response = await api.get('/slos', {
				projectId: projectsState.currentProjectId ?? undefined
			});
			slos = response.slos ?? [];
			slosStatus = 'loaded';
		} catch {
			slos = [];
			slosStatus = 'error';
		}
	}

	$effect(() => {
		if (open && isSloRule && !slosLoaded) {
			slosLoaded = true;
			loadSlos();
		}
		if (!open) slosLoaded = false;
	});

	function windowLabel(minutes: number): string {
		if (minutes >= 1440) return `${minutes / 1440}d`;
		if (minutes >= 60) return `${minutes / 60}h`;
		return `${minutes}m`;
	}

	function applyBurnPreset(id: string) {
		const preset = BURN_RATE_PRESETS.find((p) => p.id === id);
		if (!preset) return;
		longWindowMinutes = preset.longWindowMinutes;
		shortWindowMinutes = preset.shortWindowMinutes;
		burnRateThreshold = preset.burnRateThreshold;
	}

	const isFrontendProject = $derived(
		!!projectsState.currentProject && isFrontendFramework(projectsState.currentProject.framework)
	);

	const frontendRuleTypes = new Set(['new_error', 'slo_burn_rate', 'slo_budget_consumed']);
	const visibleRuleTypes = $derived(
		isFrontendProject
			? ruleTypeOptions.filter((o) => frontendRuleTypes.has(o.value))
			: ruleTypeOptions
	);

	$effect(() => {
		if (open && !visibleRuleTypes.some((o) => o.value === ruleType)) {
			ruleType = 'new_error';
		}
	});

	const ruleTypeDescriptions: Record<string, string> = {
		new_error:
			'Fires when a completely new error type is detected for the first time in your project.',
		impact_score_critical:
			'Fires when an endpoint\u2019s impact score reaches critical level (\u2265 0.75), indicating severe performance or reliability degradation.',
		impact_score_high:
			'Fires when an endpoint\u2019s impact score reaches high level (\u2265 0.50), indicating significant performance or reliability issues.',
		impact_score_medium:
			'Fires when an endpoint\u2019s impact score reaches medium level (\u2265 0.25), an early warning of emerging performance or reliability issues.',
		ai_trace_cost:
			'Fires when an individual AI trace exceeds a cost threshold. Monitors per-call spending across AI providers.',
		ai_conversation_cost:
			'Fires when the cumulative cost of a single AI conversation (over the last 24 hours) exceeds the threshold. Catches runaway agent loops and expensive sessions.',
		ai_flagged_content:
			'Fires when an AI conversation matches a flagged content term. Uses the built-in profanity list plus custom terms from the project’s AI settings; optionally narrow to specific terms below.',
		error_regression: 'Fires when an error that was previously archived (resolved) occurs again.',
		check_down:
			'Fires when a monitor transitions to down (after its failure threshold), and sends a recovery notice when it comes back up. Applies to every monitor in this project.',
		slo_burn_rate:
			'Fires when an SLO spends its error budget faster than the threshold over both the long and the short window (multi-window burn rate), and sends a recovery notice once the burn slows down.',
		slo_budget_consumed:
			"Fires when an SLO has used more than the configured share of its window's error budget, and sends a recovery notice when the budget recovers.",
		error_rate_threshold:
			'Fires when the percentage of requests returning 5xx across all endpoints exceeds the threshold within the lookback window.',
		error_count_threshold:
			'Fires when the total number of errors recorded within the lookback window reaches the threshold.',
		endpoint_p95_threshold:
			'Fires when the P95 response time of an endpoint (or all endpoints) exceeds the threshold within the lookback window.',
		endpoint_p99_threshold:
			'Fires when the P99 response time of an endpoint (or all endpoints) exceeds the threshold within the lookback window.',
		endpoint_error_rate:
			'Fires when the percentage of 5xx responses on a specific endpoint exceeds the threshold within the lookback window.',
		apdex_drop:
			'Fires when the Apdex satisfaction score drops below the threshold within the lookback window.',
		metric_threshold:
			'Fires when an aggregated metric value violates the configured comparison against the threshold within the lookback window.',
		no_data:
			'Fires when no telemetry of the selected kind (endpoints, exceptions, metrics or tasks) has been received for the configured number of minutes.',
		task_duration_threshold:
			'Fires when the P95 duration of a background task exceeds the threshold within the lookback window.',
		task_failure_rate:
			'Fires when the percentage of background task executions that recorded an error exceeds the threshold within the lookback window.',
		throughput_drop:
			'Fires when request throughput drops by more than the configured percentage compared to the preceding baseline window.'
	};

	const operatorOptions = [
		{ value: 'gt', label: '> Greater than' },
		{ value: 'gte', label: '>= Greater or equal' },
		{ value: 'lt', label: '< Less than' },
		{ value: 'lte', label: '<= Less or equal' },
		{ value: 'eq', label: '= Equal' }
	];

	const aggregationOptions = [
		{ value: 'avg', label: 'Average' },
		{ value: 'max', label: 'Maximum' },
		{ value: 'min', label: 'Minimum' },
		{ value: 'sum', label: 'Sum' },
		{ value: 'p95', label: 'P95' },
		{ value: 'p99', label: 'P99' },
		{ value: 'last', label: 'Last' }
	];

	const dataTypeOptions = [
		{ value: 'any', label: 'Any' },
		{ value: 'endpoints', label: 'Endpoints' },
		{ value: 'exceptions', label: 'Exceptions' },
		{ value: 'metrics', label: 'Metrics' },
		{ value: 'tasks', label: 'Tasks' }
	];

	function resetForm() {
		name = '';
		channelId = '';
		ruleType = 'new_error';
		cooldownMinutes = 15;
		severity = '';
		error = '';
		thresholdPercent = 5;
		lookbackMinutes = 5;
		minRequests = 10;
		endpoint = '*';
		thresholdMs = 500;
		thresholdApdex = 0.85;
		metricName = '';
		operator = 'gt';
		thresholdValue = 90;
		aggregation = 'avg';
		metricTags = '';
		metricGroupBy = '';
		dataType = 'any';
		silenceMinutes = 10;
		thresholdCount = 100;
		taskName = '*';
		minExecutions = 5;
		dropPercent = 50;
		baselineWindowMinutes = 60;
		ignorePatterns = '';
		traceName = '*';
		thresholdCost = 0.5;
		sloId = '';
		longWindowMinutes = 60;
		shortWindowMinutes = 5;
		burnRateThreshold = 14.4;
		consumedPercent = 80;
	}

	function populateFromRule(r: NotificationRule) {
		name = r.name;
		channelId = r.channelId.toString();
		ruleType = r.ruleType;
		cooldownMinutes = r.cooldownMinutes;
		severity = r.severity || '';
		const cfg = r.config || {};

		switch (r.ruleType) {
			case 'error_rate_threshold':
				thresholdPercent = cfg.thresholdPercent ?? 5;
				lookbackMinutes = cfg.lookbackMinutes ?? 5;
				minRequests = cfg.minRequests ?? 10;
				break;
			case 'endpoint_p95_threshold':
			case 'endpoint_p99_threshold':
				endpoint = cfg.endpoint ?? '*';
				thresholdMs = cfg.thresholdMs ?? 500;
				lookbackMinutes = cfg.lookbackMinutes ?? 5;
				break;
			case 'apdex_drop':
				thresholdApdex = cfg.thresholdApdex ?? 0.85;
				lookbackMinutes = cfg.lookbackMinutes ?? 15;
				minRequests = cfg.minRequests ?? 50;
				break;
			case 'metric_threshold':
				metricName = cfg.metricName ?? '';
				operator = cfg.operator ?? 'gt';
				thresholdValue = cfg.thresholdValue ?? 90;
				aggregation = cfg.aggregation ?? 'avg';
				metricTags = Object.entries(cfg.tags ?? {})
					.map(([k, v]) => `${k}=${v}`)
					.join(', ');
				metricGroupBy = (cfg.groupBy ?? []).join(', ');
				lookbackMinutes = cfg.lookbackMinutes ?? 5;
				break;
			case 'no_data':
				dataType = cfg.dataType ?? 'any';
				silenceMinutes = cfg.silenceMinutes ?? 10;
				break;
			case 'error_count_threshold':
				thresholdCount = cfg.thresholdCount ?? 100;
				lookbackMinutes = cfg.lookbackMinutes ?? 60;
				break;
			case 'task_duration_threshold':
				taskName = cfg.taskName ?? '*';
				thresholdMs = cfg.thresholdMs ?? 30000;
				lookbackMinutes = cfg.lookbackMinutes ?? 30;
				break;
			case 'task_failure_rate':
				taskName = cfg.taskName ?? '*';
				thresholdPercent = cfg.thresholdPercent ?? 10;
				lookbackMinutes = cfg.lookbackMinutes ?? 60;
				minExecutions = cfg.minExecutions ?? 5;
				break;
			case 'throughput_drop':
				dropPercent = cfg.dropPercent ?? 50;
				lookbackMinutes = cfg.lookbackMinutes ?? 15;
				baselineWindowMinutes = cfg.baselineWindowMinutes ?? 60;
				break;
			case 'endpoint_error_rate':
				endpoint = cfg.endpoint ?? '*';
				thresholdPercent = cfg.thresholdPercent ?? 2;
				lookbackMinutes = cfg.lookbackMinutes ?? 10;
				minRequests = cfg.minRequests ?? 20;
				break;
			case 'new_error':
				ignorePatterns = (cfg.ignorePatterns || []).join(', ');
				break;
			case 'impact_score_critical':
			case 'impact_score_high':
			case 'impact_score_medium':
				minRequests = cfg.minRequests ?? 50;
				break;
			case 'ai_trace_cost':
				traceName = cfg.traceName ?? '*';
				thresholdCost = cfg.thresholdCost ?? 0.5;
				break;
			case 'ai_conversation_cost':
				conversationThresholdCost = cfg.thresholdCost ?? 5;
				break;
			case 'ai_flagged_content':
				flaggedTermsFilter = (cfg.terms || []).join(', ');
				break;
			case 'slo_burn_rate':
				sloId = cfg.sloId ? String(cfg.sloId) : '';
				longWindowMinutes = cfg.longWindowMinutes ?? 60;
				shortWindowMinutes = cfg.shortWindowMinutes ?? 5;
				burnRateThreshold = cfg.burnRateThreshold ?? 14.4;
				break;
			case 'slo_budget_consumed':
				sloId = cfg.sloId ? String(cfg.sloId) : '';
				consumedPercent = cfg.consumedPercent ?? 80;
				break;
		}
	}

	function parseMetricTags(value: string): Record<string, string> {
		const tags: Record<string, string> = {};
		for (const part of value.split(',')) {
			if (!part.trim()) continue;
			const eq = part.indexOf('=');
			const key = (eq === -1 ? part : part.slice(0, eq)).trim();
			tags[key] = eq === -1 ? '' : part.slice(eq + 1).trim();
		}
		return tags;
	}

	function buildConfig(): NotificationRuleConfig {
		switch (ruleType) {
			case 'error_rate_threshold':
				return { thresholdPercent, lookbackMinutes, minRequests };
			case 'endpoint_p95_threshold':
			case 'endpoint_p99_threshold':
				return { endpoint, thresholdMs, lookbackMinutes };
			case 'apdex_drop':
				return { thresholdApdex, lookbackMinutes, minRequests };
			case 'metric_threshold':
				return {
					metricName,
					operator,
					thresholdValue,
					aggregation,
					lookbackMinutes,
					tags: parseMetricTags(metricTags),
					groupBy: metricGroupBy
						.split(',')
						.map((k) => k.trim())
						.filter(Boolean)
				};
			case 'no_data':
				return { dataType, silenceMinutes };
			case 'error_count_threshold':
				return { thresholdCount, lookbackMinutes };
			case 'task_duration_threshold':
				return { taskName, thresholdMs, lookbackMinutes };
			case 'task_failure_rate':
				return { taskName, thresholdPercent, lookbackMinutes, minExecutions };
			case 'throughput_drop':
				return { dropPercent, lookbackMinutes, baselineWindowMinutes };
			case 'endpoint_error_rate':
				return { endpoint, thresholdPercent, lookbackMinutes, minRequests };
			case 'new_error': {
				const patterns = ignorePatterns
					.split(',')
					.map((p) => p.trim())
					.filter((p) => p);
				return { ignorePatterns: patterns };
			}
			case 'impact_score_critical':
			case 'impact_score_high':
			case 'impact_score_medium':
				return { minRequests };
			case 'ai_trace_cost':
				return { traceName, thresholdCost };
			case 'ai_conversation_cost':
				return { thresholdCost: conversationThresholdCost };
			case 'ai_flagged_content': {
				const terms = flaggedTermsFilter
					.split(',')
					.map((t) => t.trim().toLowerCase())
					.filter((t) => t);
				return { terms };
			}
			case 'slo_burn_rate':
				return {
					sloId: parseInt(sloId) || 0,
					longWindowMinutes,
					shortWindowMinutes,
					burnRateThreshold
				};
			case 'slo_budget_consumed':
				return { sloId: parseInt(sloId) || 0, consumedPercent };
			default:
				return {};
		}
	}

	async function handleSubmit() {
		loading = true;
		error = '';

		try {
			const body = {
				channelId: parseInt(channelId),
				name,
				ruleType,
				config: buildConfig(),
				cooldownMinutes,
				severity
			};

			if (isEditing) {
				await api.put(`/notification-rules/${rule!.id}`, body, {
					projectId: projectsState.currentProjectId ?? undefined
				});
				toast.success('Successfully updated the Rule', { position: 'top-center' });
			} else {
				await api.post('/notification-rules', body, {
					projectId: projectsState.currentProjectId ?? undefined
				});
				toast.success('Successfully created the Rule', { position: 'top-center' });
			}
			onSaved();
		} catch (e: unknown) {
			error = e instanceof Error ? e.message : 'Failed to save rule';
		} finally {
			loading = false;
		}
	}

	function handleOpenChange(isOpen: boolean) {
		if (!isOpen) {
			resetForm();
		} else if (rule) {
			populateFromRule(rule);
		} else {
			resetForm();
		}
		open = isOpen;
	}

	$effect(() => {
		if (open && rule) {
			populateFromRule(rule);
		} else if (open && !rule) {
			resetForm();
		}
	});
</script>

<AlertDialog.Root {open} onOpenChange={handleOpenChange}>
	<AlertDialog.Content class="max-h-[85vh] max-w-lg overflow-y-auto">
		<AlertDialog.Header>
			<AlertDialog.Title>{isEditing ? 'Edit Rule' : 'New Rule'}</AlertDialog.Title>
			<AlertDialog.Description>
				{isEditing
					? 'Update the notification rule configuration'
					: 'Configure a new notification rule'}
			</AlertDialog.Description>
		</AlertDialog.Header>

		<form
			onsubmit={(e) => {
				e.preventDefault();
				handleSubmit();
			}}
			class="space-y-4"
		>
			<ErrorAlert {error} />

			<div class="space-y-2">
				<Label for="rule-name">Name</Label>
				<Input id="rule-name" bind:value={name} placeholder="e.g. High Error Rate" required />
			</div>

			<div class="space-y-2">
				<Label for="rule-channel">Channel</Label>
				<Select.Root type="single" bind:value={channelId}>
					<Select.Trigger class="w-full">
						{channels.find((c) => c.id.toString() === channelId)?.name || 'Select channel'}
					</Select.Trigger>
					<Select.Content>
						{#each channels as ch, __index (__index)}
							<Select.Item value={ch.id.toString()}>{ch.name}</Select.Item>
						{/each}
					</Select.Content>
				</Select.Root>
			</div>

			<div class="space-y-2">
				<Label for="rule-type">Rule Type</Label>
				<Select.Root type="single" bind:value={ruleType}>
					<Select.Trigger class="w-full">
						{ruleTypeOptions.find((o) => o.value === ruleType)?.label || 'Select type'}
					</Select.Trigger>
					<Select.Content>
						{#each visibleRuleTypes as option, __index (__index)}
							<Select.Item value={option.value}>{option.label}</Select.Item>
						{/each}
					</Select.Content>
				</Select.Root>
			</div>

			{#if ruleTypeDescriptions[ruleType]}
				<Alert.Root
					class="border-amber-200 bg-amber-50 text-amber-900 dark:border-amber-800 dark:bg-amber-950/50 dark:text-amber-200 "
				>
					<TriangleAlert class="text-amber-600 dark:text-amber-400" />
					<Alert.Description class="text-amber-800 dark:text-amber-300"
						>{ruleTypeDescriptions[ruleType]}</Alert.Description
					>
				</Alert.Root>
			{/if}

			<div class="space-y-3 rounded-md border p-3">
				<p class="text-sm font-medium text-muted-foreground">Rule Configuration</p>

				{#if ruleType === 'error_rate_threshold'}
					<div class="space-y-2">
						<Label for="cfg-threshold">Threshold (%)</Label>
						<Input
							id="cfg-threshold"
							type="number"
							bind:value={thresholdPercent}
							step="0.1"
							min="0"
						/>
					</div>
					<div class="space-y-2">
						<Label for="cfg-lookback">Lookback (minutes)</Label>
						<Input id="cfg-lookback" type="number" bind:value={lookbackMinutes} min="1" />
					</div>
					<div class="space-y-2">
						<Label for="cfg-min-req">Min Requests</Label>
						<Input id="cfg-min-req" type="number" bind:value={minRequests} min="1" />
					</div>
				{:else if ruleType === 'endpoint_p95_threshold' || ruleType === 'endpoint_p99_threshold'}
					<div class="space-y-2">
						<Label for="cfg-endpoint">Endpoint (* for all)</Label>
						<Input id="cfg-endpoint" bind:value={endpoint} placeholder="GET /api/users" />
					</div>
					<div class="space-y-2">
						<Label for="cfg-threshold-ms">Threshold (ms)</Label>
						<Input id="cfg-threshold-ms" type="number" bind:value={thresholdMs} min="1" />
					</div>
					<div class="space-y-2">
						<Label for="cfg-lookback-p">Lookback (minutes)</Label>
						<Input id="cfg-lookback-p" type="number" bind:value={lookbackMinutes} min="1" />
					</div>
				{:else if ruleType === 'apdex_drop'}
					<div class="space-y-2">
						<Label for="cfg-apdex">Apdex Threshold</Label>
						<Input
							id="cfg-apdex"
							type="number"
							bind:value={thresholdApdex}
							step="0.01"
							min="0"
							max="1"
						/>
					</div>
					<div class="space-y-2">
						<Label for="cfg-lookback-a">Lookback (minutes)</Label>
						<Input id="cfg-lookback-a" type="number" bind:value={lookbackMinutes} min="1" />
					</div>
					<div class="space-y-2">
						<Label for="cfg-min-req-a">Min Requests</Label>
						<Input id="cfg-min-req-a" type="number" bind:value={minRequests} min="1" />
					</div>
				{:else if ruleType === 'metric_threshold'}
					<div class="space-y-2">
						<Label for="cfg-metric">Metric Name</Label>
						<Input id="cfg-metric" bind:value={metricName} placeholder="cpu.used_pcnt" />
					</div>
					<div class="space-y-2">
						<Label for="cfg-operator">Operator</Label>
						<Select.Root type="single" bind:value={operator}>
							<Select.Trigger class="w-full">
								{operatorOptions.find((o) => o.value === operator)?.label || operator}
							</Select.Trigger>
							<Select.Content>
								{#each operatorOptions as option, __index (__index)}
									<Select.Item value={option.value}>{option.label}</Select.Item>
								{/each}
							</Select.Content>
						</Select.Root>
					</div>
					<div class="space-y-2">
						<Label for="cfg-value">Threshold Value</Label>
						<Input id="cfg-value" type="number" bind:value={thresholdValue} step="0.1" />
					</div>
					<div class="space-y-2">
						<Label for="cfg-agg">Aggregation</Label>
						<Select.Root type="single" bind:value={aggregation}>
							<Select.Trigger class="w-full">
								{aggregationOptions.find((o) => o.value === aggregation)?.label || aggregation}
							</Select.Trigger>
							<Select.Content>
								{#each aggregationOptions as option, __index (__index)}
									<Select.Item value={option.value}>{option.label}</Select.Item>
								{/each}
							</Select.Content>
						</Select.Root>
					</div>
					<div class="space-y-2">
						<Label for="cfg-lookback-m">Lookback (minutes)</Label>
						<Input id="cfg-lookback-m" type="number" bind:value={lookbackMinutes} min="1" />
					</div>
					<div class="space-y-2">
						<Label for="cfg-metric-tags">Tag Filters (optional, comma-separated)</Label>
						<Input
							id="cfg-metric-tags"
							bind:value={metricTags}
							placeholder="outcome=failed, host.name=web-1"
						/>
					</div>
					<div class="space-y-2">
						<Label for="cfg-metric-group-by">Group By Tags (optional, comma-separated)</Label>
						<Input id="cfg-metric-group-by" bind:value={metricGroupBy} placeholder="host.name" />
						<p class="text-xs text-muted-foreground">
							Evaluates every series separately and names the breaching one in the alert.
						</p>
					</div>
				{:else if ruleType === 'no_data'}
					<div class="space-y-2">
						<Label for="cfg-data-type">Data Type</Label>
						<Select.Root type="single" bind:value={dataType}>
							<Select.Trigger class="w-full">
								{dataTypeOptions.find((o) => o.value === dataType)?.label || dataType}
							</Select.Trigger>
							<Select.Content>
								{#each dataTypeOptions as option, __index (__index)}
									<Select.Item value={option.value}>{option.label}</Select.Item>
								{/each}
							</Select.Content>
						</Select.Root>
					</div>
					<div class="space-y-2">
						<Label for="cfg-silence">Silence (minutes)</Label>
						<Input id="cfg-silence" type="number" bind:value={silenceMinutes} min="1" />
					</div>
				{:else if ruleType === 'error_count_threshold'}
					<div class="space-y-2">
						<Label for="cfg-count">Threshold Count</Label>
						<Input id="cfg-count" type="number" bind:value={thresholdCount} min="1" />
					</div>
					<div class="space-y-2">
						<Label for="cfg-lookback-ec">Lookback (minutes)</Label>
						<Input id="cfg-lookback-ec" type="number" bind:value={lookbackMinutes} min="1" />
					</div>
				{:else if ruleType === 'task_duration_threshold'}
					<div class="space-y-2">
						<Label for="cfg-task">Task Name (* for all)</Label>
						<Input id="cfg-task" bind:value={taskName} placeholder="sync_users" />
					</div>
					<div class="space-y-2">
						<Label for="cfg-threshold-ms-t">Threshold (ms)</Label>
						<Input id="cfg-threshold-ms-t" type="number" bind:value={thresholdMs} min="1" />
					</div>
					<div class="space-y-2">
						<Label for="cfg-lookback-td">Lookback (minutes)</Label>
						<Input id="cfg-lookback-td" type="number" bind:value={lookbackMinutes} min="1" />
					</div>
				{:else if ruleType === 'slo_burn_rate' || ruleType === 'slo_budget_consumed'}
					<div class="space-y-2">
						<Label>SLO</Label>
						<Select.Root type="single" bind:value={sloId}>
							<Select.Trigger class="w-full">
								{slos.find((s) => String(s.id) === sloId)?.name || 'Select an SLO'}
							</Select.Trigger>
							<Select.Content class="max-h-64 overflow-y-auto">
								{#each slos as s (s.id)}
									<Select.Item value={String(s.id)} label={s.name}>
										{s.name}
										<span class="ml-2 text-xs text-muted-foreground">
											{SLI_KIND_LABELS[s.sliKind]} · {s.targetPercent}% / {s.windowDays}d
										</span>
									</Select.Item>
								{/each}
							</Select.Content>
						</Select.Root>
						{#if slosStatus === 'error'}
							<p class="text-xs text-destructive">
								The SLOs could not be loaded.
								<button
									type="button"
									class="underline underline-offset-2"
									onclick={() => loadSlos()}
								>
									Retry
								</button>
							</p>
						{:else if slosStatus === 'loaded' && slos.length === 0}
							<p class="text-xs text-muted-foreground">
								No SLOs in this project yet. Create one on the
								<a class="text-primary underline-offset-2 hover:underline" href={resolve('/slos')}
									>SLOs page</a
								>.
							</p>
						{/if}
					</div>
					{#if ruleType === 'slo_burn_rate'}
						<div class="space-y-2">
							<Label>Preset</Label>
							<div class="flex flex-wrap gap-2">
								{#each BURN_RATE_PRESETS as preset (preset.id)}
									<Button
										type="button"
										size="sm"
										variant={burnPreset === preset.id ? 'secondary' : 'outline'}
										onclick={() => applyBurnPreset(preset.id)}
									>
										{preset.label}
										{preset.burnRateThreshold}x · {windowLabel(preset.longWindowMinutes)} / {windowLabel(
											preset.shortWindowMinutes
										)}
									</Button>
								{/each}
							</div>
						</div>
						<div class="space-y-2">
							<Label for="cfg-burn-long">Long window (minutes)</Label>
							<Input id="cfg-burn-long" type="number" bind:value={longWindowMinutes} min="5" />
						</div>
						<div class="space-y-2">
							<Label for="cfg-burn-short">Short window (minutes)</Label>
							<Input id="cfg-burn-short" type="number" bind:value={shortWindowMinutes} min="1" />
						</div>
						<div class="space-y-2">
							<Label for="cfg-burn-threshold">Burn rate threshold (x)</Label>
							<Input
								id="cfg-burn-threshold"
								type="number"
								bind:value={burnRateThreshold}
								step="0.1"
								min="0.1"
							/>
							<p class="text-xs text-muted-foreground">
								Fires when both windows burn at or above this multiple of the rate that spends the
								budget exactly over the SLO window.
							</p>
						</div>
						{#if roundsBurnWindows}
							<WarningCallout title="Windows are rounded up to whole slices">
								This SLO is measured in {sloSliceMinutes}-minute slices, so the rule reads the last
								{windowLabel(roundUpToSlice(shortWindowMinutes))} and {windowLabel(
									roundUpToSlice(longWindowMinutes)
								)}.
							</WarningCallout>
						{/if}
					{:else}
						<div class="space-y-2">
							<Label for="cfg-budget-consumed">Budget consumed (%)</Label>
							<Input
								id="cfg-budget-consumed"
								type="number"
								bind:value={consumedPercent}
								min="1"
								max="100"
							/>
						</div>
					{/if}
				{:else if ruleType === 'task_failure_rate'}
					<div class="space-y-2">
						<Label for="cfg-task-f">Task Name (* for all)</Label>
						<Input id="cfg-task-f" bind:value={taskName} placeholder="sync_users" />
					</div>
					<div class="space-y-2">
						<Label for="cfg-threshold-pct">Threshold (%)</Label>
						<Input
							id="cfg-threshold-pct"
							type="number"
							bind:value={thresholdPercent}
							step="0.1"
							min="0"
						/>
					</div>
					<div class="space-y-2">
						<Label for="cfg-lookback-tf">Lookback (minutes)</Label>
						<Input id="cfg-lookback-tf" type="number" bind:value={lookbackMinutes} min="1" />
					</div>
					<div class="space-y-2">
						<Label for="cfg-min-exec">Min Executions</Label>
						<Input id="cfg-min-exec" type="number" bind:value={minExecutions} min="1" />
					</div>
				{:else if ruleType === 'throughput_drop'}
					<div class="space-y-2">
						<Label for="cfg-drop">Drop (%)</Label>
						<Input id="cfg-drop" type="number" bind:value={dropPercent} step="0.1" min="0" />
					</div>
					<div class="space-y-2">
						<Label for="cfg-lookback-tp">Lookback (minutes)</Label>
						<Input id="cfg-lookback-tp" type="number" bind:value={lookbackMinutes} min="1" />
					</div>
					<div class="space-y-2">
						<Label for="cfg-baseline">Baseline Window (minutes)</Label>
						<Input id="cfg-baseline" type="number" bind:value={baselineWindowMinutes} min="1" />
					</div>
				{:else if ruleType === 'endpoint_error_rate'}
					<div class="space-y-2">
						<Label for="cfg-endpoint-er">Endpoint</Label>
						<Input id="cfg-endpoint-er" bind:value={endpoint} placeholder="POST /api/checkout" />
					</div>
					<div class="space-y-2">
						<Label for="cfg-threshold-er">Threshold (%)</Label>
						<Input
							id="cfg-threshold-er"
							type="number"
							bind:value={thresholdPercent}
							step="0.1"
							min="0"
						/>
					</div>
					<div class="space-y-2">
						<Label for="cfg-lookback-er">Lookback (minutes)</Label>
						<Input id="cfg-lookback-er" type="number" bind:value={lookbackMinutes} min="1" />
					</div>
					<div class="space-y-2">
						<Label for="cfg-min-req-er">Min Requests</Label>
						<Input id="cfg-min-req-er" type="number" bind:value={minRequests} min="1" />
					</div>
				{:else if ruleType === 'new_error'}
					<div class="space-y-2">
						<Label for="cfg-ignore">Ignore Patterns (optional, comma-separated)</Label>
						<Input
							id="cfg-ignore"
							bind:value={ignorePatterns}
							placeholder="*timeout*, *context canceled*"
						/>
					</div>
				{:else if ruleType === 'impact_score_critical' || ruleType === 'impact_score_high' || ruleType === 'impact_score_medium'}
					<div class="space-y-2">
						<Label for="cfg-min-req-impact">Min Requests</Label>
						<Input id="cfg-min-req-impact" type="number" bind:value={minRequests} min="1" />
					</div>
				{:else if ruleType === 'ai_trace_cost'}
					<div class="space-y-2">
						<Label for="cfg-trace-name">Trace Name (* for all)</Label>
						<Input
							id="cfg-trace-name"
							bind:value={traceName}
							placeholder="Customer Support Agent"
						/>
					</div>
					<div class="space-y-2">
						<Label for="cfg-threshold-cost">Cost Threshold ($)</Label>
						<Input
							id="cfg-threshold-cost"
							type="number"
							bind:value={thresholdCost}
							step="0.01"
							min="0"
						/>
					</div>
				{:else if ruleType === 'ai_conversation_cost'}
					<div class="space-y-2">
						<Label for="cfg-conv-threshold-cost">Cost Threshold ($)</Label>
						<Input
							id="cfg-conv-threshold-cost"
							type="number"
							bind:value={conversationThresholdCost}
							step="0.01"
							min="0"
						/>
					</div>
				{:else if ruleType === 'ai_flagged_content'}
					<div class="space-y-2">
						<Label for="cfg-flagged-terms">Only These Terms (optional, comma-separated)</Label>
						<Input
							id="cfg-flagged-terms"
							bind:value={flaggedTermsFilter}
							placeholder="Leave empty to fire on any flagged conversation"
						/>
						<p class="text-xs text-muted-foreground">
							Custom flagged terms are managed in the project settings AI tab.
						</p>
					</div>
				{/if}
			</div>

			<div class="space-y-2">
				<Label for="rule-cooldown">Cooldown (minutes)</Label>
				<Input id="rule-cooldown" type="number" bind:value={cooldownMinutes} min="1" />
			</div>

			<div class="space-y-2">
				<Label for="rule-severity">Severity</Label>
				<Select.Root type="single" bind:value={severity}>
					<Select.Trigger class="w-full">
						{severityOptions.find((o) => o.value === severity)?.label || 'Auto (default)'}
					</Select.Trigger>
					<Select.Content>
						{#each severityOptions as option, __index (__index)}
							<Select.Item value={option.value}>{option.label}</Select.Item>
						{/each}
					</Select.Content>
				</Select.Root>
			</div>
		</form>

		<AlertDialog.Footer>
			<AlertDialog.Cancel disabled={loading}>Cancel</AlertDialog.Cancel>
			<Button variant={isEditing ? 'default' : 'success'} onclick={handleSubmit} disabled={loading}>
				{#if isEditing}
					<Check class="mr-2 h-4 w-4" />
					{#if loading}
						Updating...
					{:else}
						Update Rule
					{/if}
				{:else}
					<Plus class="mr-2 h-4 w-4" />
					{#if loading}
						Creating...
					{:else}
						New Rule
					{/if}
				{/if}
			</Button>
		</AlertDialog.Footer>
	</AlertDialog.Content>
</AlertDialog.Root>