fix(settings): update readMonitoringConfig to async and improve log reading efficiency

This commit is contained in:
Mauricio Siu
2025-07-05 14:43:49 -06:00
parent 5c73ced500
commit db9109a3be
2 changed files with 30 additions and 27 deletions
+4 -4
View File
@@ -609,14 +609,14 @@ export const settingsRouter = createTRPCRouter({
}, },
}) })
.input(apiReadStatsLogs) .input(apiReadStatsLogs)
.query(({ input }) => { .query(async ({ input }) => {
if (IS_CLOUD) { if (IS_CLOUD) {
return { return {
data: [], data: [],
totalCount: 0, totalCount: 0,
}; };
} }
const rawConfig = readMonitoringConfig( const rawConfig = await readMonitoringConfig(
!!input.dateRange?.start && !!input.dateRange?.end, !!input.dateRange?.start && !!input.dateRange?.end,
); );
@@ -652,11 +652,11 @@ export const settingsRouter = createTRPCRouter({
}) })
.optional(), .optional(),
) )
.query(({ input }) => { .query(async ({ input }) => {
if (IS_CLOUD) { if (IS_CLOUD) {
return []; return [];
} }
const rawConfig = readMonitoringConfig( const rawConfig = await readMonitoringConfig(
!!input?.dateRange?.start || !!input?.dateRange?.end, !!input?.dateRange?.start || !!input?.dateRange?.end,
); );
const processedLogs = processLogs(rawConfig as string, input?.dateRange); const processedLogs = processLogs(rawConfig as string, input?.dateRange);
@@ -1,5 +1,7 @@
import fs, { writeFileSync } from "node:fs"; import fs, { writeFileSync } from "node:fs";
import path from "node:path"; import path from "node:path";
import { createReadStream } from "node:fs";
import { createInterface } from "node:readline";
import { paths } from "@dokploy/server/constants"; import { paths } from "@dokploy/server/constants";
import type { Domain } from "@dokploy/server/services/domain"; import type { Domain } from "@dokploy/server/services/domain";
import { dump, load } from "js-yaml"; import { dump, load } from "js-yaml";
@@ -137,39 +139,40 @@ export const readRemoteConfig = async (serverId: string, appName: string) => {
} }
}; };
export const readMonitoringConfig = (readAll = false) => { export const readMonitoringConfig = async (readAll = false) => {
const { DYNAMIC_TRAEFIK_PATH } = paths(); const { DYNAMIC_TRAEFIK_PATH } = paths();
const configPath = path.join(DYNAMIC_TRAEFIK_PATH, "access.log"); const configPath = path.join(DYNAMIC_TRAEFIK_PATH, "access.log");
if (fs.existsSync(configPath)) { if (fs.existsSync(configPath)) {
if (!readAll) { if (!readAll) {
// Read first 500 lines // Read first 500 lines using streams
let content = ""; let content = "";
let chunk = "";
let validCount = 0; let validCount = 0;
for (const char of fs.readFileSync(configPath, "utf8")) { const fileStream = createReadStream(configPath, { encoding: "utf8" });
chunk += char; const readline = createInterface({
if (char === "\n") { input: fileStream,
try { crlfDelay: Number.POSITIVE_INFINITY,
const trimmed = chunk.trim(); });
if (
trimmed !== "" && for await (const line of readline) {
trimmed.startsWith("{") && try {
trimmed.endsWith("}") const trimmed = line.trim();
) { if (
const log = JSON.parse(trimmed); trimmed !== "" &&
if (log.ServiceName !== "dokploy-service-app@file") { trimmed.startsWith("{") &&
content += chunk; trimmed.endsWith("}")
validCount++; ) {
if (validCount >= 500) { const log = JSON.parse(trimmed);
break; if (log.ServiceName !== "dokploy-service-app@file") {
} content += `${line}\n`;
validCount++;
if (validCount >= 500) {
break;
} }
} }
} catch {
// Ignore invalid JSON
} }
chunk = ""; } catch {
// Ignore invalid JSON
} }
} }
return content; return content;