Skip to content

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

9.9 KB
Raw Blame
<script lang="ts">
	import { untrack } from 'svelte';
	import { goto } from '$app/navigation';
	import { api } from '$lib/api';
	import { formatDuration, formatDateTime } from '$lib/utils/formatters';
	import { getTimezone } from '$lib/state/timezone.svelte';
	import { Button } from '$lib/components/ui/button';
	import * as Card from '$lib/components/ui/card';
	import * as Table from '$lib/components/ui/table';
	import { LoadingCircle } from '$lib/components/ui/loading-circle';
	import { ErrorDisplay } from '$lib/components/ui/error-display';
	import { projectsState } from '$lib/state/projects.svelte';
	import { ArrowRight, TriangleAlert, ClipboardList } from '@lucide/svelte';
	import { LabelValue } from '$lib/components/ui/label-value';
	import { AttributesGrid } from '$lib/components/ui/attributes-grid/index.js';
	import SpanWaterfall from '$lib/components/spans/span-waterfall.svelte';
	import SpanGraphNotice from '$lib/components/spans/span-graph-notice.svelte';
	import SpanEmptyState from '$lib/components/spans/span-empty-state.svelte';
	import PageHeader from '$lib/components/traceway/page-header.svelte';
	import TableContainer from '$lib/components/traceway/table-container.svelte';
	import { createRowClickHandler } from '$lib/utils/navigation';
	import { createSmartBackHandler } from '$lib/utils/back-navigation';
	import { resolve } from '$app/paths';
	import DistributedTraceCard from '$lib/components/distributed-trace/distributed-trace-card.svelte';
	import TraceLogsPanel from '$lib/components/trace-logs/trace-logs-panel.svelte';
	import type { Span, SpanGraphStatus, TraceIdentity } from '$lib/types/spans';

	type TaskDetailResponse = {
		spanGraphStatus?: SpanGraphStatus;
		task: TraceIdentity & {
			id: string;
			taskName: string;
			duration: number;
			recordedAt: string;
			clientIP: string;
			attributes: Record<string, string> | null;
			serverName: string;
			appVersion: string;
		};
		exception?: {
			exceptionHash: string;
			stackTrace: string;
		};
		messages: {
			id: string;
			exceptionHash: string;
			stackTrace: string;
			recordedAt: string;
			attributes?: Record<string, string>;
		}[];
		spans: Span[];
		hasSpans: boolean;
	};

	let { data } = $props();

	const timezone = $derived(getTimezone());

	// Raw on purpose: the response can carry 20,000 spans and is only ever replaced, never edited.
	let response = $state.raw<TaskDetailResponse | null>(null);
	let loading = $state(true);
	let error = $state('');
	let notFound = $state(false);

	let loadSequence = 0;

	async function loadData() {
		const sequence = ++loadSequence;
		loading = true;
		error = '';
		notFound = false;

		try {
			const result = await api.post(
				`/tasks/${data.taskId}`,
				data.recordedAt ? { recordedAt: data.recordedAt } : {},
				{ projectId: projectsState.currentProjectId ?? undefined }
			);
			if (sequence !== loadSequence) return;
			response = result;
		} catch (e: unknown) {
			if (sequence !== loadSequence) return;
			console.error(e);
			const err = e as { status?: number; message?: string };
			if (err.status === 404) {
				notFound = true;
			} else {
				error = err.message || 'Failed to load task details';
			}
		} finally {
			if (sequence === loadSequence) loading = false;
		}
	}

	$effect(() => {
		void data.taskId;
		void data.recordedAt;
		void projectsState.currentProjectId;
		untrack(() => {
			response = null;
			loadData();
		});
		return () => {
			loadSequence++;
		};
	});
</script>

