Merge branch 'fix/check-repository' into feat/monitor-service-ts
This commit is contained in:
commit
52e598a66e
1 file changed
+15
-10
@@ -173,28 +173,33 @@ class MongoChecksRepistory implements IChecksRepository {
|
|||||||
};
|
};
|
||||||
|
|
||||||
findLatestChecksByMonitorIds = async (monitorIds: string[]): Promise<LatestChecksMap> => {
|
findLatestChecksByMonitorIds = async (monitorIds: string[]): Promise<LatestChecksMap> => {
|
||||||
|
if (monitorIds.length === 0) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
const mongoIds = monitorIds.map((id) => new mongoose.Types.ObjectId(id));
|
const mongoIds = monitorIds.map((id) => new mongoose.Types.ObjectId(id));
|
||||||
const checkMap = await CheckModel.aggregate([
|
const limitPerMonitor = 25;
|
||||||
|
const checkGroups = await CheckModel.aggregate([
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
"metadata.monitorId": { $in: mongoIds },
|
"metadata.monitorId": { $in: mongoIds },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ $sort: { createdAt: -1 } },
|
|
||||||
{
|
{
|
||||||
$group: {
|
$group: {
|
||||||
_id: "$metadata.monitorId",
|
_id: "$metadata.monitorId",
|
||||||
latestChecks: { $push: "$$ROOT" },
|
latestChecks: {
|
||||||
},
|
$topN: {
|
||||||
},
|
n: limitPerMonitor,
|
||||||
{
|
sortBy: { createdAt: -1 },
|
||||||
$project: {
|
output: "$$ROOT",
|
||||||
latestChecks: { $slice: [{ $ifNull: ["$latestChecks", []] }, 25] },
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
return checkMap.reduce<LatestChecksMap>((acc, cm) => {
|
return checkGroups.reduce<LatestChecksMap>((acc, group) => {
|
||||||
acc[cm._id.toString()] = cm.latestChecks.map((c: CheckDocument) => this.toEntity(c));
|
const monitorId = group._id.toString();
|
||||||
|
acc[monitorId] = (group.latestChecks ?? []).map((doc: CheckDocument) => this.toEntity(doc));
|
||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user