refactor(requests): simplify logic

This commit is contained in:
Mauricio Siu 2024-09-06 22:38:32 -06:00
parent d3a54163bf
commit e57f8a32ce
4 changed files with 123 additions and 82 deletions

View File

@ -11,6 +11,7 @@ import * as React from "react";
import { toast } from "sonner";
import { RequestDistributionChart } from "./request-distribution-chart";
import { RequestsTable } from "./requests-table";
import { DialogAction } from "@/components/shared/dialog-action";
export type LogEntry = NonNullable<
RouterOutputs["settings"]["readStatsLogs"]["data"]
@ -20,9 +21,8 @@ export const ShowRequests = () => {
const { data: isLogRotateActive, refetch: refetchLogRotate } =
api.settings.getLogRotateStatus.useQuery();
const { mutateAsync } = api.settings.activateLogRotate.useMutation();
const { mutateAsync: deactivateLogRotate } =
api.settings.deactivateLogRotate.useMutation();
const { mutateAsync: toggleLogRotate } =
api.settings.toggleLogRotate.useMutation();
const { data: isActive, refetch } =
api.settings.haveActivateRequests.useQuery();
@ -39,60 +39,57 @@ export const ShowRequests = () => {
<span>Showing web and API requests over time</span>
</CardDescription>
<div className="flex w-fit gap-4">
<Button
onClick={() => {
mutateAsync()
.then(async () => {
await toggleRequests({ enable: !isActive })
.then(() => {
refetch();
toast.success("Access Log Added to Traefik");
})
.catch((err) => {
toast.error(err.message);
});
<DialogAction
title={isActive ? "Deactivate Requests" : "Activate Requests"}
description="You will also need to restart Traefik to apply the changes"
onClick={async () => {
await toggleRequests({ enable: !isActive })
.then(() => {
refetch();
toast.success(
`Requests ${isActive ? "deactivated" : "activated"}`,
);
})
.catch((err) => {
toast.error(err.message);
});
}}
>
{isActive ? "Deactivate" : "Activate"}
</Button>
{!isLogRotateActive && (
<Button
variant="secondary"
onClick={() => {
mutateAsync()
.then(() => {
toast.success("Log rotate activated");
refetchLogRotate();
})
.catch((err) => {
toast.error(err.message);
});
}}
>
Activate Log Rotate
<Button>{isActive ? "Deactivate" : "Activate"}</Button>
</DialogAction>
<DialogAction
title={
isLogRotateActive
? "Activate Log Rotate"
: "Deactivate Log Rotate"
}
description={
isLogRotateActive
? "This will make the logs rotate on interval 1 day and maximum size of 100 MB and maximum 6 logs"
: "The log rotation will be disabled"
}
onClick={() => {
toggleLogRotate({
enable: !isLogRotateActive,
})
.then(() => {
toast.success(
`Log rotate ${isLogRotateActive ? "activated" : "deactivated"}`,
);
refetchLogRotate();
})
.catch((err) => {
toast.error(err.message);
});
}}
>
<Button variant="secondary">
{isLogRotateActive
? "Activate Log Rotate"
: "Deactivate Log Rotate"}
</Button>
)}
{isLogRotateActive && (
<Button
variant="secondary"
onClick={() => {
deactivateLogRotate()
.then(() => {
toast.success("Log rotate deactivated");
refetchLogRotate();
})
.catch((err) => {
toast.error(err.message);
});
}}
>
Deactivate Log Rotate
</Button>
)}
</DialogAction>
</div>
</div>
</CardHeader>

View File

@ -0,0 +1,47 @@
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { Button } from "../ui/button";
import { AlertCircle, TrashIcon } from "lucide-react";
interface Props {
title?: string;
description?: string;
onClick: () => void;
children?: React.ReactNode;
}
export const DialogAction = ({
onClick,
children,
description,
title,
}: Props) => {
return (
<AlertDialog>
<AlertDialogTrigger asChild>{children}</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
{title ?? "Are you absolutely sure?"}
</AlertDialogTitle>
<AlertDialogDescription>
{description ?? "This action cannot be undone."}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction onClick={onClick}>Confirm</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
};

View File

@ -373,16 +373,24 @@ export const settingsRouter = createTRPCRouter({
const processedLogs = processLogs(rawConfig as string);
return processedLogs || [];
}),
activateLogRotate: adminProcedure.mutation(async () => {
await logRotationManager.activate();
return true;
}),
deactivateLogRotate: adminProcedure.mutation(async () => {
return await logRotationManager.deactivate();
}),
getLogRotateStatus: adminProcedure.query(async () => {
return await logRotationManager.getStatus();
}),
toggleLogRotate: adminProcedure
.input(
z.object({
enable: z.boolean(),
}),
)
.mutation(async ({ input }) => {
if (input.enable) {
await logRotationManager.activate();
} else {
await logRotationManager.deactivate();
}
return true;
}),
haveActivateRequests: adminProcedure.query(async () => {
const config = readMainConfig();
@ -402,22 +410,17 @@ export const settingsRouter = createTRPCRouter({
}),
)
.mutation(async ({ input }) => {
const config = readMainConfig();
if (!config) return false;
const mainConfig = readMainConfig();
if (!mainConfig) return false;
const currentConfig = load(mainConfig) as {
accessLog?: {
filePath: string;
};
};
if (input.enable) {
const parsedConfig = load(config) as {
accessLog?: {
filePath: string;
format: string;
bufferingSize: number;
filters?: {
retryAttempts?: boolean;
minDuration?: string;
};
};
};
const config2 = {
const config = {
accessLog: {
filePath: "/etc/dokploy/traefik/dynamic/access.log",
format: "json",
@ -428,19 +431,13 @@ export const settingsRouter = createTRPCRouter({
},
},
};
parsedConfig.accessLog = config2.accessLog;
writeMainConfig(dump(parsedConfig));
currentConfig.accessLog = config.accessLog;
} else {
const parsedConfig = load(config) as {
accessLog?: {
filePath: string;
};
};
delete parsedConfig.accessLog;
writeMainConfig(dump(parsedConfig));
currentConfig.accessLog = undefined;
}
writeMainConfig(dump(currentConfig));
return true;
}),
});

View File

@ -99,7 +99,7 @@ class LogRotationManager {
await this.deactivateStream();
}
await execAsync(
"docker kill -s USR1 $(docker ps -q --filter name=traefik)",
"docker kill -s USR1 $(docker ps -q --filter name=dokploy-traefik)",
);
console.log("USR1 Signal send to Traefik");
} catch (error) {