Skip to content

Commit

Permalink
feat: Add log deletion API endpoint with filtering options
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc committed Jan 2, 2025
1 parent da25b2f commit bd59326
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
62 changes: 62 additions & 0 deletions api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2603,6 +2603,68 @@ paths:
schema:
$ref: "#/components/schemas/ApiResponseTrue"
/log:
delete:
operationId: clearLog
summary: Clear log
parameters:
- name: type
in: query
schema:
type: string
description: Log type filter
- name: min_level
in: query
schema:
$ref: "#/components/schemas/LogLevel"
description: Minimum log level filter
- name: max_level
in: query
schema:
$ref: "#/components/schemas/LogLevel"
description: Maximum log level filter
- name: deleted_level
in: query
schema:
type: array
items:
type: integer
description: List of log levels to delete
- name: end_time
in: query
schema:
type: string
format: date-time
description: End time for log deletion
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/ApiResponseTrue"
"400":
description: Bad request
content:
application/json:
schema:
$ref: "#/components/schemas/ApiResponseError"
example:
{ "ok": false, "status": 1, "error": "Failed to parse end_time." }
"401":
description: Authorization information is missing or invalid
content:
application/json:
schema:
$ref: "#/components/schemas/ApiResponseError"
example: { "ok": false, "status": 401, "error": "Unauthorized" }
"403":
description: Permission denied
content:
application/json:
schema:
$ref: "#/components/schemas/ApiResponseError"
example:
{ "ok": false, "status": 403, "error": "Permission denied." }
get:
operationId: queryLog
summary: Query log
Expand Down
33 changes: 32 additions & 1 deletion routes/api/log.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
import { Handlers } from "$fresh/server.ts";
import { return_data, return_error } from "../../server/utils.ts";
import { User, UserPermission } from "../../db.ts";
import { parse_int } from "../../server/parse_form.ts";
import { get_string, parse_int } from "../../server/parse_form.ts";
import { base_logger, LogLevel } from "../../utils/logger.ts";

export const handler: Handlers = {
async DELETE(req, ctx) {
const user = <User | undefined> ctx.state.user;
if (
user && !user.is_admin &&
!(Number(user.permissions) & UserPermission.QueryLog)
) {
return return_error(403, "Permission denied.");
}
let form: FormData | undefined;
try {
form = await req.formData();
} catch (_) {
return return_error(400, "Bad Request");
}
const typ = await get_string(form.get("type"));
const min_level = await parse_int(form.get("min_level"), null);
const max_level = await parse_int(form.get("max_level"), null);
const deleted_level = (await get_string(form.get("deleted_level")))
?.split(",")?.map((x) => parseInt(x))?.filter((x) => !isNaN(x));
const end = await get_string(form.get("end_time"));
const endt = parseInt(end ?? "");
let end_time: Date | null;
try {
end_time = end === null ? null : new Date(isNaN(endt) ? end : endt);
} catch (e) {
base_logger.log("Failed to parse end_time:", e);
return return_error(1, "Failed to parse end_time.");
}
base_logger.clear(typ, min_level, max_level, deleted_level, end_time);
return return_data(true);
},
async GET(req, ctx) {
const user = <User | undefined> ctx.state.user;
if (
Expand Down

0 comments on commit bd59326

Please sign in to comment.