import dayjs from 'dayjs' import sumBy from 'lodash/sumBy' import { Archive, ChevronDown, Database, Key, Zap } from 'lucide-react' import Link from 'next/link' import { useRouter } from 'next/router' import { useState } from 'react' import { useParams } from 'common' import BarChart from 'components/ui/Charts/BarChart' import Panel from 'components/ui/Panel' import { ProjectLogStatsVariables, UsageApiCounts, useProjectLogStatsQuery, } from 'data/analytics/project-log-stats-query' import { useFillTimeseriesSorted } from 'hooks/analytics/useFillTimeseriesSorted' import { useCurrentOrgPlan } from 'hooks/misc/useCurrentOrgPlan' import { useIsFeatureEnabled } from 'hooks/misc/useIsFeatureEnabled' import type { ChartIntervals } from 'types' import { Button, DropdownMenu, DropdownMenuContent, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuTrigger, Loading, } from 'ui' const CHART_INTERVALS: ChartIntervals[] = [ { key: 'minutely', label: 'Last 60 minutes', startValue: 1, startUnit: 'hour', format: 'MMM D, h:mma', availableIn: ['free', 'pro', 'enterprise', 'team'], }, { key: 'hourly', label: 'Last 24 hours', startValue: 24, startUnit: 'hour', format: 'MMM D, ha', availableIn: ['free', 'pro', 'enterprise', 'team'], }, { key: 'daily', label: 'Last 7 days', startValue: 7, startUnit: 'day', format: 'MMM D', availableIn: ['pro', 'enterprise', 'team'], }, ] const ProjectUsage = () => { const router = useRouter() const { ref: projectRef } = useParams() const { projectAuthAll: authEnabled, projectStorageAll: storageEnabled } = useIsFeatureEnabled([ 'project_auth:all', 'project_storage:all', ]) const { plan } = useCurrentOrgPlan() const [interval, setInterval] = useState('minutely') const { data, 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 { data: charts } = useFillTimeseriesSorted( data?.result || [], 'timestamp', [ 'total_auth_requests', 'total_rest_requests', 'total_storage_requests', 'total_realtime_requests', ], 0, startDateLocal.toISOString(), endDateLocal.toISOString(), 5 ) 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 (
setInterval(interval as ProjectLogStatsVariables['interval']) } > {CHART_INTERVALS.map((i) => ( {i.label} ))} Statistics for {selectedInterval.label.toLowerCase()}
} 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}

) }