Skip to content

Commit

Permalink
Merge pull request #23 from UnownHash/remove-all-dead
Browse files Browse the repository at this point in the history
feat: remove all dead devices button
  • Loading branch information
TurtIeSocks authored Dec 11, 2023
2 parents 495457d + 1b4d81d commit c809013
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 27 additions & 2 deletions packages/client/src/app/status/statusPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useQuery } from '@tanstack/react-query';
import { Loading, Spacer, Text } from '@nextui-org/react';
import { Button, Grid, Loading, Spacer, Text } from '@nextui-org/react';
import { StatusDTO } from '@rotom/types';
import { useCallback, useState } from 'react';

import { StatusCard } from './statusCard';
import { MemoizedWorkersTable as WorkersTable } from './workersTable';
Expand All @@ -12,6 +13,21 @@ export const StatusPage = (): JSX.Element => {
const { isLoading, isFetching, error, data, isSuccess } = useQuery<StatusDTO, Error>(['status'], fetchStatus, {
refetchInterval: 5000,
});
const [delLoading, setDelLoading] = useState(false);

const handleRemoveDead = useCallback(async () => {
setDelLoading(true);
const cancel = new AbortController();
const timer = setTimeout(() => cancel.abort(), 5000);
try {
await fetch('/api/device', { method: 'DELETE', signal: cancel.signal });
} catch (e) {
console.error(e);
} finally {
setDelLoading(false);
clearTimeout(timer);
}
}, []);

if (isLoading) {
return <Loading />;
Expand All @@ -30,7 +46,16 @@ export const StatusPage = (): JSX.Element => {
</Text>
<StatusCard {...data} />
<Spacer y={1} />
<Text h3>Devices</Text>
<Grid.Container justify="space-between" alignItems="center">
<Grid>
<Text h3>Devices</Text>
</Grid>
<Grid>
<Button auto color="error" size="sm" onClick={handleRemoveDead}>
{delLoading ? <Loading /> : 'Remove Dead'}
</Button>
</Grid>
</Grid.Container>
<DevicesTable devices={data.devices} workers={data.workers} />
<Spacer y={1} />
<Text h3>Workers</Text>
Expand Down
16 changes: 16 additions & 0 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,22 @@ const routes = async (fastifyInstance: FastifyInstance) => {
}
});

fastifyInstance.delete('/api/device', async (request, reply) => {
log.info('Received delete all devices request');
const deviceIds = Object.keys(controlConnections);
let deleted = 0;
for (const deviceId of deviceIds) {
const device = controlConnections[deviceId];
if (!device.isAlive) {
delete deviceInformation[deviceId];
delete controlConnections[deviceId];
deleted++;
}
}
log.info(`Deleted ${deleted} devices`);
return reply.code(200).send({ status: 'ok', error: `Deleted ${deleted} devices` });
});

interface ActionExecuteParams {
deviceId: keyof typeof controlConnections;
action: 'restart' | 'reboot' | 'getLogcat' | 'delete';
Expand Down

0 comments on commit c809013

Please sign in to comment.