import useSWR from 'swr' import dayjs from 'dayjs' import Link from 'next/link' import { FC, useState } from 'react' import { useRouter } from 'next/router' import { Button, Dropdown, IconArchive, IconChevronDown, IconDatabase, IconKey, IconZap } from 'ui' import ChartHandler from 'components/to-be-cleaned/Charts/ChartHandler' import Panel from 'components/ui/Panel' import { get } from 'lib/common/fetch' import { API_URL, DATE_FORMAT, METRICS } from 'lib/constants' import { ChartIntervals } from 'types' const CHART_INTERVALS: ChartIntervals[] = [ { key: 'minutely', label: '60 minutes', startValue: 1, startUnit: 'hour', format: 'MMM D, h:mma', }, { key: 'hourly', label: '24 hours', startValue: 24, startUnit: 'hour', format: 'MMM D, ha' }, { key: 'daily', label: '7 days', startValue: 7, startUnit: 'day', format: 'MMM D' }, ] interface Props {} const ProjectUsage: FC = ({}) => { const router = useRouter() const { ref } = router.query const [interval, setInterval] = useState('hourly') const { data, error }: any = useSWR( `${API_URL}/projects/${ref}/log-stats?interval=${interval}`, get ) const selectedInterval = CHART_INTERVALS.find((i) => i.key === interval) || CHART_INTERVALS[1] const startDate = dayjs() .subtract(selectedInterval.startValue, selectedInterval.startUnit) .format(DATE_FORMAT) const endDate = dayjs().format(DATE_FORMAT) const charts = data?.data const datetimeFormat = selectedInterval.format || 'MMM D, ha' const handleBarClick = (v: any, search: string) => { if (!v || !v.activePayload?.[0]?.payload) return // returns rechart internal tooltip data type const payload = v.activePayload[0].payload const timestamp = payload.timestamp const timestampDigits = String(timestamp).length if (timestampDigits < 16) { // pad unix timestamp with additional 0 and then forward const paddedTimestamp = String(timestamp) + '0'.repeat(16 - timestampDigits) router.push(`/project/${ref}/logs/edge-logs?te=${paddedTimestamp}`) } else { router.push(`/project/${ref}/logs/edge-logs?te=${timestamp}`) } } return (
{CHART_INTERVALS.map((i) => ( {i.label} ))} } > Statistics for past {selectedInterval.label}
{startDate && endDate && ( <>
} title="Database" href={`/project/${ref}/editor`} /> x.key == 'total_rest_requests')?.label ?? ''} provider="log-stats" interval="1d" hideChartType customDateFormat={datetimeFormat} data={charts} isLoading={!charts && !error ? true : false} onBarClick={(v) => handleBarClick(v, '/rest')} />
} title="Auth" href={`/project/${ref}/auth/users`} /> x.key == 'total_auth_requests')?.label ?? ''} provider="log-stats" interval="1d" hideChartType customDateFormat={datetimeFormat} data={charts} isLoading={!charts && !error ? true : false} onBarClick={(v) => handleBarClick(v, '/auth')} />
} title="Storage" href={`/project/${ref}/storage/buckets`} /> x.key == 'total_storage_requests')?.label ?? ''} provider="log-stats" interval="1d" hideChartType customDateFormat={datetimeFormat} data={charts} isLoading={!charts && !error ? true : false} onBarClick={(v) => handleBarClick(v, '/storage')} /> } title="Realtime" /> x.key == 'total_realtime_requests')?.label ?? '' } provider="log-stats" interval="1h" hideChartType customDateFormat={datetimeFormat} data={charts} isLoading={!charts && !error ? true : false} onBarClick={(v) => handleBarClick(v, '/realtime')} /> )} ) } export default ProjectUsage const PanelHeader = (props: any) => { const Tag = props?.href ? Link : 'div' return (

{props.icon}

{props.title}

) }