refactor service
This commit is contained in:
1 parent
82cb59ae9e
commit
56aa7b3e1a
21 files changed
+301
-162
No files matched your search
Whitespace-only changes.
@@ -1,58 +0,0 @@
|
||||
import { Check } from "@/db/models/index.js";
|
||||
import type { ICheck } from "@/db/models/index.js";
|
||||
import type { IChecksRepository } from "@/repositories/checks/IChecksRepository.js";
|
||||
import { CheckEntity } from "@/types/domain/check.js";
|
||||
import mongoose from "mongoose";
|
||||
|
||||
export class MongoChecksRepository implements IChecksRepository {
|
||||
private toEntity = (check: ICheck): CheckEntity => {
|
||||
return {
|
||||
id: check._id.toString(),
|
||||
metadata: {
|
||||
monitorId: check.metadata.monitorId.toString(),
|
||||
teamId: check.metadata.teamId.toString(),
|
||||
type: check.metadata.type,
|
||||
},
|
||||
status: check.status,
|
||||
httpStatusCode: check.httpStatusCode,
|
||||
message: check.message,
|
||||
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,
|
||||
};
|
||||
};
|
||||
|
||||
findLatestByMonitorIds = async (monitorIds: string[]) => {
|
||||
const checks = await Check.aggregate([
|
||||
{
|
||||
$match: {
|
||||
"metadata.monitorId": {
|
||||
$in: monitorIds.map((id) => new mongoose.Types.ObjectId(id)),
|
||||
},
|
||||
},
|
||||
},
|
||||
{ $sort: { createdAt: -1 } },
|
||||
{
|
||||
$group: {
|
||||
_id: "$metadata.monitorId",
|
||||
latestChecks: { $push: "$$ROOT" },
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
latestChecks: { $slice: [{ $ifNull: ["$latestChecks", []] }, 25] },
|
||||
},
|
||||
},
|
||||
]);
|
||||
console.log(checks);
|
||||
return checks;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export type {
|
||||
IMonitorRepository,
|
||||
TeamQueryConfig,
|
||||
} from "@/repositories/monitors/IMonitorRepoistory.js";
|
||||
export { default as MongoMonitorRepository } from "@/repositories/monitors/MongoMonitorRepository.js";
|
||||
@@ -0,0 +1,41 @@
|
||||
import type {
|
||||
Monitor,
|
||||
MonitorType,
|
||||
MonitorStatus,
|
||||
} from "@/types/domain/monitor.js";
|
||||
|
||||
export interface TeamQueryConfig {
|
||||
search: string;
|
||||
sortField: string;
|
||||
sortOrder: string;
|
||||
page: number;
|
||||
rowsPerPage: number;
|
||||
type: MonitorType[];
|
||||
status: MonitorStatus[];
|
||||
}
|
||||
|
||||
export interface IMonitorRepository {
|
||||
// Single fetch
|
||||
findById(monitorId: string, teamId: string): Promise<Monitor | null>;
|
||||
|
||||
// Collection fetches
|
||||
findByTeamId(teamId: string): Promise<Monitor[]>;
|
||||
findByTeamIdWithConfig(
|
||||
teamId: string,
|
||||
config: TeamQueryConfig
|
||||
): Promise<Monitor[]>;
|
||||
|
||||
findMonitorCountsByTeamId(teamId: string): Promise<{
|
||||
total: number;
|
||||
upCount: number;
|
||||
downCount: number;
|
||||
pausedCount: number;
|
||||
}>;
|
||||
findByOrgId(orgId: string): Promise<Monitor[]>;
|
||||
|
||||
// Deletions
|
||||
deleteById(id: string): Promise<boolean>;
|
||||
deleteByOrgId(orgId: string): Promise<number>;
|
||||
}
|
||||
|
||||
export default IMonitorRepository;
|
||||
@@ -0,0 +1,126 @@
|
||||
import { Monitor, IMonitor } from "@/db/models/index.js";
|
||||
import { IMonitorRepository, TeamQueryConfig } from "@/repositories/index.js";
|
||||
import type { Monitor as MonitorEntity } from "@/types/domain/monitor.js";
|
||||
import mongoose from "mongoose";
|
||||
class MongoMonitorRepository implements IMonitorRepository {
|
||||
private toEntity = (monitor: IMonitor): MonitorEntity => {
|
||||
return {
|
||||
id: monitor._id.toString(),
|
||||
orgId: monitor.orgId.toString(),
|
||||
teamId: monitor.teamId.toString(),
|
||||
name: monitor.name,
|
||||
url: monitor.url,
|
||||
port: monitor.port,
|
||||
secret: monitor.secret,
|
||||
type: monitor.type,
|
||||
interval: monitor.interval,
|
||||
rejectUnauthorized: monitor.rejectUnauthorized,
|
||||
status: monitor.status,
|
||||
n: monitor.n,
|
||||
thresholds: monitor.thresholds,
|
||||
lastCheckedAt: monitor.lastCheckedAt,
|
||||
latestStatuses: monitor.latestStatuses,
|
||||
notificationChannels: monitor.notificationChannels?.map((id) =>
|
||||
id.toString()
|
||||
),
|
||||
createdBy: monitor.createdBy.toString(),
|
||||
updatedBy: monitor.updatedBy.toString(),
|
||||
createdAt: monitor.createdAt,
|
||||
updatedAt: monitor.updatedAt,
|
||||
};
|
||||
};
|
||||
|
||||
// Single fetch
|
||||
findById = async (monitorId: string, teamId: string) => {
|
||||
const monitor = await Monitor.findOne({ _id: monitorId, teamId });
|
||||
if (!monitor) return null;
|
||||
return this.toEntity(monitor);
|
||||
};
|
||||
|
||||
// Collection fetches
|
||||
findByTeamId = async (teamId: string) => {
|
||||
const monitors = await Monitor.find({ teamId });
|
||||
return monitors.map(this.toEntity);
|
||||
};
|
||||
|
||||
findByTeamIdWithConfig = async (teamId: string, config: TeamQueryConfig) => {
|
||||
if (!config) {
|
||||
const monitors = await Monitor.find({ teamId });
|
||||
return monitors.map(this.toEntity);
|
||||
}
|
||||
|
||||
const { search, sortField, sortOrder, page, rowsPerPage, type, status } =
|
||||
config;
|
||||
|
||||
const match: Record<string, unknown> = {
|
||||
teamId: new mongoose.Types.ObjectId(teamId),
|
||||
};
|
||||
|
||||
if (search && search.trim() !== "") {
|
||||
const term = search.trim();
|
||||
(match as any).$or = [
|
||||
{ name: { $regex: term, $options: "i" } },
|
||||
{ url: { $regex: term, $options: "i" } },
|
||||
];
|
||||
}
|
||||
|
||||
if (Array.isArray(type) && type.length > 0) {
|
||||
match.type = { $in: type };
|
||||
}
|
||||
|
||||
if (Array.isArray(status) && status.length > 0) {
|
||||
match.status = { $in: status };
|
||||
}
|
||||
|
||||
const skip = page * rowsPerPage;
|
||||
|
||||
const monitors = await Monitor.find(match)
|
||||
.sort({ [sortField]: sortOrder === "asc" ? 1 : -1 })
|
||||
.skip(skip)
|
||||
.limit(rowsPerPage);
|
||||
|
||||
return monitors.map(this.toEntity);
|
||||
};
|
||||
findByOrgId = async (orgId: string) => {
|
||||
return [];
|
||||
};
|
||||
|
||||
findMonitorCountsByTeamId = async (teamId: string) => {
|
||||
const countResult = await Monitor.aggregate([
|
||||
{
|
||||
$match: { teamId: new mongoose.Types.ObjectId(teamId) },
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: null,
|
||||
total: { $sum: 1 },
|
||||
upCount: { $sum: { $cond: [{ $eq: ["$status", "up"] }, 1, 0] } },
|
||||
downCount: { $sum: { $cond: [{ $eq: ["$status", "down"] }, 1, 0] } },
|
||||
pausedCount: {
|
||||
$sum: {
|
||||
$cond: [{ $in: ["$status", ["paused", "resuming"]] }, 1, 0],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
const counts = countResult[0] || {
|
||||
total: 0,
|
||||
upCount: 0,
|
||||
downCount: 0,
|
||||
pausedCount: 0,
|
||||
};
|
||||
return counts;
|
||||
};
|
||||
|
||||
// Deletions
|
||||
deleteById = async (id: string) => {
|
||||
return false;
|
||||
};
|
||||
deleteByOrgId = async (orgId: string) => {
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
|
||||
export default MongoMonitorRepository;
|
||||
Reference in new issue
Block a user