Skip to content

Commit ba96828

Browse files
committed
core&ui: add clean all monitors
1 parent 020039a commit ba96828

File tree

6 files changed

+69
-5
lines changed

6 files changed

+69
-5
lines changed

packages/server/handler/monitor.ts

+14
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ class MonitorAdminHandler extends AuthHandler {
4747
this.response.body = { success: true };
4848
}
4949

50+
async postDelete(params) {
51+
const { _id } = params;
52+
if (!_id) throw new BadRequestError();
53+
const m = await this.ctx.db.monitor.findOne({ _id });
54+
if (!m) throw new BadRequestError();
55+
await this.ctx.db.monitor.remove({ _id }, {});
56+
this.response.body = { success: true };
57+
}
58+
59+
async postCleanAll() {
60+
await this.ctx.db.monitor.remove({}, { multi: true });
61+
this.response.body = { success: true };
62+
}
63+
5064
async postUpdateAll(params) {
5165
const {
5266
name, group, camera, desktop, ips,

packages/server/utils/color.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export const CHINESE_NAME = {
216216
lightslategray: '浅板岩灰',
217217
lightsteelblue: '浅钢蓝',
218218
lightyellow: '浅黄',
219-
lime: '酸橙色',
219+
lime: '酸橙绿',
220220
limegreen: '酸橙绿',
221221
linen: '亚麻色',
222222
magenta: '洋红',

packages/server/utils/metrics.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function createMetricsRegistry(ctx: Context) {
3232
labelNames: ['status'],
3333
async collect() {
3434
const machines = await ctx.db.monitor.find({});
35-
const onlines = machines.filter((m) => m.updateAt > new Date().getTime() - 1000 * 60);
35+
const onlines = machines.filter((m) => m.updateAt > new Date().getTime() - 1000 * 120);
3636
this.set({ status: 'online' }, onlines.length);
3737
this.set({ status: 'offline' }, machines.length - onlines.length);
3838
},

packages/ui/app/components/MonitorDisplay.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ export function MonitorTable({ monitors, openMonitorInfo }) {
8282
</HoverCard.Dropdown>
8383
</HoverCard>) : 'No Info' }
8484
</Table.Td>
85-
<Table.Td><MonitorInfoButton monitor={m} action={openMonitorInfo} /></Table.Td>
85+
<Table.Td>
86+
<MonitorInfoButton monitor={m} action={openMonitorInfo} />
87+
</Table.Td>
8688
</Table.Tr>
8789
));
8890

packages/ui/app/components/MonitorInfo.tsx

+23-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import {
66
import { notifications } from '@mantine/notifications';
77
import {
88
IconCircleChevronLeft,
9-
IconDeviceComputerCamera, IconDeviceDesktop, IconInfoCircle,
9+
IconDeviceComputerCamera, IconDeviceDesktop, IconInfoCircle, IconX,
1010
} from '@tabler/icons-react';
1111
import mpegts from 'mpegts.js';
1212

13-
function VideoPlayer({ client = null, type = 'camera' }) {
13+
function VideoPlayer({ client, type = 'camera' }) {
1414
const videoRef = React.useRef(null);
1515
const needProxy = client && client[type].startsWith('proxy://');
1616
const src = `${needProxy ? '/stream/' : 'http://'}${client.ip}${
@@ -142,6 +142,24 @@ export function MonitorInfo({
142142
}
143143

144144
export function MonitorInfoButton({ monitor, action }) {
145+
const del = async (m) => {
146+
try {
147+
const res = await (await fetch('/monitor', {
148+
method: 'POST',
149+
headers: { 'Content-Type': 'application/json' },
150+
body: JSON.stringify({ _id: m._id, operation: 'delete' }),
151+
})).json();
152+
if (res.error) {
153+
notifications.show({ title: 'Error', message: `${res.error.message}(${res.error.params})`, color: 'red' });
154+
return;
155+
}
156+
notifications.show({ title: 'Success', message: 'Client deleted', color: 'green' });
157+
} catch (e) {
158+
console.error(e);
159+
notifications.show({ title: 'Error', message: 'Failed to delete client', color: 'red' });
160+
}
161+
};
162+
145163
return (
146164
<Group>
147165
<Tooltip label="Info">
@@ -161,6 +179,9 @@ export function MonitorInfoButton({ monitor, action }) {
161179
</ActionIcon>
162180
</Tooltip>
163181
)}
182+
<Tooltip label="Delete">
183+
<ActionIcon variant="transparent" color="red" aria-label='Delete' onClick={() => del(monitor)}><IconX /></ActionIcon>
184+
</Tooltip>
164185
</Group>
165186
);
166187
}

packages/ui/app/pages/Monitor.tsx

+27
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import {
33
Button, Card, Center, Group, LoadingOverlay, Tabs, Text, Title,
44
} from '@mantine/core';
5+
import { notifications } from '@mantine/notifications';
56
import { useQuery } from '@tanstack/react-query';
67
import { MonitorBatchModal } from '../components/MonitorBatchModel';
78
import { MonitorCards, MonitorTable } from '../components/MonitorDisplay';
@@ -26,6 +27,25 @@ export default function Monitor() {
2627
setInfoTab(tab);
2728
};
2829

30+
const cleanAll = async () => {
31+
try {
32+
const res = await (await fetch('/monitor', {
33+
method: 'POST',
34+
headers: { 'Content-Type': 'application/json' },
35+
body: JSON.stringify({ operation: 'clean_all' }),
36+
})).json();
37+
if (res.error) {
38+
notifications.show({ title: 'Error', message: res.error.message, color: 'red' });
39+
return;
40+
}
41+
notifications.show({ title: 'Success', message: 'All monitors cleaned', color: 'green' });
42+
query.refetch();
43+
} catch (e) {
44+
console.error(e);
45+
notifications.show({ title: 'Error', message: 'Failed to clean all monitors', color: 'red' });
46+
}
47+
};
48+
2949
return (
3050
<>
3151
{detailM ? (
@@ -46,6 +66,13 @@ export default function Monitor() {
4666
</Button>
4767
)}
4868
<MonitorBatchModal refresh={query.refetch} />
69+
<Button
70+
variant="outline"
71+
color="red"
72+
onClick={cleanAll}
73+
>
74+
Clean All
75+
</Button>
4976
</Group>
5077
</Group>
5178
<Tabs value={activeTab} onChange={(value) => setActiveTab(value!)}>

0 commit comments

Comments
 (0)