Skip to content

Commit

Permalink
Fix asset status carry over to next day phase (#5894)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashesh3 authored Jul 21, 2023
1 parent 7933c9d commit e3216ce
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/Components/Common/Uptime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -294,58 +296,59 @@ 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;
return statusColors;
} 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"],
];
}
};
Expand Down

0 comments on commit e3216ce

Please sign in to comment.