Merge pull request #2480 from bluewave-labs/fix/time-format

fix: update time format function, resolves #2477
This commit is contained in:
Alexander Holliday authored and GitHub committed 2025-06-18 09:44:20 +08:00
commit 8d30de1ff8
3 files changed
+35 -37

No files matched your search

@@ -5,13 +5,8 @@ import { getHumanReadableDuration } from "../../../../../Utils/timeUtils";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
const PageSpeedStatusBoxes = ({ shouldRender, monitor }) => { const PageSpeedStatusBoxes = ({ shouldRender, monitor }) => {
const { time: uptimeDuration, units: uptimeUnits } = getHumanReadableDuration( const uptimeDuration = getHumanReadableDuration(monitor?.uptimeDuration);
monitor?.uptimeDuration const time = getHumanReadableDuration(monitor?.lastChecked);
);
const { time: lastCheckTime, units: lastCheckUnits } = getHumanReadableDuration(
monitor?.lastChecked
);
const { t } = useTranslation(); const { t } = useTranslation();
@@ -22,7 +17,6 @@ const PageSpeedStatusBoxes = ({ shouldRender, monitor }) => {
subHeading={ subHeading={
<> <>
{uptimeDuration} {uptimeDuration}
<Typography component="span">{uptimeUnits}</Typography>
<Typography component="span">{t("ago")}</Typography> <Typography component="span">{t("ago")}</Typography>
</> </>
} }
@@ -31,8 +25,7 @@ const PageSpeedStatusBoxes = ({ shouldRender, monitor }) => {
heading="last check" heading="last check"
subHeading={ subHeading={
<> <>
{lastCheckTime} {time}
<Typography component="span">{lastCheckUnits}</Typography>
<Typography component="span">{t("ago")}</Typography> <Typography component="span">{t("ago")}</Typography>
</> </>
} }
@@ -24,30 +24,22 @@ const UptimeStatusBoxes = ({
const timeOfLastCheck = monitorStats?.lastCheckTimestamp; const timeOfLastCheck = monitorStats?.lastCheckTimestamp;
const timeSinceLastCheck = Date.now() - timeOfLastCheck; const timeSinceLastCheck = Date.now() - timeOfLastCheck;
const { time: streakTime, units: streakUnits } = const streakTime = getHumanReadableDuration(timeSinceLastFailure);
getHumanReadableDuration(timeSinceLastFailure);
const { time: lastCheckTime, units: lastCheckUnits } = const lastCheckTime = getHumanReadableDuration(timeSinceLastCheck);
getHumanReadableDuration(timeSinceLastCheck);
return ( return (
<StatusBoxes shouldRender={!isLoading}> <StatusBoxes shouldRender={!isLoading}>
<StatBox <StatBox
gradient={true} gradient={true}
status={determineState(monitor)} status={determineState(monitor)}
heading={"active for"} heading={"active for"}
subHeading={ subHeading={streakTime}
<>
{streakTime}
<Typography component="span">{streakUnits}</Typography>
</>
}
/> />
<StatBox <StatBox
heading="last check" heading="last check"
subHeading={ subHeading={
<> <>
{lastCheckTime} {lastCheckTime}
<Typography component="span">{lastCheckUnits}</Typography>
<Typography component="span">{"ago"}</Typography> <Typography component="span">{"ago"}</Typography>
</> </>
} }
@@ -80,7 +72,7 @@ const UptimeStatusBoxes = ({
}; };
UptimeStatusBoxes.propTypes = { UptimeStatusBoxes.propTypes = {
shouldRender: PropTypes.bool, isLoading: PropTypes.bool,
monitor: PropTypes.object, monitor: PropTypes.object,
monitorStats: PropTypes.object, monitorStats: PropTypes.object,
certificateExpiry: PropTypes.string, certificateExpiry: PropTypes.string,
+28 -15
View File
@@ -79,22 +79,35 @@ export const formatDurationSplit = (ms) => {
export const getHumanReadableDuration = (ms) => { export const getHumanReadableDuration = (ms) => {
const durationObj = dayjs.duration(ms); const durationObj = dayjs.duration(ms);
if (durationObj.asDays() >= 1) {
const days = Math.floor(durationObj.asDays()); const parts = {
return { time: days, units: days === 1 ? "day" : "days" }; days: Math.floor(durationObj.asDays()),
} else if (durationObj.asHours() >= 1) { hours: durationObj.hours(),
const hoursRounded = Math.round(durationObj.asHours() * 10) / 10; minutes: durationObj.minutes(),
const hours = Number.isInteger(hoursRounded) seconds: durationObj.seconds(),
? Math.floor(hoursRounded) };
: hoursRounded;
return { time: hours, units: hours <= 1 ? "hour" : "hours" }; const result = [];
} else if (durationObj.asMinutes() >= 1) {
const minutes = Math.floor(durationObj.asMinutes()); if (parts.days > 0) {
return { time: minutes, units: minutes === 1 ? "minute" : "minutes" }; result.push(`${parts.days}d`);
} else {
const seconds = Math.floor(durationObj.asSeconds());
return { time: seconds, units: seconds === 1 ? "second" : "seconds" };
} }
if (parts.hours > 0) {
result.push(`${parts.hours}h`);
}
if (result.length < 2 && parts.minutes > 0) {
result.push(`${parts.minutes}m`);
}
if (result.length < 2 && parts.seconds > 0) {
result.push(`${parts.seconds}s`);
}
if (result.length === 0) {
// fallback for durations < 1s
return "0s";
}
return result.join(" ");
}; };
export const formatDate = (date, customOptions) => { export const formatDate = (date, customOptions) => {