-
Notifications
You must be signed in to change notification settings - Fork 268
/
ImagesList.svelte
349 lines (309 loc) · 12.3 KB
/
ImagesList.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<script lang="ts">
import { filtered, searchPattern, imagesInfos } from '../stores/images';
import { onDestroy, onMount } from 'svelte';
import ImageEmptyScreen from './image/ImageEmptyScreen.svelte';
import { router } from 'tinro';
import type { ImageInfoUI } from './image/ImageInfoUI';
import ImageActions from './image/ImageActions.svelte';
import type { ImageInfo } from '../../../main/src/plugin/api/image-info';
import NoContainerEngineEmptyScreen from './image/NoContainerEngineEmptyScreen.svelte';
import { providerInfos } from '../stores/providers';
import PushImageModal from './image/PushImageModal.svelte';
import RenameImageModal from './image/RenameImageModal.svelte';
import { ImageUtils } from './image/image-utils';
import NavPage from './ui/NavPage.svelte';
import ImageIcon from './images/ImageIcon.svelte';
import StatusIcon from './images/StatusIcon.svelte';
import type { Unsubscriber } from 'svelte/store';
import { containersInfos } from '../stores/containers';
import type { ContainerInfo } from '../../../main/src/plugin/api/container-info';
import moment from 'moment';
import Prune from './engine/Prune.svelte';
import type { EngineInfoUI } from './engine/EngineInfoUI';
import type { Menu } from '../../../main/src/plugin/menu-registry';
import { MenuContext } from '../../../main/src/plugin/menu-registry';
import Checkbox from './ui/Checkbox.svelte';
import Button from './ui/Button.svelte';
import { faArrowCircleDown, faCube, faTrash } from '@fortawesome/free-solid-svg-icons';
let searchTerm = '';
$: searchPattern.set(searchTerm);
let images: ImageInfoUI[] = [];
let multipleEngines = false;
let enginesList: EngineInfoUI[];
let pushImageModal = false;
let pushImageModalImageInfo: ImageInfoUI | undefined = undefined;
function handlePushImageModal(imageInfo: ImageInfoUI) {
pushImageModalImageInfo = imageInfo;
pushImageModal = true;
}
let renameImageModal = false;
let renameImageModalImageInfo: ImageInfoUI | undefined = undefined;
function handleRenameImageModal(imageInfo: ImageInfoUI) {
renameImageModalImageInfo = imageInfo;
renameImageModal = true;
}
$: providerConnections = $providerInfos
.map(provider => provider.containerConnections)
.flat()
.filter(providerContainerConnection => providerContainerConnection.status === 'started');
// number of selected items in the list
$: selectedItemsNumber = images.filter(image => !image.inUse).filter(image => image.selected).length;
// do we need to unselect all checkboxes if we don't have all items being selected ?
$: selectedAllCheckboxes = images.filter(image => !image.inUse).every(image => image.selected);
const imageUtils = new ImageUtils();
function updateImages() {
const computedImages = storeImages
.map((imageInfo: ImageInfo) => imageUtils.getImagesInfoUI(imageInfo, storeContainers))
.flat();
// update selected items based on current selected items
computedImages.forEach(image => {
const matchingImage = images.find(
currentImage => currentImage.id === image.id && currentImage.engineId === image.engineId,
);
if (matchingImage) {
image.selected = matchingImage.selected;
}
});
computedImages.sort((first, second) => second.createdAt - first.createdAt);
images = computedImages;
// Map engineName, engineId and engineType from currentContainers to EngineInfoUI[]
const engines = images.map(container => {
return {
name: container.engineName,
id: container.engineId,
};
});
// Remove duplicates from engines by name
const uniqueEngines = engines.filter((engine, index, self) => index === self.findIndex(t => t.name === engine.name));
if (uniqueEngines.length > 1) {
multipleEngines = true;
} else {
multipleEngines = false;
}
// Set the engines to the global variable for the Prune functionality button
enginesList = uniqueEngines;
// compute refresh interval
const interval = computeInterval();
refreshTimeouts.push(setTimeout(refreshAge, interval));
}
let imagesUnsubscribe: Unsubscriber;
let containersUnsubscribe: Unsubscriber;
let storeContainers: ContainerInfo[] = [];
let storeImages: ImageInfo[] = [];
let contributedMenus: Menu[];
onMount(async () => {
containersUnsubscribe = containersInfos.subscribe(value => {
storeContainers = value;
updateImages();
});
imagesUnsubscribe = filtered.subscribe(value => {
storeImages = value;
updateImages();
});
contributedMenus = await window.getContributedMenus(MenuContext.DASHBOARD_IMAGE);
});
onDestroy(() => {
// kill timers
refreshTimeouts.forEach(timeout => clearTimeout(timeout));
refreshTimeouts.length = 0;
// unsubscribe from the store
if (imagesUnsubscribe) {
imagesUnsubscribe();
}
if (containersUnsubscribe) {
containersUnsubscribe();
}
});
function closeModals() {
pushImageModal = false;
renameImageModal = false;
}
function gotoBuildImage(): void {
router.goto('/images/build');
}
function gotoPullImage(): void {
router.goto('/images/pull');
}
function openDetailsImage(image: ImageInfoUI) {
router.goto(`/images/${image.id}/${image.engineId}/${image.base64RepoTag}/summary`);
}
function toggleAllImages(checked: boolean) {
const toggleImages = images;
// filter out all images used by a container
toggleImages.filter(image => !image.inUse).forEach(image => (image.selected = checked));
images = toggleImages;
}
// delete the items selected in the list
let bulkDeleteInProgress = false;
async function deleteSelectedImages() {
bulkDeleteInProgress = true;
const selectedImages = images.filter(image => image.selected);
await selectedImages.reduce((prev: Promise<void>, image) => {
return prev
.then(() => imageUtils.deleteImage(image))
.catch((e: unknown) => console.log('error while removing image', e));
}, Promise.resolve());
bulkDeleteInProgress = false;
}
let refreshTimeouts: NodeJS.Timeout[] = [];
const SECOND = 1000;
function refreshAge() {
images = images.map(imageInfo => {
return { ...imageInfo, age: imageUtils.refreshAge(imageInfo) };
});
// compute new interval
const newInterval = computeInterval();
refreshTimeouts.forEach(timeout => clearTimeout(timeout));
refreshTimeouts.length = 0;
refreshTimeouts.push(setTimeout(refreshAge, newInterval));
}
function computeInterval(): number {
// no images, no refresh
if (images.length === 0) {
return -1;
}
// do we have images that have been created in less than 1 minute
// if so, need to update every second
const imagesCreatedInLessThan1Mn = images.filter(image => moment().diff(moment.unix(image.createdAt), 'minutes') < 1);
if (imagesCreatedInLessThan1Mn.length > 0) {
return 2 * SECOND;
}
// every minute for images created less than 1 hour
const imagesCreatedInLessThan1Hour = images.filter(image => moment().diff(moment.unix(image.createdAt), 'hours') < 1);
if (imagesCreatedInLessThan1Hour.length > 0) {
// every minute
return 60 * SECOND;
}
// every hour for images created less than 1 day
const imagesCreatedInLessThan1Day = images.filter(image => moment().diff(moment.unix(image.createdAt), 'days') < 1);
if (imagesCreatedInLessThan1Day.length > 0) {
// every hour
return 60 * 60 * SECOND;
}
// every day
return 60 * 60 * 24 * SECOND;
}
</script>
<NavPage bind:searchTerm="{searchTerm}" title="images">
<div slot="additional-actions" class="space-x-2 flex flex-nowrap">
{#if $imagesInfos.length > 0}
<Prune type="images" engines="{enginesList}" />
{/if}
<Button on:click="{() => gotoPullImage()}" title="Pull Image From a Registry" icon="{faArrowCircleDown}">
Pull an image
</Button>
<Button on:click="{() => gotoBuildImage()}" title="Build Image from Containerfile" icon="{faCube}">
Build an image
</Button>
</div>
<div slot="bottom-additional-actions" class="flex flex-row justify-start items-center w-full">
{#if selectedItemsNumber > 0}
<Button
on:click="{() => deleteSelectedImages()}"
title="Delete {selectedItemsNumber} selected items"
bind:inProgress="{bulkDeleteInProgress}"
icon="{faTrash}" />
<span class="pl-2">On {selectedItemsNumber} selected items.</span>
{/if}
</div>
<div class="flex min-w-full h-full" slot="content">
<table class="mx-5 w-full h-fit" class:hidden="{images.length === 0}">
<!-- title -->
<thead class="sticky top-0 bg-charcoal-700 z-[2]">
<tr class="h-7 uppercase text-xs text-gray-600">
<th class="whitespace-nowrap w-5"></th>
<th class="px-2 w-5">
<Checkbox
title="Toggle all"
bind:checked="{selectedAllCheckboxes}"
indeterminate="{selectedItemsNumber > 0 && !selectedAllCheckboxes}"
on:click="{event => toggleAllImages(event.detail)}" />
</th>
<th class="text-center font-extrabold w-10 px-2">status</th>
<th class="w-10">Name</th>
<th class="px-6 whitespace-nowrap w-10">age</th>
<th class="px-6 whitespace-nowrap text-end">size</th>
<th class="text-right pr-2">Actions</th>
</tr>
</thead>
<tbody>
{#each images as image}
<tr class="group h-12 bg-charcoal-800 hover:bg-zinc-700">
<td class="rounded-tl-lg rounded-bl-lg w-5"> </td>
<td class="px-2">
<Checkbox
title="Toggle image"
bind:checked="{image.selected}"
disabled="{image.inUse}"
disabledTooltip="Image is used by a container" />
</td>
<td class="bg-charcoal-800 group-hover:bg-zinc-700 flex flex-row justify-center content-center h-12">
<div class="grid place-content-center ml-3 mr-4">
<StatusIcon icon="{ImageIcon}" status="{image.inUse ? 'USED' : 'UNUSED'}" />
</div>
</td>
<td class="whitespace-nowrap w-10 hover:cursor-pointer" on:click="{() => openDetailsImage(image)}">
<div class="flex items-center">
<div class="">
<div class="flex flex-row items-center">
<div class="text-sm text-gray-300">{image.name}</div>
</div>
<div class="flex flex-row items-center">
<div class="text-xs text-violet-400">{image.shortId}</div>
<div class="ml-1 text-xs font-extra-light text-gray-400">{image.tag}</div>
</div>
<div class="flex flex-row text-xs font-extra-light text-gray-900">
<!-- Hide in case of single engine-->
{#if multipleEngines}
<div class="px-2 inline-flex text-xs font-extralight rounded-full bg-slate-800 text-slate-400">
{image.engineName}
</div>
{/if}
</div>
</div>
</div>
</td>
<td class="px-6 py-2 whitespace-nowrap w-10">
<div class="flex items-center">
<div class="text-sm text-gray-700">{image.age}</div>
</div>
</td>
<td class="px-6 py-2 whitespace-nowrap w-10">
<div class="flex">
<div class="w-full text-right text-sm text-gray-700">{image.humanSize}</div>
</div>
</td>
<td class="pl-6 text-right whitespace-nowrap rounded-tr-lg rounded-br-lg">
<ImageActions
image="{image}"
onPushImage="{handlePushImageModal}"
onRenameImage="{handleRenameImageModal}"
dropdownMenu="{true}"
contributions="{contributedMenus}" />
</td>
</tr>
<tr><td class="leading-[8px]"> </td></tr>
{/each}
</tbody>
</table>
{#if providerConnections.length === 0}
<NoContainerEngineEmptyScreen />
{:else if $filtered.length === 0}
<ImageEmptyScreen />
{/if}
{#if pushImageModal && pushImageModalImageInfo}
<PushImageModal
imageInfoToPush="{pushImageModalImageInfo}"
closeCallback="{() => {
closeModals();
}}" />
{/if}
{#if renameImageModal && renameImageModalImageInfo}
<RenameImageModal
imageInfoToRename="{renameImageModalImageInfo}"
closeCallback="{() => {
closeModals();
}}" />
{/if}
</div>
</NavPage>