Skip to content

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

chore: reduce duplication

Kevin Grüneberg210546be0528
diff --git a/studio/components/interfaces/BillingV2/Usage/Activity.tsx b/studio/components/interfaces/BillingV2/Usage/Activity.tsx
index 12f7e3777ee..2038079fbc7 100644
--- a/studio/components/interfaces/BillingV2/Usage/Activity.tsx
+++ b/studio/components/interfaces/BillingV2/Usage/Activity.tsx
@@ -1,66 +1,19 @@
-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 {
- ProjectUsageResponse,
- UsageMetric,
- useProjectUsageQuery,
-} from 'data/usage/project-usage-query'
-import dayjs from 'dayjs'
-import Link from 'next/link'
-import { Button, IconAlertTriangle } from 'ui'
-import { USAGE_APPROACHING_THRESHOLD } from '../Billing.constants'
-import UsageBarChart from './UsageBarChart'
-import SectionContent from './SectionContent'
-import SectionHeader from './SectionHeader'
import { USAGE_CATEGORIES } from './Usage.constants'
-import { ChartYFormatterCompactNumber, getUpgradeUrlFromV2Subscription } from './Usage.utils'
import { DataPoint } from 'data/analytics/constants'
-import Panel from 'components/ui/Panel'
-import { useProjectSubscriptionV2Query } from 'data/subscriptions/project-subscription-v2-query'
-import { useMemo } from 'react'
+import { ProjectSubscriptionResponse } from 'data/subscriptions/project-subscription-v2-query'
+import UsageFoo from './UsageFoo'
export interface ActivityProps {
projectRef: string
+ startDate: string | undefined
+ endDate: string | undefined
+ subscription: ProjectSubscriptionResponse | undefined
}
-const Activity = ({ projectRef }: ActivityProps) => {
- const { data: usage } = useProjectUsageQuery({ projectRef })
- const { data: subscription } = useProjectSubscriptionV2Query({ projectRef })
- const { current_period_start, current_period_end } = subscription ?? {}
-
- const startDate = useMemo(() => {
- return current_period_start ? new Date(current_period_start * 1000).toISOString() : undefined
- }, [current_period_start])
-
- const endDate = useMemo(() => {
- const periodEndDate = current_period_end ? new Date(current_period_end * 1000) : undefined
- // If end date is in future, set end date to now
- if (periodEndDate && dayjs(periodEndDate).isAfter(dayjs())) {
- const yesterday = dayjs(new Date()).subtract(1, 'day')
-
- /**
- * Currently, daily-stats data is only available a day later, so we'll use yesterday as end date, as otherwise the current day would just show up with "0" values
- *
- * We are actively working on removing this restriction on the data-eng/LF side and can remove this workaround once that's done
- */
- const newEndDate = yesterday.isAfter(dayjs(startDate)) ? yesterday : new Date()
-
- // LF seems to have an issue with the milliseconds, causes infinite loading sometimes
- return newEndDate.toISOString().slice(0, -5) + 'Z'
- } else if (periodEndDate) {
- return periodEndDate.toISOString()
- }
- }, [current_period_end, startDate])
-
+const Activity = ({ projectRef, subscription, startDate, endDate }: ActivityProps) => {
const categoryMeta = USAGE_CATEGORIES.find((category) => category.key === 'activity')
- const upgradeUrl = getUpgradeUrlFromV2Subscription(projectRef, subscription)
-
- const usageBasedBilling = subscription?.usage_billing_enabled
- const exceededLimitStyle = !usageBasedBilling ? 'text-red-900' : 'text-amber-900'
-
const { data: mauData, isLoading: isLoadingMauData } = useDailyStatsQuery({
projectRef,
attribute: 'total_auth_billing_period_mau',
@@ -158,171 +111,12 @@ const Activity = ({ projectRef }: ActivityProps) => {
if (categoryMeta === undefined) return null
return (
- <>
- <SectionHeader title={categoryMeta.name} description={categoryMeta.description} />
-
- {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)
-
- const chartData = chartMeta[attribute.key]?.data ?? []
-
- const notAllValuesZero = chartData.some(
- (dataPoint) => Number(dataPoint[attribute.attribute]) !== 0
- )
-
- return (
- <div id={attribute.anchor} key={attribute.key}>
- <SectionContent section={attribute} includedInPlan={usageMeta?.available_in_plan}>
- {usageMeta?.available_in_plan ? (
- <>
- <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} usage</p>
- {!usageBasedBilling && usageRatio >= 1 ? (
- <div className="flex items-center space-x-2 min-w-[115px]">
- <IconAlertTriangle
- size={14}
- strokeWidth={2}
- className={exceededLimitStyle}
- />
- <p className={`text-sm ${exceededLimitStyle}`}>Exceeded limit</p>
- </div>
- ) : !usageBasedBilling && 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-amber-900">Approaching limit</p>
- </div>
- ) : null}
- </div>
- {!usageBasedBilling && usageRatio >= USAGE_APPROACHING_THRESHOLD && (
- <Link href={upgradeUrl}>
- <a>
- <Button type="default" size="tiny">
- Upgrade project
- </Button>
- </a>
- </Link>
- )}
- </div>
- {usageMeta.limit > 0 && (
- <SparkBar
- type="horizontal"
- barClass={clsx(
- usageRatio >= 1
- ? usageBasedBilling
- ? 'bg-amber-900'
- : 'bg-red-900'
- : usageRatio >= USAGE_APPROACHING_THRESHOLD
- ? 'bg-amber-900'
- : 'bg-scale-1100'
- )}
- bgClass="bg-gray-300 dark:bg-gray-600"
- value={usageMeta?.usage ?? 0}
- max={usageMeta?.limit || 1}
- />
- )}
- <div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">
- Included in {subscription?.plan?.name.toLowerCase()} plan
- </p>
- {usageMeta?.limit === -1 ? (
- <p className="text-xs">None</p>
- ) : usageMeta?.limit === 0 ? (
- <p className="text-xs">Unlimited</p>
- ) : (
- <p className="text-xs">{(usageMeta?.limit ?? 0).toLocaleString()}</p>
- )}
- </div>
- <div className="flex items-center justify-between py-1">
- <p className="text-xs text-scale-1000">
- {attribute.chartPrefix || 'Used '}in period
- </p>
- <p className="text-xs">{(usageMeta?.usage ?? 0).toLocaleString()}</p>
- </div>
- {usageMeta.limit > 0 && (
- <div className="flex items-center justify-between border-t py-1">
- <p className="text-xs text-scale-1000">Overage in period</p>
- <p className="text-xs">
- {((usageMeta?.limit ?? 0) === -1 || usageExcess < 0
- ? 0
- : usageExcess
- ).toLocaleString()}
- </p>
- </div>
- )}
- </div>
- </div>
- <div className="space-y-1">
- <p>{attribute.name} per day</p>
- {attribute.chartDescription.split('\n').map((paragraph, idx) => (
- <p key={`para-${idx}`} className="text-sm text-scale-1000">
- {paragraph}
- </p>
- ))}
- </div>
- {chartMeta[attribute.key].isLoading ? (
- <div className="space-y-2">
- <ShimmeringLoader />
- <ShimmeringLoader className="w-3/4" />
- <ShimmeringLoader className="w-1/2" />
- </div>
- ) : chartData.length > 1 && notAllValuesZero ? (
- <UsageBarChart
- name={`${attribute.chartPrefix || ''}${attribute.name}`}
- unit={attribute.unit}
- attribute={attribute.attribute}
- data={chartData}
- yLeftMargin={chartMeta[attribute.key].margin}
- yFormatter={(value) => ChartYFormatterCompactNumber(value, attribute.unit)}
- />
- ) : (
- <Panel>
- <Panel.Content>
- <div className="flex flex-col items-center justify-center space-y-2">
- <p>No data</p>
- <p className="text-sm text-scale-1000">
- No {notAllValuesZero ? 'data' : 'usage'} in period
- </p>
- </div>
- </Panel.Content>
- </Panel>
- )}
- </>
- ) : (
- <Panel>
- <Panel.Content>
- <div className="flex w-full items-center flex-col justify-center space-y-2 md:flex-row md:justify-between">
- <div className="space-y-1">
- <p className="text-sm">Not included in plan</p>
- <div>
- <p className="text-sm text-scale-1100">
- You need to be on a higher plan in order to use this feature.
- </p>
- </div>
- </div>
- <Link href={`/project/${projectRef}/settings/billing/subscription`}>
- <a>
- <Button type="primary">Upgrade plan</Button>
- </a>
- </Link>
- </div>
- </Panel.Content>
- </Panel>
- )}
- </SectionContent>
- </div>
- )
- })}
- </>
+ <UsageFoo
+ projectRef={projectRef}
+ categoryMeta={categoryMeta}
+ chartMeta={chartMeta}
+ subscription={subscription}
+ />
)
}
diff --git a/studio/components/interfaces/BillingV2/Usage/Bandwidth.tsx b/studio/components/interfaces/BillingV2/Usage/Bandwidth.tsx
index 96a620d1b0e..9c76f21ed2e 100644
--- a/studio/components/interfaces/BillingV2/Usage/Bandwidth.tsx
+++ b/studio/components/interfaces/BillingV2/Usage/Bandwidth.tsx
@@ -1,66 +1,19 @@
-import clsx from 'clsx'
-import ShimmeringLoader from 'components/ui/ShimmeringLoader'
-import SparkBar from 'components/ui/SparkBar'
import { DataPoint } from 'data/analytics/constants'
import { useDailyStatsQuery } from 'data/analytics/daily-stats-query'
-import {
- ProjectUsageResponse,
- UsageMetric,
- useProjectUsageQuery,
-} from 'data/usage/project-usage-query'
-import dayjs from 'dayjs'
-import { USAGE_APPROACHING_THRESHOLD } from 'lib/constants'
-import { formatBytes } from 'lib/helpers'
-import Link from 'next/link'
-import { Button, IconAlertTriangle } from 'ui'
-import SectionContent from './SectionContent'
-import SectionHeader from './SectionHeader'
import { USAGE_CATEGORIES } from './Usage.constants'
-import { ChartYFormatterCompactNumber, getUpgradeUrlFromV2Subscription } from './Usage.utils'
-import UsageBarChart from './UsageBarChart'
-import { useProjectSubscriptionV2Query } from 'data/subscriptions/project-subscription-v2-query'
-import Panel from 'components/ui/Panel'
-import { useMemo } from 'react'
+import { ProjectSubscriptionResponse } from 'data/subscriptions/project-subscription-v2-query'
+import UsageFoo from './UsageFoo'
export interface BandwidthProps {
projectRef: string
+ startDate: string | undefined
+ endDate: string | undefined
+ subscription: ProjectSubscriptionResponse | undefined
}
-const Bandwidth = ({ projectRef }: BandwidthProps) => {
- const { data: usage } = useProjectUsageQuery({ projectRef })
- const { data: subscription } = useProjectSubscriptionV2Query({ projectRef })
- const { current_period_start, current_period_end } = subscription ?? {}
-
- const startDate = useMemo(() => {
- return current_period_start ? new Date(current_period_start * 1000).toISOString() : undefined
- }, [current_period_start])
-
- const endDate = useMemo(() => {
- const periodEndDate = current_period_end ? new Date(current_period_end * 1000) : undefined
- // If end date is in future, set end date to now
- if (periodEndDate && dayjs(periodEndDate).isAfter(dayjs())) {
- const yesterday = dayjs(new Date()).subtract(1, 'day')
-
- /**
- * Currently, daily-stats data is only available a day later, so we'll use yesterday as end date, as otherwise the current day would just show up with "0" values
- *
- * We are actively working on removing this restriction on the data-eng/LF side and can remove this workaround once that's done
- */
- const newEndDate = yesterday.isAfter(dayjs(startDate)) ? yesterday : new Date()
-
- // LF seems to have an issue with the milliseconds, causes infinite loading sometimes
- return newEndDate.toISOString().slice(0, -5) + 'Z'
- } else if (periodEndDate) {
- return periodEndDate.toISOString()
- }
- }, [current_period_end, startDate])
-
+const Bandwidth = ({ projectRef, subscription, startDate, endDate }: BandwidthProps) => {
const categoryMeta = USAGE_CATEGORIES.find((category) => category.key === 'bandwidth')
- const upgradeUrl = getUpgradeUrlFromV2Subscription(projectRef, subscription)
- const usageBasedBilling = subscription?.usage_billing_enabled
- const exceededLimitStyle = !usageBasedBilling ? 'text-red-900' : 'text-amber-900'
-
const { data: dbEgressData, isLoading: isLoadingDbEgressData } = useDailyStatsQuery({
projectRef,
attribute: 'total_egress_modified',
@@ -97,136 +50,12 @@ const Bandwidth = ({ projectRef }: BandwidthProps) => {
if (categoryMeta === undefined) return null
return (
- <>
- <SectionHeader title={categoryMeta.name} description={categoryMeta.description} />
-
- {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)
-
- const chartData = chartMeta[attribute.key]?.data ?? []
-
- const notAllValuesZero = chartData.some(
- (dataPoint) => Number(dataPoint[attribute.attribute]) !== 0
- )
-
- return (
- <div id={attribute.anchor} key={attribute.key}>
- <SectionContent section={attribute}>
- <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} usage</p>
- {!usageBasedBilling && usageRatio >= 1 ? (
- <div className="flex items-center space-x-2 min-w-[115px]">
- <IconAlertTriangle
- size={14}
- strokeWidth={2}
- className={exceededLimitStyle}
- />
- <p className={`text-sm ${exceededLimitStyle}`}>Exceeded limit</p>
- </div>
- ) : !usageBasedBilling && 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-amber-900">Approaching limit</p>
- </div>
- ) : null}
- </div>
- {!usageBasedBilling && usageRatio >= USAGE_APPROACHING_THRESHOLD && (
- <Link href={upgradeUrl}>
- <a>
- <Button type="default" size="tiny">
- Upgrade project
- </Button>
- </a>
- </Link>
- )}
- </div>
- {usageMeta?.limit > 0 && (
- <SparkBar
- type="horizontal"
- barClass={clsx(
- usageRatio >= 1
- ? usageBasedBilling
- ? 'bg-amber-900'
- : 'bg-red-900'
- : usageRatio >= USAGE_APPROACHING_THRESHOLD
- ? 'bg-amber-900'
- : 'bg-scale-1100'
- )}
- bgClass="bg-gray-300 dark:bg-gray-600"
- value={usageMeta?.usage ?? 0}
- max={usageMeta?.limit || 1}
- />
- )}
- <div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">
- Included in {subscription?.plan?.name.toLowerCase()} plan
- </p>
- <p className="text-xs">
- {usageMeta?.limit === 0 ? 'Unlimited' : formatBytes(usageMeta?.limit ?? 0)}
- </p>
- </div>
- <div className="flex items-center justify-between py-1">
- <p className="text-xs text-scale-1000">
- {attribute.chartPrefix || 'Used '}in period
- </p>
- <p className="text-xs">{formatBytes(usageMeta?.usage ?? 0)}</p>
- </div>
- {usageMeta?.limit > 0 && (
- <div className="flex items-center justify-between border-t py-1">
- <p className="text-xs text-scale-1000">Overage in period</p>
- <p className="text-xs">
- {usageExcess < 0 ? formatBytes(0) : formatBytes(usageExcess)}
- </p>
- </div>
- )}
- </div>
- </div>
- <div className="space-y-1">
- <p>{attribute.name} per day</p>
- {attribute.chartDescription.split('\n').map((paragraph, idx) => (
- <p key={`para-${idx}`} className="text-sm text-scale-1000">
- {paragraph}
- </p>
- ))}
- </div>
- {chartMeta[attribute.key]?.isLoading ? (
- <div className="space-y-2">
- <ShimmeringLoader />
- <ShimmeringLoader className="w-3/4" />
- <ShimmeringLoader className="w-1/2" />
- </div>
- ) : chartData.length > 1 && notAllValuesZero ? (
- <UsageBarChart
- name={`${attribute.chartPrefix || ''}${attribute.name}`}
- unit={attribute.unit}
- attribute={attribute.attribute}
- data={chartData}
- yLeftMargin={chartMeta[attribute.key].margin}
- yFormatter={(value) => ChartYFormatterCompactNumber(value, attribute.unit)}
- />
- ) : (
- <Panel>
- <Panel.Content>
- <div className="flex flex-col items-center justify-center space-y-2">
- <p>No data</p>
- <p className="text-sm text-scale-1000">
- No {notAllValuesZero ? 'data' : 'usage'} in period
- </p>
- </div>
- </Panel.Content>
- </Panel>
- )}
- </SectionContent>
- </div>
- )
- })}
- </>
+ <UsageFoo
+ projectRef={projectRef}
+ categoryMeta={categoryMeta}
+ chartMeta={chartMeta}
+ subscription={subscription}
+ />
)
}
diff --git a/studio/components/interfaces/BillingV2/Usage/SectionContent.tsx b/studio/components/interfaces/BillingV2/Usage/SectionContent.tsx
index 92a1cd43d0e..b52b0983efd 100644
--- a/studio/components/interfaces/BillingV2/Usage/SectionContent.tsx
+++ b/studio/components/interfaces/BillingV2/Usage/SectionContent.tsx
@@ -2,7 +2,6 @@ import { PropsWithChildren } from 'react'
import { Badge, IconExternalLink } from 'ui'
import { CategoryAttribute } from './Usage.constants'
import Link from 'next/link'
-import { ReactMarkdown } from 'react-markdown/lib/react-markdown'
export interface SectionContent {
section: CategoryAttribute
diff --git a/studio/components/interfaces/BillingV2/Usage/SizeAndCounts.tsx b/studio/components/interfaces/BillingV2/Usage/SizeAndCounts.tsx
index d790a670d7a..f5716aa0f4c 100644
--- a/studio/components/interfaces/BillingV2/Usage/SizeAndCounts.tsx
+++ b/studio/components/interfaces/BillingV2/Usage/SizeAndCounts.tsx
@@ -1,66 +1,19 @@
-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 {
- ProjectUsageResponse,
- UsageMetric,
- useProjectUsageQuery,
-} from 'data/usage/project-usage-query'
-import dayjs from 'dayjs'
-import { formatBytes } from 'lib/helpers'
-import Link from 'next/link'
-import { Badge, Button, IconAlertTriangle, IconExternalLink } from 'ui'
-import { USAGE_APPROACHING_THRESHOLD } from '../Billing.constants'
-import UsageBarChart from './UsageBarChart'
-import SectionContent from './SectionContent'
-import SectionHeader from './SectionHeader'
import { USAGE_CATEGORIES } from './Usage.constants'
-import { ChartYFormatterCompactNumber, getUpgradeUrlFromV2Subscription } from './Usage.utils'
import { DataPoint } from 'data/analytics/constants'
-import { useProjectSubscriptionV2Query } from 'data/subscriptions/project-subscription-v2-query'
-import Panel from 'components/ui/Panel'
-import { useMemo } from 'react'
+import { ProjectSubscriptionResponse } from 'data/subscriptions/project-subscription-v2-query'
+import UsageFoo from './UsageFoo'
export interface SizeAndCountsProps {
projectRef: string
+ startDate: string | undefined
+ endDate: string | undefined
+ subscription: ProjectSubscriptionResponse | undefined
}
-const SizeAndCounts = ({ projectRef }: SizeAndCountsProps) => {
- const { data: usage } = useProjectUsageQuery({ projectRef })
- const { data: subscription } = useProjectSubscriptionV2Query({ projectRef })
- const { current_period_start, current_period_end } = subscription ?? {}
-
- const startDate = useMemo(() => {
- return current_period_start ? new Date(current_period_start * 1000).toISOString() : undefined
- }, [current_period_start])
-
- const endDate = useMemo(() => {
- const periodEndDate = current_period_end ? new Date(current_period_end * 1000) : undefined
- // If end date is in future, set end date to now
- if (periodEndDate && dayjs(periodEndDate).isAfter(dayjs())) {
- const yesterday = dayjs(new Date()).subtract(1, 'day')
-
- /**
- * Currently, daily-stats data is only available a day later, so we'll use yesterday as end date, as otherwise the current day would just show up with "0" values
- *
- * We are actively working on removing this restriction on the data-eng/LF side and can remove this workaround once that's done
- */
- const newEndDate = yesterday.isAfter(dayjs(startDate)) ? yesterday : new Date()
-
- // LF seems to have an issue with the milliseconds, causes infinite loading sometimes
- return newEndDate.toISOString().slice(0, -5) + 'Z'
- } else if (periodEndDate) {
- return periodEndDate.toISOString()
- }
- }, [current_period_end, startDate])
-
+const SizeAndCounts = ({ projectRef, startDate, endDate, subscription }: SizeAndCountsProps) => {
const categoryMeta = USAGE_CATEGORIES.find((category) => category.key === 'sizeCount')
- const upgradeUrl = getUpgradeUrlFromV2Subscription(projectRef, subscription)
- const usageBasedBilling = subscription?.usage_billing_enabled
- const exceededLimitStyle = !usageBasedBilling ? 'text-red-900' : 'text-amber-900'
-
const { data: dbSizeData, isLoading: isLoadingDbSizeData } = useDailyStatsQuery({
projectRef,
attribute: 'total_db_size_bytes',
@@ -111,172 +64,12 @@ const SizeAndCounts = ({ projectRef }: SizeAndCountsProps) => {
if (categoryMeta === undefined) return null
return (
- <>
- <SectionHeader title={categoryMeta.name} description={categoryMeta.description} />
-
- {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)
-
- const chartData = chartMeta[attribute.key]?.data ?? []
-
- const notAllValuesZero = chartData.some(
- (dataPoint) => Number(dataPoint[attribute.attribute]) !== 0
- )
-
- return (
- <div id={attribute.anchor} key={attribute.key}>
- <SectionContent section={attribute}>
- <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} usage</p>
- {!usageBasedBilling && usageRatio >= 1 ? (
- <div className="flex items-center space-x-2 min-w-[115px]">
- <IconAlertTriangle
- size={14}
- strokeWidth={2}
- className={exceededLimitStyle}
- />
- <p className={`text-sm ${exceededLimitStyle}`}>Exceeded limit</p>
- </div>
- ) : !usageBasedBilling && 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-amber-900">Approaching limit</p>
- </div>
- ) : null}
- </div>
-
- {!usageBasedBilling && usageRatio >= USAGE_APPROACHING_THRESHOLD && (
- <Link href={upgradeUrl}>
- <a>
- <Button type="default" size="tiny">
- Upgrade project
- </Button>
- </a>
- </Link>
- )}
- </div>
- {usageMeta?.limit > 0 && (
- <SparkBar
- type="horizontal"
- barClass={clsx(
- usageRatio >= 1
- ? usageBasedBilling
- ? 'bg-amber-900'
- : 'bg-red-900'
- : usageRatio >= USAGE_APPROACHING_THRESHOLD
- ? 'bg-amber-900'
- : 'bg-scale-1100'
- )}
- bgClass="bg-gray-300 dark:bg-gray-600"
- value={usageMeta?.usage ?? 0}
- max={usageMeta?.limit || 1}
- />
- )}
- <div>
- <div className="flex items-center justify-between border-b py-1">
- <p className="text-xs text-scale-1000">
- Included in {subscription?.plan?.name.toLowerCase()} plan
- </p>
- <p className="text-xs">
- {attribute.unit === 'bytes'
- ? formatBytes(usageMeta?.limit ?? 0)
- : (usageMeta?.limit ?? 0).toLocaleString()}
- </p>
- </div>
- <div className="flex items-center justify-between py-1">
- <p className="text-xs text-scale-1000">
- {attribute.chartPrefix || 'Used '}in period
- </p>
- <p className="text-xs">
- {attribute.unit === 'bytes'
- ? formatBytes(usageMeta?.usage ?? 0)
- : (usageMeta?.usage ?? 0).toLocaleString()}
- </p>
- </div>
- {usageMeta?.limit > 0 && (
- <div className="flex items-center justify-between border-t py-1">
- <p className="text-xs text-scale-1000">Overage in period</p>
- <p className="text-xs">
- {usageExcess < 0
- ? attribute.unit === 'bytes'
- ? formatBytes(0)
- : 0
- : attribute.unit === 'bytes'
- ? formatBytes(usageExcess)
- : usageExcess.toLocaleString()}
- </p>
- </div>
- )}
- </div>
- </div>
-
- {attribute.key === 'db_size' && subscription?.plan?.id !== 'free' && (
- <div className="flex items-center justify-between">
- <div className="flex items-center space-x-2">
- <p className="text-sm">Disk size:</p>
- <p className="text-sm">{usage?.disk_volume_size_gb} GB</p>
- <Badge color="green" size="small">
- Auto-scaling
- </Badge>
- </div>
- <Link href="https://supabase.com/docs/guides/platform/database-usage#disk-management">
- <a>
- <Button size="tiny" type="default" icon={<IconExternalLink size={14} />}>
- What is disk size?
- </Button>
- </a>
- </Link>
- </div>
- )}
-
- <div className="space-y-1">
- <p>
- {attribute.chartPrefix || ''}
- {attribute.name} per day
- </p>
- {attribute.chartDescription.split('\n').map((paragraph, idx) => (
- <p key={`para-${idx}`} className="text-sm text-scale-1000">
- {paragraph}
- </p>
- ))}
- </div>
- {chartMeta[attribute.key].isLoading ? (
- <div className="space-y-2">
- <ShimmeringLoader />
- <ShimmeringLoader className="w-3/4" />
- <ShimmeringLoader className="w-1/2" />
- </div>
- ) : chartData.length > 1 && notAllValuesZero ? (
- <UsageBarChart
- name={`${attribute.chartPrefix || ''}${attribute.name}`}
- unit={attribute.unit}
- attribute={attribute.attribute}
- data={chartData}
- yLeftMargin={chartMeta[attribute.key].margin}
- yFormatter={(value) => ChartYFormatterCompactNumber(value, attribute.unit)}
- />
- ) : (
- <Panel>
- <Panel.Content>
- <div className="flex flex-col items-center justify-center space-y-2">
- <p>No data</p>
- <p className="text-sm text-scale-1000">
- No {notAllValuesZero ? 'data' : 'usage'} in period
- </p>
- </div>
- </Panel.Content>
- </Panel>
- )}
- </SectionContent>
- </div>
- )
- })}
- </>
+ <UsageFoo
+ projectRef={projectRef}
+ categoryMeta={categoryMeta}
+ chartMeta={chartMeta}
+ subscription={subscription}
+ />
)
}
diff --git a/studio/components/interfaces/BillingV2/Usage/Usage.constants.ts b/studio/components/interfaces/BillingV2/Usage/Usage.constants.tsx
similarity index 88%
rename from studio/components/interfaces/BillingV2/Usage/Usage.constants.ts
rename to studio/components/interfaces/BillingV2/Usage/Usage.constants.tsx
index 754f4a70000..4f18fead725 100644
--- a/studio/components/interfaces/BillingV2/Usage/Usage.constants.ts
+++ b/studio/components/interfaces/BillingV2/Usage/Usage.constants.tsx
@@ -1,3 +1,8 @@
+import { ProjectSubscriptionResponse } from 'data/subscriptions/project-subscription-v2-query'
+import { ProjectUsageData } from 'data/usage/project-usage-query'
+import Link from 'next/link'
+import { Badge, Button, IconExternalLink } from 'ui'
+
export const Y_DOMAIN_CEILING_MULTIPLIER = 4 / 3
export const USAGE_STATUS = {
@@ -19,14 +24,20 @@ export interface CategoryAttribute {
}[]
description: string
chartDescription: string
+ additionalInfo?: (
+ subscription?: ProjectSubscriptionResponse,
+ usage?: ProjectUsageData
+ ) => JSX.Element | null
}
-export const USAGE_CATEGORIES: {
+export interface CategoryMeta {
key: 'infra' | 'bandwidth' | 'sizeCount' | 'activity'
name: string
description: string
attributes: CategoryAttribute[]
-}[] = [
+}
+
+export const USAGE_CATEGORIES: CategoryMeta[] = [
{
key: 'infra',
name: 'Infrastructure',
@@ -129,6 +140,25 @@ export const USAGE_CATEGORIES: {
},
],
chartDescription: 'The data refreshes every 24 hours.',
+ additionalInfo: (subscription?: ProjectSubscriptionResponse, usage?: ProjectUsageData) =>
+ subscription?.plan?.id !== 'free' ? (
+ <div className="flex items-center justify-between">
+ <div className="flex items-center space-x-2">
+ <p className="text-sm">Disk size:</p>
+ <p className="text-sm">{usage?.disk_volume_size_gb} GB</p>
+ <Badge color="green" size="small">
+ Auto-scaling
+ </Badge>
+ </div>
+ <Link href="https://supabase.com/docs/guides/platform/database-usage#disk-management">
+ <a>
+ <Button size="tiny" type="default" icon={<IconExternalLink size={14} />}>
+ What is disk size?
+ </Button>
+ </a>
+ </Link>
+ </div>
+ ) : null,
},
{
anchor: 'storageSize',
diff --git a/studio/components/interfaces/BillingV2/Usage/Usage.tsx b/studio/components/interfaces/BillingV2/Usage/Usage.tsx
index 45de5f1f433..01d1fc8f79a 100644
--- a/studio/components/interfaces/BillingV2/Usage/Usage.tsx
+++ b/studio/components/interfaces/BillingV2/Usage/Usage.tsx
@@ -3,7 +3,7 @@ import { useParams } from 'common'
import { useProjectUsageQuery } from 'data/usage/project-usage-query'
import dayjs from 'dayjs'
import Link from 'next/link'
-import { useRef, useState } from 'react'
+import { useMemo, useRef, useState } from 'react'
import { Button, IconAlertCircle, IconLoader, Listbox } from 'ui'
import Activity from './Activity'
import Bandwidth from './Bandwidth'
@@ -32,12 +32,33 @@ const Usage = () => {
})
const { current_period_start, current_period_end } = subscription ?? {}
- const startDate =
- current_period_start !== undefined
- ? new Date(current_period_start * 1000).toISOString()
- : undefined
+
+ const startDate = useMemo(() => {
+ return current_period_start ? new Date(current_period_start * 1000).toISOString() : undefined
+ }, [current_period_start])
+
const endDate =
current_period_end !== undefined ? new Date(current_period_end * 1000).toISOString() : undefined
+
+ const dailyStatsEndDate = useMemo(() => {
+ // If end date is in future, set end date to now
+ if (endDate && dayjs(endDate).isAfter(dayjs())) {
+ const yesterday = dayjs(new Date()).subtract(1, 'day')
+
+ /**
+ * Currently, daily-stats data is only available a day later, so we'll use yesterday as end date, as otherwise the current day would just show up with "0" values
+ *
+ * We are actively working on removing this restriction on the data-eng/LF side and can remove this workaround once that's done
+ */
+ const newEndDate = yesterday.isAfter(dayjs(startDate)) ? yesterday : new Date()
+
+ // LF seems to have an issue with the milliseconds, causes infinite loading sometimes
+ return newEndDate.toISOString().slice(0, -5) + 'Z'
+ } else if (endDate) {
+ return endDate
+ }
+ }, [endDate, startDate])
+
const { data: ioBudgetData } = useInfraMonitoringQuery({
projectRef: selectedProjectRef,
attribute: 'disk_io_budget',
@@ -166,13 +187,28 @@ const Usage = () => {
<Infrastructure projectRef={selectedProjectRef} />
</div>
<div id="bandwidth" ref={bandwidthRef}>
- <Bandwidth projectRef={selectedProjectRef} />
+ <Bandwidth
+ projectRef={selectedProjectRef}
+ subscription={subscription}
+ startDate={startDate}
+ endDate={dailyStatsEndDate}
+ />
</div>
<div id="size_and_counts" ref={sizeAndCountsRef}>
- <SizeAndCounts projectRef={selectedProjectRef} />
+ <SizeAndCounts
+ projectRef={selectedProjectRef}
+ subscription={subscription}
+ startDate={startDate}
+ endDate={dailyStatsEndDate}
+ />
</div>
<div id="activity" ref={activityRef}>
- <Activity projectRef={selectedProjectRef} />
+ <Activity
+ projectRef={selectedProjectRef}
+ subscription={subscription}
+ startDate={startDate}
+ endDate={dailyStatsEndDate}
+ />
</div>
</>
)
diff --git a/studio/components/interfaces/BillingV2/Usage/UsageFoo.tsx b/studio/components/interfaces/BillingV2/Usage/UsageFoo.tsx
new file mode 100644
index 00000000000..6b3e4374484
--- /dev/null
+++ b/studio/components/interfaces/BillingV2/Usage/UsageFoo.tsx
@@ -0,0 +1,191 @@
+import ShimmeringLoader from 'components/ui/ShimmeringLoader'
+import SectionHeader from './SectionHeader'
+import { CategoryMeta } from './Usage.constants'
+import {
+ ProjectUsageResponse,
+ UsageMetric,
+ useProjectUsageQuery,
+} from 'data/usage/project-usage-query'
+import { DataPoint } from 'data/analytics/constants'
+import SectionContent from './SectionContent'
+import { Button, IconAlertTriangle } from 'ui'
+import { USAGE_APPROACHING_THRESHOLD } from '../Billing.constants'
+import Link from 'next/link'
+import SparkBar from 'components/ui/SparkBar'
+import clsx from 'clsx'
+import { ProjectSubscriptionResponse } from 'data/subscriptions/project-subscription-v2-query'
+import { ChartYFormatterCompactNumber, getUpgradeUrlFromV2Subscription } from './Usage.utils'
+import { formatBytes } from 'lib/helpers'
+import UsageBarChart from './UsageBarChart'
+import Panel from 'components/ui/Panel'
+
+interface UsageFooProps {
+ projectRef: string
+ categoryMeta: CategoryMeta
+ subscription: ProjectSubscriptionResponse | undefined
+ chartMeta: {
+ [key: string]: { data: DataPoint[]; margin: number; isLoading: boolean; hasNoData: boolean }
+ }
+}
+
+const UsageFoo = ({ projectRef, categoryMeta, chartMeta, subscription }: UsageFooProps) => {
+ const { data: usage } = useProjectUsageQuery({ projectRef })
+ const usageBasedBilling = subscription?.usage_billing_enabled
+ const exceededLimitStyle = !usageBasedBilling ? 'text-red-900' : 'text-amber-900'
+
+ const upgradeUrl = getUpgradeUrlFromV2Subscription(projectRef, subscription)
+
+ return (
+ <>
+ <SectionHeader title={categoryMeta.name} description={categoryMeta.description} />
+
+ {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)
+
+ const chartData = chartMeta[attribute.key]?.data ?? []
+
+ const notAllValuesZero = chartData.some(
+ (dataPoint) => Number(dataPoint[attribute.attribute]) !== 0
+ )
+
+ return (
+ <div id={attribute.anchor} key={attribute.key}>
+ <SectionContent section={attribute}>
+ <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} usage</p>
+ {!usageBasedBilling && usageRatio >= 1 ? (
+ <div className="flex items-center space-x-2 min-w-[115px]">
+ <IconAlertTriangle
+ size={14}
+ strokeWidth={2}
+ className={exceededLimitStyle}
+ />
+ <p className={`text-sm ${exceededLimitStyle}`}>Exceeded limit</p>
+ </div>
+ ) : !usageBasedBilling && 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-amber-900">Approaching limit</p>
+ </div>
+ ) : null}
+ </div>
+
+ {!usageBasedBilling && usageRatio >= USAGE_APPROACHING_THRESHOLD && (
+ <Link href={upgradeUrl}>
+ <a>
+ <Button type="default" size="tiny">
+ Upgrade project
+ </Button>
+ </a>
+ </Link>
+ )}
+ </div>
+ {usageMeta?.limit > 0 && (
+ <SparkBar
+ type="horizontal"
+ barClass={clsx(
+ usageRatio >= 1
+ ? usageBasedBilling
+ ? 'bg-amber-900'
+ : 'bg-red-900'
+ : usageRatio >= USAGE_APPROACHING_THRESHOLD
+ ? 'bg-amber-900'
+ : 'bg-scale-1100'
+ )}
+ bgClass="bg-gray-300 dark:bg-gray-600"
+ value={usageMeta?.usage ?? 0}
+ max={usageMeta?.limit || 1}
+ />
+ )}
+ <div>
+ <div className="flex items-center justify-between border-b py-1">
+ <p className="text-xs text-scale-1000">
+ Included in {subscription?.plan?.name.toLowerCase()} plan
+ </p>
+ <p className="text-xs">
+ {attribute.unit === 'bytes'
+ ? formatBytes(usageMeta?.limit ?? 0)
+ : (usageMeta?.limit ?? 0).toLocaleString()}
+ </p>
+ </div>
+ <div className="flex items-center justify-between py-1">
+ <p className="text-xs text-scale-1000">
+ {attribute.chartPrefix || 'Used '}in period
+ </p>
+ <p className="text-xs">
+ {attribute.unit === 'bytes'
+ ? formatBytes(usageMeta?.usage ?? 0)
+ : (usageMeta?.usage ?? 0).toLocaleString()}
+ </p>
+ </div>
+ {usageMeta?.limit > 0 && (
+ <div className="flex items-center justify-between border-t py-1">
+ <p className="text-xs text-scale-1000">Overage in period</p>
+ <p className="text-xs">
+ {usageExcess < 0
+ ? attribute.unit === 'bytes'
+ ? formatBytes(0)
+ : 0
+ : attribute.unit === 'bytes'
+ ? formatBytes(usageExcess)
+ : usageExcess.toLocaleString()}
+ </p>
+ </div>
+ )}
+ </div>
+ </div>
+
+ {attribute.additionalInfo?.(subscription, usage)}
+
+ <div className="space-y-1">
+ <p>
+ {attribute.chartPrefix || ''}
+ {attribute.name} per day
+ </p>
+ {attribute.chartDescription.split('\n').map((paragraph, idx) => (
+ <p key={`para-${idx}`} className="text-sm text-scale-1000">
+ {paragraph}
+ </p>
+ ))}
+ </div>
+ {chartMeta[attribute.key].isLoading ? (
+ <div className="space-y-2">
+ <ShimmeringLoader />
+ <ShimmeringLoader className="w-3/4" />
+ <ShimmeringLoader className="w-1/2" />
+ </div>
+ ) : chartData.length > 1 && notAllValuesZero ? (
+ <UsageBarChart
+ name={`${attribute.chartPrefix || ''}${attribute.name}`}
+ unit={attribute.unit}
+ attribute={attribute.attribute}
+ data={chartData}
+ yLeftMargin={chartMeta[attribute.key].margin}
+ yFormatter={(value) => ChartYFormatterCompactNumber(value, attribute.unit)}
+ />
+ ) : (
+ <Panel>
+ <Panel.Content>
+ <div className="flex flex-col items-center justify-center space-y-2">
+ <p>No data</p>
+ <p className="text-sm text-scale-1000">
+ No {notAllValuesZero ? 'data' : 'usage'} in period
+ </p>
+ </div>
+ </Panel.Content>
+ </Panel>
+ )}
+ </SectionContent>
+ </div>
+ )
+ })}
+ </>
+ )
+}
+
+export default UsageFoo