Keep logs for a set number of days
Logs grew forever. A daily prune-logs cron now deletes entries older than the app setting logs_keep_days (Settings > General, super admin; default AppData.LogsKeepDays, 90), with an index on logs.created_at. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -8,6 +8,7 @@ import type { BunextPageServerFn } from "@moduletrace/bunext/types";
|
||||
import grabRunLimitsOverview from "@/src/functions/backend/db/app-settings/grab-run-limits";
|
||||
import grabSkillLimitsOverview from "@/src/functions/backend/db/app-settings/grab-skill-limits";
|
||||
import grabHookCallRetentionOverview from "@/src/functions/backend/db/app-settings/grab-hook-events-keep-days";
|
||||
import grabLogRetentionOverview from "@/src/functions/backend/db/app-settings/grab-logs-keep-days";
|
||||
import grabChatroomRetentionOverview from "@/src/functions/backend/db/app-settings/grab-chatroom-retention";
|
||||
import grabSessionLengthOverview from "@/src/functions/backend/db/app-settings/grab-bot-session-max-messages";
|
||||
import grabStorageQuotaOverview from "@/src/functions/backend/db/app-settings/grab-storage-quota-overview";
|
||||
@@ -53,6 +54,7 @@ export default async function grabSettingsServerProps({ req }: Params) {
|
||||
run_limits: await grabRunLimitsOverview().catch(() => null),
|
||||
skill_limits: await grabSkillLimitsOverview().catch(() => null),
|
||||
hook_call_retention: await grabHookCallRetentionOverview().catch(() => null),
|
||||
log_retention: await grabLogRetentionOverview().catch(() => null),
|
||||
chatroom_retention: await grabChatroomRetentionOverview().catch(() => null),
|
||||
session_length: await grabSessionLengthOverview().catch(() => null),
|
||||
running_version: runningVersion({ env: process.env }),
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
import { useState } from "react";
|
||||
import fetchApi from "@moduletrace/twui/utils/fetch/fetchApi";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import Form from "@moduletrace/twui/form/Form";
|
||||
import Input from "@moduletrace/twui/form/Input";
|
||||
import type { LogRetentionOverview } from "@/src/types";
|
||||
import SettingsSaveBar from "../../(partials)/settings-save-bar";
|
||||
|
||||
type Props = {
|
||||
retention?: LogRetentionOverview | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* How many days logs are kept, saved on its own without a reload.
|
||||
* Zero puts the code's default back.
|
||||
*/
|
||||
export default function LogRetentionForm({ retention }: Props) {
|
||||
const [overview, setOverview] = useState(retention || undefined);
|
||||
const [dirty, setDirty] = useState(false);
|
||||
const [keep_days, setKeepDays] = useState(String(retention?.keep_days ?? ""));
|
||||
/**
|
||||
* The input only reads `defaultValue` when it mounts, so discarding or
|
||||
* saving remounts it with the saved number.
|
||||
*/
|
||||
const [form_key, setFormKey] = useState(0);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [status, setStatus] = useState<{
|
||||
msg?: string;
|
||||
error?: boolean;
|
||||
success?: boolean;
|
||||
}>();
|
||||
|
||||
const save = async () => {
|
||||
if (!window.confirm("Save how long logs are kept? Entries older than that are deleted at the next daily prune.")) return;
|
||||
|
||||
setLoading(true);
|
||||
setStatus(undefined);
|
||||
|
||||
const res = await fetchApi<{}, APIResponseObject<LogRetentionOverview>>(
|
||||
"/api/admin/settings/log-retention",
|
||||
{
|
||||
method: "POST",
|
||||
body: { keep_days: Number(keep_days) || 0 },
|
||||
},
|
||||
).catch(() => null);
|
||||
|
||||
setLoading(false);
|
||||
|
||||
if (!res?.success || !res.singleRes) {
|
||||
setStatus({
|
||||
error: true,
|
||||
msg: res?.msg || `Couldn't save it.`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
setOverview(res.singleRes);
|
||||
setKeepDays(String(res.singleRes.keep_days));
|
||||
setFormKey((key) => key + 1);
|
||||
setDirty(false);
|
||||
setStatus({
|
||||
success: true,
|
||||
msg: `Saved: logs are kept for ${res.singleRes.keep_days} days.`,
|
||||
});
|
||||
};
|
||||
|
||||
const discard = () => {
|
||||
setKeepDays(String(overview?.keep_days ?? ""));
|
||||
setFormKey((key) => key + 1);
|
||||
setDirty(false);
|
||||
setStatus(undefined);
|
||||
};
|
||||
|
||||
return (
|
||||
<Form
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
save().catch(() => {});
|
||||
}}
|
||||
className="w-full gap-3! items-stretch"
|
||||
>
|
||||
<Input
|
||||
key={`keep-${form_key}`}
|
||||
label="Keep activity for"
|
||||
type="number"
|
||||
min={0}
|
||||
max={overview?.max}
|
||||
step={1}
|
||||
numberText
|
||||
rawNumber
|
||||
defaultValue={keep_days}
|
||||
onChange={(event) => {
|
||||
setKeepDays(event.target.value);
|
||||
setDirty(true);
|
||||
setStatus(undefined);
|
||||
}}
|
||||
suffix={`days`}
|
||||
info={`Every cron run writes an entry, so a long time grows the database. 0 uses the default (${overview?.default_days ?? ""}).`}
|
||||
/>
|
||||
<SettingsSaveBar
|
||||
dirty={dirty}
|
||||
loading={loading}
|
||||
status={status}
|
||||
save_label="Save"
|
||||
onDiscard={discard}
|
||||
/>
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { useContext } from "react";
|
||||
import { AppContext } from "@/src/pages/__root";
|
||||
import isPassThroughUser from "@/src/utils/rbac/is-pass-through-user";
|
||||
import SettingsGroup from "../../(partials)/settings-group";
|
||||
import LogRetentionForm from "../(partials)/log-retention-form";
|
||||
|
||||
/**
|
||||
* How long logs (Activity) are kept. Only the super admin sees it, since
|
||||
* only the super admin reads them.
|
||||
*/
|
||||
export default function LogRetentionSection() {
|
||||
const { pageProps } = useContext(AppContext);
|
||||
|
||||
if (!isPassThroughUser({ user_types: pageProps?.user_types })) return null;
|
||||
|
||||
return (
|
||||
<SettingsGroup
|
||||
title="Activity log"
|
||||
description="How long sign-ins, routine runs, errors and other events in Activity are kept. Older entries are deleted once a day."
|
||||
id="activity-log"
|
||||
>
|
||||
<LogRetentionForm retention={pageProps?.log_retention} />
|
||||
</SettingsGroup>
|
||||
);
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import ChatroomRetentionSection from "./(sections)/chatroom-retention-section";
|
||||
import HookCallRetentionSection from "./(sections)/hook-call-retention-section";
|
||||
import InstallIconSection from "./(sections)/install-icon-section";
|
||||
import InstallNameSection from "./(sections)/install-name-section";
|
||||
import LogRetentionSection from "./(sections)/log-retention-section";
|
||||
import NotificationsSection from "./(sections)/notifications-section";
|
||||
import PromptSection from "./(sections)/prompt-section";
|
||||
import RunLimitsSection from "./(sections)/run-limits-section";
|
||||
@@ -29,6 +30,7 @@ export default function AdminSettingsGeneralPage() {
|
||||
<SkillLimitsSection />
|
||||
<HookCallRetentionSection />
|
||||
<ChatroomRetentionSection />
|
||||
<LogRetentionSection />
|
||||
<StorageSection />
|
||||
<VersionSection />
|
||||
</SettingsShell>
|
||||
|
||||
Reference in New Issue
Block a user