Files
Artveris/apps/admin/src/components/BackupRestorePanel.vue
T

256 lines
7.9 KiB
Vue

<template>
<div class="backup-panel">
<div class="backup-toolbar">
<n-button type="primary" :loading="creating" @click="createBackup">{{ t('pages.backup.create') }}</n-button>
<n-upload :show-file-list="false" accept=".zip" :custom-request="previewRestore" :disabled="restoring">
<n-button :loading="restoring">{{ t('pages.backup.uploadPreview') }}</n-button>
</n-upload>
<n-button @click="load">{{ t('common.action.refresh') }}</n-button>
</div>
<n-alert v-if="restorePreviewResult" type="warning" class="restore-alert" :show-icon="true">
<div class="restore-content">
<span>
{{ t('pages.backup.previewPassed', { time: restorePreviewResult.manifest?.createdAt || '-' }) }}
</span>
<n-button size="small" type="error" :loading="restoring" @click="commitRestore">{{ t('pages.backup.confirmRestore') }}</n-button>
</div>
</n-alert>
<n-data-table
:columns="columns"
:data="rows"
:loading="loading"
:pagination="false"
class="table-fill backup-table"
remote
:scroll-x="1320"
:row-key="(r) => r.id"
@update:sorter="onSorterChange"
/>
<div class="table-pagination">
<n-pagination v-bind="pagination" :disabled="loading" />
</div>
</div>
</template>
<script setup lang="ts">
import { computed, h, onMounted, reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import type { UploadCustomRequestOptions } from 'naive-ui';
import { NButton, NIcon, NTag, type DataTableColumns } from 'naive-ui';
import { DownloadOutline, TrashOutline } from '@vicons/ionicons5';
import { backupApi } from '@/api';
import { dialog, message } from '@/utils/feedback';
import { downloadFile } from '@/utils/download';
import { fmt, statusType } from '@/utils/format';
import { useTablePage } from '@/composables/useTablePage';
import { useRemoteSort } from '@/utils/tableSort';
import { renderTableFilterTitle } from '@/utils/tableFilter';
const { t } = useI18n();
const rows = ref<any[]>([]);
const loading = ref(false);
const creating = ref(false);
const restoring = ref(false);
const restorePreviewResult = ref<any>(null);
const query = reactive({ status: null as string | null, mode: null as string | null });
const { pagination, page, pageSize, resetPage, setPageResult } = useTablePage(load, 20);
const { sortProps, onSorterChange, sortParams } = useRemoteSort(() => {
resetPage();
return load();
});
const statusOptions = computed(() => ['PENDING', 'PROCESSING', 'SUCCESS', 'FAILED'].map((value) => ({ label: t(`common.status.${value}`), value })));
const modeOptions = computed(() => ['BACKUP', 'RESTORE', 'SCHEDULED', 'AUTO_BEFORE_RESTORE'].map((value) => ({ label: t(`pages.backup.mode.${value}`), value })));
const columns = computed<DataTableColumns<any>>(() => [
{ title: t('common.label.createdAt'), key: 'createdAt', width: 170, align: 'center', render: (row) => fmt(row.createdAt), ...sortProps('createdAt') },
{ title: t('pages.backup.fileName'), key: 'fileName', minWidth: 360, ellipsis: { tooltip: true } },
{
title: () => renderTableFilterTitle(t('common.label.type'), query.mode, modeOptions.value, (value) => setFilter('mode', value ? String(value) : null)),
key: 'mode',
width: 170,
align: 'center',
render: (row) => t(`pages.backup.mode.${row.mode}`),
},
{
title: () => renderTableFilterTitle(t('common.label.status'), query.status, statusOptions.value, (value) => setFilter('status', value ? String(value) : null)),
key: 'status',
width: 130,
align: 'center',
render: (row) => h(NTag, { size: 'small', type: statusType(row.status) }, { default: () => t(`common.status.${row.status}`) }),
},
{ title: t('pages.backup.size'), key: 'size', width: 120, align: 'center', render: (row) => formatSize(row.size) },
{ title: t('pages.backup.error'), key: 'error', minWidth: 320, ellipsis: { tooltip: true }, render: (row) => row.error || '-' },
{
title: t('common.label.operation'),
key: 'ops',
width: 110,
align: 'center',
fixed: 'right',
render: (row) =>
h('div', { class: 'row-actions' }, [
h(
NButton,
{
quaternary: true,
circle: true,
size: 'small',
disabled: row.status !== 'SUCCESS',
title: t('common.action.download'),
onClick: () => downloadBackup(row),
},
{ icon: () => h(NIcon, null, { default: () => h(DownloadOutline) }) },
),
h(
NButton,
{ quaternary: true, circle: true, size: 'small', type: 'error', title: t('common.action.delete'), onClick: () => removeBackup(row) },
{ icon: () => h(NIcon, null, { default: () => h(TrashOutline) }) },
),
]),
},
]);
function setFilter(field: 'status' | 'mode', value: string | null) {
query[field] = value;
resetPage();
void load();
}
function formatSize(value: unknown) {
const n = Number(value || 0);
if (!n) return '-';
if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`;
return `${(n / 1024 / 1024).toFixed(1)} MB`;
}
async function load() {
loading.value = true;
try {
const res: any = await backupApi.list({
status: query.status || undefined,
mode: query.mode || undefined,
page: page.value,
pageSize: pageSize.value,
...sortParams(),
});
rows.value = res.list;
setPageResult(res);
} finally {
loading.value = false;
}
}
async function createBackup() {
creating.value = true;
try {
await backupApi.create();
message.success(t('pages.backup.created'));
await load();
} finally {
creating.value = false;
}
}
async function downloadBackup(row: any) {
await downloadFile(`/backups/${row.id}/download`, row.fileName || 'artveris-backup.zip');
}
function removeBackup(row: any) {
dialog.warning({
title: t('pages.backup.deleteTitle'),
content: t('pages.backup.deleteConfirm'),
positiveText: t('common.action.confirm'),
negativeText: t('common.action.cancel'),
onPositiveClick: async () => {
await backupApi.remove(row.id);
message.success(t('common.msg.deleted'));
await load();
},
});
}
async function previewRestore({ file, onFinish, onError }: UploadCustomRequestOptions) {
restoring.value = true;
try {
const fd = new FormData();
fd.append('file', file.file as File);
restorePreviewResult.value = await backupApi.restorePreview(fd);
message.success(t('pages.backup.previewSuccess'));
await load();
onFinish();
} catch {
message.error(t('pages.backup.previewFailed'));
onError();
} finally {
restoring.value = false;
}
}
function commitRestore() {
if (!restorePreviewResult.value?.restoreId) return;
dialog.warning({
title: t('pages.backup.restoreTitle'),
content: t('pages.backup.restoreConfirm'),
positiveText: t('pages.backup.confirmRestore'),
negativeText: t('common.action.cancel'),
onPositiveClick: async () => {
restoring.value = true;
try {
await backupApi.restoreCommit(restorePreviewResult.value.restoreId);
restorePreviewResult.value = null;
message.success(t('pages.backup.restoreDone'));
await load();
} finally {
restoring.value = false;
}
},
});
}
onMounted(load);
</script>
<style scoped>
.backup-panel {
height: 100%;
min-height: 0;
display: flex;
flex-direction: column;
overflow: visible;
}
.backup-toolbar {
position: sticky;
top: 0;
z-index: 6;
flex: 0 0 auto;
display: flex;
align-items: center;
justify-content: flex-end;
gap: 10px;
flex-wrap: wrap;
margin: 0 -12px 12px;
padding: 10px 12px;
background: var(--n-color);
border-bottom: 1px solid var(--n-border-color);
}
.backup-toolbar :deep(.n-upload) {
width: auto;
}
.restore-alert {
flex: 0 0 auto;
margin-bottom: 12px;
}
.restore-content {
display: flex;
align-items: center;
gap: 12px;
flex-wrap: wrap;
}
.backup-table {
flex: 1 1 auto;
min-height: 180px;
}
</style>