Skip to content

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

Allow project filtering for org usage

Joshen Lim7f28e474036a
diff --git a/studio/components/interfaces/Organization/Usage/Activity.tsx b/studio/components/interfaces/Organization/Usage/Activity.tsx
index 5099e4c71ff..b0fb77d036c 100644
--- a/studio/components/interfaces/Organization/Usage/Activity.tsx
+++ b/studio/components/interfaces/Organization/Usage/Activity.tsx
@@ -5,7 +5,7 @@ import { PricingMetric, useOrgDailyStatsQuery } from 'data/analytics/org-daily-s
export interface ActivityProps {
orgSlug: string
- projectRef?: string // [Joshen TODO] remove
+ projectRef?: string
startDate: string | undefined
endDate: string | undefined
subscription: ProjectSubscriptionResponse | undefined
@@ -14,6 +14,7 @@ export interface ActivityProps {
const Activity = ({
orgSlug,
+ projectRef,
subscription,
startDate,
endDate,
@@ -21,6 +22,7 @@ const Activity = ({
}: ActivityProps) => {
const { data: mauData, isLoading: isLoadingMauData } = useOrgDailyStatsQuery({
orgSlug,
+ projectRef,
metric: PricingMetric.MONTHLY_ACTIVE_USERS,
interval: '1d',
startDate,
@@ -29,6 +31,7 @@ const Activity = ({
const { data: mauSSOData, isLoading: isLoadingMauSSOData } = useOrgDailyStatsQuery({
orgSlug,
+ projectRef,
metric: PricingMetric.MONTHLY_ACTIVE_SSO_USERS,
interval: '1d',
startDate,
@@ -38,6 +41,7 @@ const Activity = ({
const { data: assetTransformationsData, isLoading: isLoadingAssetTransformationsData } =
useOrgDailyStatsQuery({
orgSlug,
+ projectRef,
metric: PricingMetric.STORAGE_IMAGES_TRANSFORMED,
interval: '1d',
startDate,
@@ -47,6 +51,7 @@ const Activity = ({
const { data: funcInvocationsData, isLoading: isLoadingFuncInvocationsData } =
useOrgDailyStatsQuery({
orgSlug,
+ projectRef,
metric: PricingMetric.FUNCTION_INVOCATIONS,
interval: '1d',
startDate,
@@ -56,6 +61,7 @@ const Activity = ({
const { data: realtimeMessagesData, isLoading: isLoadingRealtimeMessagesData } =
useOrgDailyStatsQuery({
orgSlug,
+ projectRef,
metric: PricingMetric.REALTIME_MESSAGE_COUNT,
interval: '1d',
startDate,
@@ -65,6 +71,7 @@ const Activity = ({
const { data: realtimeConnectionsData, isLoading: isLoadingRealtimeConnectionsData } =
useOrgDailyStatsQuery({
orgSlug,
+ projectRef,
metric: PricingMetric.REALTIME_PEAK_CONNECTIONS,
interval: '1d',
startDate,
diff --git a/studio/components/interfaces/Organization/Usage/Bandwidth.tsx b/studio/components/interfaces/Organization/Usage/Bandwidth.tsx
index 221f8d42a20..90b0a3aa0a0 100644
--- a/studio/components/interfaces/Organization/Usage/Bandwidth.tsx
+++ b/studio/components/interfaces/Organization/Usage/Bandwidth.tsx
@@ -5,6 +5,7 @@ import UsageSection from './UsageSection/UsageSection'
export interface BandwidthProps {
orgSlug: string
+ projectRef?: string
startDate: string | undefined
endDate: string | undefined
subscription: ProjectSubscriptionResponse | undefined
@@ -14,6 +15,7 @@ export interface BandwidthProps {
// [Joshen TODO] Needs to take in org slug and eventually use daily stats org query
const Bandwidth = ({
orgSlug,
+ projectRef,
subscription,
startDate,
endDate,
@@ -21,6 +23,7 @@ const Bandwidth = ({
}: BandwidthProps) => {
const { data: egressData, isLoading: isLoadingDbEgressData } = useOrgDailyStatsQuery({
orgSlug,
+ projectRef,
metric: PricingMetric.EGRESS,
interval: '1d',
startDate,
diff --git a/studio/components/interfaces/Organization/Usage/SizeAndCounts.tsx b/studio/components/interfaces/Organization/Usage/SizeAndCounts.tsx
index c25b61b7c0b..da48ac2bfcf 100644
--- a/studio/components/interfaces/Organization/Usage/SizeAndCounts.tsx
+++ b/studio/components/interfaces/Organization/Usage/SizeAndCounts.tsx
@@ -5,6 +5,7 @@ import UsageSection from './UsageSection/UsageSection'
export interface SizeAndCountsProps {
orgSlug: string
+ projectRef?: string
startDate: string | undefined
endDate: string | undefined
subscription: OrgSubscription | undefined
@@ -13,6 +14,7 @@ export interface SizeAndCountsProps {
const SizeAndCounts = ({
orgSlug,
+ projectRef,
startDate,
endDate,
subscription,
@@ -20,6 +22,7 @@ const SizeAndCounts = ({
}: SizeAndCountsProps) => {
const { data: dbSizeData, isLoading: isLoadingDbSizeData } = useOrgDailyStatsQuery({
orgSlug,
+ projectRef,
metric: PricingMetric.DATABASE_SIZE,
interval: '1d',
startDate,
@@ -28,6 +31,7 @@ const SizeAndCounts = ({
const { data: storageSizeData, isLoading: isLoadingStorageSizeData } = useOrgDailyStatsQuery({
orgSlug,
+ projectRef,
metric: PricingMetric.STORAGE_SIZE,
interval: '1d',
startDate,
@@ -36,6 +40,7 @@ const SizeAndCounts = ({
const { data: functionCountData, isLoading: isLoadingFunctionCountData } = useOrgDailyStatsQuery({
orgSlug,
+ projectRef,
metric: PricingMetric.FUNCTION_COUNT,
interval: '1d',
startDate,
diff --git a/studio/components/interfaces/Organization/Usage/Usage.tsx b/studio/components/interfaces/Organization/Usage/Usage.tsx
index 66fc550cd22..87260dd1d79 100644
--- a/studio/components/interfaces/Organization/Usage/Usage.tsx
+++ b/studio/components/interfaces/Organization/Usage/Usage.tsx
@@ -1,22 +1,28 @@
import clsx from 'clsx'
import dayjs from 'dayjs'
-import { useMemo, useState } from 'react'
+import { useEffect, useMemo, useState } from 'react'
import { useParams } from 'common'
-import { ScaffoldContainer, ScaffoldDivider } from 'components/layouts/Scaffold'
+import { ScaffoldContainer } from 'components/layouts/Scaffold'
import DateRangePicker from 'components/to-be-cleaned/DateRangePicker'
import AlertError from 'components/ui/AlertError'
import ShimmeringLoader from 'components/ui/ShimmeringLoader'
+import { useProjectsQuery } from 'data/projects/projects-query'
import { useOrgSubscriptionQuery } from 'data/subscriptions/org-subscription-query'
+import { useSelectedOrganization } from 'hooks'
import { TIME_PERIODS_BILLING, TIME_PERIODS_REPORTS } from 'lib/constants'
+import { Listbox } from 'ui'
import Activity from './Activity'
import Bandwidth from './Bandwidth'
import SizeAndCounts from './SizeAndCounts'
const Usage = () => {
- const { slug } = useParams()
+ const { slug, projectRef } = useParams()
const [dateRange, setDateRange] = useState<any>()
+ const [selectedProjectRef, setSelectedProjectRef] = useState<string>()
+ const organization = useSelectedOrganization()
+ const { data: projects, isSuccess } = useProjectsQuery()
const {
data: subscription,
error: subscriptionError,
@@ -24,6 +30,15 @@ const Usage = () => {
isError: isErrorSubscription,
isSuccess: isSuccessSubscription,
} = useOrgSubscriptionQuery({ orgSlug: slug })
+ const orgProjects = projects?.filter((project) => project.organization_id === organization?.id)
+
+ useEffect(() => {
+ if (projectRef && isSuccess && orgProjects !== undefined) {
+ if (orgProjects.find((project) => project.ref === projectRef)) {
+ setSelectedProjectRef(projectRef)
+ }
+ }
+ }, [projectRef, isSuccess])
const billingCycleStart = dayjs.unix(subscription?.current_period_start ?? 0).utc()
const billingCycleEnd = dayjs.unix(subscription?.current_period_end ?? 0).utc()
@@ -67,7 +82,7 @@ const Usage = () => {
return (
<>
- <ScaffoldContainer>
+ <ScaffoldContainer className="sticky top-0 border-b bg-scale-200 z-10 overflow-hidden">
<div className="py-4 flex items-center space-x-4">
{isLoadingSubscription && <ShimmeringLoader className="w-[250px]" />}
@@ -91,6 +106,37 @@ const Usage = () => {
currentBillingPeriodStart={subscription?.current_period_start}
className="!w-[200px]"
/>
+
+ <Listbox
+ size="tiny"
+ name="schema"
+ className="w-[180px]"
+ value={selectedProjectRef}
+ onChange={(value: any) => {
+ if (value === 'all-projects') setSelectedProjectRef(undefined)
+ else setSelectedProjectRef(value)
+ }}
+ >
+ <Listbox.Option
+ key="all-projects"
+ id="all-projects"
+ value="all-projects"
+ label="All projects"
+ >
+ All projects
+ </Listbox.Option>
+ {orgProjects?.map((project) => (
+ <Listbox.Option
+ key={project.ref}
+ id={project.ref}
+ value={project.ref}
+ label={project.name}
+ >
+ {project.name}
+ </Listbox.Option>
+ ))}
+ </Listbox>
+
<div className="flex flex-col xl:flex-row xl:gap-3">
<p className={clsx('text-sm transition', isLoadingSubscription && 'opacity-50')}>
Organization is on the {subscription.plan.name} plan
@@ -105,10 +151,9 @@ const Usage = () => {
</div>
</ScaffoldContainer>
- <ScaffoldDivider />
-
<Bandwidth
orgSlug={slug as string}
+ projectRef={selectedProjectRef}
subscription={subscription}
startDate={startDate}
endDate={endDate}
@@ -117,6 +162,7 @@ const Usage = () => {
<SizeAndCounts
orgSlug={slug as string}
+ projectRef={selectedProjectRef}
subscription={subscription}
startDate={startDate}
endDate={endDate}
@@ -125,6 +171,7 @@ const Usage = () => {
<Activity
orgSlug={slug as string}
+ projectRef={selectedProjectRef}
subscription={subscription}
startDate={startDate}
endDate={endDate}
diff --git a/studio/components/interfaces/Organization/Usage/UsageSection/UsageSection.tsx b/studio/components/interfaces/Organization/Usage/UsageSection/UsageSection.tsx
index f6bb64ce8b4..c66ea1d7e8e 100644
--- a/studio/components/interfaces/Organization/Usage/UsageSection/UsageSection.tsx
+++ b/studio/components/interfaces/Organization/Usage/UsageSection/UsageSection.tsx
@@ -12,6 +12,7 @@ export interface ChartMeta {
export interface UsageSectionProps {
orgSlug: string
+ projectRef?: string
categoryKey: CategoryMetaKey
subscription?: OrgSubscription
chartMeta: ChartMeta
@@ -20,6 +21,7 @@ export interface UsageSectionProps {
const UsageSection = ({
orgSlug,
+ projectRef,
categoryKey,
chartMeta,
subscription,
diff --git a/studio/data/__templates/README.md b/studio/data/__templates/README.md
index 174a86fc988..b16309d2788 100644
--- a/studio/data/__templates/README.md
+++ b/studio/data/__templates/README.md
@@ -54,7 +54,7 @@ The blog posts in particular are very helpful for learning best practices and ho
### Queries
-The 3 states that are provided in a query (`isLoading`, `isError`, `isSuccess`) **must** be covered in the UI whenever we're using it. Typically, we'd use the following UI components to cover them, and the states are mutually exclusive so it's preferred not to nest them in a ternary operator for better readability.
+The 3 states that are provided in a query (`isLoading`, `isError`, `isSuccess`) **must** be covered in the UI whenever we're using it (unless reasonably unrequired). Typically, we'd use the following UI components to cover them, and the states are mutually exclusive so it's preferred not to nest them in a ternary operator for better readability.
```jsx
const { data, error, isLoading, isError, isSuccess } = useQuery()