forked from containers/podman-desktop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
VolumesList.svelte
305 lines (275 loc) · 10.7 KB
/
VolumesList.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
<script lang="ts">
import { onDestroy, onMount } from 'svelte';
import { router } from 'tinro';
import type { Unsubscriber } from 'svelte/store';
import type { VolumeInfoUI } from './VolumeInfoUI';
import { fetchVolumes, filtered, searchPattern, volumeListInfos, volumesInitialized } from '../../stores/volumes';
import { providerInfos } from '../../stores/providers';
import NavPage from '../ui/NavPage.svelte';
import { VolumeUtils } from './volume-utils';
import NoContainerEngineEmptyScreen from '../image/NoContainerEngineEmptyScreen.svelte';
import VolumeEmptyScreen from './VolumeEmptyScreen.svelte';
import VolumeActions from './VolumeActions.svelte';
import VolumeIcon from '../images/VolumeIcon.svelte';
import StatusIcon from '../images/StatusIcon.svelte';
import Prune from '../engine/Prune.svelte';
import moment from 'moment';
import type { EngineInfoUI } from '../engine/EngineInfoUI';
import EmptyScreen from '../ui/EmptyScreen.svelte';
import Checkbox from '../ui/Checkbox.svelte';
let searchTerm = '';
$: searchPattern.set(searchTerm);
let volumes: VolumeInfoUI[] = [];
let multipleEngines = false;
let enginesList: EngineInfoUI[];
$: providerConnections = $providerInfos
.map(provider => provider.containerConnections)
.flat()
.filter(providerContainerConnection => providerContainerConnection.status === 'started');
// number of selected items in the list
$: selectedItemsNumber = volumes.filter(volume => !volume.inUse).filter(volume => volume.selected).length;
// do we need to unselect all checkboxes if we don't have all items being selected ?
$: selectedAllCheckboxes = volumes.filter(volume => !volume.inUse).every(volume => volume.selected);
const volumeUtils = new VolumeUtils();
let fetchingInProgress = false;
let volumesUnsubscribe: Unsubscriber;
onMount(async () => {
if (!volumesInitialized) {
fetchingInProgress = true;
try {
await fetchVolumes();
} finally {
fetchingInProgress = false;
}
} else {
// fetch in background
fetchVolumes().catch((error: unknown) => {
console.error('unable to fetch the volumes', error);
});
}
volumesUnsubscribe = filtered.subscribe(value => {
const computedVolumes = value
.map(volumeListInfo => volumeListInfo.Volumes)
.flat()
.map(volume => volumeUtils.toVolumeInfoUI(volume));
// Map engineName, engineId and engineType from currentContainers to EngineInfoUI[]
const engines = computedVolumes.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;
// update selected items based on current selected items
computedVolumes.forEach(volume => {
const matchingVolume = volumes.find(
currentVolume => currentVolume.name === volume.name && currentVolume.engineId === volume.engineId,
);
if (matchingVolume) {
volume.selected = matchingVolume.selected;
}
});
volumes = computedVolumes;
// compute refresh interval
const interval = computeInterval();
refreshTimeouts.push(setTimeout(refreshAge, interval));
});
});
onDestroy(() => {
// kill timers
refreshTimeouts.forEach(timeout => clearTimeout(timeout));
refreshTimeouts.length = 0;
// unsubscribe from the store
if (volumesUnsubscribe) {
volumesUnsubscribe();
}
});
function toggleAllVolumes(checked: boolean) {
const toggleVolumes = volumes;
// filter out all volumes used by a container
toggleVolumes.filter(volume => !volume.inUse).forEach(volume => (volume.selected = checked));
volumes = toggleVolumes;
}
// delete the items selected in the list
let bulkDeleteInProgress = false;
async function deleteSelectedVolumes() {
const selectedVolumes = volumes.filter(volume => volume.selected);
if (selectedVolumes.length > 0) {
bulkDeleteInProgress = true;
await Promise.all(
selectedVolumes.map(async volume => {
try {
await window.removeVolume(volume.engineId, volume.name);
} catch (e) {
console.log('error while removing volume', e);
}
}),
);
bulkDeleteInProgress = false;
}
}
function openDetailsVolume(volume: VolumeInfoUI) {
router.goto(`/volumes/${encodeURI(volume.name)}/${encodeURI(volume.engineId)}/summary`);
}
let refreshTimeouts: NodeJS.Timeout[] = [];
const SECOND = 1000;
function refreshAge() {
volumes = volumes.map(volumeInfo => {
return { ...volumeInfo, age: volumeUtils.refreshAge(volumeInfo) };
});
// compute new interval
const newInterval = computeInterval();
refreshTimeouts.forEach(timeout => clearTimeout(timeout));
refreshTimeouts.length = 0;
refreshTimeouts.push(setTimeout(refreshAge, newInterval));
}
function computeInterval(): number {
// no volumes, no refresh
if (volumes.length === 0) {
return -1;
}
// do we have volumes that have been created in less than 1 minute
// if so, need to update every second
const volumesCreatedInLessThan1Mn = volumes.filter(volume => moment().diff(volume.created, 'minutes') < 1);
if (volumesCreatedInLessThan1Mn.length > 0) {
return 2 * SECOND;
}
// every minute for images created less than 1 hour
const volumesCreatedInLessThan1Hour = volumes.filter(volume => moment().diff(volume.created, 'hours') < 1);
if (volumesCreatedInLessThan1Hour.length > 0) {
// every minute
return 60 * SECOND;
}
// every hour for images created less than 1 day
const volumesCreatedInLessThan1Day = volumes.filter(volume => moment().diff(volume.created, 'days') < 1);
if (volumesCreatedInLessThan1Day.length > 0) {
// every hour
return 60 * 60 * SECOND;
}
// every day
return 60 * 60 * 24 * SECOND;
}
</script>
<NavPage bind:searchTerm="{searchTerm}" title="volumes">
<div slot="additional-actions" class="space-x-2 flex flex-nowrap">
{#if $volumeListInfos.map(volumeInfo => volumeInfo.Volumes).flat().length > 0}
<Prune type="volumes" engines="{enginesList}" />
{/if}
</div>
<div slot="bottom-additional-actions" class="flex flex-row justify-start items-center w-full">
{#if selectedItemsNumber > 0}
<button
class="pf-c-button pf-m-primary"
on:click="{() => deleteSelectedVolumes()}"
title="Delete {selectedItemsNumber} selected items"
type="button">
<span class="pf-c-button__icon pf-m-start">
{#if bulkDeleteInProgress}
<div class="mr-4">
<i class="pf-c-button__progress">
<span class="pf-c-spinner pf-m-md" role="progressbar">
<span class="pf-c-spinner__clipper"></span>
<span class="pf-c-spinner__lead-ball"></span>
<span class="pf-c-spinner__tail-ball"></span>
</span>
</i>
</div>
{:else}
<i class="fas fa-trash" aria-hidden="true"></i>
{/if}
</span>
</button>
<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="{volumes.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
bind:checked="{selectedAllCheckboxes}"
indeterminate="{selectedItemsNumber > 0 && !selectedAllCheckboxes}"
on:click="{event => toggleAllVolumes(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">age</th>
<th class="px-6 whitespace-nowrap text-end">size</th>
<th class="text-right pr-2">Actions</th>
</tr>
</thead>
<tbody>
{#each volumes as volume}
<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
bind:checked="{volume.selected}"
disabled="{volume.inUse}"
disabledTooltip="Volume is used by a container" />
</td>
<td class="bg-charcoal-800 group-hover:bg-zinc-700 flex flex-row justify-center h-12">
<div class="grid place-content-center ml-3 mr-4">
<StatusIcon icon="{VolumeIcon}" status="{volume.inUse ? 'USED' : 'UNUSED'}" />
</div>
</td>
<td class="whitespace-nowrap w-10 hover:cursor-pointer" on:click="{() => openDetailsVolume(volume)}">
<div class="flex items-center">
<div class="">
<div class="flex flex-row items-center">
<div class="text-sm text-gray-300">{volume.shortName}</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">
{volume.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">{volume.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">{volume.humanSize}</div>
</div>
</td>
<td class="pl-6 text-right whitespace-nowrap rounded-tr-lg rounded-br-lg">
<VolumeActions volume="{volume}" />
</td>
</tr>
<tr><td class="leading-[8px]"> </td></tr>
{/each}
</tbody>
</table>
{#if providerConnections.length === 0}
<NoContainerEngineEmptyScreen />
{:else if fetchingInProgress}
<EmptyScreen
icon="{VolumeIcon}"
title="Fetching volumes..."
message="Grabbing volumes from your container engine(s)." />
{:else if $filtered.map(volumeInfo => volumeInfo.Volumes).flat().length === 0}
<VolumeEmptyScreen />
{/if}
</div>
</NavPage>