update, pause
This commit is contained in:
1 parent
e7e4ce7f57
commit
6b5fd8d17f
9 files changed
+53
-27
No files matched your search
@@ -184,18 +184,13 @@ class MongoChecksRepository implements IChecksRepository {
|
||||
}
|
||||
const mongoIds = monitorIds.map((id) => new mongoose.Types.ObjectId(id));
|
||||
const limitPerMonitor = options?.limitPerMonitor ?? 25;
|
||||
const maxIntervalMs = Number(10 * 60 * 1000);
|
||||
const bufferMs = Number(maxIntervalMs);
|
||||
const lookbackMs = limitPerMonitor * maxIntervalMs + bufferMs;
|
||||
|
||||
const cutoffDate = new Date(Date.now() - lookbackMs);
|
||||
const checkGroups = await CheckModel.aggregate([
|
||||
{
|
||||
$match: {
|
||||
"metadata.monitorId": { $in: mongoIds },
|
||||
createdAt: { $gte: cutoffDate },
|
||||
},
|
||||
},
|
||||
{ $sort: { "metadata.monitorId": 1, createdAt: -1 } },
|
||||
{
|
||||
$group: {
|
||||
_id: "$metadata.monitorId",
|
||||
|
||||
@@ -24,8 +24,10 @@ export interface IMonitorsRepository {
|
||||
// collection fetch
|
||||
findAll(): Promise<Monitor[] | null>;
|
||||
findByTeamId(teamId: string, config: TeamQueryConfig): Promise<Monitor[] | null>;
|
||||
|
||||
// update
|
||||
update(monitorId: string, updates: Partial<Monitor>): Promise<Monitor>;
|
||||
updateById(monitorId: string, updates: Partial<Monitor>): Promise<Monitor>;
|
||||
togglePauseById(monitorId: string, teamId: string): Promise<Monitor>;
|
||||
// delete
|
||||
deleteByTeamId(teamId: string): Promise<{ monitors: Monitor[]; deletedCount: number }>;
|
||||
|
||||
@@ -34,4 +36,5 @@ export interface IMonitorsRepository {
|
||||
|
||||
// other
|
||||
findMonitorsSummaryByTeamId(teamId: string, config?: SummaryConfig): Promise<any>;
|
||||
findGroupsByTeamId(teamId: string): Promise<string[]>;
|
||||
}
|
||||
@@ -96,7 +96,7 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
||||
return count;
|
||||
};
|
||||
|
||||
update = async (monitorId: string, patch: Partial<Monitor>) => {
|
||||
updateById = async (monitorId: string, patch: Partial<Monitor>) => {
|
||||
const updatedMonitor = await MonitorModel.findOneAndUpdate(
|
||||
{ _id: monitorId },
|
||||
{
|
||||
@@ -112,6 +112,25 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
||||
return this.toEntity(updatedMonitor);
|
||||
};
|
||||
|
||||
togglePauseById = async (monitorId: string, teamId: string) => {
|
||||
const monitor = await MonitorModel.findOneAndUpdate(
|
||||
{ _id: monitorId, teamId },
|
||||
[
|
||||
{
|
||||
$set: {
|
||||
isActive: { $not: "$isActive" },
|
||||
status: "$$REMOVE",
|
||||
},
|
||||
},
|
||||
],
|
||||
{ new: true }
|
||||
);
|
||||
if (!monitor) {
|
||||
throw new AppError({ message: `Monitor with ID ${monitorId} not found for the given team.`, status: 404 });
|
||||
}
|
||||
return this.toEntity(monitor);
|
||||
};
|
||||
|
||||
deleteByTeamId = async (teamId: string) => {
|
||||
const monitors = await MonitorModel.find({ teamId });
|
||||
const { deletedCount } = await MonitorModel.deleteMany({ teamId });
|
||||
@@ -157,6 +176,14 @@ class MongoMonitorsRepository implements IMonitorsRepository {
|
||||
return summary ?? { totalMonitors: 0, upMonitors: 0, downMonitors: 0, pausedMonitors: 0 };
|
||||
};
|
||||
|
||||
findGroupsByTeamId = async (teamId: string): Promise<string[]> => {
|
||||
const groups = await MonitorModel.distinct("group", {
|
||||
teamId: new mongoose.Types.ObjectId(teamId),
|
||||
group: { $nin: [null, ""] },
|
||||
});
|
||||
return groups.sort();
|
||||
};
|
||||
|
||||
private mapDocuments = (documents: MonitorDocument[]): Monitor[] => {
|
||||
if (!documents?.length) {
|
||||
return [];
|
||||
|
||||
@@ -48,10 +48,10 @@ export interface IMonitorService {
|
||||
explain?: boolean;
|
||||
}): Promise<{ summary: any; count: number; monitors: any[] }>;
|
||||
getAllGames(): any;
|
||||
getGroupsByTeamId(args: { teamId: string }): Promise<any[]>;
|
||||
getGroupsByTeamId(args: { teamId: string }): Promise<string[]>;
|
||||
|
||||
// update
|
||||
editMonitor(args: { teamId: string; monitorId: string; body: any }): Promise<void>;
|
||||
editMonitor(args: { teamId: string; monitorId: string; body: Monitor }): Promise<Monitor>;
|
||||
pauseMonitor(args: { teamId: string; monitorId: string }): Promise<any>;
|
||||
|
||||
// delete
|
||||
@@ -423,21 +423,22 @@ export class MonitorService implements IMonitorService {
|
||||
return this.games;
|
||||
};
|
||||
|
||||
getGroupsByTeamId = async ({ teamId }: { teamId: string }): Promise<any[]> => {
|
||||
const groups = await this.db.monitorModule.getGroupsByTeamId({ teamId });
|
||||
getGroupsByTeamId = async ({ teamId }: { teamId: string }): Promise<string[]> => {
|
||||
const groups = await this.monitorsRepository.findGroupsByTeamId(teamId);
|
||||
return groups;
|
||||
};
|
||||
|
||||
editMonitor = async ({ teamId, monitorId, body }: { teamId: string; monitorId: string; body: any }): Promise<void> => {
|
||||
editMonitor = async ({ teamId, monitorId, body }: { teamId: string; monitorId: string; body: Monitor }) => {
|
||||
await this.verifyTeamAccess({ teamId, monitorId });
|
||||
const editedMonitor = await this.db.monitorModule.editMonitor({ monitorId, body });
|
||||
const editedMonitor = await this.monitorsRepository.updateById(monitorId, body);
|
||||
await this.jobQueue.updateJob(editedMonitor);
|
||||
return editedMonitor;
|
||||
};
|
||||
|
||||
pauseMonitor = async ({ teamId, monitorId }: { teamId: string; monitorId: string }): Promise<any> => {
|
||||
await this.verifyTeamAccess({ teamId, monitorId });
|
||||
const monitor = await this.db.monitorModule.pauseMonitor({ monitorId });
|
||||
monitor.isActive === true ? await this.jobQueue.resumeJob(monitor._id, monitor) : await this.jobQueue.pauseJob(monitor);
|
||||
const monitor = await this.monitorsRepository.togglePauseById(monitorId, teamId);
|
||||
monitor.isActive === true ? await this.jobQueue.resumeJob(monitor) : await this.jobQueue.pauseJob(monitor);
|
||||
return monitor;
|
||||
};
|
||||
|
||||
|
||||
@@ -195,7 +195,7 @@ class StatusService {
|
||||
|
||||
// Return early if not enough data points
|
||||
if (monitor.statusWindow.length < monitor.statusWindowSize) {
|
||||
const updated = await this.monitorsRepository.update(monitor.id, monitor);
|
||||
const updated = await this.monitorsRepository.updateById(monitor.id, monitor);
|
||||
return {
|
||||
monitor: updated,
|
||||
statusChanged: false,
|
||||
@@ -285,7 +285,7 @@ class StatusService {
|
||||
}
|
||||
|
||||
monitor.status = newStatus;
|
||||
const updated = await this.monitorsRepository.update(monitor.id, monitor);
|
||||
const updated = await this.monitorsRepository.updateById(monitor.id, monitor);
|
||||
|
||||
return {
|
||||
monitor: updated,
|
||||
|
||||
Reference in new issue
Block a user