Skip to content

Commit dfacfd5

Browse files
authored
Reports clean ups (supabase#37248)
* use similar ui styles for different charts * fix naming * use similar styles, fix height * rm year from tooltip timestamp * remove unnecessary code in useChartData
1 parent 5e15856 commit dfacfd5

File tree

9 files changed

+23
-139
lines changed

9 files changed

+23
-139
lines changed

apps/studio/components/interfaces/Reports/ReportChart.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ const ReportChart = ({
4949
const {
5050
data,
5151
isLoading: isLoadingChart,
52-
chartAttributes,
5352
highlightedValue,
5453
} = useChartData({
5554
attributes: chart.attributes,
@@ -72,9 +71,7 @@ const ReportChart = ({
7271
const { data: filledData, isError: isFillError } = useFillTimeseriesSorted(
7372
chartDataArray,
7473
'period_start',
75-
(chartAttributes.length > 0 ? chartAttributes : chart.attributes).map(
76-
(attr: any) => attr.attribute
77-
),
74+
chart.attributes.map((attr: any) => attr.attribute),
7875
0,
7976
startDate,
8077
endDate,
@@ -159,9 +156,7 @@ const ReportChart = ({
159156
return (
160157
<LogChartHandler
161158
{...chart}
162-
attributes={
163-
(chartAttributes.length > 0 ? chartAttributes : chart.attributes) as MultiAttribute[]
164-
}
159+
attributes={chart.attributes as MultiAttribute[]}
165160
data={finalData}
166161
isLoading={isLoadingChart || isLoading}
167162
highlightedValue={highlightedValue as any}

apps/studio/components/ui/Charts/Charts.constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ export const DEFAULT_STACK_COLORS: ValidStackColor[] = [
8282

8383
export enum DateTimeFormats {
8484
FULL = 'MMM D, YYYY, hh:mma',
85-
FULL_SECONDS = 'MMM D, YYYY, hh:mm:ssa',
85+
FULL_SECONDS = 'MMM D, hh:mm:ssa',
8686
DATE_ONLY = 'MMM D, YYYY',
8787
}

apps/studio/components/ui/Charts/ComposedChartHandler.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ const ComposedChartHandler = ({
238238
return (
239239
<Panel
240240
className={cn(
241-
'flex min-h-[320px] w-full flex-col items-center justify-center gap-y-2',
241+
'flex min-h-[280px] w-full flex-col items-center justify-center gap-y-2',
242242
className
243243
)}
244244
wrapWithLoading={false}

apps/studio/components/ui/Charts/LogChartHandler.tsx

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,6 @@ const LogChartHandler = ({
112112
const [chartStyle, setChartStyle] = useState<string>(defaultChartStyle)
113113
const chartHighlight = useChartHighlight()
114114

115-
if (isLoading) {
116-
return (
117-
<Panel
118-
className={cn(
119-
'flex min-h-[280px] w-full flex-col items-center justify-center gap-y-2',
120-
className
121-
)}
122-
wrapWithLoading={false}
123-
noMargin
124-
noHideOverflow
125-
>
126-
<Loader2 size={18} className="animate-spin text-border-strong" />
127-
<p className="text-xs text-foreground-lighter">Loading data for {label}</p>
128-
</Panel>
129-
)
130-
}
131-
132115
if (!data) {
133116
return (
134117
<div className="flex h-52 w-full flex-col items-center justify-center gap-y-2">
@@ -143,10 +126,16 @@ const LogChartHandler = ({
143126
<Panel
144127
noMargin
145128
noHideOverflow
146-
className={cn('relative w-full scroll-mt-16', className)}
129+
className={cn('relative w-full overflow-hidden scroll-mt-16', className)}
147130
wrapWithLoading={false}
148131
id={id ?? label.toLowerCase().replaceAll(' ', '-')}
149132
>
133+
{isLoading && (
134+
<div className="absolute inset-0 rounded-md flex w-full flex-col items-center justify-center gap-y-2 bg-surface-100 backdrop z-20">
135+
<Loader2 size={18} className="animate-spin text-border-strong" />
136+
<p className="text-xs text-foreground-lighter">Loading data for {label}</p>
137+
</div>
138+
)}
150139
<Panel.Content className="flex flex-col gap-4">
151140
<div className="absolute right-6 z-50 flex justify-between scroll-mt-16">{children}</div>
152141
<ComposedChart
@@ -186,12 +175,8 @@ export const useAttributeQueries = (
186175
data: ChartData | undefined,
187176
isVisible: boolean
188177
) => {
189-
const projectRef = typeof ref === 'string' ? ref : Array.isArray(ref) ? ref[0] : ''
190-
191178
const infraAttributes = attributes.filter((attr) => attr.provider === 'infra-monitoring')
192179
const dailyStatsAttributes = attributes.filter((attr) => attr.provider === 'daily-stats')
193-
const mockAttributes = attributes.filter((attr) => attr.provider === 'mock')
194-
const referenceLineAttributes = attributes.filter((attr) => attr.provider === 'reference-line')
195180

196181
const infraQueries = useInfraMonitoringQueries(
197182
infraAttributes.map((attr) => attr.attribute as InfraMonitoringAttribute),

apps/studio/data/reports/auth-report-query.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ export function useAuthLogsReport({
307307
data: rawData,
308308
error,
309309
isLoading,
310+
isFetching,
310311
} = useQuery(
311312
['auth-logs-report', projectRef, logsMetric, startDate, endDate, interval, sql],
312313
async () => {
@@ -339,5 +340,6 @@ export function useAuthLogsReport({
339340
attributes: chartAttributes,
340341
isLoading,
341342
error,
343+
isFetching,
342344
}
343345
}

apps/studio/hooks/useChartData.ts

Lines changed: 4 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@
1111
import { useMemo } from 'react'
1212
import { useRouter } from 'next/router'
1313

14-
import { useDatabaseSelectorStateSnapshot } from 'state/database-selector'
1514
import type { AnalyticsInterval, DataPoint } from 'data/analytics/constants'
1615
import { useAuthLogsReport } from 'data/reports/auth-report-query'
1716
import type { ChartData } from 'components/ui/Charts/Charts.types'
1817
import type { MultiAttribute } from 'components/ui/Charts/ComposedChart.utils'
19-
import { useAttributeQueries } from 'components/ui/Charts/LogChartHandler'
2018
import { useEdgeFunctionReport } from 'data/reports/edgefn-query'
21-
import { useInfraMonitoringQuery } from 'data/analytics/infra-monitoring-query'
22-
import type { InfraMonitoringAttribute } from 'data/analytics/infra-monitoring-query'
2319

2420
export const useChartData = ({
2521
attributes,
@@ -42,10 +38,8 @@ export const useChartData = ({
4238
}) => {
4339
const router = useRouter()
4440
const { ref } = router.query
45-
const state = useDatabaseSelectorStateSnapshot()
4641

4742
const logsAttributes = attributes.filter((attr) => attr.provider === 'logs')
48-
const nonLogsAttributes = attributes.filter((attr) => attr.provider !== 'logs')
4943

5044
const isEdgeFunctionRoute = router.asPath.includes('/reports/edge-functions')
5145

@@ -76,66 +70,15 @@ export const useChartData = ({
7670
functionIds,
7771
})
7872

79-
const {
80-
data: infraData,
81-
error: infraError,
82-
isLoading: isInfraLoading,
83-
} = useInfraMonitoringQuery(
84-
{
85-
projectRef: ref as string,
86-
attribute: nonLogsAttributes[0]?.attribute as InfraMonitoringAttribute,
87-
interval: interval as AnalyticsInterval,
88-
startDate,
89-
endDate,
90-
databaseIdentifier: state.selectedDatabaseId,
91-
},
92-
{ enabled: enabled && nonLogsAttributes.length > 0 }
93-
)
94-
9573
const logsData = isEdgeFunctionRoute ? edgeFunctionData : authData
9674
const logsChartAttributes = isEdgeFunctionRoute
9775
? edgeFunctionChartAttributes
9876
: authChartAttributes
9977
const isLogsLoading = isEdgeFunctionRoute ? isEdgeFunctionLoading : isAuthLoading
10078

101-
const chartAttributes = useMemo(
102-
() => nonLogsAttributes.concat(logsChartAttributes || []),
103-
[nonLogsAttributes, logsChartAttributes]
104-
)
105-
106-
const databaseIdentifier = state.selectedDatabaseId
107-
108-
// Use the custom hook at the top level of the component
109-
const attributeQueries = useAttributeQueries(
110-
attributes,
111-
ref,
112-
startDate,
113-
endDate,
114-
interval as AnalyticsInterval,
115-
databaseIdentifier,
116-
data,
117-
true
118-
)
119-
120-
// Combine all the data into a single dataset
12179
const combinedData = useMemo(() => {
12280
if (data) return data
12381

124-
const regularAttributeQueries = attributeQueries.filter(
125-
(q) => q.data?.provider !== 'logs' && q.data?.provider !== 'reference-line'
126-
)
127-
const isLoading =
128-
(logsAttributes.length > 0 && isLogsLoading) ||
129-
regularAttributeQueries.some((query: any) => query.isLoading)
130-
if (isLoading) {
131-
return undefined
132-
}
133-
134-
const hasError = regularAttributeQueries.some((query: any) => !query.data)
135-
if (hasError) {
136-
return undefined
137-
}
138-
13982
// Get all unique timestamps from all datasets
14083
const timestamps = new Set<string>()
14184
if (logsData) {
@@ -145,17 +88,6 @@ export const useChartData = ({
14588
}
14689
})
14790
}
148-
regularAttributeQueries.forEach((query: any) => {
149-
query.data?.data?.forEach((point: any) => {
150-
if (point?.period_start) {
151-
timestamps.add(point.period_start)
152-
}
153-
})
154-
})
155-
156-
const referenceLineQueries = attributeQueries.filter(
157-
(q) => q.data?.provider === 'reference-line'
158-
)
15991

16092
// Combine data points for each timestamp
16193
const combined = Array.from(timestamps)
@@ -166,44 +98,20 @@ export const useChartData = ({
16698
const logPoint = logsData?.find((p: any) => p.period_start === timestamp) || {}
16799
Object.assign(point, logPoint)
168100

169-
chartAttributes.forEach((attr) => {
170-
if (!attr) return
171-
if (attr.provider === 'logs') return
172-
if (attr.provider === 'reference-line') return
173-
if (attr.customValue !== undefined) {
174-
point[attr.attribute] = attr.customValue
175-
return
176-
}
177-
178-
const query = regularAttributeQueries.find((q) => q.data?.attribute === attr.attribute)
179-
const matchingPoint = query?.data?.data?.find((p: any) => p.period_start === timestamp)
180-
point[attr.attribute] = matchingPoint?.[attr.attribute] ?? 0
181-
})
182-
183-
// Add reference line values for each timestamp
184-
referenceLineQueries.forEach((query: any) => {
185-
const attr = query.data.attribute
186-
const value = query.data.total
187-
point[attr] = value
188-
})
189-
190101
return point as DataPoint
191102
})
192103

193104
return combined as DataPoint[]
194-
}, [data, attributeQueries, attributes, chartAttributes, isLogsLoading, logsData, logsAttributes])
105+
}, [data, attributes, isLogsLoading, logsData, logsAttributes])
195106

196-
const loading =
197-
(logsAttributes.length > 0 && isLogsLoading) ||
198-
attributeQueries.some((query: any) => query.isLoading)
107+
const loading = logsAttributes.length > 0 && isLogsLoading
199108

200109
// Calculate highlighted value based on the first attribute's data
201110
const _highlightedValue = useMemo(() => {
202111
if (highlightedValue !== undefined) return highlightedValue
203112

204113
const firstAttr = attributes[0]
205-
const firstQuery = attributeQueries[0]
206-
const firstData = firstQuery?.data
114+
const firstData = logsChartAttributes.find((p: any) => p.attribute === firstAttr.attribute)
207115

208116
if (!firstData) return undefined
209117

@@ -222,12 +130,11 @@ export const useChartData = ({
222130
: shouldHighlightTotalGroupedValue
223131
? firstData.totalGrouped?.[firstAttr.attribute as keyof typeof firstData.totalGrouped]
224132
: (firstData.data?.[firstData.data?.length - 1] as any)?.[firstAttr.attribute]
225-
}, [highlightedValue, attributes, attributeQueries])
133+
}, [highlightedValue, attributes])
226134

227135
return {
228136
data: combinedData,
229137
isLoading: loading,
230-
chartAttributes,
231138
highlightedValue: _highlightedValue,
232139
}
233140
}

apps/studio/pages/project/[ref]/reports/auth.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,7 @@ const AuthUsage = () => {
8282
setIsRefreshing(true)
8383
AUTH_REPORT_ATTRIBUTES.forEach((attr) => {
8484
attr.attributes.forEach((subAttr) => {
85-
queryClient.invalidateQueries([
86-
'auth-metrics',
87-
ref,
88-
subAttr.attribute,
89-
selectedDateRange.period_start.date,
90-
selectedDateRange.period_end.date,
91-
selectedDateRange.interval,
92-
])
85+
queryClient.invalidateQueries(['auth-logs-report', 'auth-metrics'])
9386
})
9487
})
9588
refetch()
@@ -150,6 +143,7 @@ const AuthUsage = () => {
150143
endDate={selectedDateRange?.period_end?.date}
151144
updateDateRange={updateDateRange}
152145
orgPlanId={orgPlan?.id}
146+
isLoading={isRefreshing}
153147
/>
154148
))}
155149
<div>

packages/ui/src/components/Loading/Loading.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react'
2-
import { Loader } from 'lucide-react'
2+
import { Loader2 } from 'lucide-react'
33

44
import styleHandler from '../../lib/theme/styleHandler'
55

@@ -23,7 +23,7 @@ export default function Loading({ children, active }: Props) {
2323
return (
2424
<div className={classNames.join(' ')}>
2525
<div className={contentClasses.join(' ')}>{children}</div>
26-
{active && <Loader size={24} className={spinnerClasses.join(' ')} />}
26+
{active && <Loader2 size={24} className={spinnerClasses.join(' ')} />}
2727
</div>
2828
)
2929
}

packages/ui/src/lib/theme/defaultTheme.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1417,8 +1417,9 @@ export default {
14171417
},
14181418
spinner: `
14191419
absolute
1420-
text-brand animate-spin
1420+
text-foreground-lighter animate-spin
14211421
inset-0
1422+
size-5
14221423
m-auto
14231424
`,
14241425
},

0 commit comments

Comments
 (0)