|
| 1 | +<script lang="ts"> |
| 2 | + import { formatDateTime, formatServerTime } from 'common-lib/format' |
| 3 | + import Dayjs from 'dayjs' |
| 4 | + import { fade, slide } from 'svelte/transition' |
| 5 | + import type { |
| 6 | + ConnectorError, |
| 7 | + ConnectorErrorFilter, |
| 8 | + ConnectorErrorKind, |
| 9 | + InputEndpointStatus, |
| 10 | + OutputEndpointStatus |
| 11 | + } from './connectorMetrics' |
| 12 | + import { getCaseDependentName } from './felderaRelation' |
| 13 | + import InlineDropdown from './InlineDropdown.svelte' |
| 14 | + import SegmentedControl from './SegmentedControl.svelte' |
| 15 | + import Tooltip from './Tooltip.svelte' |
| 16 | +
|
| 17 | + const inputFilterOptions = ['all', 'parse', 'transport'] as const |
| 18 | + const outputFilterOptions = ['all', 'encode', 'transport'] as const |
| 19 | +
|
| 20 | + let filterOptionLabels: Record<string, string> = { |
| 21 | + transport: 'Transport', |
| 22 | + parse: 'Parse', |
| 23 | + encode: 'Encode', |
| 24 | + all: 'All' |
| 25 | + } |
| 26 | +
|
| 27 | + let tagsFilter: ConnectorErrorFilter = $state('all') |
| 28 | +
|
| 29 | + let { |
| 30 | + relationName, |
| 31 | + connectorName, |
| 32 | + direction, |
| 33 | + filter = 'all', |
| 34 | + status, |
| 35 | + loading = false, |
| 36 | + onClose |
| 37 | + }: { |
| 38 | + relationName: string |
| 39 | + /** Endpoint name, prefixed with the relation name. */ |
| 40 | + connectorName: string |
| 41 | + direction: 'input' | 'output' |
| 42 | + filter?: ConnectorErrorFilter |
| 43 | + /** The connector status with its error lists. `null` while it is not available. */ |
| 44 | + status: InputEndpointStatus | OutputEndpointStatus | null |
| 45 | + loading?: boolean |
| 46 | + onClose: () => void |
| 47 | + } = $props() |
| 48 | +
|
| 49 | + const filterOptions = $derived(direction === 'input' ? inputFilterOptions : outputFilterOptions) |
| 50 | + const filterItems = $derived( |
| 51 | + filterOptions.map((value) => ({ value, label: filterOptionLabels[value] ?? value })) |
| 52 | + ) |
| 53 | +
|
| 54 | + $effect(() => { |
| 55 | + tagsFilter = filter |
| 56 | + }) |
| 57 | +
|
| 58 | + const strippedConnectorName = $derived( |
| 59 | + connectorName.slice(getCaseDependentName(relationName).name.length + 1) |
| 60 | + ) |
| 61 | +
|
| 62 | + /** One row of the error list: an error, or a gap. A gap stands for `count` errors that are |
| 63 | + * missing between two listed errors, by `ConnectorError.index`, e.g. older errors that the |
| 64 | + * pipeline dropped because it keeps only the most recent ones. */ |
| 65 | + type DisplayItem = { kind: 'error'; error: ConnectorError } | { kind: 'gap'; count: number } |
| 66 | +
|
| 67 | + function withGaps(errors: ConnectorError[]): DisplayItem[] { |
| 68 | + const byTag = new Map<string, ConnectorError[]>() |
| 69 | + for (const e of errors) { |
| 70 | + const k = e.tag ?? 'other' |
| 71 | + const existing = byTag.get(k) |
| 72 | + if (existing) { |
| 73 | + existing.push(e) |
| 74 | + } else { |
| 75 | + byTag.set(k, [e]) |
| 76 | + } |
| 77 | + } |
| 78 | + const result: DisplayItem[] = [] |
| 79 | + for (const tagErrors of byTag.values()) { |
| 80 | + const sorted = [...tagErrors].sort((a, b) => a.index - b.index) |
| 81 | + for (let i = 0; i < sorted.length; i++) { |
| 82 | + if (i > 0) { |
| 83 | + const gap = sorted[i].index - sorted[i - 1].index - 1 |
| 84 | + if (gap > 0) { |
| 85 | + result.push({ kind: 'gap', count: gap }) |
| 86 | + } |
| 87 | + } |
| 88 | + result.push({ kind: 'error', error: sorted[i] }) |
| 89 | + } |
| 90 | + } |
| 91 | + return result |
| 92 | + } |
| 93 | +
|
| 94 | + /** Returns the error lists of the connector by error kind, e.g. `{ parse: [...], transport: |
| 95 | + * [...] }` for an input connector. If no errors for an error kind are there the field remains |
| 96 | + * with an empty array. */ |
| 97 | + function getErrorsByKind( |
| 98 | + s: InputEndpointStatus | OutputEndpointStatus |
| 99 | + ): Partial<Record<ConnectorErrorKind, ConnectorError[]>> { |
| 100 | + if (direction === 'input') { |
| 101 | + return { |
| 102 | + parse: (s as InputEndpointStatus).parse_errors ?? [], |
| 103 | + transport: s.transport_errors ?? [] |
| 104 | + } |
| 105 | + } |
| 106 | + return { |
| 107 | + encode: (s as OutputEndpointStatus).encode_errors ?? [], |
| 108 | + transport: s.transport_errors ?? [] |
| 109 | + } |
| 110 | + } |
| 111 | +
|
| 112 | + const displayItems = $derived.by(() => { |
| 113 | + if (!status) { |
| 114 | + return [] as DisplayItem[] |
| 115 | + } |
| 116 | + const errorsByKind = getErrorsByKind(status) |
| 117 | + const combined = |
| 118 | + tagsFilter === 'all' ? Object.values(errorsByKind).flat() : (errorsByKind[tagsFilter] ?? []) |
| 119 | + return withGaps(combined).reverse() |
| 120 | + }) |
| 121 | +</script> |
| 122 | + |
| 123 | +<div class="bg-white-dark flex h-full flex-col gap-2 rounded px-4 pt-4"> |
| 124 | + <div class="flex items-start justify-between"> |
| 125 | + <div> |
| 126 | + <div class="font-medium">{relationName} · {strippedConnectorName}</div> |
| 127 | + </div> |
| 128 | + <button class="fd fd-x text-[20px]" onclick={onClose} aria-label="Close"></button> |
| 129 | + </div> |
| 130 | + <div class="flex flex-nowrap items-center gap-2"> |
| 131 | + <SegmentedControl |
| 132 | + value={tagsFilter} |
| 133 | + onValueChange={(v) => (tagsFilter = v as ConnectorErrorFilter)} |
| 134 | + items={filterItems} |
| 135 | + class="-mt-2" |
| 136 | + /> |
| 137 | + <span class="text-surface-500">errors</span> |
| 138 | + </div> |
| 139 | + <div class="-mr-4 scrollbar flex-1 overflow-y-auto pr-4"> |
| 140 | + {#if loading} |
| 141 | + <div class="p-2 text-surface-500">Loading…</div> |
| 142 | + {:else if displayItems.length === 0} |
| 143 | + <div class="p-2 text-surface-500"> |
| 144 | + {#if tagsFilter === 'all'} |
| 145 | + No errors |
| 146 | + {:else} |
| 147 | + No errors of this type |
| 148 | + {/if} |
| 149 | + </div> |
| 150 | + {:else} |
| 151 | + {#each displayItems as item} |
| 152 | + {#if item.kind === 'gap'} |
| 153 | + <div class="px-2 py-1 text-center text-surface-500 italic"> |
| 154 | + {#if item.count !== 1} |
| 155 | + {item.count} other errors skipped |
| 156 | + {:else} |
| 157 | + {item.count} other error skipped |
| 158 | + {/if} |
| 159 | + </div> |
| 160 | + {:else} |
| 161 | + <InlineDropdown> |
| 162 | + {#snippet header(isOpen, toggle)} |
| 163 | + <div |
| 164 | + class="flex w-full cursor-pointer items-center gap-2 py-2 pr-2" |
| 165 | + onclick={toggle} |
| 166 | + role="presentation" |
| 167 | + > |
| 168 | + <div |
| 169 | + class="fd fd-chevron-down text-[20px] transition-transform {isOpen |
| 170 | + ? 'rotate-180' |
| 171 | + : ''}" |
| 172 | + ></div> |
| 173 | + <span class="flex-1 overflow-hidden text-nowrap text-ellipsis"> |
| 174 | + {#if !isOpen} |
| 175 | + <span out:fade={{ duration: 100 }}>{item.error.message}</span> |
| 176 | + {/if} |
| 177 | + </span> |
| 178 | + {#if status?.fatal_error != null && item.error.message === status.fatal_error} |
| 179 | + <span class="fd fd-circle-x text-error-500"></span> |
| 180 | + {/if} |
| 181 | + <span class="ml-auto">{formatServerTime(item.error.timestamp)}</span> |
| 182 | + <Tooltip placement="top">{formatDateTime(Dayjs(item.error.timestamp))}</Tooltip> |
| 183 | + </div> |
| 184 | + {/snippet} |
| 185 | + {#snippet content()} |
| 186 | + <div |
| 187 | + class="mr-16 -mb-7 ml-7 -translate-y-7 pb-2" |
| 188 | + transition:slide={{ duration: 150 }} |
| 189 | + > |
| 190 | + {item.error.message} |
| 191 | + </div> |
| 192 | + {/snippet} |
| 193 | + </InlineDropdown> |
| 194 | + {/if} |
| 195 | + {/each} |
| 196 | + {/if} |
| 197 | + </div> |
| 198 | +</div> |