import { useParams } from 'common' import dayjs from 'dayjs' import sumBy from 'lodash/sumBy' import Link from 'next/link' import { useRouter } from 'next/router' import { useState } from 'react' import { ChartIntervals } from 'types' import { Button, DropdownMenu, DropdownMenuContent, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuTrigger, IconArchive, IconChevronDown, IconDatabase, IconKey, IconZap, Loading, } from 'ui' import BarChart from 'components/ui/Charts/BarChart' import Panel from 'components/ui/Panel' import { UsageApiCounts, useProjectLogStatsQuery } from 'data/analytics/project-log-stats-query' import useFillTimeseriesSorted from 'hooks/analytics/useFillTimeseriesSorted' import { useIsFeatureEnabled } from 'hooks' 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' }, ] const ProjectUsage = () => { const router = useRouter() const { ref: projectRef } = useParams() const { projectAuthAll: authEnabled, projectStorageAll: storageEnabled } = useIsFeatureEnabled([ 'project_auth:all', 'project_storage:all', ]) const [interval, setInterval] = useState('hourly') const { data, error, isLoading } = useProjectLogStatsQuery({ projectRef, interval }) const selectedInterval = CHART_INTERVALS.find((i) => i.key === interval) || CHART_INTERVALS[1] const startDateLocal = dayjs().subtract( selectedInterval.startValue, selectedInterval.startUnit as dayjs.ManipulateType ) const endDateLocal = dayjs() const charts = useFillTimeseriesSorted( data?.result || [], 'timestamp', [ 'total_auth_requests', 'total_rest_requests', 'total_storage_requests', 'total_realtime_requests', ], 0, startDateLocal.toISOString(), endDateLocal.toISOString() ) const datetimeFormat = selectedInterval.format || 'MMM D, ha' const handleBarClick = ( value: UsageApiCounts, // TODO (ziinc): link to edge logs with correct filter applied _type: 'rest' | 'realtime' | 'storage' | 'auth' ) => { const unit = selectedInterval.startUnit const selectedStart = dayjs(value?.timestamp) const selectedEnd = selectedStart.add(1, unit) router.push( `/project/${projectRef}/logs/edge-logs?ite=${encodeURIComponent(selectedEnd.toISOString())}` ) } return (
{CHART_INTERVALS.map((i) => ( {i.label} ))} Statistics for past {selectedInterval.label}
} title="Database" href={`/project/${projectRef}/editor`} /> handleBarClick(v as UsageApiCounts, 'rest')} customDateFormat={datetimeFormat} highlightedValue={sumBy(charts, 'total_rest_requests')} /> {authEnabled && (
} title="Auth" href={`/project/${projectRef}/auth/users`} /> handleBarClick(v as UsageApiCounts, 'auth')} customDateFormat={datetimeFormat} highlightedValue={sumBy(charts || [], 'total_auth_requests')} /> )} {storageEnabled && ( } title="Storage" href={`/project/${projectRef}/storage/buckets`} /> handleBarClick(v as UsageApiCounts, 'storage')} customDateFormat={datetimeFormat} highlightedValue={sumBy(charts, 'total_storage_requests')} /> )} } title="Realtime" /> handleBarClick(v as UsageApiCounts, 'realtime')} customDateFormat={datetimeFormat} highlightedValue={sumBy(charts, 'total_realtime_requests')} /> ) } export default ProjectUsage const PanelHeader = (props: any) => { const Tag = props?.href ? Link : 'div' return (
{props.icon}

{props.title}

) }