import { BarChart2 } from 'lucide-react' import { useMemo } from 'react' import AlertError from 'components/ui/AlertError' import Panel from 'components/ui/Panel' import { GenericSkeletonLoader } from 'components/ui/ShimmeringLoader' import { DataPoint } from 'data/analytics/constants' import { useOrgDailyComputeStatsQuery } from 'data/analytics/org-daily-compute-stats-query' import { ComputeUsageMetric, computeUsageMetricLabel } from 'data/analytics/org-daily-stats-query' import type { OrgSubscription } from 'data/subscriptions/types' import { useIsFeatureEnabled } from 'hooks/misc/useIsFeatureEnabled' import { DOCS_URL } from 'lib/constants' import { SectionContent } from './SectionContent' import { Attribute, AttributeColor } from './Usage.constants' import UsageBarChart from './UsageBarChart' export interface ComputeProps { orgSlug: string projectRef?: string startDate: string | undefined endDate: string | undefined subscription: OrgSubscription | undefined } const Compute = ({ orgSlug, projectRef, startDate, endDate }: ComputeProps) => { const allAttributeKeys = Object.values(ComputeUsageMetric).map((it) => it.toLowerCase()) const { data: egressData, isLoading, error, isSuccess, } = useOrgDailyComputeStatsQuery({ orgSlug, projectRef, startDate, endDate, }) const { billingAll } = useIsFeatureEnabled(['billing:all']) const chartData: DataPoint[] = egressData?.data ?? [] const COMPUTE_TO_COLOR: Record = { [ComputeUsageMetric.COMPUTE_HOURS_BRANCH]: 'blue', [ComputeUsageMetric.COMPUTE_HOURS_XS]: 'white', [ComputeUsageMetric.COMPUTE_HOURS_SM]: 'green', [ComputeUsageMetric.COMPUTE_HOURS_MD]: 'dark-green', [ComputeUsageMetric.COMPUTE_HOURS_L]: 'yellow', [ComputeUsageMetric.COMPUTE_HOURS_XL]: 'dark-yellow', [ComputeUsageMetric.COMPUTE_HOURS_2XL]: 'orange', [ComputeUsageMetric.COMPUTE_HOURS_4XL]: 'dark-orange', [ComputeUsageMetric.COMPUTE_HOURS_8XL]: 'red', [ComputeUsageMetric.COMPUTE_HOURS_12XL]: 'dark-red', [ComputeUsageMetric.COMPUTE_HOURS_16XL]: 'purple', [ComputeUsageMetric.COMPUTE_HOURS_24XL]: 'purple', [ComputeUsageMetric.COMPUTE_HOURS_24XL_OPTIMIZED_CPU]: 'purple', [ComputeUsageMetric.COMPUTE_HOURS_24XL_OPTIMIZED_MEMORY]: 'purple', [ComputeUsageMetric.COMPUTE_HOURS_24XL_HIGH_MEMORY]: 'purple', [ComputeUsageMetric.COMPUTE_HOURS_48XL]: 'purple', [ComputeUsageMetric.COMPUTE_HOURS_48XL_OPTIMIZED_CPU]: 'purple', [ComputeUsageMetric.COMPUTE_HOURS_48XL_OPTIMIZED_MEMORY]: 'purple', [ComputeUsageMetric.COMPUTE_HOURS_48XL_HIGH_MEMORY]: 'purple', } const attributes: Attribute[] = Object.keys(ComputeUsageMetric).map((it) => ({ key: it.toLowerCase(), color: COMPUTE_TO_COLOR[it as ComputeUsageMetric] || 'white', name: computeUsageMetricLabel(it as ComputeUsageMetric), })) const attributeKeysWithData = useMemo(() => { return allAttributeKeys.filter((attributeKey) => chartData.some((data) => data[attributeKey])) }, [chartData]) const notAllValuesZero = useMemo(() => { return attributeKeysWithData.length > 0 }, [attributeKeysWithData]) return (
{isLoading && } {error != null && } {isSuccess && ( <>
{chartData.length > 0 && (

Compute Hours usage

)} {attributeKeysWithData.map((key) => (

{computeUsageMetricLabel(key.toUpperCase() as ComputeUsageMetric)} {' '} Compute Hours usage in period

{chartData.reduce((prev, cur) => prev + ((cur[key] as number) ?? 0), 0)} hours

))}

Compute Hours usage per day

The data refreshes every hour.

{isLoading ? ( ) : chartData.length > 0 && notAllValuesZero ? ( ) : (

No data in period

May take up to one hour to show

)} )}
) } export default Compute