feat: add MonitorLatestResponse and up status summary

This commit is contained in:
moonrailgun 2024-10-13 23:19:40 +08:00
parent e5e77dbdee
commit 316b95467d
4 changed files with 168 additions and 47 deletions

View File

@ -1,10 +1,9 @@
import { useResizeObserver } from '@/hooks/useResizeObserver'; import { useResizeObserver } from '@/hooks/useResizeObserver';
import { getStatusBgColorClassName, HealthStatus } from '@/utils/health';
import { cn } from '@/utils/style'; import { cn } from '@/utils/style';
import clsx from 'clsx'; import clsx from 'clsx';
import React from 'react'; import React from 'react';
export type HealthStatus = 'health' | 'error' | 'warning' | 'none';
export interface HealthBarBeat { export interface HealthBarBeat {
title?: string; title?: string;
status: HealthStatus; status: HealthStatus;
@ -52,12 +51,7 @@ export const HealthBar: React.FC<HealthBarProps> = React.memo((props) => {
'h-4 w-[5px]': size === 'small', 'h-4 w-[5px]': size === 'small',
'h-8 w-2': size === 'large', 'h-8 w-2': size === 'large',
}, },
{ getStatusBgColorClassName(beat.status)
'bg-green-500': beat.status === 'health',
'bg-red-600': beat.status === 'error',
'bg-yellow-400': beat.status === 'warning',
'bg-gray-400': beat.status === 'none',
}
)} )}
/> />
))} ))}

View File

