Skip to content

Commit e217098

Browse files
committed
correctly calculate grouped uptime percentage
1 parent 0c96ffe commit e217098

File tree

2 files changed

+80
-75
lines changed

2 files changed

+80
-75
lines changed

Client/src/Pages/Uptime/Details/Components/ChartBoxes/index.jsx

+7-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ const ChartBoxes = ({
3030
return <SkeletonLayout />;
3131
}
3232

33+
const totalUpChecks = monitor?.upChecks?.totalChecks ?? 0;
34+
const totalDownChecks = monitor?.downChecks?.totalChecks ?? 0;
35+
const denominator =
36+
totalUpChecks + totalDownChecks > 0 ? totalUpChecks + totalDownChecks : 1;
37+
const groupedUptimePercentage = (totalUpChecks / denominator) * 100;
38+
3339
return (
3440
<Stack
3541
direction="row"
@@ -73,11 +79,7 @@ const ChartBoxes = ({
7379
<Typography component="span">
7480
{hoveredUptimeData !== null
7581
? Math.floor(hoveredUptimeData?.avgResponseTime ?? 0)
76-
: Math.floor(
77-
((monitor?.upChecks?.totalChecks ?? 0) /
78-
(monitor?.totalChecks ?? 1)) *
79-
100
80-
)}
82+
: Math.floor(groupedUptimePercentage)}
8183
<Typography component="span">
8284
{hoveredUptimeData !== null ? " ms" : " %"}
8385
</Typography>

Server/db/mongo/modules/monitorModule.js

+73-70
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ const calculateGroupStats = (group) => {
313313
avgResponseTime:
314314
checksWithResponseTime.length > 0
315315
? checksWithResponseTime.reduce((sum, check) => sum + check.responseTime, 0) /
316-
checksWithResponseTime.length
316+
checksWithResponseTime.length
317317
: 0,
318318
};
319319
};
@@ -372,7 +372,10 @@ const getUptimeDetailsById = async (req) => {
372372

373373
const getDistributedUptimeDetailsById = async (req) => {
374374
try {
375-
const { monitorId } = req.params;
375+
const { monitorId } = req?.params ?? {};
376+
if (typeof monitorId === "undefined") {
377+
throw new Error();
378+
}
376379
const monitor = await Monitor.findById(monitorId);
377380
if (monitor === null || monitor === undefined) {
378381
throw new Error(this.stringService.dbFindMonitorById(monitorId));
@@ -605,98 +608,98 @@ const getMonitorsByTeamId = async (req) => {
605608
filteredMonitors: [
606609
...(filter !== undefined
607610
? [
608-
{
609-
$match: {
610-
$or: [
611-
{ name: { $regex: filter, $options: "i" } },
612-
{ url: { $regex: filter, $options: "i" } },
613-
],
611+
{
612+
$match: {
613+
$or: [
614+
{ name: { $regex: filter, $options: "i" } },
615+
{ url: { $regex: filter, $options: "i" } },
616+
],
617+
},
614618
},
615-
},
616-
]
619+
]
617620
: []),
618621
{ $sort: sort },
619622
{ $skip: skip },
620623
...(rowsPerPage ? [{ $limit: rowsPerPage }] : []),
621624
...(limit
622625
? [
623-
{
624-
$lookup: {
625-
from: "checks",
626-
let: { monitorId: "$_id" },
627-
pipeline: [
628-
{
629-
$match: {
630-
$expr: { $eq: ["$monitorId", "$$monitorId"] },
626+
{
627+
$lookup: {
628+
from: "checks",
629+
let: { monitorId: "$_id" },
630+
pipeline: [
631+
{
632+
$match: {
633+
$expr: { $eq: ["$monitorId", "$$monitorId"] },
634+
},
631635
},
632-
},
633-
{ $sort: { createdAt: -1 } },
634-
...(limit ? [{ $limit: limit }] : []),
635-
],
636-
as: "standardchecks",
636+
{ $sort: { createdAt: -1 } },
637+
...(limit ? [{ $limit: limit }] : []),
638+
],
639+
as: "standardchecks",
640+
},
637641
},
638-
},
639-
]
642+
]
640643
: []),
641644
...(limit
642645
? [
643-
{
644-
$lookup: {
645-
from: "pagespeedchecks",
646-
let: { monitorId: "$_id" },
647-
pipeline: [
648-
{
649-
$match: {
650-
$expr: { $eq: ["$monitorId", "$$monitorId"] },
646+
{
647+
$lookup: {
648+
from: "pagespeedchecks",
649+
let: { monitorId: "$_id" },
650+
pipeline: [
651+
{
652+
$match: {
653+
$expr: { $eq: ["$monitorId", "$$monitorId"] },
654+
},
651655
},
652-
},
653-
{ $sort: { createdAt: -1 } },
654-
...(limit ? [{ $limit: limit }] : []),
655-
],
656-
as: "pagespeedchecks",
656+
{ $sort: { createdAt: -1 } },
657+
...(limit ? [{ $limit: limit }] : []),
658+
],
659+
as: "pagespeedchecks",
660+
},
657661
},
658-
},
659-
]
662+
]
660663
: []),
661664
...(limit
662665
? [
663-
{
664-
$lookup: {
665-
from: "hardwarechecks",
666-
let: { monitorId: "$_id" },
667-
pipeline: [
668-
{
669-
$match: {
670-
$expr: { $eq: ["$monitorId", "$$monitorId"] },
666+
{
667+
$lookup: {
668+
from: "hardwarechecks",
669+
let: { monitorId: "$_id" },
670+
pipeline: [
671+
{
672+
$match: {
673+
$expr: { $eq: ["$monitorId", "$$monitorId"] },
674+
},
671675
},
672-
},
673-
{ $sort: { createdAt: -1 } },
674-
...(limit ? [{ $limit: limit }] : []),
675-
],
676-
as: "hardwarechecks",
676+
{ $sort: { createdAt: -1 } },
677+
...(limit ? [{ $limit: limit }] : []),
678+
],
679+
as: "hardwarechecks",
680+
},
677681
},
678-
},
679-
]
682+
]
680683
: []),
681684
...(limit
682685
? [
683-
{
684-
$lookup: {
685-
from: "distributeduptimechecks",
686-
let: { monitorId: "$_id" },
687-
pipeline: [
688-
{
689-
$match: {
690-
$expr: { $eq: ["$monitorId", "$$monitorId"] },
686+
{
687+
$lookup: {
688+
from: "distributeduptimechecks",
689+
let: { monitorId: "$_id" },
690+
pipeline: [
691+
{
692+
$match: {
693+
$expr: { $eq: ["$monitorId", "$$monitorId"] },
694+
},
691695
},
692-
},
693-
{ $sort: { createdAt: -1 } },
694-
...(limit ? [{ $limit: limit }] : []),
695-
],
696-
as: "distributeduptimechecks",
696+
{ $sort: { createdAt: -1 } },
697+
...(limit ? [{ $limit: limit }] : []),
698+
],
699+
as: "distributeduptimechecks",
700+
},
697701
},
698-
},
699-
]
702+
]
700703
: []),
701704

702705
{

0 commit comments

Comments
 (0)