<div class="space-y-6">
	<PageHeader
		title={decodeURIComponent(data.task)}
		subtitle={`Task ID: ${data.taskId}`}
		onBack={createSmartBackHandler({
			fallbackPath: resolve('/tasks/[task]', { task: encodeURIComponent(data.task) })
		})}
	/>

	{#if loading}
		<div class="flex items-center justify-center py-20">
			<LoadingCircle size="xlg" />
		</div>
	{:else if notFound}
		<ErrorDisplay
			status={404}
			title="Task Not Found"
			description="The task instance you're looking for doesn't exist or may have expired."
			onBack={createSmartBackHandler({
				fallbackPath: resolve('/tasks/[task]', { task: encodeURIComponent(data.task) })
			})}
			backLabel="Back to Task"
			onRetry={loadData}
			identifier={data.taskId}
		/>
	{:else if error}
		<ErrorDisplay
			status={400}
			title="Failed to Load Task"
			description={error}
			onBack={createSmartBackHandler({
				fallbackPath: resolve('/tasks/[task]', { task: encodeURIComponent(data.task) })
			})}
			backLabel="Back to Task"
			onRetry={loadData}
		/>
	{:else if response}
		<!-- Task Details Card -->
		<Card.Root>
			<Card.Header>
				<Card.Title>Task Details</Card.Title>
				<Card.Description>Details of this specific task execution</Card.Description>
			</Card.Header>
			<Card.Content class="space-y-6">
				<div class="grid grid-cols-2 gap-4 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-5">
					<LabelValue label="Task" value={decodeURIComponent(data.task)} mono />
					<LabelValue label="Duration" value={formatDuration(response.task.duration)} mono large />
					<LabelValue
						label="Recorded At"
						value={formatDateTime(response.task.recordedAt, { timezone })}
						mono
					/>
					<LabelValue label="Server" value={response.task.serverName} mono />
					<LabelValue label="Version" value={response.task.appVersion || '-'} mono />
				</div>

				{#if response.task.attributes && Object.keys(response.task.attributes).length > 0}
					<hr class="border-border" />
					<div>
						<p class="mb-3 text-sm font-medium">Attributes</p>
						<AttributesGrid attributes={response.task.attributes} />
					</div>
				{/if}
			</Card.Content>
		</Card.Root>

		<!-- Exception Card (if exception exists) -->
		{#if response.exception}
			<Card.Root class="border-red-500/30 bg-red-500/5">
				<Card.Header>
					<div class="flex items-center gap-2">
						<TriangleAlert class="h-5 w-5 text-red-500" />
						<Card.Title class="text-red-600 dark:text-red-400">Exception Occurred</Card.Title>
					</div>
					<Card.Description>This task execution resulted in an exception</Card.Description>
				</Card.Header>
				<Card.Content>
					<div class="mb-4 max-h-32 overflow-x-auto rounded-md bg-muted p-3">
						<pre class="font-mono text-sm whitespace-pre-wrap">{response.exception.stackTrace
								.split('\n')
								.slice(0, 4)
								.join('\n')}{response.exception.stackTrace.split('\n').length > 4
								? '\n...'
								: ''}</pre>
					</div>
					<Button
						variant="outline"
						size="sm"
						onclick={() => goto(resolve(`/issues/${response!.exception!.exceptionHash}`))}
					>
						View Full Exception
						<ArrowRight class="ml-2 h-4 w-4" />
					</Button>
				</Card.Content>
			</Card.Root>
		{/if}

		<!-- Messages Section (if messages exist) -->
		{#if response.messages.length > 0}
			<Card.Root>
				<Card.Header>
					<div class="flex items-center gap-2">
						<ClipboardList class="h-5 w-5 text-muted-foreground" />
						<Card.Title>Messages</Card.Title>
					</div>
					<Card.Description>
						{response.messages.length} message{response.messages.length === 1 ? '' : 's'} logged during
						this task
					</Card.Description>
				</Card.Header>
				<Card.Content class="px-0 pb-0">
					<TableContainer class="rounded-none border-0">
						<Table.Root>
							<Table.Header>
								<Table.Row>
									<Table.Head class="pl-6">Message</Table.Head>
									<Table.Head class="w-[180px]">Recorded At</Table.Head>
									<Table.Head class="w-[100px] pr-6">Attributes</Table.Head>
								</Table.Row>
							</Table.Header>
							<Table.Body>
								{#each response.messages as message, __index (__index)}
									<Table.Row
										class="cursor-pointer"
										onclick={createRowClickHandler(
											`/issues/${message.exceptionHash}/${message.id}`
										)}
									>
										<Table.Cell class="pl-6">
											<div class="max-w-md truncate font-mono text-sm">
												{message.stackTrace.split('\n')[0]}
											</div>
										</Table.Cell>
										<Table.Cell class="font-mono text-sm text-muted-foreground">
											{formatDateTime(message.recordedAt, { timezone })}
										</Table.Cell>
										<Table.Cell class="pr-6">
											{#if message.attributes && Object.keys(message.attributes).length > 0}
												<span class="text-xs text-muted-foreground">
													{Object.keys(message.attributes).length} key{Object.keys(
														message.attributes
													).length === 1
														? ''
														: 's'}
												</span>
											{:else}
												<span class="text-xs text-muted-foreground">-</span>
											{/if}
										</Table.Cell>
									</Table.Row>
								{/each}
							</Table.Body>
						</Table.Root>
					</TableContainer>
				</Card.Content>
			</Card.Root>
		{/if}

		<!-- Spans Section -->
		<Card.Root>
			<Card.Header>
				<Card.Title>Spans</Card.Title>
				<Card.Description>
					{#if response.hasSpans}
						Timing breakdown of operations within this task
					{:else}
						No spans recorded for this task
					{/if}
				</Card.Description>
			</Card.Header>
			<Card.Content class="space-y-3">
				<SpanGraphNotice status={response.spanGraphStatus} />
				{#if response.hasSpans}
					<SpanWaterfall
						spans={response.spans}
						traceDuration={response.task.duration}
						traceStartTime={response.task.recordedAt}
						traceId={response.task.traceId}
						rootSpanId={response.task.spanId}
					/>
				{:else if !response.spanGraphStatus || response.spanGraphStatus.state === 'complete'}
					<SpanEmptyState framework={projectsState.currentProject?.framework ?? 'gin'} />
				{/if}
			</Card.Content>
		</Card.Root>

		<TraceLogsPanel
			projectId={projectsState.currentProjectId ?? ''}
			traceId={response.task.traceId}
			spans={response.spans ?? []}
			rootSpan={{ spanId: response.task.spanId, name: response.task.taskName }}
			traceRecordedAt={response.task.recordedAt}
		/>

		{#if response.task.traceId}
			<DistributedTraceCard
				traceId={response.task.traceId}
				currentNodeId={response.task.id}
				recordedAt={response.task.recordedAt}
			/>
		{/if}
	{/if}
</div>