@ -3,11 +3,18 @@ import React, { useMemo } from 'react';
import { bodySchema } from './schema'; import { bodySchema } from './schema';
import { Empty } from 'antd'; import { Empty } from 'antd';
import { useTranslation } from '@i18next-toolkit/react'; import { useTranslation } from '@i18next-toolkit/react';
import { MonitorListItem } from '../MonitorListItem';
import { cn } from '@/utils/style'; import { cn } from '@/utils/style';
import { Tooltip } from '@/components/ui/tooltip'; import {
import { MonitorHealthBar } from '../MonitorHealthBar'; Tooltip,
import { HealthBar, HealthStatus } from '@/components/HealthBar'; TooltipContent,
TooltipTrigger,
} from '@/components/ui/tooltip';
import { HealthBar } from '@/components/HealthBar';
import { getMonitorProvider, getProviderDisplay } from '../provider';
import {
getStatusBgColorClassName,
parseHealthStatusByPercent,
} from '@/utils/health';
interface StatusPageBodyProps { interface StatusPageBodyProps {
workspaceId: string; workspaceId: string;
@ -66,7 +73,6 @@ export const StatusItemMonitor: React.FC<{
showCurrent: boolean; showCurrent: boolean;
workspaceId: string; workspaceId: string;
}> = React.memo((props) => { }> = React.memo((props) => {
const { t } = useTranslation();
const { data: info } = trpc.monitor.getPublicInfo.useQuery( const { data: info } = trpc.monitor.getPublicInfo.useQuery(
{ {
monitorIds: [props.id], monitorIds: [props.id],
@ -81,6 +87,22 @@ export const StatusItemMonitor: React.FC<{
monitorId: props.id, monitorId: props.id,
}); });
const { summaryStatus, summaryPercent } = useMemo(() => {
let upCount = 0;
let totalCount = 0;
list.forEach((item) => {
upCount += item.upCount;
totalCount += item.totalCount;
});
const percent = Number(((upCount / totalCount) * 100).toFixed(1));
return {
summaryPercent: percent,
summaryStatus: parseHealthStatusByPercent(percent, totalCount),
};
}, [list]);
if (isLoading) { if (isLoading) {
return null; return null;
} }
@ -91,45 +113,38 @@ export const StatusItemMonitor: React.FC<{
'mb-1 flex items-center overflow-hidden rounded-lg bg-green-500 bg-opacity-0 px-4 py-3 hover:bg-opacity-10' 'mb-1 flex items-center overflow-hidden rounded-lg bg-green-500 bg-opacity-0 px-4 py-3 hover:bg-opacity-10'
)} )}
> >
{/* <div> <div>
<span <span
className={cn( className={cn(
'inline-block min-w-[62px] rounded-full p-0.5 text-center text-white', 'inline-block min-w-[62px] rounded-full p-0.5 text-center text-white',
upPercent === 100 ? 'bg-green-400' : 'bg-amber-400' getStatusBgColorClassName(summaryStatus)
)} )}
> >
{upPercent}% {summaryPercent}%
</span> </span>
</div> */} </div>
<div className="flex-1 pl-2"> <div className="flex-1 pl-2">
<div className="text-nowrap text-base">{info?.name}</div> <div className="text-nowrap text-base">{info?.name}</div>
</div> </div>
{/* {props.showCurrent && latestResponse && ( {props.showCurrent && info && (
<Tooltip title={t('Current')}> <MonitorLatestResponse
<div className="px-2 text-sm text-gray-800 dark:text-gray-400"> workspaceId={props.workspaceId}
{latestResponse} monitorId={info.id}
</div> monitorType={info.type}
</Tooltip> />
)} */} )}
<div className="flex-shrink basis-[260px] items-center overflow-hidden px-1"> <div className="flex-shrink basis-[250px] items-center overflow-hidden px-1">
<HealthBar <HealthBar
className="justify-end" className="justify-end"
size="small" size="small"
beats={[...list].reverse().map((item) => { beats={[...list].reverse().map((item) => {
let status: HealthStatus = 'none'; const status = parseHealthStatusByPercent(
item.upRate,
if (item.upRate === 1) { item.totalCount
status = 'health'; );
} else if (item.upRate === 0 && item.totalCount === 0) {
status = 'none';
} else if (item.upCount === 0 && item.totalCount !== 0) {
status = 'error';
} else {
status = 'warning';
}
return { return {
status, status,
@ -140,16 +155,47 @@ export const StatusItemMonitor: React.FC<{
</div> </div>
</div> </div>
); );
// return (
// <MonitorListItem
// key={item.id}
// workspaceId={props.workspaceId}
// monitorId={item.id}
// monitorName={item.name}
// monitorType={item.type}
// showCurrentResponse={props.showCurrent}
// />
// );
}); });
StatusItemMonitor.displayName = 'StatusItemMonitor'; StatusItemMonitor.displayName = 'StatusItemMonitor';
const MonitorLatestResponse: React.FC<{
workspaceId: string;
monitorId: string;
monitorType: string;
}> = React.memo((props) => {
const { t } = useTranslation();
const { data: recentText } = trpc.monitor.recentData.useQuery(
{
workspaceId: props.workspaceId,
monitorId: props.monitorId,
take: 1,
},
{
select: (data) => {
const provider = getMonitorProvider(props.monitorType);
const value = data[0].value;
if (!value) {
return '';
}
const { text } = getProviderDisplay(value, provider);
return text;
},
}
);
return (
<Tooltip>
<TooltipTrigger asChild={true}>
<div className="px-2 text-sm text-gray-800 dark:text-gray-400">
{recentText}
</div>
</TooltipTrigger>
<TooltipContent>{t('Current')}</TooltipContent>
</Tooltip>
);
});
MonitorLatestResponse.displayName = 'MonitorLatestResponse';

View File

@ -0,0 +1,45 @@
import { describe, test, expect } from 'vitest';
import {
parseHealthStatusByPercent,
getStatusBgColorClassName,
} from './health';
describe('parseHealthStatusByPercent', () => {
test('should return "health" when percent is 100', () => {
expect(parseHealthStatusByPercent(100, 0)).toEqual('health');
});
test('should return "none" when percent is 0 and count is 0', () => {
expect(parseHealthStatusByPercent(0, 0)).toEqual('none');
});
test('should return "error" when percent is 0 and count is not 0', () => {
expect(parseHealthStatusByPercent(0, 1)).toEqual('error');
});
test('should return "warning" for other cases', () => {
expect(parseHealthStatusByPercent(50, 1)).toEqual('warning');
});
});
describe('getStatusBgColorClassName', () => {
test('should return bg-green-500 for health status', () => {
expect(getStatusBgColorClassName('health')).toEqual('bg-green-500');
});
test('should return bg-red-600 for error status', () => {
expect(getStatusBgColorClassName('error')).toEqual('bg-red-600');
});
test('should return bg-yellow-400 for warning status', () => {
expect(getStatusBgColorClassName('warning')).toEqual('bg-yellow-400');
});
test('should return bg-gray-400 for none status', () => {
expect(getStatusBgColorClassName('none')).toEqual('bg-gray-400');
});
test('should return empty string for other status', () => {
expect(getStatusBgColorClassName('other' as any)).toEqual('');
});
});

View File

@ -0,0 +1,36 @@
export type HealthStatus = 'health' | 'error' | 'warning' | 'none';
/**
*
* @param percent 0 - 100
* @param count
* @returns
*/
export function parseHealthStatusByPercent(
percent: number,
count: number
): HealthStatus {
if (percent === 100) {
return 'health';
} else if (percent === 0 && count === 0) {
return 'none';
} else if (percent === 0 && count !== 0) {
return 'error';
} else {
return 'warning';
}
}
export function getStatusBgColorClassName(status: HealthStatus): string {
if (status === 'health') {
return 'bg-green-500';
} else if (status === 'error') {
return 'bg-red-600';
} else if (status === 'warning') {
return 'bg-yellow-400';
} else if (status === 'none') {
return 'bg-gray-400';
} else {
return '';
}
}