diff --git a/studio/components/interfaces/BillingV2/Usage/Bandwidth.tsx b/studio/components/interfaces/BillingV2/Usage/Bandwidth.tsx index 835dcd19e03..e14131babe0 100644 --- a/studio/components/interfaces/BillingV2/Usage/Bandwidth.tsx +++ b/studio/components/interfaces/BillingV2/Usage/Bandwidth.tsx @@ -1,24 +1,39 @@ import { useParams } from 'common' import ShimmeringLoader from 'components/ui/ShimmeringLoader' -import { useInfraMonitoringQuery } from 'data/analytics/infra-monitoring-query' -import dayjs from 'dayjs' -import { DATE_FORMAT } from 'lib/constants' +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 BarChart from './BarChart' -import { generateUsageData } from './Usage.utils' +import { Button } from 'ui' +import SparkBar from 'components/ui/SparkBar' +import { formatBytes } from 'lib/helpers' const Bandwidth = () => { const { ref } = useParams() + const { data: usage } = useProjectUsageQuery({ projectRef: ref }) + const { data: subscription } = useProjectSubscriptionQuery({ projectRef: ref }) + 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 currentDate = 1682669326710 / 1000 - const startDate = dayjs.unix(currentDate).subtract(24, 'hour').format(DATE_FORMAT) - const endDate = dayjs.unix(currentDate).format(DATE_FORMAT) - - const { data: ioBudgetData, isLoading: isLoadingIoBudgetData } = useInfraMonitoringQuery({ + const { db_egress } = usage ?? {} + const dbEgressExcess = (db_egress?.usage ?? 0) - (db_egress?.limit ?? 0) + const { data: dbEgressData, isLoading: isLoadingDbEgressData } = useDailyStatsQuery({ projectRef: ref, - attribute: 'disk_io_budget', + attribute: 'total_rest_egress', + interval: '1d', + startDate, + endDate, + }) + + const { storage_egress } = usage ?? {} + const storageEgressExcess = (storage_egress?.usage ?? 0) - (storage_egress?.limit ?? 0) + const { data: storageEgressData, isLoading: isLoadingStorageEgressData } = useDailyStatsQuery({ + projectRef: ref, + attribute: 'total_storage_egress', + interval: '1d', startDate, endDate, - interval: '1h', }) return ( @@ -31,24 +46,57 @@ const Bandwidth = () => { + + {/* DATABASE EGRESS - need to fix if no value yet (API will return period_start as 0 in first data point) */}
-

IO Budget

+

Database Egress

Some description here

-
-

IO budget per day

-

- Just FYI for now its a percentage for IO budget, which is different from what the - figma designs are (in minutes). We will need o11y or infra to help update this -

+
+
+

+ Egress {subscription?.tier.key.toLowerCase()} quota usage +

+ +
+ +
+
+

+ Included in {subscription?.tier.name.toLowerCase()} +

+

{formatBytes(db_egress?.limit ?? 0)}

+
+
+

Used

+

{formatBytes(db_egress?.usage ?? 0)}

+
+
+

Extra volume used this month

+

+ {dbEgressExcess < 0 ? formatBytes(0) : formatBytes(dbEgressExcess)} +

+
+
- {isLoadingIoBudgetData ? ( +
+

Database egress over time

+

Some description here

+
+ {isLoadingDbEgressData ? (
@@ -56,16 +104,78 @@ const Bandwidth = () => {
) : ( + )} +
+
+
+
+ + {/* STORAGE EGRESS - need to fix if no value yet (API will return period_start as 0 in first data point) */} +
+
+
+
+
+

Storage Egress

+

Some description here

+
+
+
+
+
+

+ Storage egress {subscription?.tier.key.toLowerCase()} quota usage +

+ +
+ +
+
+

+ Included in {subscription?.tier.name.toLowerCase()} +

+

{formatBytes(storage_egress?.limit ?? 0)}

+
+
+

Used

+

{formatBytes(storage_egress?.usage ?? 0)}

+
+
+

Extra volume used this month

+

+ {storageEgressExcess < 0 ? formatBytes(0) : formatBytes(storageEgressExcess)} +

+
+
+
+
+

Storage egress over time

+

Some description here

+
+ {isLoadingStorageEgressData ? ( +
+ + + +
+ ) : ( + )}
diff --git a/studio/components/interfaces/BillingV2/Usage/Infrastructure.tsx b/studio/components/interfaces/BillingV2/Usage/Infrastructure.tsx index f5f0f6b5a0c..6614c10dc09 100644 --- a/studio/components/interfaces/BillingV2/Usage/Infrastructure.tsx +++ b/studio/components/interfaces/BillingV2/Usage/Infrastructure.tsx @@ -2,8 +2,6 @@ import { useParams } from 'common' import ShimmeringLoader from 'components/ui/ShimmeringLoader' import { useInfraMonitoringQuery } from 'data/analytics/infra-monitoring-query' import { useProjectSubscriptionQuery } from 'data/subscriptions/project-subscription-query' -import dayjs from 'dayjs' -import { DATE_FORMAT } from 'lib/constants' import BarChart from './BarChart' const Infrastructure = () => { @@ -16,18 +14,26 @@ const Infrastructure = () => { const { data: cpuUsageData, isLoading: isLoadingCpuUsageData } = useInfraMonitoringQuery({ projectRef: ref, attribute: 'cpu_usage', + interval: '1d', startDate, endDate, - interval: '1d', modifier: (x: number) => x * 100, }) const { data: memoryUsageData, isLoading: isLoadingMemoryUsageData } = useInfraMonitoringQuery({ projectRef: ref, attribute: 'ram_usage', + interval: '1d', startDate, endDate, + }) + + const { data: ioBudgetData, isLoading: isLoadingIoBudgetData } = useInfraMonitoringQuery({ + projectRef: ref, + attribute: 'disk_io_budget', interval: '1d', + startDate, + endDate, }) return ( @@ -108,6 +114,40 @@ const Infrastructure = () => {
+ + {/* DISK IO BUDGET */} +
+
+
+
+
+

IO Budget

+

Some description here

+
+
+
+
+

Disk IO budget per day

+

Some description here

+
+ {isLoadingIoBudgetData ? ( +
+ + + +
+ ) : ( + + )} +
+
+
+
) } diff --git a/studio/components/interfaces/BillingV2/Usage/Usage.tsx b/studio/components/interfaces/BillingV2/Usage/Usage.tsx index 956b48669ff..6cbfa0815ba 100644 --- a/studio/components/interfaces/BillingV2/Usage/Usage.tsx +++ b/studio/components/interfaces/BillingV2/Usage/Usage.tsx @@ -75,9 +75,9 @@ const Usage = () => {
- {/* - - */} + + {/* */} + {/* */} ) } diff --git a/studio/data/analytics/daily-stats-query.ts b/studio/data/analytics/daily-stats-query.ts index 2655fecdec8..c8032574f88 100644 --- a/studio/data/analytics/daily-stats-query.ts +++ b/studio/data/analytics/daily-stats-query.ts @@ -4,10 +4,11 @@ import { API_URL } from 'lib/constants' import { useCallback } from 'react' import { analyticsKeys } from './keys' import { AnalyticsData } from './constants' +import dayjs from 'dayjs' export type DailyStatsVariables = { projectRef?: string - attribute?: + attribute: | 'total_realtime_egress' | 'total_realtime_requests' | 'total_realtime_ingress' @@ -50,17 +51,11 @@ export type DailyStatsVariables = { endDate?: string interval?: string dateFormat?: string + modifier?: (x: number) => number } export async function getDailyStats( - { - projectRef, - attribute, - startDate, - endDate, - interval = '1d', - dateFormat = 'DD MMM', - }: DailyStatsVariables, + { projectRef, attribute, startDate, endDate, interval = '1d' }: DailyStatsVariables, signal?: AbortSignal ) { if (!projectRef) throw new Error('Project ref is required') @@ -82,14 +77,44 @@ export type DailyStatsData = Awaited> export type DailyStatsError = unknown export const useDailyStatsQuery = ( - { projectRef, attribute, startDate, endDate, interval = '1d' }: DailyStatsVariables, + { + projectRef, + attribute, + startDate, + endDate, + interval = '1d', + dateFormat, + modifier, + }: DailyStatsVariables, { enabled = true, ...options }: UseQueryOptions = {} ) => useQuery( analyticsKeys.dailyStats(projectRef, { attribute, startDate, endDate, interval }), ({ signal }) => getDailyStats({ projectRef, attribute, startDate, endDate, interval }, signal), { - enabled: enabled && typeof projectRef !== 'undefined', + enabled: + enabled && + typeof projectRef !== 'undefined' && + typeof attribute !== 'undefined' && + typeof startDate !== 'undefined' && + typeof endDate !== 'undefined', + // @ts-ignore + select(data) { + return { + ...data, + data: data.data.map((x) => { + return { + ...x, + [attribute]: + modifier !== undefined ? modifier(Number(x[attribute])) : Number(x[attribute]), + period_start: + Number(x.period_start) > 0 + ? dayjs(x.period_start).format(dateFormat) + : x.period_start, + } + }), + } + }, ...options, } )