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";
|
||||
|
||||
export interface IChecksRepository {}
|
||||
import type { LatestChecksByMonitor } from "@/repositories/checks/MongoCheckRepository.js";
|
||||
export interface IChecksRepository {
|
||||
// Create
|
||||
// Single fetch
|
||||
// Collection fetch
|
||||
findLatestChecksByMonitorIds(
|
||||
monitorIds: string[]
|
||||
): Promise<LatestChecksByMonitor>;
|
||||
// Update
|
||||
// Delete
|
||||
}
|
||||
|
||||
export default IChecksRepository;
|
||||
@@ -1,4 +1,66 @@
|
||||
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;
|
||||
@@ -160,32 +160,12 @@ class MonitorService implements IMonitorService {
|
||||
}
|
||||
);
|
||||
|
||||
const monitorIds = monitors.map((m) => new mongoose.Types.ObjectId(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 checks = await this.checksRepository.findLatestChecksByMonitorIds(
|
||||
monitors.map((m) => m.id)
|
||||
);
|
||||
|
||||
const checksMap = new Map(checks.map((c: any) => [c.id, c.latestChecks]));
|
||||
|
||||
return {
|
||||
count: counts.total,
|
||||
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 {
|
||||
id: string;
|
||||
monitorId: string;
|
||||
status: MonitorStatus;
|
||||
responseTime: number;
|
||||
createdAt: Date;
|
||||
export interface ICpuInfo {
|
||||
physical_core: number;
|
||||
logical_core: number;
|
||||
frequency: number;
|
||||
current_frequency: number;
|
||||
temperature: number[]; // per-core temps
|
||||
free_percent: number;
|
||||
usage_percent: number;
|
||||
}
|
||||
|
||||
// Optional payloads for different monitor types
|
||||
export interface PageSpeedPayload {
|
||||
lighthouse?: Record<string, unknown>;
|
||||
export interface IMemoryInfo {
|
||||
total_bytes: number;
|
||||
available_bytes: number;
|
||||
used_bytes: number;
|
||||
usage_percent: number;
|
||||
}
|
||||
|
||||
export interface InfraCpuMetrics {
|
||||
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 {
|
||||
export interface IHostInfo {
|
||||
os?: string;
|
||||
platform?: string;
|
||||
kernel_version?: string;
|
||||
pretty_name?: string;
|
||||
}
|
||||
|
||||
export interface InfraPayload {
|
||||
system?: {
|
||||
cpu?: InfraCpuMetrics;
|
||||
memory?: InfraMemoryMetrics;
|
||||
disk?: InfraDiskMetrics[];
|
||||
net?: InfraNetMetrics[];
|
||||
host?: InfraHostInfo;
|
||||
export interface IDiskInfo {
|
||||
device: string;
|
||||
total_bytes: number;
|
||||
free_bytes: number;
|
||||
used_bytes: number;
|
||||
usage_percent: number;
|
||||
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 {
|
||||
healthy?: boolean;
|
||||
export interface IDockerContainerHealth {
|
||||
healthy: boolean;
|
||||
source?: string;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
export interface DockerExposedPort {
|
||||
port?: string;
|
||||
protocol?: string;
|
||||
export interface IDockerExposedPort {
|
||||
port: string;
|
||||
protocol: string;
|
||||
}
|
||||
|
||||
export interface DockerContainerSummary {
|
||||
container_id?: string;
|
||||
container_name?: string;
|
||||
status?: string;
|
||||
health?: DockerHealth;
|
||||
running?: boolean;
|
||||
export interface IDockerContainerSummary {
|
||||
container_id: string;
|
||||
container_name: string;
|
||||
status: string;
|
||||
health?: IDockerContainerHealth;
|
||||
running: boolean;
|
||||
base_image?: string;
|
||||
exposed_ports?: DockerExposedPort[];
|
||||
exposed_ports?: IDockerExposedPort[];
|
||||
started_at?: number;
|
||||
finished_at?: number;
|
||||
}
|
||||
|
||||
export interface DockerPayload {
|
||||
data?: DockerContainerSummary[];
|
||||
capture?: Record<string, unknown>;
|
||||
export interface CheckEntity {
|
||||
id: string;
|
||||
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