From e3216ce5bce9115c0feb82afed4443acb8fc82f4 Mon Sep 17 00:00:00 2001 From: Ashesh <3626859+Ashesh3@users.noreply.github.com> Date: Fri, 21 Jul 2023 19:02:21 +0530 Subject: [PATCH] Fix asset status carry over to next day phase (#5894) --- src/Components/Common/Uptime.tsx | 43 +++++++++++++++++--------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/Components/Common/Uptime.tsx b/src/Components/Common/Uptime.tsx index 6946dec4501..ff490d40f89 100644 --- a/src/Components/Common/Uptime.tsx +++ b/src/Components/Common/Uptime.tsx @@ -8,10 +8,10 @@ import { AssetStatus, AssetUptimeRecord } from "../Assets/AssetTypes"; import { reverse } from "lodash"; const STATUS_COLORS = { - operational: "bg-green-500", - not_monitored: "bg-gray-400", - down: "bg-red-500", - maintenance: "bg-blue-500", + Operational: "bg-green-500", + "Not Monitored": "bg-gray-400", + Down: "bg-red-500", + "Under Maintenance": "bg-blue-500", }; const STATUS_COLORS_TEXT = { @@ -219,7 +219,8 @@ export default function Uptime(props: { assetId: string }) { }); } } else { - statusToCarryOver = recordsByDayBefore[i][0].status; + statusToCarryOver = + recordsByDayBefore[i][recordsByDayBefore[i].length - 1].status; } } @@ -286,6 +287,7 @@ export default function Uptime(props: { assetId: string }) { const statusColors: (typeof STATUS_COLORS)[keyof typeof STATUS_COLORS][] = []; let dayUptimeScore = 0; + const recordsInPeriodCache: { [key: number]: AssetUptimeRecord[] } = {}; for (let i = 0; i < 3; i++) { const start = i * 8; const end = (i + 1) * 8; @@ -294,48 +296,49 @@ export default function Uptime(props: { assetId: string }) { moment(record.timestamp).hour() >= start && moment(record.timestamp).hour() < end ); + recordsInPeriodCache[i] = recordsInPeriod; if (recordsInPeriod.length === 0) { + const previousLatestRecord = + recordsInPeriodCache[i - 1]?.[dayRecords.length - 1]; if ( - moment(dayRecords[0].timestamp) + moment(previousLatestRecord?.timestamp) .hour(end) .minute(0) .second(0) .isBefore(moment()) ) { - if ( - statusColors[statusColors.length - 1] === - STATUS_COLORS["operational"] - ) { + if (previousLatestRecord?.status === AssetStatus["operational"]) { dayUptimeScore += 1; } statusColors.push( - statusColors[statusColors.length - 1] ?? - STATUS_COLORS["not_monitored"] + STATUS_COLORS[ + previousLatestRecord?.status as keyof typeof STATUS_COLORS + ] ?? STATUS_COLORS["Not Monitored"] ); } else { - statusColors.push(STATUS_COLORS["not_monitored"]); + statusColors.push(STATUS_COLORS["Not Monitored"]); } } else if ( recordsInPeriod.some( (record) => record.status === AssetStatus["down"] ) ) { - statusColors.push(STATUS_COLORS["down"]); + statusColors.push(STATUS_COLORS["Down"]); } else if ( recordsInPeriod.some( (record) => record.status === AssetStatus["maintenance"] ) ) { - statusColors.push(STATUS_COLORS["maintenance"]); + statusColors.push(STATUS_COLORS["Under Maintenance"]); } else if ( recordsInPeriod.some( (record) => record.status === AssetStatus["operational"] ) ) { - statusColors.push(STATUS_COLORS["operational"]); + statusColors.push(STATUS_COLORS["Operational"]); dayUptimeScore += 1; } else { - statusColors.push(STATUS_COLORS["not_monitored"]); + statusColors.push(STATUS_COLORS["Not Monitored"]); } } uptimeScore[day] = dayUptimeScore; @@ -343,9 +346,9 @@ export default function Uptime(props: { assetId: string }) { } else { uptimeScore[day] = 0; return [ - STATUS_COLORS["not_monitored"], - STATUS_COLORS["not_monitored"], - STATUS_COLORS["not_monitored"], + STATUS_COLORS["Not Monitored"], + STATUS_COLORS["Not Monitored"], + STATUS_COLORS["Not Monitored"], ]; } };