Skip to content

Commit

Permalink
feat(admin): added total file size statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Sep 19, 2023
1 parent 4ac6a07 commit b26d34e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
3 changes: 3 additions & 0 deletions server/admin/src/client/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions server/admin/src/client/i18n/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ export const zhTranslation = {
tushan: 'Tailchat Admin后台 由 tushan 提供技术支持',
},
},
file: {
fileTotalSize: '文件总大小',
},
analytics: {
activeGroupTop5: '前 5 名活跃群组',
activeUserTop5: '前 5 名活跃用户',
Expand Down
43 changes: 32 additions & 11 deletions server/admin/src/client/resources/file.tsx
Original file line number Diff line number Diff line change
@@ -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';
22 changes: 22 additions & 0 deletions server/admin/src/server/router/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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 };

0 comments on commit b26d34e

Please sign in to comment.