Skip to content

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

fix: billing labelling issues with max, sum and egress

Jonathan Summers-Muirb13c3e685a0c
diff --git a/studio/components/interfaces/Billing/PAYGUsage/PAYGUsage.constants.ts b/studio/components/interfaces/Billing/PAYGUsage/PAYGUsage.constants.ts
index 8bc077f2ce9..5917d61ef6d 100644
--- a/studio/components/interfaces/Billing/PAYGUsage/PAYGUsage.constants.ts
+++ b/studio/components/interfaces/Billing/PAYGUsage/PAYGUsage.constants.ts
@@ -10,12 +10,14 @@ export const chargeableProducts: ChargeableProduct[] = [
attribute: 'total_db_size_bytes',
costPerUnit: 0.125,
unitQuantity: 1000000000, // 1GB
+ pricingModel: 'max',
},
{
title: 'Database Egress',
- attribute: 'total_db_egress_bytes',
+ attribute: 'total_egress_modified',
costPerUnit: 0.09,
unitQuantity: 1000000000, // 1GB
+ pricingModel: 'sum',
},
],
},
@@ -28,12 +30,14 @@ export const chargeableProducts: ChargeableProduct[] = [
attribute: 'total_storage_size_bytes',
costPerUnit: 0.021,
unitQuantity: 1000000000, // 1GB
+ pricingModel: 'max',
},
{
title: 'Storage Egress',
attribute: 'total_storage_egress',
costPerUnit: 0.09,
unitQuantity: 1000000000, // 1GB
+ pricingModel: 'sum',
},
],
},
diff --git a/studio/components/interfaces/Billing/PAYGUsage/PAYGUsage.tsx b/studio/components/interfaces/Billing/PAYGUsage/PAYGUsage.tsx
index 57678df3d1b..64d99fec28b 100644
--- a/studio/components/interfaces/Billing/PAYGUsage/PAYGUsage.tsx
+++ b/studio/components/interfaces/Billing/PAYGUsage/PAYGUsage.tsx
@@ -21,6 +21,7 @@ const PAYGUsage: FC<Props> = ({ paygStats }) => {
<div className="flex flex-col">
{chargeableProducts.map((product: ChargeableProduct) => {
const productCost = deriveProductCost(paygStats, product)
+
return (
<div
key={product.title}
@@ -63,7 +64,7 @@ const PAYGUsage: FC<Props> = ({ paygStats }) => {
endDate={endDate}
interval="1d"
highlight="maximum"
- defaultChartStyle="line"
+ defaultChartStyle="bar"
customDateFormat={'MMM D, YYYY'}
/>
</div>
diff --git a/studio/components/interfaces/Billing/PAYGUsage/PAYGUsage.types.ts b/studio/components/interfaces/Billing/PAYGUsage/PAYGUsage.types.ts
index a1497f6c05a..bcd9100382d 100644
--- a/studio/components/interfaces/Billing/PAYGUsage/PAYGUsage.types.ts
+++ b/studio/components/interfaces/Billing/PAYGUsage/PAYGUsage.types.ts
@@ -9,4 +9,10 @@ export interface ProductFeature {
attribute: string
costPerUnit: number
unitQuantity: number
+ minusAttribute?: string
+ pricingModel: PricingModel
}
+
+export type PaygStats = { [key: string]: { sum: number; max: number } }
+
+export type PricingModel = 'sum' | 'max'
diff --git a/studio/components/interfaces/Billing/PAYGUsage/PAYGUsage.utils.ts b/studio/components/interfaces/Billing/PAYGUsage/PAYGUsage.utils.ts
index aecd59dd324..e871bfe88ad 100644
--- a/studio/components/interfaces/Billing/PAYGUsage/PAYGUsage.utils.ts
+++ b/studio/components/interfaces/Billing/PAYGUsage/PAYGUsage.utils.ts
@@ -1,8 +1,18 @@
-import { sum } from 'lodash'
-import { ChargeableProduct, ProductFeature } from './PAYGUsage.types'
+import { get, maxBy, sum } from 'lodash'
+import { ChargeableProduct, PaygStats, ProductFeature } from './PAYGUsage.types'
-export const deriveFeatureCost = (paygStats: any, feature: ProductFeature) => {
- const maximumValueOfTheMonth = paygStats?.[feature.attribute] ?? 0
+/**
+ *
+ * Derives the most appropriate cost
+ * depending on what kind of product feature we are displaying cost for.
+ *
+ * @param paygStats
+ * @param feature
+ *
+ * @returns number
+ */
+export const deriveFeatureCost = (paygStats: PaygStats, feature: ProductFeature) => {
+ const maximumValueOfTheMonth = paygStats?.[feature.attribute][feature.pricingModel] ?? 0
return (maximumValueOfTheMonth / feature.unitQuantity) * feature.costPerUnit
}
diff --git a/studio/components/interfaces/Billing/Subscription/Subscription.tsx b/studio/components/interfaces/Billing/Subscription/Subscription.tsx
index 33e6fcdf9f0..0031d22602e 100644
--- a/studio/components/interfaces/Billing/Subscription/Subscription.tsx
+++ b/studio/components/interfaces/Billing/Subscription/Subscription.tsx
@@ -16,7 +16,7 @@ import { chargeableProducts } from '../PAYGUsage/PAYGUsage.constants'
interface Props {
project: any
subscription: StripeSubscription
- paygStats?: Dictionary<number>
+ paygStats?: { [key: string]: { sum: number; max: number } }
loading?: boolean
showProjectName?: boolean
currentPeriodStart: number
@@ -132,12 +132,16 @@ const Subscription: FC<Props> = ({
{isPayg &&
chargeableProducts.map((product) =>
product.features.map((feature) => {
- const cost = deriveFeatureCost(paygStats, feature).toFixed(3)
+ const derivedCost = deriveFeatureCost(paygStats, feature)
+ const cost = derivedCost.toFixed(3)
+
return (
<CostBreakdownRow
key={feature.attribute}
item={feature.title}
- amount={formatBytes(paygStats?.[feature.attribute] ?? 0)}
+ amount={formatBytes(
+ paygStats?.[feature.attribute]?.[feature.pricingModel] ?? 0
+ )}
unitPrice={`${feature.costPerUnit}/GB`}
price={cost}
/>
diff --git a/studio/components/to-be-cleaned/Charts/ChartHandler.tsx b/studio/components/to-be-cleaned/Charts/ChartHandler.tsx
index 5b8458d20e9..cf8798a2588 100644
--- a/studio/components/to-be-cleaned/Charts/ChartHandler.tsx
+++ b/studio/components/to-be-cleaned/Charts/ChartHandler.tsx
@@ -15,7 +15,7 @@ import { API_URL } from 'lib/constants'
import { get } from 'lib/common/fetch'
import { BarChart, AreaChart } from './ChartRenderer'
import { ChartData } from './ChartHandler.types'
-import { TooltipProps } from 'recharts'
+import { AreaProps } from 'recharts'
interface Props {
label: string
@@ -34,6 +34,7 @@ interface Props {
format?: string
highlightedValue?: string | number
onBarClick?: (v: any) => void
+ areaType?: AreaProps['type']
}
/**
@@ -62,6 +63,7 @@ const ChartHandler: FC<Props> = ({
format,
highlightedValue,
onBarClick,
+ areaType,
}) => {
const router = useRouter()
const { ref } = router.query
@@ -120,6 +122,8 @@ const ChartHandler: FC<Props> = ({
highlightedValue = highlightedValue
? highlightedValue
+ : provider === 'daily-stats' && !attribute.includes('ingress') && !attribute.includes('egress')
+ ? chartData?.maximum
: provider === 'daily-stats'
? chartData?.total
: provider === 'log-stats'
diff --git a/studio/hooks/queries/useProjectPaygStatistics.ts b/studio/hooks/queries/useProjectPaygStatistics.ts
index bc4451fbe25..750f88a3ee5 100644
--- a/studio/hooks/queries/useProjectPaygStatistics.ts
+++ b/studio/hooks/queries/useProjectPaygStatistics.ts
@@ -16,7 +16,7 @@ export function useProjectPaygStatistics(ref?: string, tierProdId?: string) {
const startDate = dayjs().utc().startOf('month').format(DATE_FORMAT)
const endDate = dayjs().utc().endOf('month').format(DATE_FORMAT)
const attributes =
- 'total_db_size_bytes,total_db_egress_bytes,total_storage_size_bytes,total_storage_egress'
+ 'total_egress,total_db_size_bytes,total_db_egress_bytes,total_storage_size_bytes,total_storage_egress'
const url = `${API_URL}/projects/${ref}/daily-stats?attribute=${attributes}&startDate=${encodeURIComponent(
startDate
)}&endDate=${encodeURIComponent(endDate)}&interval='1d'`
@@ -48,9 +48,25 @@ function getPaygStats(data: any): Dictionary<number> {
const paygStats: any = {}
if (!data) return paygStats
- chargeableProducts.forEach((product: any) => {
- product.features.forEach((feature: any) => {
- paygStats[feature.attribute] = _get(maxBy(data, feature.attribute), feature.attribute)
+ let dataModified: any = []
+ data.map((x: any) => {
+ dataModified.push({ ...x, total_egress_modified: x.total_egress - x.total_storage_egress })
+ })
+
+ chargeableProducts.forEach((product) => {
+ product.features.forEach((feature) => {
+ // calculate max
+ const max = _get(maxBy(dataModified, feature.attribute), feature.attribute)
+ // calculate sum
+ let sum = 0
+ dataModified.map((x: any) => {
+ sum = sum + x[feature.attribute]
+ })
+ // set sum and max under attribute name
+ paygStats[feature.attribute] = {
+ sum,
+ max,
+ }
})
})
return paygStats