The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
refactor new homepage usage (#42394)
Jordi Enricf237b7bf2e44
diff --git a/apps/studio/components/interfaces/HomeNew/ProjectUsageSection.tsx b/apps/studio/components/interfaces/HomeNew/ProjectUsageSection.tsxindex 219108b4905..fae2ba99123 100644--- a/apps/studio/components/interfaces/HomeNew/ProjectUsageSection.tsx+++ b/apps/studio/components/interfaces/HomeNew/ProjectUsageSection.tsx@@ -2,8 +2,14 @@ import { useParams } from 'common'import NoDataPlaceholder from 'components/ui/Charts/NoDataPlaceholder'import { ChartIntervalDropdown } from 'components/ui/Logs/ChartIntervalDropdown'import { CHART_INTERVALS } from 'components/ui/Logs/logs.utils'+import {+ ProjectLogStatsVariables,+ UsageApiCounts,+ useProjectLogStatsQuery,+} from 'data/analytics/project-log-stats-query'import { useSendEventMutation } from 'data/telemetry/send-event-mutation'import dayjs from 'dayjs'+import { useFillTimeseriesSorted } from 'hooks/analytics/useFillTimeseriesSorted'import { useCurrentOrgPlan } from 'hooks/misc/useCurrentOrgPlan'import { useIsFeatureEnabled } from 'hooks/misc/useIsFeatureEnabled'import { useSelectedOrganizationQuery } from 'hooks/misc/useSelectedOrganization'@@ -14,19 +20,16 @@ import { Card, CardContent, CardHeader, CardTitle, Loading } from 'ui'import { Row } from 'ui-patterns'import { LogsBarChart } from 'ui-patterns/LogsBarChart'-import { useServiceHealthMetrics } from '../Observability/useServiceHealthMetrics'-import { normalizeChartBuckets } from './ChartDataTransform.utils'-import type { LogsBarChartDatum } from './ProjectUsage.metrics'-import {- computeSuccessAndNonSuccessRates,- sumErrors,- sumTotal,- sumWarnings,-} from './ProjectUsage.metrics'+type LogsBarChartDatum = {+ timestamp: string+ error_count: number+ ok_count: number+ warning_count: number+}type ChartIntervalKey = '1hr' | '1day' | '7day'-type ServiceKey = 'db' | 'functions' | 'auth' | 'storage' | 'realtime'+type ServiceKey = 'db' | 'auth' | 'storage' | 'realtime'type ServiceEntry = {key: ServiceKey@@ -39,8 +42,6 @@ type ServiceEntry = {type ServiceComputed = ServiceEntry & {data: LogsBarChartDatum[]total: number- warn: number- err: numberisLoading: booleanerror: unknown | null}@@ -58,7 +59,6 @@ export const ProjectUsageSection = () => {const DEFAULT_INTERVAL: ChartIntervalKey = plan?.id === 'free' ? '1hr' : '1day'const [interval, setInterval] = useState<ChartIntervalKey>(DEFAULT_INTERVAL)- const [refreshKey, setRefreshKey] = useState(0)const selectedInterval = CHART_INTERVALS.find((i) => i.key === interval) || CHART_INTERVALS[1]@@ -67,28 +67,44 @@ export const ProjectUsageSection = () => {return { datetimeFormat: format }}, [selectedInterval])- const {- services: healthServices,- isLoading: isHealthLoading,- endDate,- } = useServiceHealthMetrics(projectRef!, interval, refreshKey)+ // Use V1 data fetching+ const { data: logStatsData, isPending: isLoading } = useProjectLogStatsQuery({+ projectRef,+ interval,+ })++ // Calculate date range for gap filling+ const startDateLocal = dayjs().subtract(+ selectedInterval.startValue,+ selectedInterval.startUnit as dayjs.ManipulateType+ )+ const endDateLocal = dayjs()++ // Fill gaps in timeseries data+ const { data: filledCharts } = useFillTimeseriesSorted({+ data: logStatsData?.result ?? [],+ timestampKey: 'timestamp',+ valueKey: [+ 'total_auth_requests',+ 'total_rest_requests',+ 'total_storage_requests',+ 'total_realtime_requests',+ ],+ defaultValue: 0,+ startDate: startDateLocal.toISOString(),+ endDate: endDateLocal.toISOString(),+ minPointsToFill: 5,+ })const serviceBase: ServiceEntry[] = useMemo(() => [{key: 'db',title: 'Database requests',-href: `/project/${projectRef}/editor`,route: '/logs/postgres-logs',enabled: true,},- {- key: 'functions',- title: 'Functions requests',- route: '/logs/edge-functions-logs',- enabled: true,- },{key: 'auth',title: 'Auth requests',@@ -116,31 +132,39 @@ export const ProjectUsageSection = () => {const services: ServiceComputed[] = useMemo(() =>serviceBase.map((s) => {- const healthData = healthServices[s.key]- // Normalize chart data to consistent bucket sizes using the same endDate from the query- const normalizedData = normalizeChartBuckets(- healthData.eventChartData,- interval,- new Date(endDate)- )- const total = sumTotal(normalizedData)- const warn = sumWarnings(normalizedData)- const err = sumErrors(normalizedData)+ // Map service keys to V1 data field names+ const dataKeyMap: Record<ServiceKey, keyof UsageApiCounts> = {+ db: 'total_rest_requests',+ auth: 'total_auth_requests',+ storage: 'total_storage_requests',+ realtime: 'total_realtime_requests',+ }++ const dataKey = dataKeyMap[s.key]++ // Transform V1 data to LogsBarChart format+ // Since V1 doesn't have error/warning breakdown, we show everything as "ok"+ const transformedData: LogsBarChartDatum[] = (filledCharts || []).map((item) => ({+ timestamp: item.timestamp,+ error_count: 0,+ warning_count: 0,+ ok_count: Number(item[dataKey]) || 0,+ }))++ // Calculate total from filled data+ const total = transformedData.reduce((sum, item) => sum + item.ok_count, 0)+return {...s,- data: normalizedData,+ data: transformedData,total,- warn,- err,- isLoading: healthData.isLoading,- error: healthData.error,+ isLoading,+ error: null,}}),- [serviceBase, healthServices, interval, endDate]+ [serviceBase, filledCharts, isLoading])- const isLoading = isHealthLoading-const handleBarClick =(logRoute: string, serviceKey: ServiceKey) => (datum: LogsBarChartDatum) => {if (!datum?.timestamp) return@@ -173,28 +197,13 @@ export const ProjectUsageSection = () => {const enabledServices = services.filter((s) => s.enabled)const totalRequests = enabledServices.reduce((sum, s) => sum + (s.total || 0), 0)- const totalErrors = enabledServices.reduce((sum, s) => sum + (s.err || 0), 0)- const totalWarnings = enabledServices.reduce((sum, s) => sum + (s.warn || 0), 0)- const { successRate } = computeSuccessAndNonSuccessRates(- totalRequests,- totalWarnings,- totalErrors- )return (<div className="space-y-6"><div className="flex flex-row justify-between items-center gap-x-2">- <div className="flex flex-col md:flex-row md:items-center md:gap-6">- <div className="flex items-start gap-2 heading-section text-foreground-light">- <span className="text-foreground">{totalRequests.toLocaleString()}</span>- <span>Total Requests</span>- </div>- <div className="flex items-start gap-2 heading-section text-foreground-light">- <span className="text-foreground">- {successRate === 100 ? '100' : successRate.toFixed(1)}%- </span>- <span>Success Rate</span>- </div>+ <div className="flex items-start gap-2 heading-section text-foreground-light">+ <span className="text-foreground">{totalRequests.toLocaleString()}</span>+ <span>Total Requests</span></div><ChartIntervalDropdownvalue={interval}@@ -212,54 +221,35 @@ export const ProjectUsageSection = () => {<CardHeader className="flex flex-row items-end justify-between gap-2 space-y-0 pb-0 border-b-0"><div className="flex items-center gap-2"><div className="flex flex-col">- <div className="flex items-center gap-2">- <CardTitle className="text-foreground-light">- {s.href ? (- <Link- href={s.href}- onClick={() => {- if (projectRef && organization?.slug) {- sendEvent({- action: 'home_project_usage_service_clicked',- properties: {- service_type: s.key,- total_requests: s.total || 0,- error_count: s.err || 0,- },- groups: {- project: projectRef,- organization: organization.slug,- },- })- }- }}- >- {s.title}- </Link>- ) : (- s.title- )}- </CardTitle>- </div>+ <CardTitle className="text-xs font-mono uppercase text-foreground-light">+ {s.href ? (+ <Link+ href={s.href}+ onClick={() => {+ if (projectRef && organization?.slug) {+ sendEvent({+ action: 'home_project_usage_service_clicked',+ properties: {+ service_type: s.key,+ total_requests: s.total || 0,+ },+ groups: {+ project: projectRef,+ organization: organization.slug,+ },+ })+ }+ }}+ >+ {s.title}+ </Link>+ ) : (+ s.title+ )}+ </CardTitle><span className="text-foreground text-xl">{(s.total || 0).toLocaleString()}</span></div></div>- <div className="flex items-end gap-4 text-foreground-light">- <div className="flex flex-col items-end">- <div className="flex items-center gap-2">- <div className="w-1.5 h-1.5 bg-warning rounded-full" />- <span className="heading-meta">Warn</span>- </div>- <span className="text-foreground text-xl">{(s.warn || 0).toLocaleString()}</span>- </div>- <div className="flex flex-col items-end">- <div className="flex items-center gap-2">- <div className="w-1.5 h-1.5 bg-destructive rounded-full" />- <span className="heading-meta">Err</span>- </div>- <span className="text-foreground text-xl">{(s.err || 0).toLocaleString()}</span>- </div>- </div></CardHeader><CardContent className="p-6 pt-4 flex-1 h-full overflow-hidden"><Loading isFullHeight active={isLoading}>@@ -268,6 +258,18 @@ export const ProjectUsageSection = () => {data={s.data}DateTimeFormat={datetimeFormat}onBarClick={handleBarClick(s.route, s.key)}+ hideZeroValues={true}+ chartConfig={{+ error_count: {+ label: 'Errors',+ },+ warning_count: {+ label: 'Warnings',+ },+ ok_count: {+ label: 'Requests',+ },+ }}EmptyState={<NoDataPlaceholdersize="small"diff --git a/apps/studio/components/ui/Charts/BarChart.tsx b/apps/studio/components/ui/Charts/BarChart.tsxindex 99fbde86426..1510920af9f 100644--- a/apps/studio/components/ui/Charts/BarChart.tsx+++ b/apps/studio/components/ui/Charts/BarChart.tsx@@ -1,3 +1,4 @@+import { CHART_COLORS, DateTimeFormats } from 'components/ui/Charts/Charts.constants'import dayjs from 'dayjs'import { ComponentProps, useMemo, useState } from 'react'import {@@ -10,9 +11,8 @@ import {XAxis,YAxis,} from 'recharts'--import { CHART_COLORS, DateTimeFormats } from 'components/ui/Charts/Charts.constants'import type { CategoricalChartState } from 'recharts/types/chart/types'+import { ChartHeader } from './ChartHeader'import type { CommonChartProps, Datum } from './Charts.types'import { numberFormatter, useChartSize } from './Charts.utils'@@ -24,7 +24,7 @@ export interface BarChartProps<D = Datum> extends CommonChartProps<D> {xAxisKey: stringcustomDateFormat?: stringdisplayDateInUtc?: boolean- onBarClick?: (datum: Datum, tooltipData?: CategoricalChartState) => void+ onBarClick?: (datum: D, tooltipData?: CategoricalChartState) => voidemptyStateMessage?: stringshowLegend?: booleanxAxisIsDate?: boolean@@ -34,7 +34,7 @@ export interface BarChartProps<D = Datum> extends CommonChartProps<D> {syncId?: string}-const BarChart = ({+function BarChart<D extends Datum = Datum>({data,yAxisKey,xAxisKey,@@ -56,7 +56,7 @@ const BarChart = ({YAxisProps,showGrid = false,syncId,-}: BarChartProps) => {+}: BarChartProps<D>) {const { hoveredIndex, isHovered, isCurrentChart, setHover, clearHover } =useChartHoverState('default')const { Container } = useChartSize(size)@@ -191,7 +191,7 @@ const BarChart = ({animationDuration={300}maxBarSize={48}>- {data?.map((_entry: Datum, index: number) => (+ {data?.map((_entry: D, index: number) => (<Cellkey={`cell-${index}`}className={`transition-all duration-300 ${onBarClick ? 'cursor-pointer' : ''}`}@@ -207,7 +207,7 @@ const BarChart = ({</RechartBarChart></Container>{data && (- <div className="text-foreground-lighter -mt-9 flex items-center justify-between text-xs">+ <div className="text-foreground-lighter -mt-10 flex items-center justify-between text-[10px] font-mono"><span>{xAxisIsDate ? day(data[0][xAxisKey]).format(customDateFormat) : data[0][xAxisKey]}</span>diff --git a/packages/common/telemetry-constants.ts b/packages/common/telemetry-constants.tsindex 46c8f39fdff..46c17648a60 100644--- a/packages/common/telemetry-constants.ts+++ b/packages/common/telemetry-constants.ts@@ -1769,9 +1769,9 @@ export interface HomeProjectUsageServiceClickedEvent {*/total_requests: number/**- * Number of errors for this service+ * Number of errors for this service (optional, only sent when error data is available)*/- error_count: number+ error_count?: number}groups: TelemetryGroups}diff --git a/packages/ui-patterns/src/LogsBarChart/index.tsx b/packages/ui-patterns/src/LogsBarChart/index.tsxindex e540df49b44..b9cde134772 100644--- a/packages/ui-patterns/src/LogsBarChart/index.tsx+++ b/packages/ui-patterns/src/LogsBarChart/index.tsx@@ -30,12 +30,16 @@ export const LogsBarChart = ({EmptyState,DateTimeFormat = 'MMM D, YYYY, hh:mma',isFullHeight = false,+ chartConfig,+ hideZeroValues = false,}: {data: LogsBarChartDatum[]onBarClick?: (datum: LogsBarChartDatum, tooltipData?: CategoricalChartState) => voidEmptyState?: ReactNodeDateTimeFormat?: stringisFullHeight?: boolean+ chartConfig?: ChartConfig+ hideZeroValues?: boolean}) => {const [focusDataIndex, setFocusDataIndex] = useState<number | null>(null)@@ -47,27 +51,24 @@ export const LogsBarChart = ({const startDate = dayjs(data[0]['timestamp']).format(DateTimeFormat)const endDate = dayjs(data[data?.length - 1]?.['timestamp']).format(DateTimeFormat)+ const defaultChartConfig = {+ error_count: {+ label: 'Errors',+ },+ ok_count: {+ label: 'Ok',+ },+ warning_count: {+ label: 'Warnings',+ },+ } satisfies ChartConfig+return (<divdata-testid="logs-bar-chart"className={cn('flex flex-col gap-y-3', isFullHeight ? 'h-full' : 'h-24')}>- <ChartContainer- className="h-full"- config={- {- error_count: {- label: 'Errors',- },- ok_count: {- label: 'Ok',- },- warning_count: {- label: 'Warnings',- },- } satisfies ChartConfig- }- >+ <ChartContainer className="h-full" config={chartConfig ?? defaultChartConfig}><RechartBarChartdata={data}onMouseMove={(e: any) => {@@ -95,12 +96,33 @@ export const LogsBarChart = ({tickLine={{ stroke: CHART_COLORS.AXIS }}/><ChartTooltip- content={- <ChartTooltipContent- className="text-foreground-light -mt-5"- labelFormatter={(v: string) => dayjs(v).format(DateTimeFormat)}- />- }+ animationDuration={0}+ position={{ y: 80 }}+ content={(props) => {+ if (!props.active || !props.payload || props.payload.length === 0) {+ return null+ }++ // Filter payload based on hideZeroValues+ const filteredPayload = hideZeroValues+ ? props.payload.filter((item) => Number(item.value) !== 0)+ : props.payload++ // Don't show tooltip if all values are filtered out+ if (filteredPayload.length === 0) {+ return null+ }++ return (+ <ChartTooltipContent+ active={props.active}+ payload={filteredPayload}+ label={props.label}+ className="text-foreground-light -mt-5 !transition-none"+ labelFormatter={(v: string) => dayjs(v).format(DateTimeFormat)}+ />+ )+ }}/>{/* Error bars */}