diff --git a/src/components/Charts/DeviceNetworkHealth.tsx b/src/components/Charts/DeviceNetworkHealth.tsx index 586d0f15..cb2018d0 100644 --- a/src/components/Charts/DeviceNetworkHealth.tsx +++ b/src/components/Charts/DeviceNetworkHealth.tsx @@ -20,28 +20,30 @@ type DeviceNetworkHealthProps = { downloadRate: number[] } const DeviceNetworkHealth = ({ uploadRate, downloadRate }: DeviceNetworkHealthProps) => { - const chartData: ChartData[] = [ - { - id: 'uploadRate', - color: '#8DC6BC', - data: uploadRate.map((yValue, index: number) => ({ - x: index + 1, - y: yValue, - })), - }, - { - id: 'downloadRate', - color: '#D92344', - data: downloadRate.map((yValue, index: number) => ({ - x: index + 1, - y: yValue, - })), - }, - ] - const currentLoad = - chartData[0].data.length > 0 ? chartData[0].data[chartData[0].data.length - 1].y : 0 + const THRESHOLD = 60; + const GOOD_COLOR = '#8DC6BC'; + const POOR_COLOR_UPLOAD = '#e95460'; + const POOR_COLOR_DOWNLOAD = '#D92344'; - const message = currentLoad > 60 ? 'Good' : 'Poor' + const processDataRate = (rate: number[], id: string, poorColor: string) => { + const dataObj = rate.map((yValue, index: number) => ({ x: index + 1, y: yValue })); + const currentLoad = dataObj.length > 0 ? dataObj[dataObj.length - 1].y : 0; + const message = currentLoad > THRESHOLD ? 'Good' : 'Poor'; + const color = message === 'Good' ? GOOD_COLOR : poorColor; + + return { + id, + color, + data: dataObj, + currentLoad, + message, + }; + }; + + const upload = processDataRate(uploadRate, 'uploadRate', POOR_COLOR_UPLOAD); + const download = processDataRate(downloadRate, 'downloadRate', POOR_COLOR_DOWNLOAD); + + const chartData: ChartData[] = [upload, download]; return ( @@ -70,21 +72,21 @@ const DeviceNetworkHealth = ({ uploadRate, downloadRate }: DeviceNetworkHealthPr Network - {currentLoad} GB + {upload.currentLoad} GB : } + icon={ upload.message === 'Good' ? : } weight={'semibold'} > - {message} + { upload.message} - {message === 'Poor' && ( + { upload.message === 'Poor' && ( - {((currentLoad / 60) * 100).toFixed(0)}% Utilization + {((upload.currentLoad / 60) * 100).toFixed(0)}% Utilization )}