correctly calculate grouped uptime percentage

This commit is contained in:
Alex Holliday committed 2025-02-12 15:18:27 -08:00
1 parent 0c96ffe3d0
commit e2170981e4
2 files changed
+80 -75

No files matched your search

@@ -30,6 +30,12 @@ const ChartBoxes = ({
return <SkeletonLayout />; return <SkeletonLayout />;
} }
const totalUpChecks = monitor?.upChecks?.totalChecks ?? 0;
const totalDownChecks = monitor?.downChecks?.totalChecks ?? 0;
const denominator =
totalUpChecks + totalDownChecks > 0 ? totalUpChecks + totalDownChecks : 1;
const groupedUptimePercentage = (totalUpChecks / denominator) * 100;
return ( return (
<Stack <Stack
direction="row" direction="row"
@@ -73,11 +79,7 @@ const ChartBoxes = ({
<Typography component="span"> <Typography component="span">
{hoveredUptimeData !== null {hoveredUptimeData !== null
? Math.floor(hoveredUptimeData?.avgResponseTime ?? 0) ? Math.floor(hoveredUptimeData?.avgResponseTime ?? 0)
: Math.floor( : Math.floor(groupedUptimePercentage)}
((monitor?.upChecks?.totalChecks ?? 0) /
(monitor?.totalChecks ?? 1)) *
100
)}
<Typography component="span"> <Typography component="span">
{hoveredUptimeData !== null ? " ms" : " %"} {hoveredUptimeData !== null ? " ms" : " %"}
</Typography> </Typography>
+73 -70
View File
@@ -313,7 +313,7 @@ const calculateGroupStats = (group) => {
avgResponseTime: avgResponseTime:
checksWithResponseTime.length > 0 checksWithResponseTime.length > 0
? checksWithResponseTime.reduce((sum, check) => sum + check.responseTime, 0) / ? checksWithResponseTime.reduce((sum, check) => sum + check.responseTime, 0) /
checksWithResponseTime.length checksWithResponseTime.length
: 0, : 0,
}; };
}; };
@@ -372,7 +372,10 @@ const getUptimeDetailsById = async (req) => {
const getDistributedUptimeDetailsById = async (req) => { const getDistributedUptimeDetailsById = async (req) => {
try { try {
const { monitorId } = req.params; const { monitorId } = req?.params ?? {};
if (typeof monitorId === "undefined") {
throw new Error();
}
const monitor = await Monitor.findById(monitorId); const monitor = await Monitor.findById(monitorId);
if (monitor === null || monitor === undefined) { if (monitor === null || monitor === undefined) {
throw new Error(this.stringService.dbFindMonitorById(monitorId)); throw new Error(this.stringService.dbFindMonitorById(monitorId));
@@ -605,98 +608,98 @@ const getMonitorsByTeamId = async (req) => {
filteredMonitors: [ filteredMonitors: [
...(filter !== undefined ...(filter !== undefined
? [ ? [
{ {
$match: { $match: {
$or: [ $or: [
{ name: { $regex: filter, $options: "i" } }, { name: { $regex: filter, $options: "i" } },
{ url: { $regex: filter, $options: "i" } }, { url: { $regex: filter, $options: "i" } },
], ],
},
}, },
}, ]
]
: []), : []),
{ $sort: sort }, { $sort: sort },
{ $skip: skip }, { $skip: skip },
...(rowsPerPage ? [{ $limit: rowsPerPage }] : []), ...(rowsPerPage ? [{ $limit: rowsPerPage }] : []),
...(limit ...(limit
? [ ? [
{ {
$lookup: { $lookup: {
from: "checks", from: "checks",
let: { monitorId: "$_id" }, let: { monitorId: "$_id" },
pipeline: [ pipeline: [
{ {
$match: { $match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] }, $expr: { $eq: ["$monitorId", "$$monitorId"] },
},
}, },
}, { $sort: { createdAt: -1 } },
{ $sort: { createdAt: -1 } }, ...(limit ? [{ $limit: limit }] : []),
...(limit ? [{ $limit: limit }] : []), ],
], as: "standardchecks",
as: "standardchecks", },
}, },
}, ]
]
: []), : []),
...(limit ...(limit
? [ ? [
{ {
$lookup: { $lookup: {
from: "pagespeedchecks", from: "pagespeedchecks",
let: { monitorId: "$_id" }, let: { monitorId: "$_id" },
pipeline: [ pipeline: [
{ {
$match: { $match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] }, $expr: { $eq: ["$monitorId", "$$monitorId"] },
},
}, },
}, { $sort: { createdAt: -1 } },
{ $sort: { createdAt: -1 } }, ...(limit ? [{ $limit: limit }] : []),
...(limit ? [{ $limit: limit }] : []), ],
], as: "pagespeedchecks",
as: "pagespeedchecks", },
}, },
}, ]
]
: []), : []),
...(limit ...(limit
? [ ? [
{ {
$lookup: { $lookup: {
from: "hardwarechecks", from: "hardwarechecks",
let: { monitorId: "$_id" }, let: { monitorId: "$_id" },
pipeline: [ pipeline: [
{ {
$match: { $match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] }, $expr: { $eq: ["$monitorId", "$$monitorId"] },
},
}, },
}, { $sort: { createdAt: -1 } },
{ $sort: { createdAt: -1 } }, ...(limit ? [{ $limit: limit }] : []),
...(limit ? [{ $limit: limit }] : []), ],
], as: "hardwarechecks",
as: "hardwarechecks", },
}, },
}, ]
]
: []), : []),
...(limit ...(limit
? [ ? [
{ {
$lookup: { $lookup: {
from: "distributeduptimechecks", from: "distributeduptimechecks",
let: { monitorId: "$_id" }, let: { monitorId: "$_id" },
pipeline: [ pipeline: [
{ {
$match: { $match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] }, $expr: { $eq: ["$monitorId", "$$monitorId"] },
},
}, },
}, { $sort: { createdAt: -1 } },
{ $sort: { createdAt: -1 } }, ...(limit ? [{ $limit: limit }] : []),
...(limit ? [{ $limit: limit }] : []), ],
], as: "distributeduptimechecks",
as: "distributeduptimechecks", },
}, },
}, ]
]
: []), : []),
{ {