remove legacy try/catch blocks
This commit is contained in:
1 parent
c28be97fb0
commit
49e8d26bfc
1 file changed
+170
-230
@@ -64,28 +64,18 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
|
|||||||
};
|
};
|
||||||
|
|
||||||
createGeoChecks = async (geoChecks: Omit<GeoCheck, "id" | "__v" | "createdAt" | "updatedAt">[]): Promise<GeoCheck[]> => {
|
createGeoChecks = async (geoChecks: Omit<GeoCheck, "id" | "__v" | "createdAt" | "updatedAt">[]): Promise<GeoCheck[]> => {
|
||||||
try {
|
const docs = await GeoCheckModel.insertMany(
|
||||||
const docs = await GeoCheckModel.insertMany(
|
geoChecks.map((geoCheck) => ({
|
||||||
geoChecks.map((geoCheck) => ({
|
metadata: {
|
||||||
metadata: {
|
monitorId: new mongoose.Types.ObjectId(geoCheck.metadata.monitorId),
|
||||||
monitorId: new mongoose.Types.ObjectId(geoCheck.metadata.monitorId),
|
teamId: new mongoose.Types.ObjectId(geoCheck.metadata.teamId),
|
||||||
teamId: new mongoose.Types.ObjectId(geoCheck.metadata.teamId),
|
type: geoCheck.metadata.type,
|
||||||
type: geoCheck.metadata.type,
|
},
|
||||||
},
|
results: geoCheck.results,
|
||||||
results: geoCheck.results,
|
expiry: new Date(geoCheck.expiry),
|
||||||
expiry: new Date(geoCheck.expiry),
|
}))
|
||||||
}))
|
);
|
||||||
);
|
return docs.map((doc) => this.toEntity(doc));
|
||||||
return docs.map((doc) => this.toEntity(doc));
|
|
||||||
} catch (error: unknown) {
|
|
||||||
this.logger.error({
|
|
||||||
message: error instanceof Error ? `Failed to createGeoChecks: ${error.message}` : "Failed to createGeoChecks",
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "createGeoChecks",
|
|
||||||
stack: error instanceof Error ? error.stack : undefined,
|
|
||||||
});
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
findByMonitorId = async (
|
findByMonitorId = async (
|
||||||
@@ -96,95 +86,85 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
|
|||||||
rowsPerPage: number,
|
rowsPerPage: number,
|
||||||
continents?: GeoContinent[]
|
continents?: GeoContinent[]
|
||||||
): Promise<FlatGeoChecksQueryResult> => {
|
): Promise<FlatGeoChecksQueryResult> => {
|
||||||
try {
|
const matchStage: Record<string, unknown> = {
|
||||||
const matchStage: Record<string, unknown> = {
|
"metadata.monitorId": new mongoose.Types.ObjectId(monitorId),
|
||||||
"metadata.monitorId": new mongoose.Types.ObjectId(monitorId),
|
createdAt: { $gte: getDateForRange(dateRange) },
|
||||||
createdAt: { $gte: getDateForRange(dateRange) },
|
};
|
||||||
};
|
|
||||||
|
|
||||||
const convertedSortOrder = sortOrder === "asc" ? 1 : -1;
|
const convertedSortOrder = sortOrder === "asc" ? 1 : -1;
|
||||||
|
|
||||||
let skip = 0;
|
let skip = 0;
|
||||||
if (page && rowsPerPage) {
|
if (page && rowsPerPage) {
|
||||||
skip = page * rowsPerPage;
|
skip = page * rowsPerPage;
|
||||||
}
|
|
||||||
|
|
||||||
// Common pipeline stages for both paths
|
|
||||||
const pipeline: PipelineStage[] = [
|
|
||||||
{ $match: matchStage },
|
|
||||||
{ $unwind: "$results" },
|
|
||||||
// Filter by continent if specified
|
|
||||||
...(continents && continents.length > 0
|
|
||||||
? [
|
|
||||||
{
|
|
||||||
$match: {
|
|
||||||
"results.location.continent": { $in: continents },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
]
|
|
||||||
: []),
|
|
||||||
// Project to flat structure
|
|
||||||
{
|
|
||||||
$project: {
|
|
||||||
_id: 0,
|
|
||||||
monitorId: "$metadata.monitorId",
|
|
||||||
teamId: "$metadata.teamId",
|
|
||||||
type: "$metadata.type",
|
|
||||||
location: "$results.location",
|
|
||||||
status: "$results.status",
|
|
||||||
statusCode: "$results.statusCode",
|
|
||||||
timings: "$results.timings",
|
|
||||||
createdAt: 1,
|
|
||||||
updatedAt: 1,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ $sort: { createdAt: convertedSortOrder } },
|
|
||||||
{ $skip: skip },
|
|
||||||
{ $limit: rowsPerPage },
|
|
||||||
];
|
|
||||||
|
|
||||||
// Count pipeline
|
|
||||||
const countPipeline: PipelineStage[] = [
|
|
||||||
{ $match: matchStage },
|
|
||||||
{ $unwind: "$results" },
|
|
||||||
...(continents && continents.length > 0
|
|
||||||
? [
|
|
||||||
{
|
|
||||||
$match: {
|
|
||||||
"results.location.continent": { $in: continents },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
]
|
|
||||||
: []),
|
|
||||||
{ $count: "count" },
|
|
||||||
];
|
|
||||||
|
|
||||||
const [countResult, dataResults] = await Promise.all([GeoCheckModel.aggregate(countPipeline), GeoCheckModel.aggregate(pipeline)]);
|
|
||||||
|
|
||||||
const geoChecksCount = countResult[0]?.count || 0;
|
|
||||||
const geoChecks: FlatGeoCheck[] = dataResults.map((doc) => ({
|
|
||||||
id: `${doc.monitorId.toString()}-${new Date(doc.createdAt).getTime()}-${doc.location.continent}-${doc.location.city}-${Math.random().toString(36).substring(2, 15)}`,
|
|
||||||
monitorId: doc.monitorId.toString(),
|
|
||||||
teamId: doc.teamId.toString(),
|
|
||||||
type: doc.type,
|
|
||||||
location: doc.location,
|
|
||||||
status: doc.status,
|
|
||||||
statusCode: doc.statusCode,
|
|
||||||
timings: doc.timings,
|
|
||||||
createdAt: new Date(doc.createdAt).toISOString(),
|
|
||||||
updatedAt: new Date(doc.updatedAt).toISOString(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
return { geoChecksCount, geoChecks };
|
|
||||||
} catch (error: unknown) {
|
|
||||||
this.logger.error({
|
|
||||||
message: error instanceof Error ? `Error finding geo checks by monitor ID: ${error.message}` : "Error finding geo checks by monitor ID",
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "findByMonitorId",
|
|
||||||
stack: error instanceof Error ? error.stack : undefined,
|
|
||||||
});
|
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Common pipeline stages for both paths
|
||||||
|
const pipeline: PipelineStage[] = [
|
||||||
|
{ $match: matchStage },
|
||||||
|
{ $unwind: "$results" },
|
||||||
|
// Filter by continent if specified
|
||||||
|
...(continents && continents.length > 0
|
||||||
|
? [
|
||||||
|
{
|
||||||
|
$match: {
|
||||||
|
"results.location.continent": { $in: continents },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
: []),
|
||||||
|
// Project to flat structure
|
||||||
|
{
|
||||||
|
$project: {
|
||||||
|
_id: 0,
|
||||||
|
monitorId: "$metadata.monitorId",
|
||||||
|
teamId: "$metadata.teamId",
|
||||||
|
type: "$metadata.type",
|
||||||
|
location: "$results.location",
|
||||||
|
status: "$results.status",
|
||||||
|
statusCode: "$results.statusCode",
|
||||||
|
timings: "$results.timings",
|
||||||
|
createdAt: 1,
|
||||||
|
updatedAt: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ $sort: { createdAt: convertedSortOrder } },
|
||||||
|
{ $skip: skip },
|
||||||
|
{ $limit: rowsPerPage },
|
||||||
|
];
|
||||||
|
|
||||||
|
// Count pipeline
|
||||||
|
const countPipeline: PipelineStage[] = [
|
||||||
|
{ $match: matchStage },
|
||||||
|
{ $unwind: "$results" },
|
||||||
|
...(continents && continents.length > 0
|
||||||
|
? [
|
||||||
|
{
|
||||||
|
$match: {
|
||||||
|
"results.location.continent": { $in: continents },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
: []),
|
||||||
|
{ $count: "count" },
|
||||||
|
];
|
||||||
|
|
||||||
|
const [countResult, dataResults] = await Promise.all([GeoCheckModel.aggregate(countPipeline), GeoCheckModel.aggregate(pipeline)]);
|
||||||
|
|
||||||
|
const geoChecksCount = countResult[0]?.count || 0;
|
||||||
|
const geoChecks: FlatGeoCheck[] = dataResults.map((doc) => ({
|
||||||
|
id: `${doc.monitorId.toString()}-${new Date(doc.createdAt).getTime()}-${doc.location.continent}-${doc.location.city}-${Math.random().toString(36).substring(2, 15)}`,
|
||||||
|
monitorId: doc.monitorId.toString(),
|
||||||
|
teamId: doc.teamId.toString(),
|
||||||
|
type: doc.type,
|
||||||
|
location: doc.location,
|
||||||
|
status: doc.status,
|
||||||
|
statusCode: doc.statusCode,
|
||||||
|
timings: doc.timings,
|
||||||
|
createdAt: new Date(doc.createdAt).toISOString(),
|
||||||
|
updatedAt: new Date(doc.updatedAt).toISOString(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
return { geoChecksCount, geoChecks };
|
||||||
};
|
};
|
||||||
|
|
||||||
findGroupedByMonitorIdAndDateRange = async (monitorId: string, dateRange: DateRange, continents?: GeoContinent[]): Promise<GroupedGeoCheck[]> => {
|
findGroupedByMonitorIdAndDateRange = async (monitorId: string, dateRange: DateRange, continents?: GeoContinent[]): Promise<GroupedGeoCheck[]> => {
|
||||||
@@ -192,146 +172,106 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
|
|||||||
const dateString = getDateFormat(dateRange);
|
const dateString = getDateFormat(dateRange);
|
||||||
|
|
||||||
const end = new Date();
|
const end = new Date();
|
||||||
try {
|
const pipeline: PipelineStage[] = [
|
||||||
const pipeline: PipelineStage[] = [
|
// Match geo checks for this monitor in date range
|
||||||
// Match geo checks for this monitor in date range
|
{
|
||||||
{
|
$match: {
|
||||||
$match: {
|
"metadata.monitorId": new mongoose.Types.ObjectId(monitorId),
|
||||||
"metadata.monitorId": new mongoose.Types.ObjectId(monitorId),
|
createdAt: {
|
||||||
createdAt: {
|
$gte: start,
|
||||||
$gte: start,
|
$lte: end,
|
||||||
$lte: end,
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// Unwind the results array to process each location separately
|
||||||
|
{
|
||||||
|
$unwind: "$results",
|
||||||
|
},
|
||||||
|
// Filter by continent if specified
|
||||||
|
...(continents && continents.length > 0
|
||||||
|
? [
|
||||||
|
{
|
||||||
|
$match: {
|
||||||
|
"results.location.continent": { $in: continents },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
: []),
|
||||||
|
// Group by date bucket and continent
|
||||||
|
{
|
||||||
|
$group: {
|
||||||
|
_id: {
|
||||||
|
bucketDate: {
|
||||||
|
$dateToString: {
|
||||||
|
format: dateString,
|
||||||
|
date: "$createdAt",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
continent: "$results.location.continent",
|
||||||
|
},
|
||||||
|
avgResponseTime: { $avg: "$results.timings.total" },
|
||||||
|
totalChecks: { $sum: 1 },
|
||||||
|
upChecks: {
|
||||||
|
$sum: {
|
||||||
|
$cond: ["$results.status", 1, 0],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
// Unwind the results array to process each location separately
|
},
|
||||||
{
|
// Calculate uptime percentage
|
||||||
$unwind: "$results",
|
{
|
||||||
},
|
$project: {
|
||||||
// Filter by continent if specified
|
_id: 0,
|
||||||
...(continents && continents.length > 0
|
bucketDate: "$_id.bucketDate",
|
||||||
? [
|
continent: "$_id.continent",
|
||||||
|
avgResponseTime: { $round: ["$avgResponseTime", 2] },
|
||||||
|
totalChecks: 1,
|
||||||
|
uptimePercentage: {
|
||||||
|
$round: [
|
||||||
{
|
{
|
||||||
$match: {
|
$multiply: [
|
||||||
"results.location.continent": { $in: continents },
|
{
|
||||||
},
|
$divide: ["$upChecks", "$totalChecks"],
|
||||||
|
},
|
||||||
|
100,
|
||||||
|
],
|
||||||
},
|
},
|
||||||
]
|
2,
|
||||||
: []),
|
],
|
||||||
// Group by date bucket and continent
|
|
||||||
{
|
|
||||||
$group: {
|
|
||||||
_id: {
|
|
||||||
bucketDate: {
|
|
||||||
$dateToString: {
|
|
||||||
format: dateString,
|
|
||||||
date: "$createdAt",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
continent: "$results.location.continent",
|
|
||||||
},
|
|
||||||
avgResponseTime: { $avg: "$results.timings.total" },
|
|
||||||
totalChecks: { $sum: 1 },
|
|
||||||
upChecks: {
|
|
||||||
$sum: {
|
|
||||||
$cond: ["$results.status", 1, 0],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
// Calculate uptime percentage
|
},
|
||||||
{
|
// Sort by date and continent
|
||||||
$project: {
|
{
|
||||||
_id: 0,
|
$sort: {
|
||||||
bucketDate: "$_id.bucketDate",
|
bucketDate: 1,
|
||||||
continent: "$_id.continent",
|
continent: 1,
|
||||||
avgResponseTime: { $round: ["$avgResponseTime", 2] },
|
|
||||||
totalChecks: 1,
|
|
||||||
uptimePercentage: {
|
|
||||||
$round: [
|
|
||||||
{
|
|
||||||
$multiply: [
|
|
||||||
{
|
|
||||||
$divide: ["$upChecks", "$totalChecks"],
|
|
||||||
},
|
|
||||||
100,
|
|
||||||
],
|
|
||||||
},
|
|
||||||
2,
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
// Sort by date and continent
|
},
|
||||||
{
|
];
|
||||||
$sort: {
|
|
||||||
bucketDate: 1,
|
|
||||||
continent: 1,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const results = await GeoCheckModel.aggregate(pipeline);
|
const results = await GeoCheckModel.aggregate(pipeline);
|
||||||
return results as GroupedGeoCheck[];
|
return results as GroupedGeoCheck[];
|
||||||
} catch (error: unknown) {
|
|
||||||
this.logger.error({
|
|
||||||
message: error instanceof Error ? `Error finding grouped geo checks: ${error.message}` : "Error finding grouped geo checks",
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "findGroupedByMonitorIdAndDateRange",
|
|
||||||
stack: error instanceof Error ? error.stack : undefined,
|
|
||||||
});
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
deleteByMonitorId = async (monitorId: string): Promise<number> => {
|
deleteByMonitorId = async (monitorId: string): Promise<number> => {
|
||||||
try {
|
const result = await GeoCheckModel.deleteMany({
|
||||||
const result = await GeoCheckModel.deleteMany({
|
"metadata.monitorId": new mongoose.Types.ObjectId(monitorId),
|
||||||
"metadata.monitorId": new mongoose.Types.ObjectId(monitorId),
|
});
|
||||||
});
|
return result.deletedCount || 0;
|
||||||
return result.deletedCount || 0;
|
|
||||||
} catch (error: unknown) {
|
|
||||||
this.logger.error({
|
|
||||||
message: error instanceof Error ? `Error deleting geo checks by monitor ID: ${error.message}` : "Error deleting geo checks by monitor ID",
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "deleteByMonitorId",
|
|
||||||
stack: error instanceof Error ? error.stack : undefined,
|
|
||||||
});
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
deleteByTeamId = async (teamId: string): Promise<number> => {
|
deleteByTeamId = async (teamId: string): Promise<number> => {
|
||||||
try {
|
const result = await GeoCheckModel.deleteMany({
|
||||||
const result = await GeoCheckModel.deleteMany({
|
"metadata.teamId": new mongoose.Types.ObjectId(teamId),
|
||||||
"metadata.teamId": new mongoose.Types.ObjectId(teamId),
|
});
|
||||||
});
|
return result.deletedCount || 0;
|
||||||
return result.deletedCount || 0;
|
|
||||||
} catch (error: unknown) {
|
|
||||||
this.logger.error({
|
|
||||||
message: error instanceof Error ? `Error deleting geo checks by team ID: ${error.message}` : "Error deleting geo checks by team ID",
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "deleteByTeamId",
|
|
||||||
stack: error instanceof Error ? error.stack : undefined,
|
|
||||||
});
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
deleteByMonitorIdsNotIn = async (monitorIds: string[]): Promise<number> => {
|
deleteByMonitorIdsNotIn = async (monitorIds: string[]): Promise<number> => {
|
||||||
try {
|
const objectIds = monitorIds.map((id) => new mongoose.Types.ObjectId(id));
|
||||||
const objectIds = monitorIds.map((id) => new mongoose.Types.ObjectId(id));
|
const result = await GeoCheckModel.deleteMany({ "metadata.monitorId": { $nin: objectIds } });
|
||||||
const result = await GeoCheckModel.deleteMany({ "metadata.monitorId": { $nin: objectIds } });
|
return result.deletedCount || 0;
|
||||||
return result.deletedCount || 0;
|
|
||||||
} catch (error: unknown) {
|
|
||||||
this.logger.error({
|
|
||||||
message: error instanceof Error ? `Error deleting orphaned geo checks: ${error.message}` : "Error deleting orphaned geo checks",
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "deleteByMonitorIdsNotIn",
|
|
||||||
stack: error instanceof Error ? error.stack : undefined,
|
|
||||||
});
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user