import
This commit is contained in:
1 parent
7ef1803cf0
commit
cdc58e5905
3 files changed
+53
-34
No files matched your search
@@ -37,6 +37,13 @@ export interface IMonitorRepository {
|
|||||||
monitorData: Monitor
|
monitorData: Monitor
|
||||||
): Promise<Monitor>;
|
): Promise<Monitor>;
|
||||||
|
|
||||||
|
createMany(
|
||||||
|
userId: string,
|
||||||
|
orgId: string,
|
||||||
|
teamId: string,
|
||||||
|
monitorData: Monitor[]
|
||||||
|
): Promise<Monitor[]>;
|
||||||
|
|
||||||
// Single fetch
|
// Single fetch
|
||||||
findAll(): Promise<Monitor[]>;
|
findAll(): Promise<Monitor[]>;
|
||||||
findById(monitorId: string, teamId: string): Promise<Monitor | null>;
|
findById(monitorId: string, teamId: string): Promise<Monitor | null>;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Monitor, IMonitor } from "@/db/models/index.js";
|
import { Monitor, IMonitor } from "@/db/models/index.js";
|
||||||
import { IMonitorRepository, TeamQueryConfig } from "@/repositories/index.js";
|
import { IMonitorRepository, TeamQueryConfig } from "@/repositories/index.js";
|
||||||
|
import { IImportedMonitor } from "@/services/business/MonitorService.js";
|
||||||
import type { Monitor as MonitorEntity } from "@/types/domain/monitor.js";
|
import type { Monitor as MonitorEntity } from "@/types/domain/monitor.js";
|
||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
class MongoMonitorRepository implements IMonitorRepository {
|
class MongoMonitorRepository implements IMonitorRepository {
|
||||||
@@ -51,6 +52,28 @@ class MongoMonitorRepository implements IMonitorRepository {
|
|||||||
return this.toEntity(monitor);
|
return this.toEntity(monitor);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
createMany = async (
|
||||||
|
userId: string,
|
||||||
|
orgId: string,
|
||||||
|
teamId: string,
|
||||||
|
monitorData: MonitorEntity[]
|
||||||
|
) => {
|
||||||
|
const inserted = await Monitor.insertMany(
|
||||||
|
monitorData.map((monitor) => ({
|
||||||
|
name: monitor.name,
|
||||||
|
type: monitor.type,
|
||||||
|
interval: monitor.interval,
|
||||||
|
url: monitor.url,
|
||||||
|
n: monitor.n,
|
||||||
|
orgId: new mongoose.Types.ObjectId(orgId),
|
||||||
|
teamId: new mongoose.Types.ObjectId(teamId),
|
||||||
|
createdBy: new mongoose.Types.ObjectId(userId),
|
||||||
|
updatedBy: new mongoose.Types.ObjectId(userId),
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
return inserted.map(this.toEntity);
|
||||||
|
};
|
||||||
|
|
||||||
// Single fetch
|
// Single fetch
|
||||||
findById = async (monitorId: string, teamId: string) => {
|
findById = async (monitorId: string, teamId: string) => {
|
||||||
const monitor = await Monitor.findOne({ _id: monitorId, teamId });
|
const monitor = await Monitor.findOne({ _id: monitorId, teamId });
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
import { IMonitor, Monitor, MonitorStats, Check } from "@/db/models/index.js";
|
import { MonitorStats, Check } from "@/db/models/index.js";
|
||||||
import { StatsHourly, StatsDaily } from "@/db/models/index.js";
|
import { StatsHourly, StatsDaily } from "@/db/models/index.js";
|
||||||
import ApiError from "@/utils/ApiError.js";
|
import ApiError from "@/utils/ApiError.js";
|
||||||
import { IJobQueue } from "@/services/infrastructure/JobQueue.js";
|
import { IJobQueue } from "@/services/infrastructure/JobQueue.js";
|
||||||
@@ -10,15 +10,7 @@ import { Entitlements } from "@/types/entitlements.js";
|
|||||||
import { getStartDate } from "@/utils/TimeUtils.js";
|
import { getStartDate } from "@/utils/TimeUtils.js";
|
||||||
import { IMonitorRepository } from "@/repositories/index.js";
|
import { IMonitorRepository } from "@/repositories/index.js";
|
||||||
import type IChecksRepository from "@/repositories/checks/IChecksRepository.js";
|
import type IChecksRepository from "@/repositories/checks/IChecksRepository.js";
|
||||||
import type { Monitor as MonitorEntity } from "@/types/domain/index.js";
|
import type { Monitor } from "@/types/domain/index.js";
|
||||||
|
|
||||||
export interface IImportedMonitor {
|
|
||||||
name: string;
|
|
||||||
type: MonitorType;
|
|
||||||
interval: number;
|
|
||||||
url: string;
|
|
||||||
n: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum MonitorImportCode {
|
export enum MonitorImportCode {
|
||||||
OK = "OK",
|
OK = "OK",
|
||||||
@@ -45,9 +37,9 @@ export interface IMonitorService {
|
|||||||
orgId: string,
|
orgId: string,
|
||||||
userId: string,
|
userId: string,
|
||||||
currentTeamId: string,
|
currentTeamId: string,
|
||||||
monitorData: MonitorEntity
|
monitorData: Monitor
|
||||||
) => Promise<MonitorEntity>;
|
) => Promise<Monitor>;
|
||||||
getAll: (teamId: string) => Promise<MonitorEntity[]>;
|
getAll: (teamId: string) => Promise<Monitor[]>;
|
||||||
getAllEmbedChecks: (
|
getAllEmbedChecks: (
|
||||||
teamId: string,
|
teamId: string,
|
||||||
search: string,
|
search: string,
|
||||||
@@ -64,7 +56,7 @@ export interface IMonitorService {
|
|||||||
pausedCount: number;
|
pausedCount: number;
|
||||||
monitors: any[];
|
monitors: any[];
|
||||||
}>;
|
}>;
|
||||||
get: (teamId: string, monitorId: string) => Promise<MonitorEntity>;
|
get: (teamId: string, monitorId: string) => Promise<Monitor>;
|
||||||
getEmbedChecks: (
|
getEmbedChecks: (
|
||||||
teamId: string,
|
teamId: string,
|
||||||
monitorId: string,
|
monitorId: string,
|
||||||
@@ -74,13 +66,13 @@ export interface IMonitorService {
|
|||||||
userId: string,
|
userId: string,
|
||||||
teamId: string,
|
teamId: string,
|
||||||
monitorId: string
|
monitorId: string
|
||||||
) => Promise<MonitorEntity>;
|
) => Promise<Monitor>;
|
||||||
update: (
|
update: (
|
||||||
userId: string,
|
userId: string,
|
||||||
teamId: string,
|
teamId: string,
|
||||||
monitorId: string,
|
monitorId: string,
|
||||||
updateData: Partial<MonitorEntity>
|
updateData: Partial<Monitor>
|
||||||
) => Promise<MonitorEntity>;
|
) => Promise<Monitor>;
|
||||||
delete: (teamId: string, monitorId: string) => Promise<boolean>;
|
delete: (teamId: string, monitorId: string) => Promise<boolean>;
|
||||||
deleteAllInOrg: (orgId: string) => Promise<boolean>;
|
deleteAllInOrg: (orgId: string) => Promise<boolean>;
|
||||||
export: (teamId: string) => Promise<Array<Record<string, unknown>>>;
|
export: (teamId: string) => Promise<Array<Record<string, unknown>>>;
|
||||||
@@ -89,7 +81,7 @@ export interface IMonitorService {
|
|||||||
teamId: string,
|
teamId: string,
|
||||||
userId: string,
|
userId: string,
|
||||||
entitlements: Entitlements,
|
entitlements: Entitlements,
|
||||||
data: { monitors: Array<IImportedMonitor> }
|
data: { monitors: Array<Monitor> }
|
||||||
) => Promise<MonitorImportResult>;
|
) => Promise<MonitorImportResult>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,7 +105,7 @@ class MonitorService implements IMonitorService {
|
|||||||
orgId: string,
|
orgId: string,
|
||||||
userId: string,
|
userId: string,
|
||||||
currentTeamId: string,
|
currentTeamId: string,
|
||||||
monitorData: MonitorEntity
|
monitorData: Monitor
|
||||||
) => {
|
) => {
|
||||||
const monitor = await this.monitorRepository.create(
|
const monitor = await this.monitorRepository.create(
|
||||||
orgId,
|
orgId,
|
||||||
@@ -506,7 +498,7 @@ class MonitorService implements IMonitorService {
|
|||||||
return {};
|
return {};
|
||||||
};
|
};
|
||||||
|
|
||||||
private getChecksRecent = async (monitor: MonitorEntity, startDate: Date) => {
|
private getChecksRecent = async (monitor: Monitor, startDate: Date) => {
|
||||||
const endDate = new Date();
|
const endDate = new Date();
|
||||||
const matchStage: {
|
const matchStage: {
|
||||||
"metadata.monitorId": mongoose.Types.ObjectId;
|
"metadata.monitorId": mongoose.Types.ObjectId;
|
||||||
@@ -618,7 +610,7 @@ class MonitorService implements IMonitorService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
private getChecksOtherRanges = async (
|
private getChecksOtherRanges = async (
|
||||||
monitor: MonitorEntity,
|
monitor: Monitor,
|
||||||
range: string,
|
range: string,
|
||||||
startDate: Date
|
startDate: Date
|
||||||
) => {
|
) => {
|
||||||
@@ -744,9 +736,9 @@ class MonitorService implements IMonitorService {
|
|||||||
userId: string,
|
userId: string,
|
||||||
teamId: string,
|
teamId: string,
|
||||||
monitorId: string,
|
monitorId: string,
|
||||||
updateData: Partial<MonitorEntity>
|
updateData: Partial<Monitor>
|
||||||
) {
|
) {
|
||||||
const allowedFields: (keyof MonitorEntity)[] = [
|
const allowedFields: (keyof Monitor)[] = [
|
||||||
"name",
|
"name",
|
||||||
"secret",
|
"secret",
|
||||||
"interval",
|
"interval",
|
||||||
@@ -755,7 +747,7 @@ class MonitorService implements IMonitorService {
|
|||||||
"notificationChannels",
|
"notificationChannels",
|
||||||
"thresholds",
|
"thresholds",
|
||||||
];
|
];
|
||||||
const safeUpdate: Partial<MonitorEntity> = {};
|
const safeUpdate: Partial<Monitor> = {};
|
||||||
|
|
||||||
for (const field of allowedFields) {
|
for (const field of allowedFields) {
|
||||||
if (updateData[field] !== undefined) {
|
if (updateData[field] !== undefined) {
|
||||||
@@ -816,10 +808,10 @@ class MonitorService implements IMonitorService {
|
|||||||
teamId: string,
|
teamId: string,
|
||||||
userId: string,
|
userId: string,
|
||||||
entitlements: Entitlements,
|
entitlements: Entitlements,
|
||||||
data: { monitors: Array<IImportedMonitor> }
|
data: { monitors: Array<Monitor> }
|
||||||
) => {
|
) => {
|
||||||
const attempted = data.monitors.length ?? 0;
|
const attempted = data.monitors.length ?? 0;
|
||||||
let result: IMonitor[] = [];
|
let result: Monitor[] = [];
|
||||||
let canImport = 0;
|
let canImport = 0;
|
||||||
try {
|
try {
|
||||||
// Get current count
|
// Get current count
|
||||||
@@ -842,14 +834,11 @@ class MonitorService implements IMonitorService {
|
|||||||
canImport = entitlements.monitorsMax - monitorCount;
|
canImport = entitlements.monitorsMax - monitorCount;
|
||||||
const importableMonitors = data?.monitors.slice(0, canImport);
|
const importableMonitors = data?.monitors.slice(0, canImport);
|
||||||
|
|
||||||
result = await Monitor.insertMany(
|
result = await this.monitorRepository.createMany(
|
||||||
importableMonitors.map((monitor) => ({
|
userId,
|
||||||
...monitor,
|
orgId,
|
||||||
orgId: new mongoose.Types.ObjectId(orgId),
|
teamId,
|
||||||
teamId: new mongoose.Types.ObjectId(teamId),
|
importableMonitors
|
||||||
createdBy: new mongoose.Types.ObjectId(userId),
|
|
||||||
updatedBy: new mongoose.Types.ObjectId(userId),
|
|
||||||
}))
|
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const monitor of result) {
|
for (const monitor of result) {
|
||||||
|
|||||||
Reference in new issue
Block a user