mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-01-18 15:49:45 -06:00
Added checks to monitors for getMonitorsById and getMonitorsByUserId. Added TTL to checks (1 month)
This commit is contained in:
@@ -195,7 +195,9 @@ const getAllMonitors = async (req, res) => {
|
||||
const getMonitorById = async (req, res) => {
|
||||
try {
|
||||
const monitor = await Monitor.findById(req.params.monitorId);
|
||||
return monitor;
|
||||
const checks = await Check.find({ monitorId: monitor._id });
|
||||
const monitorWithChecks = { ...monitor.toObject(), checks };
|
||||
return monitorWithChecks;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
@@ -212,7 +214,18 @@ const getMonitorById = async (req, res) => {
|
||||
const getMonitorsByUserId = async (req, res) => {
|
||||
try {
|
||||
const monitors = await Monitor.find({ userId: req.params.userId });
|
||||
return monitors;
|
||||
// Map each monitor to include its associated checks
|
||||
const monitorsWithChecks = await Promise.all(
|
||||
monitors.map(async (monitor) => {
|
||||
// Checks are order oldest -> newest
|
||||
const checks = await Check.find({ monitorId: monitor._id }).sort({
|
||||
createdAt: 1,
|
||||
});
|
||||
return { ...monitor.toObject(), checks };
|
||||
})
|
||||
);
|
||||
|
||||
return monitorsWithChecks;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
@@ -1,28 +1,34 @@
|
||||
const mongoose = require('mongoose')
|
||||
const mongoose = require("mongoose");
|
||||
|
||||
const CheckSchema = mongoose.Schema(
|
||||
{
|
||||
monitorId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: 'Monitor',
|
||||
immutable: true,
|
||||
},
|
||||
status: {
|
||||
type: Boolean,
|
||||
},
|
||||
responseTime: {
|
||||
type: Number, // milliseconds
|
||||
},
|
||||
statusCode: {
|
||||
type: Number, // 200, ... , 500
|
||||
},
|
||||
message: {
|
||||
type: String,
|
||||
}
|
||||
{
|
||||
monitorId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "Monitor",
|
||||
immutable: true,
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
}
|
||||
status: {
|
||||
type: Boolean,
|
||||
},
|
||||
responseTime: {
|
||||
type: Number, // milliseconds
|
||||
},
|
||||
statusCode: {
|
||||
type: Number, // 200, ... , 500
|
||||
},
|
||||
message: {
|
||||
type: String,
|
||||
},
|
||||
expiry: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
expires: 60 * 60 * 24 * 30, // 30 days in seconds
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
timestamps: true,
|
||||
}
|
||||
);
|
||||
|
||||
module.exports = mongoose.model('Check', CheckSchema);
|
||||
module.exports = mongoose.model("Check", CheckSchema);
|
||||
|
||||
Reference in New Issue
Block a user