import AlertError from 'components/ui/AlertError' import ShimmeringLoader from 'components/ui/ShimmeringLoader' import type { OrgSubscription } from 'data/subscriptions/types' import SectionContent from '../SectionContent' import { CategoryAttribute } from '../Usage.constants' import { useOrgProjectsQuery } from 'data/projects/org-projects' import { PROJECT_STATUS } from 'lib/constants' import { Button, Alert_Shadcn_, CriticalIcon, AlertTitle_Shadcn_, AlertDescription_Shadcn_, } from 'ui' import MotionNumber from '@number-flow/react' import Link from 'next/link' import { useMemo } from 'react' import { InfoTooltip } from 'ui-patterns/info-tooltip' import { OrgUsageResponse } from 'data/usage/org-usage-query' import { PricingMetric } from 'data/analytics/org-daily-stats-query' import Panel from 'components/ui/Panel' export interface DiskUsageProps { slug: string projectRef?: string attribute: CategoryAttribute subscription?: OrgSubscription usage?: OrgUsageResponse currentBillingCycleSelected: boolean } const DiskUsage = ({ slug, projectRef, attribute, subscription, usage, currentBillingCycleSelected, }: DiskUsageProps) => { const { data: diskUsage, isError, isLoading, isSuccess, error, } = useOrgProjectsQuery( { orgSlug: slug, }, { enabled: currentBillingCycleSelected, } ) const relevantProjects = useMemo(() => { return diskUsage ? diskUsage.projects .filter((project) => { // We do want to show branches that are exceeding the 8 GB limit, as people could have persistent or very long-living branches const isBranchExceedingFreeQuota = project.is_branch && project.databases.some((db) => (db.disk_volume_size_gb ?? 8) > 8) const isActiveProject = project.status !== PROJECT_STATUS.INACTIVE const isHostedOnAws = project.databases.every((db) => db.cloud_provider === 'AWS') return ( (!project.is_branch || isBranchExceedingFreeQuota) && isActiveProject && isHostedOnAws ) }) .filter((it) => it.ref === projectRef || !projectRef) : [] }, [diskUsage, projectRef]) const hasProjectsExceedingDiskSize = useMemo(() => { return relevantProjects.some((it) => it.databases.some( (db) => db.type === 'READ_REPLICA' || (db.disk_volume_size_gb && db.disk_volume_size_gb > 8) ) ) }, [relevantProjects]) const gp3UsageInPeriod = usage?.usages.find( (it) => it.metric === PricingMetric.DISK_SIZE_GB_HOURS_GP3 ) const io2UsageInPeriod = usage?.usages.find( (it) => it.metric === PricingMetric.DISK_SIZE_GB_HOURS_IO2 ) return (
{isLoading && (
)} {isError && } {isSuccess && (
{currentBillingCycleSelected && subscription?.usage_billing_enabled === false && hasProjectsExceedingDiskSize && ( Projects exceeding quota You have projects that are exceeding 8 GB of provisioned disk size, but do not allow any overages with the Spend Cap on. Reduce the disk size or disable the spend cap. )}

{attribute.name} usage

Included in {subscription?.plan?.name} Plan

8 GB GP3 disk per project

Overages in period

{(gp3UsageInPeriod?.usage ?? 0).toLocaleString()} GP3 GB-Hrs {io2UsageInPeriod?.usage ? ` / ${io2UsageInPeriod.usage.toLocaleString()} IO2 GB-Hrs` : ``}

{currentBillingCycleSelected ? (

Current disk size per project

Breakdown of disk per project. Head to your project's disk management section to see database size used.

{relevantProjects.length === 0 && (

No active projects

You don't have any active projects in this organization.

)} {relevantProjects.map((project, idx) => { const primaryDiskUsage = project.databases .filter((it) => it.type === 'PRIMARY') .reduce((acc, curr) => acc + (curr.disk_volume_size_gb ?? 8), 0) const replicaDbs = project.databases.filter((it) => it.type !== 'PRIMARY') const replicaDiskUsage = replicaDbs.reduce( (acc, curr) => acc + (curr.disk_volume_size_gb ?? 8), 0 ) const totalDiskUsage = primaryDiskUsage + replicaDiskUsage return (
{project.name}
{' '} GB Disk provisioned

{primaryDiskUsage} GB for Primary Database

{replicaDbs.length > 0 && ( <>

{replicaDiskUsage} GB for {replicaDbs.length} Read{' '} {replicaDbs.length === 1 ? 'Replica' : 'Replicas'}

Read replicas have their own disk and use 25% more disk to account for WAL files.

)}
) })}
) : (

Data not available

Switch to current billing cycle to see current disk size per project.

)}
)}
) } export default DiskUsage