mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
refactor(requests): simplify logic
This commit is contained in:
@@ -11,6 +11,7 @@ import * as React from "react";
|
|||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { RequestDistributionChart } from "./request-distribution-chart";
|
import { RequestDistributionChart } from "./request-distribution-chart";
|
||||||
import { RequestsTable } from "./requests-table";
|
import { RequestsTable } from "./requests-table";
|
||||||
|
import { DialogAction } from "@/components/shared/dialog-action";
|
||||||
|
|
||||||
export type LogEntry = NonNullable<
|
export type LogEntry = NonNullable<
|
||||||
RouterOutputs["settings"]["readStatsLogs"]["data"]
|
RouterOutputs["settings"]["readStatsLogs"]["data"]
|
||||||
@@ -20,9 +21,8 @@ export const ShowRequests = () => {
|
|||||||
const { data: isLogRotateActive, refetch: refetchLogRotate } =
|
const { data: isLogRotateActive, refetch: refetchLogRotate } =
|
||||||
api.settings.getLogRotateStatus.useQuery();
|
api.settings.getLogRotateStatus.useQuery();
|
||||||
|
|
||||||
const { mutateAsync } = api.settings.activateLogRotate.useMutation();
|
const { mutateAsync: toggleLogRotate } =
|
||||||
const { mutateAsync: deactivateLogRotate } =
|
api.settings.toggleLogRotate.useMutation();
|
||||||
api.settings.deactivateLogRotate.useMutation();
|
|
||||||
|
|
||||||
const { data: isActive, refetch } =
|
const { data: isActive, refetch } =
|
||||||
api.settings.haveActivateRequests.useQuery();
|
api.settings.haveActivateRequests.useQuery();
|
||||||
@@ -39,60 +39,57 @@ export const ShowRequests = () => {
|
|||||||
<span>Showing web and API requests over time</span>
|
<span>Showing web and API requests over time</span>
|
||||||
</CardDescription>
|
</CardDescription>
|
||||||
<div className="flex w-fit gap-4">
|
<div className="flex w-fit gap-4">
|
||||||
<Button
|
<DialogAction
|
||||||
onClick={() => {
|
title={isActive ? "Deactivate Requests" : "Activate Requests"}
|
||||||
mutateAsync()
|
description="You will also need to restart Traefik to apply the changes"
|
||||||
.then(async () => {
|
onClick={async () => {
|
||||||
await toggleRequests({ enable: !isActive })
|
await toggleRequests({ enable: !isActive })
|
||||||
.then(() => {
|
.then(() => {
|
||||||
refetch();
|
refetch();
|
||||||
toast.success("Access Log Added to Traefik");
|
toast.success(
|
||||||
})
|
`Requests ${isActive ? "deactivated" : "activated"}`,
|
||||||
.catch((err) => {
|
);
|
||||||
toast.error(err.message);
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
toast.error(err.message);
|
toast.error(err.message);
|
||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{isActive ? "Deactivate" : "Activate"}
|
<Button>{isActive ? "Deactivate" : "Activate"}</Button>
|
||||||
</Button>
|
</DialogAction>
|
||||||
{!isLogRotateActive && (
|
|
||||||
<Button
|
<DialogAction
|
||||||
variant="secondary"
|
title={
|
||||||
onClick={() => {
|
isLogRotateActive
|
||||||
mutateAsync()
|
? "Activate Log Rotate"
|
||||||
.then(() => {
|
: "Deactivate Log Rotate"
|
||||||
toast.success("Log rotate activated");
|
}
|
||||||
refetchLogRotate();
|
description={
|
||||||
})
|
isLogRotateActive
|
||||||
.catch((err) => {
|
? "This will make the logs rotate on interval 1 day and maximum size of 100 MB and maximum 6 logs"
|
||||||
toast.error(err.message);
|
: "The log rotation will be disabled"
|
||||||
});
|
}
|
||||||
}}
|
onClick={() => {
|
||||||
>
|
toggleLogRotate({
|
||||||
Activate Log Rotate
|
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>
|
</Button>
|
||||||
)}
|
</DialogAction>
|
||||||
{isLogRotateActive && (
|
|
||||||
<Button
|
|
||||||
variant="secondary"
|
|
||||||
onClick={() => {
|
|
||||||
deactivateLogRotate()
|
|
||||||
.then(() => {
|
|
||||||
toast.success("Log rotate deactivated");
|
|
||||||
refetchLogRotate();
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
toast.error(err.message);
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
Deactivate Log Rotate
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
|
|||||||
47
apps/dokploy/components/shared/dialog-action.tsx
Normal file
47
apps/dokploy/components/shared/dialog-action.tsx
Normal 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>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -373,16 +373,24 @@ export const settingsRouter = createTRPCRouter({
|
|||||||
const processedLogs = processLogs(rawConfig as string);
|
const processedLogs = processLogs(rawConfig as string);
|
||||||
return processedLogs || [];
|
return processedLogs || [];
|
||||||
}),
|
}),
|
||||||
activateLogRotate: adminProcedure.mutation(async () => {
|
|
||||||
await logRotationManager.activate();
|
|
||||||
return true;
|
|
||||||
}),
|
|
||||||
deactivateLogRotate: adminProcedure.mutation(async () => {
|
|
||||||
return await logRotationManager.deactivate();
|
|
||||||
}),
|
|
||||||
getLogRotateStatus: adminProcedure.query(async () => {
|
getLogRotateStatus: adminProcedure.query(async () => {
|
||||||
return await logRotationManager.getStatus();
|
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 () => {
|
haveActivateRequests: adminProcedure.query(async () => {
|
||||||
const config = readMainConfig();
|
const config = readMainConfig();
|
||||||
|
|
||||||
@@ -402,22 +410,17 @@ export const settingsRouter = createTRPCRouter({
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.mutation(async ({ input }) => {
|
.mutation(async ({ input }) => {
|
||||||
const config = readMainConfig();
|
const mainConfig = readMainConfig();
|
||||||
if (!config) return false;
|
if (!mainConfig) return false;
|
||||||
|
|
||||||
|
const currentConfig = load(mainConfig) as {
|
||||||
|
accessLog?: {
|
||||||
|
filePath: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
if (input.enable) {
|
if (input.enable) {
|
||||||
const parsedConfig = load(config) as {
|
const config = {
|
||||||
accessLog?: {
|
|
||||||
filePath: string;
|
|
||||||
format: string;
|
|
||||||
bufferingSize: number;
|
|
||||||
filters?: {
|
|
||||||
retryAttempts?: boolean;
|
|
||||||
minDuration?: string;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
const config2 = {
|
|
||||||
accessLog: {
|
accessLog: {
|
||||||
filePath: "/etc/dokploy/traefik/dynamic/access.log",
|
filePath: "/etc/dokploy/traefik/dynamic/access.log",
|
||||||
format: "json",
|
format: "json",
|
||||||
@@ -428,19 +431,13 @@ export const settingsRouter = createTRPCRouter({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
parsedConfig.accessLog = config2.accessLog;
|
currentConfig.accessLog = config.accessLog;
|
||||||
writeMainConfig(dump(parsedConfig));
|
|
||||||
} else {
|
} else {
|
||||||
const parsedConfig = load(config) as {
|
currentConfig.accessLog = undefined;
|
||||||
accessLog?: {
|
|
||||||
filePath: string;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
delete parsedConfig.accessLog;
|
|
||||||
writeMainConfig(dump(parsedConfig));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
writeMainConfig(dump(currentConfig));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ class LogRotationManager {
|
|||||||
await this.deactivateStream();
|
await this.deactivateStream();
|
||||||
}
|
}
|
||||||
await execAsync(
|
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");
|
console.log("USR1 Signal send to Traefik");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user