new docker-log domain,service, repo
This commit is contained in:
1 parent
3e06cd39b1
commit
6ea4ca1aa9
5 files changed
+288
No files matched your search
@@ -0,0 +1,60 @@
|
||||
import { Schema, model, Types } from "mongoose";
|
||||
import { DockerLogStreams, type DockerLogLine } from "@/domain/docker/docker.type.js";
|
||||
import type { DockerLog, DockerLogMetadata } from "@/domain/docker/docker-log.type.js";
|
||||
|
||||
type DockerLogMetadataDocument = Omit<DockerLogMetadata, "monitorId" | "teamId"> & {
|
||||
monitorId: Types.ObjectId;
|
||||
teamId: Types.ObjectId;
|
||||
};
|
||||
|
||||
type DockerLogDocumentBase = Omit<DockerLog, "id" | "metadata" | "checkedAt" | "expiry" | "createdAt" | "updatedAt"> & {
|
||||
metadata: DockerLogMetadataDocument;
|
||||
checkedAt: Date;
|
||||
expiry: Date;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
};
|
||||
|
||||
export interface DockerLogDocument extends DockerLogDocumentBase {
|
||||
_id: Types.ObjectId;
|
||||
}
|
||||
|
||||
const dockerLogMetadataSchema = new Schema<DockerLogMetadataDocument>(
|
||||
{
|
||||
monitorId: { type: Schema.Types.ObjectId, required: true },
|
||||
teamId: { type: Schema.Types.ObjectId, required: true },
|
||||
containerId: { type: String, required: true },
|
||||
containerName: { type: String, required: true },
|
||||
},
|
||||
{ _id: false }
|
||||
);
|
||||
|
||||
const dockerLogLineSchema = new Schema<DockerLogLine>(
|
||||
{
|
||||
ts: { type: String, required: true },
|
||||
stream: { type: String, enum: DockerLogStreams, required: true },
|
||||
text: { type: String, default: "" },
|
||||
},
|
||||
{ _id: false }
|
||||
);
|
||||
|
||||
const DockerLogSchema = new Schema<DockerLogDocument>(
|
||||
{
|
||||
metadata: { type: dockerLogMetadataSchema, required: true },
|
||||
lines: { type: [dockerLogLineSchema], required: true, default: [] },
|
||||
gap: { type: Boolean, default: false },
|
||||
checkedAt: { type: Date, required: true },
|
||||
expiry: { type: Date, required: true, index: { expires: 0 } }, // TTL: Mongo deletes the doc once `expiry` passes
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
DockerLogSchema.index({ "metadata.monitorId": 1, "metadata.containerName": 1, checkedAt: -1 }); // API paging
|
||||
DockerLogSchema.index({ "metadata.monitorId": 1, "metadata.containerId": 1, checkedAt: -1 }); // cursor seed
|
||||
DockerLogSchema.index({ "metadata.teamId": 1 });
|
||||
|
||||
const DockerLogModel = model<DockerLogDocument>("DockerLog", DockerLogSchema);
|
||||
|
||||
export type { DockerLogMetadataDocument };
|
||||
export { DockerLogModel };
|
||||
export default DockerLogModel;
|
||||
@@ -0,0 +1,10 @@
|
||||
import { DockerLog } from "@/domain/docker/docker-log.type.js";
|
||||
|
||||
export interface IDockerLogsRepository {
|
||||
createDockerLogs(logs: Omit<DockerLog, "id" | "createdAt" | "updatedAt">[]): Promise<number>;
|
||||
findLastLineTimestamp(monitorId: string, containerId: string): Promise<string | null>;
|
||||
findByContainerName(args: { monitorId: string; containerName: string; before?: Date; limit: number }): Promise<DockerLog[]>;
|
||||
deleteByMonitorId(monitorId: string): Promise<number>;
|
||||
deleteByTeamId(teamId: string): Promise<number>;
|
||||
deleteByMonitorIdsNotIn(monitorIds: string[]): Promise<number>;
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
import DockerLogModel, { DockerLogDocument } from "@/domain/docker/docker-log.model.js";
|
||||
import { IDockerLogsRepository } from "@/domain/docker/docker-log.repository.interface.js";
|
||||
import { DockerLog } from "@/domain/docker/docker-log.type.js";
|
||||
import { toDateString, toStringId } from "@/utils/mongoMappers.js";
|
||||
import mongoose from "mongoose";
|
||||
|
||||
const SERVICE_NAME = "DockerLogsRepository";
|
||||
|
||||
class MongoDockerLogsRepository implements IDockerLogsRepository {
|
||||
static SERVICE_NAME = SERVICE_NAME;
|
||||
|
||||
private toEntity = (doc: DockerLogDocument): DockerLog => {
|
||||
return {
|
||||
id: toStringId(doc._id),
|
||||
metadata: {
|
||||
monitorId: toStringId(doc.metadata.monitorId),
|
||||
teamId: toStringId(doc.metadata.teamId),
|
||||
containerId: doc.metadata.containerId,
|
||||
containerName: doc.metadata.containerName,
|
||||
},
|
||||
lines: (doc.lines ?? []).map((line) => ({ ts: line.ts, stream: line.stream, text: line.text ?? "" })),
|
||||
gap: doc.gap ?? false,
|
||||
checkedAt: toDateString(doc.checkedAt),
|
||||
expiry: toDateString(doc.expiry),
|
||||
createdAt: toDateString(doc.createdAt),
|
||||
updatedAt: toDateString(doc.updatedAt),
|
||||
};
|
||||
};
|
||||
|
||||
createDockerLogs = async (logs: Omit<DockerLog, "id" | "createdAt" | "updatedAt">[]): Promise<number> => {
|
||||
if (logs.length === 0) return 0;
|
||||
const docs = await DockerLogModel.insertMany(
|
||||
logs.map((log) => {
|
||||
return {
|
||||
metadata: {
|
||||
monitorId: new mongoose.Types.ObjectId(log.metadata.monitorId),
|
||||
teamId: new mongoose.Types.ObjectId(log.metadata.teamId),
|
||||
containerId: log.metadata.containerId,
|
||||
containerName: log.metadata.containerName,
|
||||
},
|
||||
lines: log.lines,
|
||||
gap: log.gap,
|
||||
checkedAt: new Date(log.checkedAt),
|
||||
expiry: new Date(log.expiry),
|
||||
};
|
||||
}),
|
||||
{ ordered: false }
|
||||
);
|
||||
return docs.length;
|
||||
};
|
||||
|
||||
findLastLineTimestamp = async (monitorId: string, containerId: string): Promise<string | null> => {
|
||||
const doc = await DockerLogModel.findOne({
|
||||
"metadata.monitorId": new mongoose.Types.ObjectId(monitorId),
|
||||
"metadata.containerId": containerId,
|
||||
})
|
||||
.sort({ checkedAt: -1 })
|
||||
.select({ lines: { $slice: -1 } })
|
||||
.lean<Pick<DockerLogDocument, "lines">>();
|
||||
return doc?.lines?.[0]?.ts ?? null;
|
||||
};
|
||||
|
||||
findByContainerName = async ({
|
||||
monitorId,
|
||||
containerName,
|
||||
before,
|
||||
limit,
|
||||
}: {
|
||||
monitorId: string;
|
||||
containerName: string;
|
||||
before?: Date;
|
||||
limit: number;
|
||||
}): Promise<DockerLog[]> => {
|
||||
const docs = await DockerLogModel.find({
|
||||
"metadata.monitorId": new mongoose.Types.ObjectId(monitorId),
|
||||
"metadata.containerName": containerName,
|
||||
...(before ? { checkedAt: { $lt: before } } : {}),
|
||||
})
|
||||
.sort({ checkedAt: -1 })
|
||||
.limit(limit)
|
||||
.lean<DockerLogDocument[]>();
|
||||
return docs.map(this.toEntity);
|
||||
};
|
||||
|
||||
deleteByMonitorId = async (monitorId: string) =>
|
||||
(await DockerLogModel.deleteMany({ "metadata.monitorId": new mongoose.Types.ObjectId(monitorId) })).deletedCount ?? 0;
|
||||
|
||||
deleteByTeamId = async (teamId: string) =>
|
||||
(await DockerLogModel.deleteMany({ "metadata.teamId": new mongoose.Types.ObjectId(teamId) })).deletedCount ?? 0;
|
||||
|
||||
deleteByMonitorIdsNotIn = async (monitorIds: string[]) =>
|
||||
(await DockerLogModel.deleteMany({ "metadata.monitorId": { $nin: monitorIds.map((id) => new mongoose.Types.ObjectId(id)) } })).deletedCount ?? 0;
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
import { IDockerLogsRepository } from "@/domain/docker/docker-log.repository.interface.js";
|
||||
import { DOCKER_LOG_RETENTION_DAYS, DockerLog } from "@/domain/docker/docker-log.type.js";
|
||||
import { DOCKER_LOG_TAIL_LINES, DockerLogLine } from "@/domain/docker/docker.type.js";
|
||||
import { DockerStatusPayload, MonitorStatusResponse } from "@/types/network.js";
|
||||
import { ILogger } from "@/utils/logger.js";
|
||||
import mongoose from "mongoose";
|
||||
|
||||
const SERVICE_NAME = "DockerLogsService";
|
||||
const MS_PER_DAY = 24 * 60 * 60 * 1000;
|
||||
|
||||
export interface IDockerLogsService {
|
||||
buildDockerLogs(status: MonitorStatusResponse<DockerStatusPayload>): Promise<DockerLog[]>;
|
||||
createDockerLogs(logs: DockerLog[]): Promise<number>;
|
||||
}
|
||||
|
||||
export class DockerLogsSerivce implements IDockerLogsService {
|
||||
static SERVICE_NAME = SERVICE_NAME;
|
||||
|
||||
private logger: ILogger;
|
||||
private dockerLogsRepository: IDockerLogsRepository;
|
||||
|
||||
private lastTimestampByContainer = new Map<string, string>();
|
||||
|
||||
constructor({ logger, dockerLogsRepository }: { logger: ILogger; dockerLogsRepository: IDockerLogsRepository }) {
|
||||
this.logger = logger;
|
||||
this.dockerLogsRepository = dockerLogsRepository;
|
||||
}
|
||||
|
||||
private generateCursorKey = (monitorId: string, containerId: string) => `${monitorId}:${containerId}`;
|
||||
|
||||
private getLastTimestamp = async (monitorId: string, containerId: string) => {
|
||||
const key = this.generateCursorKey(monitorId, containerId);
|
||||
const cached = this.lastTimestampByContainer.get(key);
|
||||
if (cached !== undefined) return cached || null;
|
||||
const stored = await this.dockerLogsRepository.findLastLineTimestamp(monitorId, containerId);
|
||||
this.lastTimestampByContainer.set(key, stored ?? "");
|
||||
return stored;
|
||||
};
|
||||
|
||||
private newLines = (lines: DockerLogLine[], lastTimestamp: string | null): DockerLogLine[] => {
|
||||
return lastTimestamp
|
||||
? lines.filter((line) => {
|
||||
return line.ts > lastTimestamp;
|
||||
})
|
||||
: lines;
|
||||
};
|
||||
|
||||
private maxTimestamp = (lines: DockerLogLine[]) => {
|
||||
return lines.reduce((max, line) => (line.ts > max ? line.ts : max), "");
|
||||
};
|
||||
|
||||
buildDockerLogs = async (status: MonitorStatusResponse<DockerStatusPayload>): Promise<DockerLog[]> => {
|
||||
const payload = typeof status.payload === "object" ? status.payload : null;
|
||||
const containerLogs = payload?.logs ?? [];
|
||||
if (containerLogs.length === 0) return [];
|
||||
|
||||
const now = new Date();
|
||||
const checkedAt = now.toISOString();
|
||||
const expiry = new Date(now.getTime() + DOCKER_LOG_RETENTION_DAYS * MS_PER_DAY).toISOString();
|
||||
const results: DockerLog[] = [];
|
||||
|
||||
for (const dockerContainerLog of containerLogs) {
|
||||
const { containerId, containerName, lines } = dockerContainerLog;
|
||||
try {
|
||||
const lastTimestamp = await this.getLastTimestamp(status.monitorId, containerId);
|
||||
const fresh = this.newLines(lines, lastTimestamp);
|
||||
if (fresh.length === 0) continue;
|
||||
this.lastTimestampByContainer.set(this.generateCursorKey(status.monitorId, containerId), this.maxTimestamp(fresh));
|
||||
results.push({
|
||||
id: new mongoose.Types.ObjectId().toString(),
|
||||
metadata: {
|
||||
monitorId: status.monitorId,
|
||||
teamId: status.teamId,
|
||||
containerId,
|
||||
containerName,
|
||||
},
|
||||
lines: fresh,
|
||||
gap: fresh.length === DOCKER_LOG_TAIL_LINES,
|
||||
checkedAt,
|
||||
expiry,
|
||||
createdAt: checkedAt,
|
||||
updatedAt: checkedAt,
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
this.logger.warn({
|
||||
message: `Failed to build docker logs for container ${containerName}`,
|
||||
service: SERVICE_NAME,
|
||||
method: "buildDockerLogs",
|
||||
details: { monitorId: status.monitorId, containerId, error: error instanceof Error ? error.message : String(error) },
|
||||
});
|
||||
}
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
createDockerLogs = async (logs: DockerLog[]): Promise<number> => this.dockerLogsRepository.createDockerLogs(logs);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { DockerLogLine } from "@/domain/docker/docker.type.js";
|
||||
|
||||
export const DOCKER_LOG_RETENTION_DAYS = 7;
|
||||
export const DOCKER_LOG_PAGE_DEFAULT = 20;
|
||||
export const DOCKER_LOG_PAGE_MAX = 50;
|
||||
|
||||
export interface DockerLogMetadata {
|
||||
monitorId: string;
|
||||
teamId: string;
|
||||
containerId: string;
|
||||
containerName: string;
|
||||
}
|
||||
|
||||
export interface DockerLog {
|
||||
id: string;
|
||||
metadata: DockerLogMetadata;
|
||||
lines: DockerLogLine[];
|
||||
gap: boolean;
|
||||
checkedAt: string;
|
||||
expiry: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export interface DockerLogPage {
|
||||
logs: DockerLog[];
|
||||
nextCursor: string | null;
|
||||
}
|
||||
Reference in new issue
Block a user