|
1 | 1 | <script lang="ts"> |
2 | | -import type { V1Pod } from '@kubernetes/client-node'; |
3 | | -import { getContext, onDestroy, onMount, tick } from 'svelte'; |
4 | | -import { Streams } from '/@/stream/streams'; |
5 | | -import type { IDisposable } from '@kubernetes-dashboard/channels'; |
6 | | -import { EmptyScreen } from '@podman-desktop/ui-svelte'; |
7 | | -import NoLogIcon from '/@/component/icons/NoLogIcon.svelte'; |
8 | | -import type { Terminal } from '@xterm/xterm'; |
9 | | -import TerminalWindow from '/@/component/terminal/TerminalWindow.svelte'; |
10 | | -import { SvelteMap } from 'svelte/reactivity'; |
11 | | -import { ansi256Colours, colourizedANSIContainerName } from '/@/component/terminal/terminal-colors'; |
12 | | -
|
13 | | -interface Props { |
| 2 | + import type { V1Pod } from '@kubernetes/client-node'; |
| 3 | + import { getContext, onDestroy, onMount, tick } from 'svelte'; |
| 4 | + import { Streams } from '/@/stream/streams'; |
| 5 | + import type { IDisposable, PodLogsOptions } from '@kubernetes-dashboard/channels'; |
| 6 | + import { EmptyScreen, Button, Input } from '@podman-desktop/ui-svelte'; |
| 7 | + import NoLogIcon from '/@/component/icons/NoLogIcon.svelte'; |
| 8 | + import type { Terminal } from '@xterm/xterm'; |
| 9 | + import TerminalWindow from '/@/component/terminal/TerminalWindow.svelte'; |
| 10 | + import { SvelteMap } from 'svelte/reactivity'; |
| 11 | + import { ansi256Colours, colourizedANSIContainerName, colorizeLogLevel } from '/@/component/terminal/terminal-colors'; |
| 12 | +
|
| 13 | + interface Props { |
14 | 14 | object: V1Pod; |
15 | | -} |
16 | | -let { object }: Props = $props(); |
| 15 | + } |
| 16 | + let { object }: Props = $props(); |
17 | 17 |
|
18 | | -// Logs has been initialized |
19 | | -let noLogs = $state(true); |
| 18 | + // Logs has been initialized |
| 19 | + let noLogs = $state(true); |
20 | 20 |
|
21 | | -let logsTerminal = $state<Terminal>(); |
| 21 | + let logsTerminal = $state<Terminal>(); |
22 | 22 |
|
23 | | -let disposables: IDisposable[] = []; |
24 | | -const streams = getContext<Streams>(Streams); |
| 23 | + // Log retrieval mode and options |
| 24 | + let isStreaming = $state(true); |
| 25 | + let previous = $state(false); |
| 26 | + let tailLines = $state<number | undefined>(undefined); |
| 27 | + let sinceSeconds = $state<number | undefined>(undefined); |
| 28 | + let timestamps = $state(false); |
| 29 | + let fontSize = $state(10); |
25 | 30 |
|
26 | | -// Create a map that will store the ANSI 256 colour for each container name |
27 | | -// if we run out of colours, we'll start from the beginning. |
28 | | -const colourizedContainerName = new SvelteMap<string, string>(); |
| 31 | + let disposables: IDisposable[] = []; |
| 32 | + const streams = getContext<Streams>(Streams); |
29 | 33 |
|
30 | | -onMount(async () => { |
31 | | - logsTerminal?.clear(); |
| 34 | + // Create a map that will store the ANSI 256 colour for each container name |
| 35 | + // if we run out of colours, we'll start from the beginning. |
| 36 | + const colourizedContainerName = new SvelteMap<string, string>(); |
32 | 37 |
|
33 | | - const containerCount = object.spec?.containers.length ?? 0; |
| 38 | + async function loadLogs() { |
| 39 | + logsTerminal?.clear(); |
| 40 | + noLogs = true; |
| 41 | + |
| 42 | + disposables.forEach(disposable => disposable.dispose()); |
| 43 | + disposables = []; |
34 | 44 |
|
35 | | - // Go through each name of pod.containers array and determine |
36 | | - // how much spacing is required for each name to be printed. |
37 | | - let maxNameLength = 0; |
38 | | - if (containerCount > 1) { |
39 | | - object.spec?.containers.forEach((container, index) => { |
40 | | - if (container.name.length > maxNameLength) { |
41 | | - maxNameLength = container.name.length; |
42 | | - } |
43 | | - const colour = ansi256Colours[index % ansi256Colours.length]; |
44 | | - colourizedContainerName.set(container.name, colourizedANSIContainerName(container.name, colour)); |
45 | | - }); |
46 | | - } |
| 45 | + const containerCount = object.spec?.containers.length ?? 0; |
47 | 46 |
|
48 | | - const multiContainers = |
49 | | - containerCount > 1 |
50 | | - ? (name: string, data: string, callback: (data: string) => void): void => { |
51 | | - const padding = ' '.repeat(maxNameLength - name.length); |
52 | | - const colouredName = colourizedContainerName.get(name); |
53 | | -
|
54 | | - // All lines are prefixed, except the last one if it's empty. |
55 | | - const lines = data |
56 | | - .split('\n') |
57 | | - .map((line, index, arr) => |
58 | | - index < arr.length - 1 || line.length > 0 ? `${padding}${colouredName}|${line}` : line, |
59 | | - ); |
60 | | - callback(lines.join('\n')); |
| 47 | + // Go through each name of pod.containers array and determine |
| 48 | + // how much spacing is required for each name to be printed. |
| 49 | + let maxNameLength = 0; |
| 50 | + if (containerCount > 1) { |
| 51 | + object.spec?.containers.forEach((container, index) => { |
| 52 | + if (container.name.length > maxNameLength) { |
| 53 | + maxNameLength = container.name.length; |
61 | 54 | } |
62 | | - : (_name: string, data: string, callback: (data: string) => void): void => { |
63 | | - callback(data); |
64 | | - }; |
65 | | -
|
66 | | - for (const containerName of object.spec?.containers.map(c => c.name) ?? []) { |
67 | | - disposables.push( |
68 | | - await streams.streamPodLogs.subscribe( |
69 | | - object.metadata?.name ?? '', |
70 | | - object.metadata?.namespace ?? '', |
71 | | - containerName, |
72 | | - chunk => { |
73 | | - multiContainers(containerName, chunk.data, data => { |
74 | | - if (noLogs) { |
75 | | - noLogs = false; |
76 | | - } |
77 | | - logsTerminal?.write(data + '\r'); |
78 | | - tick() |
79 | | - .then(() => { |
80 | | - window.dispatchEvent(new Event('resize')); |
81 | | - }) |
82 | | - .catch(console.error); |
83 | | - }); |
84 | | - }, |
85 | | - ), |
86 | | - ); |
| 55 | + const colour = ansi256Colours[index % ansi256Colours.length]; |
| 56 | + colourizedContainerName.set(container.name, colourizedANSIContainerName(container.name, colour)); |
| 57 | + }); |
| 58 | + } |
| 59 | +
|
| 60 | + const multiContainers = |
| 61 | + containerCount > 1 |
| 62 | + ? (name: string, data: string, callback: (data: string) => void): void => { |
| 63 | + const padding = ' '.repeat(maxNameLength - name.length); |
| 64 | + const colouredName = colourizedContainerName.get(name); |
| 65 | +
|
| 66 | + // All lines are prefixed, except the last one if it's empty. |
| 67 | + const lines = data |
| 68 | + .split('\n') |
| 69 | + .map(line => colorizeLogLevel(line)) |
| 70 | + .map((line, index, arr) => |
| 71 | + index < arr.length - 1 || line.length > 0 ? `${padding}${colouredName}|${line}` : line, |
| 72 | + ); |
| 73 | + callback(lines.join('\n')); |
| 74 | + } |
| 75 | + : (_name: string, data: string, callback: (data: string) => void): void => { |
| 76 | + const lines = data |
| 77 | + .split('\n') |
| 78 | + .map(line => colorizeLogLevel(line)); |
| 79 | + callback(lines.join('\n')); |
| 80 | + }; |
| 81 | + |
| 82 | + const options: PodLogsOptions = { |
| 83 | + stream: isStreaming, |
| 84 | + previous, |
| 85 | + tailLines, |
| 86 | + sinceSeconds, |
| 87 | + timestamps, |
| 88 | + }; |
| 89 | + |
| 90 | + for (const containerName of object.spec?.containers.map(c => c.name) ?? []) { |
| 91 | + disposables.push( |
| 92 | + await streams.streamPodLogs.subscribe( |
| 93 | + object.metadata?.name ?? '', |
| 94 | + object.metadata?.namespace ?? '', |
| 95 | + containerName, |
| 96 | + options, |
| 97 | + chunk => { |
| 98 | + multiContainers(containerName, chunk.data, data => { |
| 99 | + if (noLogs) { |
| 100 | + noLogs = false; |
| 101 | + } |
| 102 | + logsTerminal?.write(data + '\r'); |
| 103 | + tick().then(() => { |
| 104 | + window.dispatchEvent(new Event('resize')); |
| 105 | + }).catch(console.error); |
| 106 | + }); |
| 107 | + }), |
| 108 | + ); |
| 109 | + } |
87 | 110 | } |
88 | | -}); |
89 | 111 |
|
90 | | -onDestroy(() => { |
91 | | - disposables.forEach(disposable => disposable.dispose()); |
92 | | - disposables = []; |
93 | | -}); |
| 112 | + onMount(async () => { |
| 113 | + await loadLogs(); |
| 114 | + }); |
| 115 | +
|
| 116 | + onDestroy(() => { |
| 117 | + disposables.forEach(disposable => disposable.dispose()); |
| 118 | + disposables = []; |
| 119 | + }); |
94 | 120 | </script> |
95 | 121 |
|
96 | | -<EmptyScreen |
97 | | - icon={NoLogIcon} |
98 | | - title="No Log" |
99 | | - message="Log output of Pod {object.metadata?.name}" |
100 | | - hidden={noLogs === false} /> |
101 | | - |
102 | | -<div |
103 | | - class="min-w-full flex flex-col" |
104 | | - class:invisible={noLogs === true} |
105 | | - class:h-0={noLogs === true} |
106 | | - class:h-full={noLogs === false}> |
107 | | - <TerminalWindow class="h-full" bind:terminal={logsTerminal} convertEol disableStdIn /> |
| 122 | +<div class="flex flex-col h-full"> |
| 123 | + <div class="flex items-center gap-4 p-4 bg-[var(--pd-content-header-bg)] border-b border-[var(--pd-content-divider)]"> |
| 124 | + <div class="flex items-center gap-2"> |
| 125 | + <label class="flex items-center gap-2 cursor-pointer"> |
| 126 | + <input type="radio" bind:group={isStreaming} value={true} class="cursor-pointer" /> |
| 127 | + <span class="text-sm">Stream</span> |
| 128 | + </label> |
| 129 | + <label class="flex items-center gap-2 cursor-pointer"> |
| 130 | + <input type="radio" bind:group={isStreaming} value={false} class="cursor-pointer" /> |
| 131 | + <span class="text-sm">Retrieve</span> |
| 132 | + </label> |
| 133 | + </div> |
| 134 | + |
| 135 | + <label class="flex items-center gap-2 cursor-pointer"> |
| 136 | + <input type="checkbox" bind:checked={previous} class="cursor-pointer" /> |
| 137 | + <span class="text-sm">Previous</span> |
| 138 | + </label> |
| 139 | + |
| 140 | + <label class="flex items-center gap-2"> |
| 141 | + <span class="text-sm">Tail:</span> |
| 142 | + <Input |
| 143 | + type="number" |
| 144 | + bind:value={tailLines} |
| 145 | + placeholder="All" |
| 146 | + class="w-24" |
| 147 | + min="1" |
| 148 | + /> |
| 149 | + </label> |
| 150 | + |
| 151 | + <label class="flex items-center gap-2"> |
| 152 | + <span class="text-sm">Since (seconds):</span> |
| 153 | + <Input |
| 154 | + type="number" |
| 155 | + bind:value={sinceSeconds} |
| 156 | + placeholder="All" |
| 157 | + class="w-24" |
| 158 | + min="1" |
| 159 | + /> |
| 160 | + </label> |
| 161 | + |
| 162 | + <label class="flex items-center gap-2 cursor-pointer"> |
| 163 | + <input type="checkbox" bind:checked={timestamps} class="cursor-pointer" /> |
| 164 | + <span class="text-sm">Timestamps</span> |
| 165 | + </label> |
| 166 | + |
| 167 | + <label class="flex items-center gap-2"> |
| 168 | + <span class="text-sm">Font Size:</span> |
| 169 | + <Input type="number" bind:value={fontSize} class="w-20" min="8" max="24" /> |
| 170 | + </label> |
| 171 | + |
| 172 | + <Button on:click={loadLogs} class="ml-auto"> |
| 173 | + {isStreaming ? 'Restart Stream' : 'Retrieve Logs'} |
| 174 | + </Button> |
| 175 | + </div> |
| 176 | + |
| 177 | + <EmptyScreen |
| 178 | + icon={NoLogIcon} |
| 179 | + title="No Log" |
| 180 | + message="Log output of Pod {object.metadata?.name}" |
| 181 | + hidden={noLogs === false} /> |
| 182 | + |
| 183 | + <div |
| 184 | + class="min-w-full flex flex-col" |
| 185 | + class:invisible={noLogs === true} |
| 186 | + class:h-0={noLogs === true} |
| 187 | + class:flex-1={noLogs === false}> |
| 188 | + <TerminalWindow class="h-full" bind:terminal={logsTerminal} convertEol disableStdIn fontSize={fontSize} /> |
| 189 | + </div> |
108 | 190 | </div> |
0 commit comments