getallembedchecks
This commit is contained in:
1 parent
6e341108bb
commit
7ef1803cf0
4 files changed
+189
-97
No files matched your search
@@ -1,5 +1,14 @@
|
|||||||
import type { CheckEntity } from "@/types/domain/index.js";
|
import type { CheckEntity } from "@/types/domain/index.js";
|
||||||
|
import type { LatestChecksByMonitor } from "@/repositories/checks/MongoCheckRepository.js";
|
||||||
export interface IChecksRepository {}
|
export interface IChecksRepository {
|
||||||
|
// Create
|
||||||
|
// Single fetch
|
||||||
|
// Collection fetch
|
||||||
|
findLatestChecksByMonitorIds(
|
||||||
|
monitorIds: string[]
|
||||||
|
): Promise<LatestChecksByMonitor>;
|
||||||
|
// Update
|
||||||
|
// Delete
|
||||||
|
}
|
||||||
|
|
||||||
export default IChecksRepository;
|
export default IChecksRepository;
|
||||||
@@ -1,4 +1,66 @@
|
|||||||
import type { IChecksRepository } from "@/repositories/index.js";
|
import type { IChecksRepository } from "@/repositories/index.js";
|
||||||
class MongoCheckRepository implements IChecksRepository {}
|
import { CheckEntity } from "@/types/domain/check.js";
|
||||||
|
import type { ICheck } from "@/db/models/index.js";
|
||||||
|
import { Check } from "@/db/models/index.js";
|
||||||
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
|
export type LatestChecksByMonitor = Array<{
|
||||||
|
id: string;
|
||||||
|
latestChecks: CheckEntity[];
|
||||||
|
}>;
|
||||||
|
|
||||||
|
class MongoCheckRepository implements IChecksRepository {
|
||||||
|
private toEntity = (check: ICheck): CheckEntity => {
|
||||||
|
const entity: CheckEntity = {
|
||||||
|
id: check._id.toString(),
|
||||||
|
monitorId: check.metadata.monitorId.toString(),
|
||||||
|
teamId: check.metadata.teamId.toString(),
|
||||||
|
type: check.metadata.type,
|
||||||
|
status: check.status,
|
||||||
|
httpStatusCode: check.httpStatusCode,
|
||||||
|
message: check.message,
|
||||||
|
responseTime: check.responseTime,
|
||||||
|
timings: check.timings,
|
||||||
|
errorMessage: check.errorMessage,
|
||||||
|
ackAt: check.ackAt,
|
||||||
|
ackBy: check.ackBy?.toString(),
|
||||||
|
system: check.system,
|
||||||
|
capture: check.capture,
|
||||||
|
lighthouse: check.lighthouse,
|
||||||
|
dockerContainers: check.dockerContainers,
|
||||||
|
createdAt: check.createdAt,
|
||||||
|
updatedAt: check.updatedAt,
|
||||||
|
expiry: check.expiry,
|
||||||
|
};
|
||||||
|
|
||||||
|
return entity;
|
||||||
|
};
|
||||||
|
|
||||||
|
findLatestChecksByMonitorIds = async (monitorIds: string[]) => {
|
||||||
|
const mongoIds = monitorIds.map((id) => new mongoose.Types.ObjectId(id));
|
||||||
|
const checkMap = await Check.aggregate([
|
||||||
|
{
|
||||||
|
$match: {
|
||||||
|
"metadata.monitorId": { $in: mongoIds },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ $sort: { createdAt: -1 } },
|
||||||
|
{
|
||||||
|
$group: {
|
||||||
|
_id: "$metadata.monitorId",
|
||||||
|
latestChecks: { $push: "$$ROOT" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$project: {
|
||||||
|
latestChecks: { $slice: [{ $ifNull: ["$latestChecks", []] }, 25] },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
return checkMap.map((cm) => {
|
||||||
|
return { id: cm._id, latestChecks: cm.latestChecks };
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export default MongoCheckRepository;
|
export default MongoCheckRepository;
|
||||||
@@ -160,32 +160,12 @@ class MonitorService implements IMonitorService {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const monitorIds = monitors.map((m) => new mongoose.Types.ObjectId(m.id));
|
const checks = await this.checksRepository.findLatestChecksByMonitorIds(
|
||||||
|
monitors.map((m) => m.id)
|
||||||
const checks = await Check.aggregate([
|
|
||||||
{
|
|
||||||
$match: {
|
|
||||||
"metadata.monitorId": { $in: monitorIds },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ $sort: { createdAt: -1 } },
|
|
||||||
{
|
|
||||||
$group: {
|
|
||||||
_id: "$metadata.monitorId",
|
|
||||||
latestChecks: { $push: "$$ROOT" },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$project: {
|
|
||||||
latestChecks: { $slice: [{ $ifNull: ["$latestChecks", []] }, 25] },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
const checksMap = new Map(
|
|
||||||
checks.map((c: any) => [c._id.toString(), c.latestChecks])
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const checksMap = new Map(checks.map((c: any) => [c.id, c.latestChecks]));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
count: counts.total,
|
count: counts.total,
|
||||||
upCount: counts.upCount,
|
upCount: counts.upCount,
|
||||||
|
|||||||
@@ -1,96 +1,137 @@
|
|||||||
import type { MonitorStatus } from "@/types/domain/index.js";
|
import type { MonitorStatus, MonitorType } from "@/types/domain/index.js";
|
||||||
|
import type { GotTimings } from "@/db/models/checks/Check.js";
|
||||||
|
|
||||||
export interface CheckEntityBase {
|
export interface ICpuInfo {
|
||||||
id: string;
|
physical_core: number;
|
||||||
monitorId: string;
|
logical_core: number;
|
||||||
status: MonitorStatus;
|
frequency: number;
|
||||||
responseTime: number;
|
current_frequency: number;
|
||||||
createdAt: Date;
|
temperature: number[]; // per-core temps
|
||||||
|
free_percent: number;
|
||||||
|
usage_percent: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optional payloads for different monitor types
|
export interface IMemoryInfo {
|
||||||
export interface PageSpeedPayload {
|
total_bytes: number;
|
||||||
lighthouse?: Record<string, unknown>;
|
available_bytes: number;
|
||||||
|
used_bytes: number;
|
||||||
|
usage_percent: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface InfraCpuMetrics {
|
export interface IHostInfo {
|
||||||
physical_core?: number;
|
|
||||||
logical_core?: number;
|
|
||||||
frequency?: number;
|
|
||||||
current_frequency?: number;
|
|
||||||
temperature?: Array<{ sensor?: string; value?: number }>;
|
|
||||||
free_percent?: number;
|
|
||||||
usage_percent?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface InfraMemoryMetrics {
|
|
||||||
total_bytes?: number;
|
|
||||||
available_bytes?: number;
|
|
||||||
used_bytes?: number;
|
|
||||||
usage_percent?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface InfraDiskMetrics {
|
|
||||||
device?: string;
|
|
||||||
mountpoint?: string;
|
|
||||||
fs_type?: string;
|
|
||||||
total_bytes?: number;
|
|
||||||
used_bytes?: number;
|
|
||||||
usage_percent?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface InfraNetMetrics {
|
|
||||||
iface?: string;
|
|
||||||
rx_bytes?: number;
|
|
||||||
tx_bytes?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface InfraHostInfo {
|
|
||||||
os?: string;
|
os?: string;
|
||||||
platform?: string;
|
platform?: string;
|
||||||
kernel_version?: string;
|
kernel_version?: string;
|
||||||
pretty_name?: string;
|
pretty_name?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface InfraPayload {
|
export interface IDiskInfo {
|
||||||
system?: {
|
device: string;
|
||||||
cpu?: InfraCpuMetrics;
|
total_bytes: number;
|
||||||
memory?: InfraMemoryMetrics;
|
free_bytes: number;
|
||||||
disk?: InfraDiskMetrics[];
|
used_bytes: number;
|
||||||
net?: InfraNetMetrics[];
|
usage_percent: number;
|
||||||
host?: InfraHostInfo;
|
total_inodes?: number;
|
||||||
|
free_inodes?: number;
|
||||||
|
used_inodes?: number;
|
||||||
|
inodes_usage_percent?: number;
|
||||||
|
read_bytes?: number;
|
||||||
|
write_bytes?: number;
|
||||||
|
read_time?: number;
|
||||||
|
write_time?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface INetInfo {
|
||||||
|
name: string;
|
||||||
|
bytes_sent: number;
|
||||||
|
bytes_recv: number;
|
||||||
|
packets_sent: number;
|
||||||
|
packets_recv: number;
|
||||||
|
err_in: number;
|
||||||
|
err_out: number;
|
||||||
|
drop_in: number;
|
||||||
|
drop_out: number;
|
||||||
|
fifo_in: number;
|
||||||
|
fifo_out: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ISystemInfo {
|
||||||
|
cpu: ICpuInfo;
|
||||||
|
memory: IMemoryInfo;
|
||||||
|
disk: IDiskInfo[];
|
||||||
|
host: IHostInfo;
|
||||||
|
net: INetInfo[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ICaptureInfo {
|
||||||
|
version?: string;
|
||||||
|
mode?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ILighthouseAudit {
|
||||||
|
id?: string;
|
||||||
|
title?: string;
|
||||||
|
score?: number | null;
|
||||||
|
displayValue?: string;
|
||||||
|
numericValue?: number;
|
||||||
|
numericUnit?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ICheckLighthouseFields {
|
||||||
|
accessibility: number;
|
||||||
|
bestPractices: number;
|
||||||
|
seo: number;
|
||||||
|
performance: number;
|
||||||
|
audits: {
|
||||||
|
cls: ILighthouseAudit;
|
||||||
|
si: ILighthouseAudit;
|
||||||
|
fcp: ILighthouseAudit;
|
||||||
|
lcp: ILighthouseAudit;
|
||||||
|
tbt: ILighthouseAudit;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DockerHealth {
|
export interface IDockerContainerHealth {
|
||||||
healthy?: boolean;
|
healthy: boolean;
|
||||||
source?: string;
|
source?: string;
|
||||||
message?: string;
|
message?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DockerExposedPort {
|
export interface IDockerExposedPort {
|
||||||
port?: string;
|
port: string;
|
||||||
protocol?: string;
|
protocol: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DockerContainerSummary {
|
export interface IDockerContainerSummary {
|
||||||
container_id?: string;
|
container_id: string;
|
||||||
container_name?: string;
|
container_name: string;
|
||||||
status?: string;
|
status: string;
|
||||||
health?: DockerHealth;
|
health?: IDockerContainerHealth;
|
||||||
running?: boolean;
|
running: boolean;
|
||||||
base_image?: string;
|
base_image?: string;
|
||||||
exposed_ports?: DockerExposedPort[];
|
exposed_ports?: IDockerExposedPort[];
|
||||||
started_at?: number;
|
started_at?: number;
|
||||||
finished_at?: number;
|
finished_at?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DockerPayload {
|
export interface CheckEntity {
|
||||||
data?: DockerContainerSummary[];
|
id: string;
|
||||||
capture?: Record<string, unknown>;
|
monitorId: string;
|
||||||
|
teamId: string;
|
||||||
|
type: MonitorType;
|
||||||
|
status: MonitorStatus;
|
||||||
|
httpStatusCode?: number;
|
||||||
|
message: string;
|
||||||
|
responseTime?: number;
|
||||||
|
timings?: GotTimings;
|
||||||
|
errorMessage?: string;
|
||||||
|
ackAt?: Date;
|
||||||
|
ackBy?: string;
|
||||||
|
system?: ISystemInfo;
|
||||||
|
capture?: ICaptureInfo;
|
||||||
|
lighthouse?: ICheckLighthouseFields;
|
||||||
|
dockerContainers?: IDockerContainerSummary[];
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt: Date;
|
||||||
|
expiry: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CheckEntity = CheckEntityBase &
|
|
||||||
Partial<PageSpeedPayload> &
|
|
||||||
Partial<InfraPayload> &
|
|
||||||
Partial<DockerPayload>;
|
|
||||||
Reference in new issue
Block a user