'use client'
import { useState } from 'react'
import dayjs from 'dayjs'
import { DateTimeFormats } from './Charts.constants'
import { numberFormatter } from './Charts.utils'
import { MultiAttribute } from './ComposedChartHandler'
import { Tooltip, TooltipContent, TooltipTrigger } from 'ui'
import { cn } from 'ui'
interface CustomIconProps {
color: string
}
const CustomIcon = ({ color }: CustomIconProps) => (
)
const MaxConnectionsIcon = () => (
)
interface TooltipProps {
active?: boolean
payload?: any[]
label?: string | number
attributes?: MultiAttribute[]
isPercentage?: boolean
valuePrecision?: number
showMaxValue?: boolean
showTotal?: boolean
}
export const formatBytes = (bytes: number, precision: number = 1) => {
if (bytes === 0) return '0 Bytes'
const k = 1024
const sizes = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return `${(bytes / Math.pow(k, i)).toFixed(precision)} ${sizes[i]}`
}
const isMaxAttribute = (attributes?: MultiAttribute[]) => attributes?.find((a) => a.isMaxValue)
/**
* Calculate the total aggregate of the chart values
* by summing the values of the attributes
* that are not in the `ignoreAttributes` array
*/
export const calculateTotalChartAggregate = (
payload: { dataKey: string; value: number }[],
ignoreAttributes?: string[]
) =>
payload
?.filter((p) => !ignoreAttributes?.includes(p.dataKey))
.reduce((acc, curr) => acc + curr.value, 0)
const CustomTooltip = ({
active,
payload,
attributes,
isPercentage,
valuePrecision,
showTotal,
}: TooltipProps) => {
if (active && payload && payload.length) {
const timestamp = payload[0].payload.timestamp
const maxValueAttribute = isMaxAttribute(attributes)
const maxValueData =
maxValueAttribute && payload?.find((p: any) => p.dataKey === maxValueAttribute.attribute)
const maxValue = maxValueData?.value
const isRamChart = payload?.some((p: any) => p.dataKey.toLowerCase().includes('ram_'))
const total =
showTotal &&
calculateTotalChartAggregate(
payload,
maxValueAttribute?.attribute ? [maxValueAttribute.attribute] : []
)
const getIcon = (name: string, color: string) => {
switch (name.toLowerCase().includes('max')) {
case false:
return
{dayjs(timestamp).format(DateTimeFormats.FULL_SECONDS)}