Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Refactor each billing section to render via a map rather than manually declaring each attribute

Joshen Lim62b0d72a8c71
diff --git a/studio/components/interfaces/BillingV2/Usage/Activity.tsx b/studio/components/interfaces/BillingV2/Usage/Activity.tsx
index b5c7b444af3..d5cc12fffc7 100644
--- a/studio/components/interfaces/BillingV2/Usage/Activity.tsx
+++ b/studio/components/interfaces/BillingV2/Usage/Activity.tsx
@@ -1,14 +1,19 @@
-import { useParams } from 'common'
+import clsx from 'clsx'
import ShimmeringLoader from 'components/ui/ShimmeringLoader'
import SparkBar from 'components/ui/SparkBar'
import { useDailyStatsQuery } from 'data/analytics/daily-stats-query'
import { useProjectSubscriptionQuery } from 'data/subscriptions/project-subscription-query'
-import { useProjectUsageQuery } from 'data/usage/project-usage-query'
-import { Button } from 'ui'
+import {
+ ProjectUsageResponse,
+ UsageMetric,
+ useProjectUsageQuery,
+} from 'data/usage/project-usage-query'
+import { Button, IconAlertTriangle } from 'ui'
+import { USAGE_APPROACHING_THRESHOLD } from '../Billing.constants'
import BarChart from './BarChart'
import SectionContent from './SectionContent'
import SectionHeader from './SectionHeader'
-import { generateUsageData } from './Usage.utils'
+import { USAGE_CATEGORIES } from './Usage.constants'
export interface ActivityProps {
projectRef: string
@@ -20,10 +25,9 @@ const Activity = ({ projectRef }: ActivityProps) => {
const { current_period_start, current_period_end } = subscription?.billing ?? {}
const startDate = new Date((current_period_start ?? 0) * 1000).toISOString()
const endDate = new Date((current_period_end ?? 0) * 1000).toISOString()
+ const categoryMeta = USAGE_CATEGORIES.find((category) => category.key === 'activity')
const MAU_KEY = 'total_auth_billing_period_mau'
- const { monthly_active_users } = usage ?? {}
- const mauExcess = (monthly_active_users?.usage ?? 0) - (monthly_active_users?.limit ?? 0)
const { data: mauData, isLoading: isLoadingMauData } = useDailyStatsQuery({
projectRef,
attribute: MAU_KEY,
@@ -33,9 +37,6 @@ const Activity = ({ projectRef }: ActivityProps) => {
})
const ASSET_TRANSFORMATIONS_KEY = 'total_storage_image_render_count'
- const { storage_image_render_count } = usage ?? {}
- const assetTransformationExcess =
- (storage_image_render_count?.usage ?? 0) - (storage_image_render_count?.limit ?? 0)
const { data: assetTransformationsData, isLoading: isLoadingAssetTransformationsData } =
useDailyStatsQuery({
projectRef,
@@ -46,8 +47,6 @@ const Activity = ({ projectRef }: ActivityProps) => {
})
const FUNC_INVOCATIONS_KEY = 'total_func_invocations'
- const { func_invocations } = usage ?? {}
- const funcInvocationsExcess = (func_invocations?.usage ?? 0) - (func_invocations?.limit ?? 0)
const { data: funcInvocationsData, isLoading: isLoadingFuncInvocationsData } = useDailyStatsQuery(
{
projectRef,
@@ -58,243 +57,136 @@ const Activity = ({ projectRef }: ActivityProps) => {
}
)
+ if (categoryMeta === undefined) return null
+
return (
<>
<SectionHeader title="Activity" description="Some description here" />
- <SectionContent title="Monthly Active Users (MAU)" description="Some description here">
- <div className="space-y-2">
- <div className="flex items-center justify-between">
- <p className="text-sm">
- Monthly active users {subscription?.tier.key.toLowerCase()} quota usage
- </p>
- <Button type="default" size="tiny" onClick={() => {}}>
- Upgrade project
- </Button>
- </div>
- <SparkBar
- type="horizontal"
- barClass="bg-scale-1200"
- value={monthly_active_users?.usage ?? 0}
- max={monthly_active_users?.limit ?? 0}
- />
- <div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">
- Included in {subscription?.tier.name.toLowerCase()}
- </p>
- <p className="text-xs">{(monthly_active_users?.limit ?? 0).toLocaleString()}</p>
- </div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">Used</p>
- <p className="text-xs">{(monthly_active_users?.usage ?? 0).toLocaleString()}</p>
- </div>
- <div className="flex items-center justify-between py-1">
- <p className="text-xs text-scale-1000">Extra volume used this month</p>
- <p className="text-xs">{(mauExcess < 0 ? 0 : mauExcess).toLocaleString()}</p>
- </div>
- </div>
- </div>
- <div className="space-y-1">
- <p>Monthly active users over time</p>
- <p className="text-sm text-scale-1000">Some description here</p>
- </div>
- {isLoadingMauData ? (
- <div className="space-y-2">
- <ShimmeringLoader />
- <ShimmeringLoader className="w-3/4" />
- <ShimmeringLoader className="w-1/2" />
- </div>
- ) : (
- <BarChart
- hasQuota
- attribute={MAU_KEY}
- data={mauData?.data ?? []}
- yLimit={monthly_active_users?.limit ?? 0}
- yLeftMargin={18}
- yFormatter={(value) => value.toLocaleString()}
- />
- )}
- </SectionContent>
+ {categoryMeta.attributes.map((attribute) => {
+ const usageMeta = usage?.[attribute.key as keyof ProjectUsageResponse] as UsageMetric
+ const usageRatio =
+ typeof usageMeta !== 'number' ? (usageMeta?.usage ?? 0) / (usageMeta?.limit ?? 0) : 0
+ const usageExcess = (usageMeta?.usage ?? 0) - (usageMeta?.limit ?? 0)
- <SectionContent title="Storage Image Transformations" description="Some description here">
- <div className="space-y-2">
- <div className="flex items-center justify-between">
- <p className="text-sm">
- Asset transformations {subscription?.tier.key.toLowerCase()} quota usage
- </p>
- <Button type="default" size="tiny" onClick={() => {}}>
- Upgrade project
- </Button>
- </div>
- <SparkBar
- type="horizontal"
- barClass="bg-scale-1200"
- value={storage_image_render_count?.usage ?? 0}
- max={storage_image_render_count?.limit ?? 0}
- />
- <div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">
- Included in {subscription?.tier.name.toLowerCase()}
- </p>
- {storage_image_render_count?.limit === -1 ? (
- <p className="text-xs">None</p>
- ) : (
- <p className="text-xs">
- {(storage_image_render_count?.limit ?? 0).toLocaleString()}
- </p>
- )}
- </div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">Used</p>
- <p className="text-xs">{(storage_image_render_count?.usage ?? 0).toLocaleString()}</p>
- </div>
- <div className="flex items-center justify-between py-1">
- <p className="text-xs text-scale-1000">Extra volume used this month</p>
- <p className="text-xs">
- {((storage_image_render_count?.limit ?? 0) === -1 || assetTransformationExcess < 0
- ? 0
- : assetTransformationExcess
- ).toLocaleString()}
- </p>
- </div>
- </div>
- </div>
- {storage_image_render_count?.available_in_plan && (
- <>
- <div className="space-y-1">
- <p>Asset transformations over time</p>
- <p className="text-sm text-scale-1000">Some description here</p>
- </div>
- {isLoadingAssetTransformationsData ? (
- <div className="space-y-2">
- <ShimmeringLoader />
- <ShimmeringLoader className="w-3/4" />
- <ShimmeringLoader className="w-1/2" />
- </div>
- ) : (
- <BarChart
- hasQuota
- attribute={ASSET_TRANSFORMATIONS_KEY}
- data={assetTransformationsData?.data ?? []}
- yLimit={storage_image_render_count?.limit ?? 0}
- />
- )}
- </>
- )}
- </SectionContent>
+ const isLoadingData =
+ attribute.key === 'monthly_active_users'
+ ? isLoadingMauData
+ : attribute.key === 'storage_image_render_count'
+ ? isLoadingAssetTransformationsData
+ : isLoadingFuncInvocationsData
+ const usageData =
+ attribute.key === 'monthly_active_users'
+ ? mauData
+ : attribute.key === 'storage_image_render_count'
+ ? assetTransformationsData
+ : funcInvocationsData
- <SectionContent title="Edge Function Invocations" description="Some description here">
- <div className="space-y-2">
- <div className="flex items-center justify-between">
- <p className="text-sm">
- Edge function invocations {subscription?.tier.key.toLowerCase()} quota usage
- </p>
- <Button type="default" size="tiny" onClick={() => {}}>
- Upgrade project
- </Button>
- </div>
- <SparkBar
- type="horizontal"
- barClass="bg-scale-1200"
- value={func_invocations?.usage ?? 0}
- max={func_invocations?.limit ?? 0}
- />
- <div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">
- Included in {subscription?.tier.name.toLowerCase()}
- </p>
- <p className="text-xs">{(func_invocations?.limit ?? 0).toLocaleString()}</p>
- </div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">Used</p>
- <p className="text-xs">{(func_invocations?.usage ?? 0).toLocaleString()}</p>
- </div>
- <div className="flex items-center justify-between py-1">
- <p className="text-xs text-scale-1000">Extra volume used this month</p>
- <p className="text-xs">
- {(funcInvocationsExcess < 0 ? 0 : funcInvocationsExcess).toLocaleString()}
- </p>
- </div>
- </div>
- </div>
- <div className="space-y-1">
- <p>Edge function invocations over time</p>
- <p className="text-sm text-scale-1000">Some description here</p>
- </div>
- {isLoadingFuncInvocationsData ? (
- <div className="space-y-2">
- <ShimmeringLoader />
- <ShimmeringLoader className="w-3/4" />
- <ShimmeringLoader className="w-1/2" />
- </div>
- ) : (
- <BarChart
- hasQuota
- attribute={FUNC_INVOCATIONS_KEY}
- data={funcInvocationsData?.data ?? []}
- unit={undefined}
- yLimit={func_invocations?.limit ?? 0}
- yLeftMargin={26}
- yFormatter={(value) => value.toLocaleString()}
- />
- )}
- </SectionContent>
-
- {/* Sample Chart */}
- <div className="border-b">
- <div className="1xl:px-28 mx-auto flex flex-col gap-10 px-5 lg:px-16 2xl:px-32 py-16">
- <div className="grid grid-cols-12">
- <div className="col-span-5">
- <div className="sticky top-16">
- <p className="text-base">Sample Chart</p>
- <p className="text-sm text-scale-1000">Some description here</p>
+ return (
+ <SectionContent
+ key={attribute.key}
+ title={attribute.name}
+ description={attribute.description}
+ >
+ <div className="space-y-2">
+ <div className="flex items-center justify-between">
+ <div className="flex items-center space-x-4">
+ <p className="text-sm">
+ {attribute.name} {subscription?.tier.key.toLowerCase()} quota usage
+ </p>
+ {usageRatio >= 1 ? (
+ <div className="flex items-center space-x-2 min-w-[115px]">
+ <IconAlertTriangle size={14} strokeWidth={2} className="text-red-900" />
+ <p className="text-sm text-red-900">Exceeded limit</p>
+ </div>
+ ) : usageRatio >= USAGE_APPROACHING_THRESHOLD ? (
+ <div className="flex items-center space-x-2 min-w-[115px]">
+ <IconAlertTriangle size={14} strokeWidth={2} className="text-amber-900" />
+ <p className="text-sm text-red-900">Reaching limit</p>
+ </div>
+ ) : null}
+ </div>
+ <Button type="default" size="tiny" onClick={() => {}}>
+ Upgrade project
+ </Button>
</div>
- </div>
- <div className="col-span-7 space-y-6">
- <div className="space-y-2">
- <div className="flex items-center justify-between">
- <p className="text-sm">Sample quota usage</p>
- <Button type="default" size="tiny" onClick={() => {}}>
- Upgrade project
- </Button>
+ <SparkBar
+ type="horizontal"
+ barClass={clsx(
+ usageRatio >= 1
+ ? 'bg-red-900'
+ : usageRatio >= USAGE_APPROACHING_THRESHOLD
+ ? 'bg-amber-900'
+ : 'bg-scale-1100'
+ )}
+ value={usageMeta?.usage ?? 0}
+ max={usageMeta?.limit ?? 0}
+ />
+ <div>
+ <div className="flex items-center justify-between border-b py-1">
+ <p className="text-xs text-scale-1000">
+ Included in {subscription?.tier.name.toLowerCase()}
+ </p>
+ {usageMeta?.limit === -1 ? (
+ <p className="text-xs">None</p>
+ ) : (
+ <p className="text-xs">{(usageMeta?.limit ?? 0).toLocaleString()}</p>
+ )}
</div>
- <SparkBar type="horizontal" barClass="bg-scale-1200" value={75} max={100} />
- <div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">
- Included in {subscription?.tier.name.toLowerCase()}
- </p>
- <p className="text-xs">{(10000).toLocaleString()}</p>
- </div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">Used</p>
- <p className="text-xs">0</p>
- </div>
- <div className="flex items-center justify-between py-1">
- <p className="text-xs text-scale-1000">Extra volume used this month</p>
- <p className="text-xs">0</p>
- </div>
+ <div className="flex items-center justify-between border-b py-1">
+ <p className="text-xs text-scale-1000">Used</p>
+ <p className="text-xs">{(usageMeta?.usage ?? 0).toLocaleString()}</p>
+ </div>
+ <div className="flex items-center justify-between py-1">
+ <p className="text-xs text-scale-1000">Extra volume used this month</p>
+ <p className="text-xs">
+ {((usageMeta?.limit ?? 0) === -1 || usageExcess < 0
+ ? 0
+ : usageExcess
+ ).toLocaleString()}
+ </p>
</div>
</div>
- <div className="space-y-1">
- <p>Sample usage over time</p>
- <p className="text-sm text-scale-1000">
- Some description here to explain what this metric is about
- </p>
- </div>
- <BarChart
- hasQuota
- attribute="sample_data"
- data={generateUsageData('sample_data', 30)}
- yLimit={70}
- />
</div>
- </div>
- </div>
- </div>
+ {usageMeta.available_in_plan && (
+ <>
+ <div className="space-y-1">
+ <p>{attribute.name} over time</p>
+ <p className="text-sm text-scale-1000">{attribute.chartDescription}</p>
+ </div>
+ {isLoadingData ? (
+ <div className="space-y-2">
+ <ShimmeringLoader />
+ <ShimmeringLoader className="w-3/4" />
+ <ShimmeringLoader className="w-1/2" />
+ </div>
+ ) : (
+ <BarChart
+ hasQuota
+ attribute={
+ attribute.key === 'monthly_active_users'
+ ? MAU_KEY
+ : attribute.key === 'storage_image_render_count'
+ ? ASSET_TRANSFORMATIONS_KEY
+ : FUNC_INVOCATIONS_KEY
+ }
+ data={usageData?.data ?? []}
+ yLimit={usageMeta?.limit ?? 0}
+ yLeftMargin={
+ attribute.key === 'monthly_active_users'
+ ? 18
+ : attribute.key === 'storage_image_render_count'
+ ? undefined
+ : 26
+ }
+ yFormatter={(value) => value.toLocaleString()}
+ />
+ )}
+ </>
+ )}
+ </SectionContent>
+ )
+ })}
</>
)
}
diff --git a/studio/components/interfaces/BillingV2/Usage/Bandwidth.tsx b/studio/components/interfaces/BillingV2/Usage/Bandwidth.tsx
index edbfe152ad8..f7a660c8d70 100644
--- a/studio/components/interfaces/BillingV2/Usage/Bandwidth.tsx
+++ b/studio/components/interfaces/BillingV2/Usage/Bandwidth.tsx
@@ -1,14 +1,20 @@
-import { useParams } from 'common'
+import clsx from 'clsx'
import ShimmeringLoader from 'components/ui/ShimmeringLoader'
import SparkBar from 'components/ui/SparkBar'
import { useDailyStatsQuery } from 'data/analytics/daily-stats-query'
import { useProjectSubscriptionQuery } from 'data/subscriptions/project-subscription-query'
-import { useProjectUsageQuery } from 'data/usage/project-usage-query'
+import {
+ ProjectUsageResponse,
+ UsageMetric,
+ useProjectUsageQuery,
+} from 'data/usage/project-usage-query'
+import { USAGE_APPROACHING_THRESHOLD } from 'lib/constants'
import { formatBytes } from 'lib/helpers'
-import { Button } from 'ui'
+import { Button, IconAlertTriangle } from 'ui'
import BarChart from './BarChart'
import SectionContent from './SectionContent'
import SectionHeader from './SectionHeader'
+import { USAGE_CATEGORIES } from './Usage.constants'
export interface BandwidthProps {
projectRef: string
@@ -20,11 +26,10 @@ const Bandwidth = ({ projectRef }: BandwidthProps) => {
const { current_period_start, current_period_end } = subscription?.billing ?? {}
const startDate = new Date((current_period_start ?? 0) * 1000).toISOString()
const endDate = new Date((current_period_end ?? 0) * 1000).toISOString()
+ const categoryMeta = USAGE_CATEGORIES.find((category) => category.key === 'bandwidth')
// [JOSHEN TODO] Double check if this is the right attribute
const DB_EGRESS_KEY = 'total_db_egress_bytes'
- const { db_egress } = usage ?? {}
- const dbEgressExcess = (db_egress?.usage ?? 0) - (db_egress?.limit ?? 0)
const { data: dbEgressData, isLoading: isLoadingDbEgressData } = useDailyStatsQuery({
projectRef,
attribute: DB_EGRESS_KEY,
@@ -34,8 +39,6 @@ const Bandwidth = ({ projectRef }: BandwidthProps) => {
})
const STORAGE_EGRESS_KEY = 'total_storage_egress'
- const { storage_egress } = usage ?? {}
- const storageEgressExcess = (storage_egress?.usage ?? 0) - (storage_egress?.limit ?? 0)
const { data: storageEgressData, isLoading: isLoadingStorageEgressData } = useDailyStatsQuery({
projectRef,
attribute: STORAGE_EGRESS_KEY,
@@ -44,123 +47,104 @@ const Bandwidth = ({ projectRef }: BandwidthProps) => {
endDate,
})
+ if (categoryMeta === undefined) return null
+
return (
<>
<SectionHeader title="Bandwidth" description="Some description here" />
- <SectionContent title="Database Egress" description="Some description here">
- <div className="space-y-2">
- <div className="flex items-center justify-between">
- <p className="text-sm">Egress {subscription?.tier.key.toLowerCase()} quota usage</p>
- <Button type="default" size="tiny" onClick={() => {}}>
- Upgrade project
- </Button>
- </div>
- <SparkBar
- type="horizontal"
- barClass="bg-scale-1200"
- value={db_egress?.usage ?? 0}
- max={db_egress?.limit ?? 0}
- />
- <div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">
- Included in {subscription?.tier.name.toLowerCase()}
- </p>
- <p className="text-xs">{formatBytes(db_egress?.limit ?? 0)}</p>
- </div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">Used</p>
- <p className="text-xs">{formatBytes(db_egress?.usage ?? 0)}</p>
- </div>
- <div className="flex items-center justify-between py-1">
- <p className="text-xs text-scale-1000">Extra volume used this month</p>
- <p className="text-xs">
- {dbEgressExcess < 0 ? formatBytes(0) : formatBytes(dbEgressExcess)}
- </p>
- </div>
- </div>
- </div>
- <div className="space-y-1">
- <p>Database egress over time</p>
- <p className="text-sm text-scale-1000">Some description here</p>
- </div>
- {isLoadingDbEgressData ? (
- <div className="space-y-2">
- <ShimmeringLoader />
- <ShimmeringLoader className="w-3/4" />
- <ShimmeringLoader className="w-1/2" />
- </div>
- ) : (
- <BarChart
- hasQuota
- attribute={DB_EGRESS_KEY}
- data={dbEgressData?.data ?? []}
- unit={undefined}
- yLimit={db_egress?.limit ?? 0}
- yLeftMargin={14}
- yFormatter={(value) => formatBytes(value, 1, 'GB').replace(/\s/g, '')}
- />
- )}
- </SectionContent>
+ {categoryMeta.attributes.map((attribute) => {
+ const usageMeta = usage?.[attribute.key as keyof ProjectUsageResponse] as UsageMetric
+ const usageRatio =
+ typeof usageMeta !== 'number' ? (usageMeta?.usage ?? 0) / (usageMeta?.limit ?? 0) : 0
+ const usageExcess = (usageMeta?.usage ?? 0) - (usageMeta?.limit ?? 0)
- <SectionContent title="Storage Egress" description="Some description here">
- <div className="space-y-2">
- <div className="flex items-center justify-between">
- <p className="text-sm">
- Storage egress {subscription?.tier.key.toLowerCase()} quota usage
- </p>
- <Button type="default" size="tiny" onClick={() => {}}>
- Upgrade project
- </Button>
- </div>
- <SparkBar
- type="horizontal"
- barClass="bg-scale-1200"
- value={storage_egress?.usage ?? 0}
- max={storage_egress?.limit ?? 0}
- />
- <div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">
- Included in {subscription?.tier.name.toLowerCase()}
- </p>
- <p className="text-xs">{formatBytes(storage_egress?.limit ?? 0)}</p>
- </div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">Used</p>
- <p className="text-xs">{formatBytes(storage_egress?.usage ?? 0)}</p>
+ const isLoadingData =
+ attribute.key === 'db_egress' ? isLoadingDbEgressData : isLoadingStorageEgressData
+ const usageData = attribute.key === 'db_egress' ? dbEgressData : storageEgressData
+
+ return (
+ <SectionContent
+ key={attribute.key}
+ title={attribute.name}
+ description={attribute.description}
+ >
+ <div className="space-y-2">
+ <div className="flex items-center justify-between">
+ <div className="flex items-center space-x-4">
+ <p className="text-sm">
+ {attribute.name} {subscription?.tier.key.toLowerCase()} quota usage
+ </p>
+ {usageRatio >= 1 ? (
+ <div className="flex items-center space-x-2 min-w-[115px]">
+ <IconAlertTriangle size={14} strokeWidth={2} className="text-red-900" />
+ <p className="text-sm text-red-900">Exceeded limit</p>
+ </div>
+ ) : usageRatio >= USAGE_APPROACHING_THRESHOLD ? (
+ <div className="flex items-center space-x-2 min-w-[115px]">
+ <IconAlertTriangle size={14} strokeWidth={2} className="text-amber-900" />
+ <p className="text-sm text-red-900">Reaching limit</p>
+ </div>
+ ) : null}
+ </div>
+ <Button type="default" size="tiny" onClick={() => {}}>
+ Upgrade project
+ </Button>
+ </div>
+ <SparkBar
+ type="horizontal"
+ barClass={clsx(
+ usageRatio >= 1
+ ? 'bg-red-900'
+ : usageRatio >= USAGE_APPROACHING_THRESHOLD
+ ? 'bg-amber-900'
+ : 'bg-scale-1100'
+ )}
+ value={usageMeta?.usage ?? 0}
+ max={usageMeta?.limit ?? 0}
+ />
+ <div>
+ <div className="flex items-center justify-between border-b py-1">
+ <p className="text-xs text-scale-1000">
+ Included in {subscription?.tier.name.toLowerCase()}
+ </p>
+ <p className="text-xs">{formatBytes(usageMeta?.limit ?? 0)}</p>
+ </div>
+ <div className="flex items-center justify-between border-b py-1">
+ <p className="text-xs text-scale-1000">Used</p>
+ <p className="text-xs">{formatBytes(usageMeta?.usage ?? 0)}</p>
+ </div>
+ <div className="flex items-center justify-between py-1">
+ <p className="text-xs text-scale-1000">Extra volume used this month</p>
+ <p className="text-xs">
+ {usageExcess < 0 ? formatBytes(0) : formatBytes(usageExcess)}
+ </p>
+ </div>
+ </div>
</div>
- <div className="flex items-center justify-between py-1">
- <p className="text-xs text-scale-1000">Extra volume used this month</p>
- <p className="text-xs">
- {storageEgressExcess < 0 ? formatBytes(0) : formatBytes(storageEgressExcess)}
- </p>
+ <div className="space-y-1">
+ <p>{attribute.name} over time</p>
+ <p className="text-sm text-scale-1000">{attribute.chartDescription}</p>
</div>
- </div>
- </div>
- <div className="space-y-1">
- <p>Storage egress over time</p>
- <p className="text-sm text-scale-1000">Some description here</p>
- </div>
- {isLoadingStorageEgressData ? (
- <div className="space-y-2">
- <ShimmeringLoader />
- <ShimmeringLoader className="w-3/4" />
- <ShimmeringLoader className="w-1/2" />
- </div>
- ) : (
- <BarChart
- hasQuota
- attribute={STORAGE_EGRESS_KEY}
- data={storageEgressData?.data ?? []}
- unit={undefined}
- yLimit={storage_egress?.limit ?? 0}
- yLeftMargin={14}
- yFormatter={(value) => formatBytes(value, 1, 'GB').replace(/\s/g, '')}
- />
- )}
- </SectionContent>
+ {isLoadingData ? (
+ <div className="space-y-2">
+ <ShimmeringLoader />
+ <ShimmeringLoader className="w-3/4" />
+ <ShimmeringLoader className="w-1/2" />
+ </div>
+ ) : (
+ <BarChart
+ hasQuota
+ attribute={attribute.key === 'db_egress' ? DB_EGRESS_KEY : STORAGE_EGRESS_KEY}
+ data={usageData?.data ?? []}
+ yLimit={usageMeta?.limit ?? 0}
+ yLeftMargin={14}
+ yFormatter={(value) => formatBytes(value, 1, 'GB').replace(/\s/g, '')}
+ />
+ )}
+ </SectionContent>
+ )
+ })}
</>
)
}
diff --git a/studio/components/interfaces/BillingV2/Usage/SectionContent.tsx b/studio/components/interfaces/BillingV2/Usage/SectionContent.tsx
index 1f3dc1d0deb..a0b6a45dba1 100644
--- a/studio/components/interfaces/BillingV2/Usage/SectionContent.tsx
+++ b/studio/components/interfaces/BillingV2/Usage/SectionContent.tsx
@@ -12,7 +12,7 @@ const SectionContent = ({ title, description, children }: PropsWithChildren<Sect
<div className="grid grid-cols-12">
<div className="col-span-5">
<div className="sticky top-16">
- <p className="text-base">{title}</p>
+ <p className="text-base capitalize">{title}</p>
<p className="text-sm text-scale-1000">{description}</p>
</div>
</div>
diff --git a/studio/components/interfaces/BillingV2/Usage/SizeAndCounts.tsx b/studio/components/interfaces/BillingV2/Usage/SizeAndCounts.tsx
index 1dd99bce697..495551464f6 100644
--- a/studio/components/interfaces/BillingV2/Usage/SizeAndCounts.tsx
+++ b/studio/components/interfaces/BillingV2/Usage/SizeAndCounts.tsx
@@ -1,14 +1,20 @@
-import { useParams } from 'common'
+import clsx from 'clsx'
import ShimmeringLoader from 'components/ui/ShimmeringLoader'
import SparkBar from 'components/ui/SparkBar'
import { useDailyStatsQuery } from 'data/analytics/daily-stats-query'
import { useProjectSubscriptionQuery } from 'data/subscriptions/project-subscription-query'
-import { useProjectUsageQuery } from 'data/usage/project-usage-query'
+import {
+ ProjectUsageResponse,
+ UsageMetric,
+ useProjectUsageQuery,
+} from 'data/usage/project-usage-query'
import { formatBytes } from 'lib/helpers'
-import { Button } from 'ui'
+import { Button, IconAlertTriangle } from 'ui'
+import { USAGE_APPROACHING_THRESHOLD } from '../Billing.constants'
import BarChart from './BarChart'
import SectionContent from './SectionContent'
import SectionHeader from './SectionHeader'
+import { USAGE_CATEGORIES } from './Usage.constants'
export interface SizeAndCountsProps {
projectRef: string
@@ -23,10 +29,9 @@ const SizeAndCounts = ({ projectRef }: SizeAndCountsProps) => {
const { current_period_start, current_period_end } = subscription?.billing ?? {}
const startDate = new Date((current_period_start ?? 0) * 1000).toISOString()
const endDate = new Date((current_period_end ?? 0) * 1000).toISOString()
+ const categoryMeta = USAGE_CATEGORIES.find((category) => category.key === 'sizeCount')
const TOTAL_DB_SIZE_KEY = 'total_db_size_bytes'
- const { db_size } = usage ?? {}
- const dbSizeExcess = (db_size?.usage ?? 0) - (db_size?.limit ?? 0)
const { data: dbSizeData, isLoading: isLoadingDbSizeData } = useDailyStatsQuery({
projectRef,
attribute: TOTAL_DB_SIZE_KEY,
@@ -36,8 +41,6 @@ const SizeAndCounts = ({ projectRef }: SizeAndCountsProps) => {
})
const TOTAL_STORAGE_SIZE_KEY = 'total_storage_size_bytes'
- const { storage_size } = usage ?? {}
- const storageSizeExcess = (storage_size?.usage ?? 0) - (storage_size?.limit ?? 0)
const { data: storageSizeData, isLoading: isLoadingStorageSizeData } = useDailyStatsQuery({
projectRef,
attribute: TOTAL_STORAGE_SIZE_KEY,
@@ -46,121 +49,106 @@ const SizeAndCounts = ({ projectRef }: SizeAndCountsProps) => {
endDate,
})
+ if (categoryMeta === undefined) return null
+
return (
<>
<SectionHeader title="Size & Counts" description="Some description here" />
- <SectionContent title="Database Size" description="Some description here">
- <div className="space-y-2">
- <div className="flex items-center justify-between">
- <p className="text-sm">Database size per day</p>
- <Button type="default" size="tiny" onClick={() => {}}>
- Upgrade project
- </Button>
- </div>
- <SparkBar
- type="horizontal"
- barClass="bg-scale-1200"
- value={db_size?.usage ?? 0}
- max={db_size?.limit ?? 0}
- />
- <div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">
- Included in {subscription?.tier.name.toLowerCase()}
- </p>
- <p className="text-xs">{formatBytes(db_size?.limit ?? 0)}</p>
- </div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">Used</p>
- <p className="text-xs">{formatBytes(db_size?.usage ?? 0)}</p>
- </div>
- <div className="flex items-center justify-between py-1">
- <p className="text-xs text-scale-1000">Extra volume used this month</p>
- <p className="text-xs">
- {dbSizeExcess < 0 ? formatBytes(0) : formatBytes(dbSizeExcess)}
- </p>
- </div>
- </div>
- </div>
- <div className="space-y-1">
- <p>Database egress over time</p>
- <p className="text-sm text-scale-1000">Some description here</p>
- </div>
- {isLoadingDbSizeData ? (
- <div className="space-y-2">
- <ShimmeringLoader />
- <ShimmeringLoader className="w-3/4" />
- <ShimmeringLoader className="w-1/2" />
- </div>
- ) : (
- <BarChart
- hasQuota
- attribute={TOTAL_DB_SIZE_KEY}
- data={dbSizeData?.data ?? []}
- unit={undefined}
- yLimit={db_size?.limit ?? 0}
- yLeftMargin={14}
- yFormatter={(value) => formatBytes(value, 1, 'GB').replace(/\s/g, '')}
- />
- )}
- </SectionContent>
+ {categoryMeta.attributes.map((attribute) => {
+ const usageMeta = usage?.[attribute.key as keyof ProjectUsageResponse] as UsageMetric
+ const usageRatio =
+ typeof usageMeta !== 'number' ? (usageMeta?.usage ?? 0) / (usageMeta?.limit ?? 0) : 0
+ const usageExcess = (usageMeta?.usage ?? 0) - (usageMeta?.limit ?? 0)
- <SectionContent title="Storage Size" description="Some description here">
- <div className="space-y-2">
- <div className="flex items-center justify-between">
- <p className="text-sm">Storage {subscription?.tier.key.toLowerCase()} quota usage</p>
- <Button type="default" size="tiny" onClick={() => {}}>
- Upgrade project
- </Button>
- </div>
- <SparkBar
- type="horizontal"
- barClass="bg-scale-1200"
- value={storage_size?.usage ?? 0}
- max={storage_size?.limit ?? 0}
- />
- <div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">
- Included in {subscription?.tier.name.toLowerCase()}
- </p>
- <p className="text-xs">{formatBytes(storage_size?.limit ?? 0)}</p>
- </div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">Used</p>
- <p className="text-xs">{formatBytes(storage_size?.usage ?? 0)}</p>
+ const isLoadingData =
+ attribute.key === 'db_size' ? isLoadingDbSizeData : isLoadingStorageSizeData
+ const usageData = attribute.key === 'db_size' ? dbSizeData : storageSizeData
+
+ return (
+ <SectionContent
+ key={attribute.key}
+ title={attribute.name}
+ description={attribute.description}
+ >
+ <div className="space-y-2">
+ <div className="flex items-center justify-between">
+ <div className="flex items-center space-x-4">
+ <p className="text-sm">
+ {attribute.name} {subscription?.tier.key.toLowerCase()} quota usage
+ </p>
+ {usageRatio >= 1 ? (
+ <div className="flex items-center space-x-2 min-w-[115px]">
+ <IconAlertTriangle size={14} strokeWidth={2} className="text-red-900" />
+ <p className="text-sm text-red-900">Exceeded limit</p>
+ </div>
+ ) : usageRatio >= USAGE_APPROACHING_THRESHOLD ? (
+ <div className="flex items-center space-x-2 min-w-[115px]">
+ <IconAlertTriangle size={14} strokeWidth={2} className="text-amber-900" />
+ <p className="text-sm text-red-900">Reaching limit</p>
+ </div>
+ ) : null}
+ </div>
+ <Button type="default" size="tiny" onClick={() => {}}>
+ Upgrade project
+ </Button>
+ </div>
+ <SparkBar
+ type="horizontal"
+ barClass={clsx(
+ usageRatio >= 1
+ ? 'bg-red-900'
+ : usageRatio >= USAGE_APPROACHING_THRESHOLD
+ ? 'bg-amber-900'
+ : 'bg-scale-1100'
+ )}
+ value={usageMeta?.usage ?? 0}
+ max={usageMeta?.limit ?? 0}
+ />
+ <div>
+ <div className="flex items-center justify-between border-b py-1">
+ <p className="text-xs text-scale-1000">
+ Included in {subscription?.tier.name.toLowerCase()}
+ </p>
+ <p className="text-xs">{formatBytes(usageMeta?.limit ?? 0)}</p>
+ </div>
+ <div className="flex items-center justify-between border-b py-1">
+ <p className="text-xs text-scale-1000">Used</p>
+ <p className="text-xs">{formatBytes(usageMeta?.usage ?? 0)}</p>
+ </div>
+ <div className="flex items-center justify-between py-1">
+ <p className="text-xs text-scale-1000">Extra volume used this month</p>
+ <p className="text-xs">
+ {usageExcess < 0 ? formatBytes(0) : formatBytes(usageExcess)}
+ </p>
+ </div>
+ </div>
</div>
- <div className="flex items-center justify-between py-1">
- <p className="text-xs text-scale-1000">Extra volume used this month</p>
- <p className="text-xs">
- {storageSizeExcess < 0 ? formatBytes(0) : formatBytes(storageSizeExcess)}
- </p>
+ <div className="space-y-1">
+ <p>{attribute.name} over time</p>
+ <p className="text-sm text-scale-1000">{attribute.chartDescription}</p>
</div>
- </div>
- </div>
- <div className="space-y-1">
- <p>Storage size over time</p>
- <p className="text-sm text-scale-1000">Some description here</p>
- </div>
- {isLoadingStorageSizeData ? (
- <div className="space-y-2">
- <ShimmeringLoader />
- <ShimmeringLoader className="w-3/4" />
- <ShimmeringLoader className="w-1/2" />
- </div>
- ) : (
- <BarChart
- hasQuota
- attribute={TOTAL_STORAGE_SIZE_KEY}
- data={storageSizeData?.data ?? []}
- unit={undefined}
- yLimit={storage_size?.limit ?? 0}
- yLeftMargin={14}
- yFormatter={(value) => formatBytes(value, 1, 'GB').replace(/\s/g, '')}
- />
- )}
- </SectionContent>
+ {isLoadingData ? (
+ <div className="space-y-2">
+ <ShimmeringLoader />
+ <ShimmeringLoader className="w-3/4" />
+ <ShimmeringLoader className="w-1/2" />
+ </div>
+ ) : (
+ <BarChart
+ hasQuota
+ attribute={
+ attribute.key === 'db_egress' ? TOTAL_DB_SIZE_KEY : TOTAL_STORAGE_SIZE_KEY
+ }
+ data={usageData?.data ?? []}
+ yLimit={usageMeta?.limit ?? 0}
+ yLeftMargin={14}
+ yFormatter={(value) => formatBytes(value, 1, 'GB').replace(/\s/g, '')}
+ />
+ )}
+ </SectionContent>
+ )
+ })}
</>
)
}
diff --git a/studio/components/interfaces/BillingV2/Usage/Usage.constants.ts b/studio/components/interfaces/BillingV2/Usage/Usage.constants.ts
index 154148b8314..68ad0b6806e 100644
--- a/studio/components/interfaces/BillingV2/Usage/Usage.constants.ts
+++ b/studio/components/interfaces/BillingV2/Usage/Usage.constants.ts
@@ -6,21 +6,99 @@ export const USAGE_STATUS = {
EXCEEDED: 'EXCEEDED',
}
+export interface CategoryAttribute {
+ key: string
+ name: string
+ description: string
+ chartDescription: string
+}
export const USAGE_CATEGORIES: {
key: 'infra' | 'bandwidth' | 'sizeCount' | 'activity'
name: string
- attributes: string[]
+ attributes: CategoryAttribute[]
}[] = [
{
key: 'infra',
name: 'Infrastructure',
- attributes: ['cpu_usage', 'ram_usage', 'disk_io_budget'],
+ attributes: [
+ {
+ key: 'cpu_usage',
+ name: 'CPU usage',
+ description: 'Some description here',
+ chartDescription: 'Some description here',
+ },
+ {
+ key: 'ram_usage',
+ name: 'Memory usage',
+ description: 'Some description here',
+ chartDescription: 'Some description here',
+ },
+ {
+ key: 'disk_io_budget',
+ name: 'IO budget',
+ description: 'Some description here',
+ chartDescription: 'Some description here',
+ },
+ ],
+ },
+ {
+ key: 'bandwidth',
+ name: 'Bandwidth',
+ attributes: [
+ {
+ key: 'db_egress',
+ name: 'Database egress',
+ description: 'Some description here',
+ chartDescription: 'Some description here',
+ },
+ {
+ key: 'storage_egress',
+ name: 'Storage egress',
+ description: 'Some description here',
+ chartDescription: 'Some description here',
+ },
+ ],
+ },
+ {
+ key: 'sizeCount',
+ name: 'Size & Counts',
+ attributes: [
+ {
+ key: 'db_size',
+ name: 'Database size',
+ description: 'Some description here',
+ chartDescription: 'Some description here',
+ },
+ {
+ key: 'storage_size',
+ name: 'Storage size',
+ description: 'Some description here',
+ chartDescription: 'Some description here',
+ },
+ ],
},
- { key: 'bandwidth', name: 'Bandwidth', attributes: ['db_egress', 'storage_egress'] },
- { key: 'sizeCount', name: 'Size & Counts', attributes: ['db_size', 'storage_size'] },
{
key: 'activity',
name: 'Activity',
- attributes: ['monthly_active_users', 'storage_image_render_count', 'func_invocations'],
+ attributes: [
+ {
+ key: 'monthly_active_users',
+ name: 'Monthly active users (MAU)',
+ description: 'Some description here',
+ chartDescription: 'Some description here',
+ },
+ {
+ key: 'storage_image_render_count',
+ name: 'Storage image transformations',
+ description: 'Some description here',
+ chartDescription: 'Some description here',
+ },
+ {
+ key: 'func_invocations',
+ name: 'Edge function invocations',
+ description: 'Some description here',
+ chartDescription: 'Some description here',
+ },
+ ],
},
]
diff --git a/studio/components/interfaces/BillingV2/Usage/Usage.tsx b/studio/components/interfaces/BillingV2/Usage/Usage.tsx
index d98d8b20996..8b4ec1fe140 100644
--- a/studio/components/interfaces/BillingV2/Usage/Usage.tsx
+++ b/studio/components/interfaces/BillingV2/Usage/Usage.tsx
@@ -120,6 +120,7 @@ const Usage = () => {
<div className="flex items-center space-x-6 !mt-2">
{USAGE_CATEGORIES.map((category) => {
const status = getUsageStatus(category.attributes, usage)
+
return (
<div
key={category.key}
@@ -141,6 +142,12 @@ const Usage = () => {
</div>
</div>
+ {/*
+ [Joshen] Could potentially run a map here based on USAGE_CATEGORIES, rather than defining each section
+ but thinking it's gonna "cover up" too much details and make it harder to add attribute specific components
+ e.g for database size, we also need to show disk volume size. Not to mention that are little nuances across
+ each attribute RE formatting (bytes vs locale string)
+ */}
<div ref={infrastructureRef}>
<Infrastructure projectRef={selectedProjectRef} />
</div>
diff --git a/studio/components/interfaces/BillingV2/Usage/Usage.utils.ts b/studio/components/interfaces/BillingV2/Usage/Usage.utils.ts
index 1fe841d96d8..410d3978d89 100644
--- a/studio/components/interfaces/BillingV2/Usage/Usage.utils.ts
+++ b/studio/components/interfaces/BillingV2/Usage/Usage.utils.ts
@@ -1,9 +1,9 @@
import { DataPoint } from 'data/analytics/constants'
import { ProjectUsageResponse } from 'data/usage/project-usage-query'
import { USAGE_APPROACHING_THRESHOLD } from '../Billing.constants'
-import { USAGE_STATUS } from './Usage.constants'
+import { CategoryAttribute, USAGE_STATUS } from './Usage.constants'
-// [Joshen] This is just for development
+// [Joshen] This is just for development to generate some test data for chart rendering
export const generateUsageData = (attribute: string, days: number): DataPoint[] => {
const tempArray = new Array(days).fill(0)
return tempArray.map((x, idx) => {
@@ -15,11 +15,11 @@ export const generateUsageData = (attribute: string, days: number): DataPoint[]
})
}
-export const getUsageStatus = (attributes: string[], usage?: ProjectUsageResponse) => {
+export const getUsageStatus = (attributes: CategoryAttribute[], usage?: ProjectUsageResponse) => {
if (!usage) return USAGE_STATUS.NORMAL
const attributeStatuses = attributes.map((attribute) => {
- const usageMeta = usage?.[attribute as keyof ProjectUsageResponse]
+ const usageMeta = usage?.[attribute.key as keyof ProjectUsageResponse]
const usageRatio =
typeof usageMeta !== 'number' ? (usageMeta?.usage ?? 0) / (usageMeta?.limit ?? 0) : 0
if (usageRatio >= 1) return USAGE_STATUS.EXCEEDED