Files
EvoBGP/web/src/lib/settings/settings-api.ts
T
Denozordec db75126bea
CI / changes (push) Successful in 9s
CI / commitlint (push) Has been skipped
CI / openapi (push) Successful in 26s
CI / web (push) Successful in 33s
CI / go (push) Successful in 56s
CI / bird2 (push) Successful in 14s
CI / release (push) Successful in 20s
feat(runtime-logs): enhance auto-cleanup features and documentation
Added new endpoints for estimating and executing runtime log auto-cleanup based on tenant settings. Introduced configuration options for auto-cleanup policies, including scheduling and file size limits. Updated the API documentation and UI components to reflect these changes, improving user interaction with runtime log management. Enhanced error handling and added new UI elements for better visibility of audit logs and cleanup actions.
2026-06-12 22:44:39 +07:00

118 lines
3.7 KiB
TypeScript

import { apiJSON, apiMutate } from '$lib/api/client.js';
import type { AppSettings } from '$lib/api/types.js';
import { emptyBirdSettingsForm, type BirdSettingsForm } from './bird-settings.schema.js';
import {
emptyRevisionSettingsForm,
type RevisionSettingsForm
} from './revision-settings.schema.js';
import {
emptyRuntimeLogsSettingsForm,
type RuntimeLogsSettingsForm
} from './runtime-logs-settings.schema.js';
import {
BIRD_SETTING_KEYS,
BOOLEAN_SETTING_KEYS,
KNOWN_SETTING_KEYS,
NUMERIC_SETTING_KEYS,
RUNTIME_LOGS_SETTING_KEYS,
type BirdSettingKey,
type KnownSettingKey,
type RevisionSettingKey,
type RuntimeLogsSettingKey
} from './settings-known-keys.js';
export type AdditionalSettingEntry = { id: number; key: string; value: string };
export type PartitionedSettings = {
bird: BirdSettingsForm;
revision: RevisionSettingsForm;
runtimeLogs: RuntimeLogsSettingsForm;
additional: AdditionalSettingEntry[];
};
function parseKnownValue(key: KnownSettingKey, value: unknown): string {
if (NUMERIC_SETTING_KEYS.has(key)) {
if (typeof value === 'number' && Number.isFinite(value)) return String(value);
if (typeof value === 'string') return value;
return '';
}
if (typeof value === 'string') return value;
return '';
}
export function partitionSettings(
settings: AppSettings,
nextId = 1
): { partitioned: PartitionedSettings; nextId: number } {
const bird = emptyBirdSettingsForm();
const revision = emptyRevisionSettingsForm();
const runtimeLogs = emptyRuntimeLogsSettingsForm();
const additional: AdditionalSettingEntry[] = [];
let idCounter = nextId;
for (const [key, value] of Object.entries(settings as Record<string, unknown>)) {
if ((BIRD_SETTING_KEYS as readonly string[]).includes(key)) {
bird[key as BirdSettingKey] = parseKnownValue(key as KnownSettingKey, value);
} else if (key === 'revision_retention_minutes') {
revision.revision_retention_minutes = parseKnownValue(key as RevisionSettingKey, value);
} else if ((RUNTIME_LOGS_SETTING_KEYS as readonly string[]).includes(key)) {
const rk = key as RuntimeLogsSettingKey;
if (rk === 'runtime_logs_auto_enabled') {
runtimeLogs.runtime_logs_auto_enabled =
value === true || value === 1 || value === 'true' || value === '1' ? 'true' : 'false';
} else if (rk === 'runtime_logs_auto_mode') {
const m = String(value ?? '').trim();
runtimeLogs.runtime_logs_auto_mode =
m === 'delete' ? 'delete' : m === 'truncate' ? 'truncate' : '';
} else {
runtimeLogs[rk] = parseKnownValue(key as KnownSettingKey, value);
}
} else {
additional.push({
id: idCounter++,
key,
value: typeof value === 'string' ? value : String(value)
});
}
}
return {
partitioned: { bird, revision, runtimeLogs, additional },
nextId: idCounter
};
}
export async function loadSettings(): Promise<AppSettings> {
return apiJSON<AppSettings>('/v1/settings');
}
export async function patchSettings(
payload: Record<string, string | number | boolean>
): Promise<void> {
await apiMutate('/v1/settings', 'PATCH', payload);
}
export function buildPayloadFromFormFields(
keys: readonly KnownSettingKey[],
form: Record<string, string>,
errors: Partial<Record<string, string[]>>
): Record<string, string | number | boolean> {
const payload: Record<string, string | number | boolean> = {};
for (const key of keys) {
const value = String(form[key] ?? '').trim();
if (errors[key]?.length) continue;
if (BOOLEAN_SETTING_KEYS.has(key)) {
payload[key] = value === 'true';
continue;
}
if (!value) continue;
if (NUMERIC_SETTING_KEYS.has(key)) payload[key] = Number(value);
else payload[key] = value;
}
return payload;
}
export function isKnownSettingKey(key: string): key is KnownSettingKey {
return (KNOWN_SETTING_KEYS as readonly string[]).includes(key);
}