feat(admin): added total file size statistics

pull/146/merge
moonrailgun 1 year ago
parent 4ac6a0759a
commit b26d34e8e2

@ -37,6 +37,9 @@ export const enTranslation = {
'Tailchat: The next-generation noIM Application in your own workspace',
},
},
file: {
fileTotalSize: 'File Total Size',
},
analytics: {
activeGroupTop5: 'Active Group Top 5',
activeUserTop5: 'Active User Top 5',

@ -128,6 +128,9 @@ export const zhTranslation = {
tushan: 'Tailchat Admin后台 由 tushan 提供技术支持',
},
},
file: {
fileTotalSize: '文件总大小',
},
analytics: {
activeGroupTop5: '前 5 名活跃群组',
activeUserTop5: '前 5 名活跃用户',

@ -1,19 +1,40 @@
import filesize from 'filesize';
import React from 'react';
import { createTextField, ListTable } from 'tushan';
import {
createTextField,
ListTable,
useAsync,
useTranslation,
Typography,
} from 'tushan';
import { fileFields } from '../fields';
import { request } from '../request';
export const FileList: React.FC = React.memo(() => {
const { t } = useTranslation();
const { value: totalSize = 0 } = useAsync(async () => {
const { data } = await request.get('/file/filesizeSum');
return data.totalSize ?? 0;
}, []);
return (
<ListTable
filter={[
createTextField('q', {
label: 'Search',
}),
]}
fields={fileFields}
action={{ detail: true, delete: true }}
batchAction={{ delete: true }}
/>
<>
<Typography.Paragraph style={{ textAlign: 'right' }}>
{t('custom.file.fileTotalSize')}: {filesize(totalSize)}
</Typography.Paragraph>
<ListTable
filter={[
createTextField('q', {
label: 'Search',
}),
]}
fields={fileFields}
action={{ detail: true, delete: true }}
batchAction={{ delete: true }}
showSizeChanger={true}
/>
</>
);
});
FileList.displayName = 'FileList';

@ -6,6 +6,7 @@ import { Router } from 'express';
import { callBrokerAction } from '../broker';
import { auth } from '../middleware/auth';
import Busboy from '@fastify/busboy';
import fileModel from '../../../../models/file';
const router = Router();
@ -57,4 +58,25 @@ router.put('/upload', auth(), async (req, res) => {
req.pipe(busboy);
});
router.get('/filesizeSum', auth(), async (req, res) => {
const ret = await fileModel.aggregate([
{
$group: {
_id: '$objectName' as any,
size: { $first: '$size' },
},
},
{
$group: {
_id: null,
totalSize: { $sum: '$size' },
},
},
]);
const totalSize = ret[0].totalSize;
res.json({ totalSize });
});
export { router as fileRouter };

Loading…
Cancel
Save