Files
Checkmate/client/src/Utils/MonitorUtils.ts
T

88 lines
2.1 KiB
TypeScript
Raw Normal View History

2026-02-13 21:19:55 +00:00
import type { MonitorStatus, MonitorType } from "@/Types/Monitor";
2026-02-20 18:10:43 +00:00
import type { PaletteKey } from "@/Utils/Theme/Theme";
2026-02-20 18:13:39 +00:00
import type { ValueType } from "@/Components/design-elements/StatusLabel";
2026-01-23 23:54:58 +00:00
export const getMonitorPath = (type: MonitorType): string => {
const pathMap: Record<MonitorType, string> = {
http: "uptime",
port: "uptime",
ping: "uptime",
2026-02-12 19:13:53 +00:00
game: "uptime",
2026-02-19 13:02:38 +05:30
grpc: "uptime",
websocket: "uptime",
2026-01-23 23:54:58 +00:00
unknown: "uptime",
2026-02-12 19:13:53 +00:00
docker: "uptime",
hardware: "infrastructure",
pagespeed: "pagespeed",
2026-01-23 23:54:58 +00:00
};
return pathMap[type];
};
export const getStatusPalette = (status: MonitorStatus): PaletteKey => {
2026-02-11 20:11:22 +00:00
if (status === "up") {
2026-01-23 23:54:58 +00:00
return "success";
}
2026-02-11 20:11:22 +00:00
if (status === "down") {
2026-01-23 23:54:58 +00:00
return "error";
}
if (status === "exceeded") {
2026-02-14 22:52:14 +00:00
return "error";
}
2026-01-23 23:54:58 +00:00
return "warning";
};
export const getValuePalette = (value: ValueType): PaletteKey => {
const paletteMap: Record<ValueType, PaletteKey> = {
positive: "success",
negative: "error",
neutral: "warning",
};
return paletteMap[value];
};
export const getStatusColor = (status: MonitorStatus, theme: any): string => {
2026-02-11 20:11:22 +00:00
if (status === "up") {
2026-01-23 23:54:58 +00:00
return theme.palette.success.light;
}
2026-02-11 20:11:22 +00:00
if (status === "down") {
2026-01-23 23:54:58 +00:00
return theme.palette.error.light;
}
return theme.palette.warning.light;
};
export const getResponseTimeColor = (responseTime: number): PaletteKey => {
if (responseTime < 200) {
return "success";
} else if (responseTime < 300) {
return "warning";
} else {
return "error";
}
};
export const getInfraGaugeColor = (val: number, theme: any) => {
if (val < 50) {
return theme.palette.success.main;
} else if (val < 80) {
return theme.palette.warning.light;
} else {
return theme.palette.error.light;
}
};
export const getPageSpeedPalette = (score: number): PaletteKey => {
if (score >= 90) return "success";
else if (score >= 50) return "warning";
else return "error";
};
export const formatUrl = (url: string, maxLength: number = 55) => {
if (!url) return "";
const strippedUrl = url.replace(/^https?:\/\//, "");
return strippedUrl.length > maxLength
? `${strippedUrl.slice(0, maxLength)}…`
: strippedUrl;
};