add missing date range, add date range lookup
This commit is contained in:
1 parent
cb425b2b3b
commit
a3518e4462
1 file changed
+61
-9
@@ -386,8 +386,12 @@ const getHardwareDetailsById = async (req) => {
|
|||||||
const { dateRange } = req.query;
|
const { dateRange } = req.query;
|
||||||
const monitor = await Monitor.findById(monitorId);
|
const monitor = await Monitor.findById(monitorId);
|
||||||
const dates = getDateRange(dateRange);
|
const dates = getDateRange(dateRange);
|
||||||
console.log(dates);
|
const formatLookup = {
|
||||||
|
day: "%Y-%m-%dT%H:00:00Z",
|
||||||
|
week: "%Y-%m-%dT%H:00:00Z",
|
||||||
|
month: "%Y-%m-%dT00:00:00Z",
|
||||||
|
};
|
||||||
|
const dateString = formatLookup[dateRange];
|
||||||
const hardwareStats = await HardwareCheck.aggregate([
|
const hardwareStats = await HardwareCheck.aggregate([
|
||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
@@ -451,7 +455,11 @@ const getHardwareDetailsById = async (req) => {
|
|||||||
{
|
{
|
||||||
$match: {
|
$match: {
|
||||||
$expr: {
|
$expr: {
|
||||||
$eq: ["$monitorId", monitor._id],
|
$and: [
|
||||||
|
{ $eq: ["$monitorId", monitor._id] },
|
||||||
|
{ $gte: ["$createdAt", dates.start] },
|
||||||
|
{ $lte: ["$createdAt", dates.end] },
|
||||||
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -459,7 +467,7 @@ const getHardwareDetailsById = async (req) => {
|
|||||||
$group: {
|
$group: {
|
||||||
_id: {
|
_id: {
|
||||||
$dateToString: {
|
$dateToString: {
|
||||||
format: "%Y-%m-%d-%H",
|
format: dateString,
|
||||||
date: "$createdAt",
|
date: "$createdAt",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -469,8 +477,10 @@ const getHardwareDetailsById = async (req) => {
|
|||||||
avgMemoryUsage: {
|
avgMemoryUsage: {
|
||||||
$avg: "$memory.usage_percent",
|
$avg: "$memory.usage_percent",
|
||||||
},
|
},
|
||||||
avgTemperature: {
|
avgTemperatures: {
|
||||||
$avg: "$cpu.temperature",
|
$push: {
|
||||||
|
$ifNull: ["$cpu.temperature", [0]],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
disks: {
|
disks: {
|
||||||
$push: "$disk",
|
$push: "$disk",
|
||||||
@@ -482,7 +492,39 @@ const getHardwareDetailsById = async (req) => {
|
|||||||
_id: 1,
|
_id: 1,
|
||||||
avgCpuUsage: 1,
|
avgCpuUsage: 1,
|
||||||
avgMemoryUsage: 1,
|
avgMemoryUsage: 1,
|
||||||
avgTemperature: 1,
|
avgTemperature: {
|
||||||
|
$map: {
|
||||||
|
input: {
|
||||||
|
$range: [
|
||||||
|
0,
|
||||||
|
{
|
||||||
|
$size: {
|
||||||
|
// Handle null temperatures array
|
||||||
|
$ifNull: [
|
||||||
|
{ $arrayElemAt: ["$avgTemperatures", 0] },
|
||||||
|
[0], // Default to single-element array if null
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
as: "index",
|
||||||
|
in: {
|
||||||
|
$avg: {
|
||||||
|
$map: {
|
||||||
|
input: "$avgTemperatures",
|
||||||
|
as: "tempArray",
|
||||||
|
in: {
|
||||||
|
$ifNull: [
|
||||||
|
{ $arrayElemAt: ["$$tempArray", "$$index"] },
|
||||||
|
0, // Default to 0 if element is null
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
disks: {
|
disks: {
|
||||||
$map: {
|
$map: {
|
||||||
input: {
|
input: {
|
||||||
@@ -596,11 +638,21 @@ const getHardwareDetailsById = async (req) => {
|
|||||||
upChecks: {
|
upChecks: {
|
||||||
$arrayElemAt: ["$upChecks", 0],
|
$arrayElemAt: ["$upChecks", 0],
|
||||||
},
|
},
|
||||||
checks: "$checks",
|
checks: {
|
||||||
|
$sortArray: {
|
||||||
|
input: "$checks",
|
||||||
|
sortBy: { _id: 1 },
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
return hardwareStats;
|
|
||||||
|
const monitorStats = {
|
||||||
|
...monitor.toObject(),
|
||||||
|
stats: hardwareStats[0],
|
||||||
|
};
|
||||||
|
return monitorStats;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.method = "getHardwareDetailsById";
|
error.method = "getHardwareDetailsById";
|
||||||
|
|||||||
Reference in new issue
Block a user