Skip to content

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

feat: add cumulative users chart

TzeYiing2a7c7871e6fe
diff --git a/studio/components/interfaces/Reports/ReportWidget.tsx b/studio/components/interfaces/Reports/ReportWidget.tsx
index 5e66d717734..84d54267af2 100644
--- a/studio/components/interfaces/Reports/ReportWidget.tsx
+++ b/studio/components/interfaces/Reports/ReportWidget.tsx
@@ -2,6 +2,7 @@ import { Button, Dropdown, IconMoreVertical } from 'ui'
import LoadingOpacity from 'components/ui/LoadingOpacity'
import Panel from 'components/ui/Panel'
import { useRouter } from 'next/router'
+import { BaseReportParams } from './Reports.types'
import { LogsEndpointParams } from '../Settings/Logs'
export interface ReportWidgetProps<T = any> {
@@ -10,7 +11,7 @@ export interface ReportWidgetProps<T = any> {
description: string
className?: string
renderer: (props: ReportWidgetProps) => React.ReactNode
- params: LogsEndpointParams
+ params: BaseReportParams | LogsEndpointParams
isLoading: boolean
}
@@ -34,7 +35,7 @@ const ReportWidget: React.FC<ReportWidgetProps> = (props) => {
router.push({
pathname: `/project/${ref}/logs/explorer`,
query: {
- q: props.params.sql,
+ q: props.params?.sql,
its: props.params.iso_timestamp_start,
ite: props.params.iso_timestamp_end,
},
diff --git a/studio/components/interfaces/Reports/Reports.constants.ts b/studio/components/interfaces/Reports/Reports.constants.ts
index 2f99d265a3d..797ee40d311 100644
--- a/studio/components/interfaces/Reports/Reports.constants.ts
+++ b/studio/components/interfaces/Reports/Reports.constants.ts
@@ -177,6 +177,20 @@ order by
timestamp
`,
},
+ cumulativeUsers: {
+ queryType: 'db',
+ sql: `
+select
+date_trunc('day', u.created_at) as timestamp,
+count(u.id) as count
+from auth.users as u
+where u.confirmed_at is not null
+group by
+ timestamp
+order by
+ timestamp desc
+ `,
+ },
newUsers: {
queryType: 'logs',
sql: `
diff --git a/studio/components/interfaces/Reports/Reports.types.ts b/studio/components/interfaces/Reports/Reports.types.ts
index f86e32ef143..419303e66af 100644
--- a/studio/components/interfaces/Reports/Reports.types.ts
+++ b/studio/components/interfaces/Reports/Reports.types.ts
@@ -1,13 +1,30 @@
+import { ResponseError } from 'types'
+import { LogsEndpointParams } from '../Settings/Logs'
+import { DEFAULT_QUERY_PARAMS } from './Reports.constants'
+
export enum Presets {
API_OVERVIEW = 'overview',
API_BOTS = 'api_bots',
AUTH = 'auth',
}
-export interface BaseReportParams {
- iso_timestamp_start: string
- iso_timestamp_end: string
+export interface QueryDataBase {
+ isLoading: boolean
+ error: string
+}
+export interface DbQueryData<T = any> extends QueryDataBase {
+ data: T[]
+ params: BaseReportParams
+ logData?: never
}
+export interface DbQueryHandler {
+ runQuery: () => void
+ setParams?: never
+ changeQuery?: never
+}
+export type MetaQueryResponse = any & { error: ResponseError }
+
+export type BaseReportParams = typeof DEFAULT_QUERY_PARAMS & { sql?: string } & unknown
export type VariableSql = string | ((params: BaseReportParams) => string)
diff --git a/studio/components/interfaces/Reports/Reports.utils.tsx b/studio/components/interfaces/Reports/Reports.utils.tsx
index 1f624714b2c..4d07a2686ee 100644
--- a/studio/components/interfaces/Reports/Reports.utils.tsx
+++ b/studio/components/interfaces/Reports/Reports.utils.tsx
@@ -1,12 +1,12 @@
import { IconRefreshCw } from '@supabase/ui'
-import useDbQuery, { DbQueryData, DbQueryHandler } from 'hooks/analytics/useDbQuery'
+import useDbQuery from 'hooks/analytics/useDbQuery'
import useLogsQuery, { LogsQueryData, LogsQueryHandlers } from 'hooks/analytics/useLogsQuery'
import React from 'react'
import { Button } from 'ui'
import { DatePickerToFrom } from '../Settings/Logs'
import DatePickers from '../Settings/Logs/Logs.DatePickers'
import { DEFAULT_QUERY_PARAMS, REPORTS_DATEPICKER_HELPERS } from './Reports.constants'
-import { PresetConfig } from './Reports.types'
+import { DbQueryData, DbQueryHandler, PresetConfig } from './Reports.types'
/**
* Converts a query params string to an object
@@ -16,7 +16,7 @@ export const queryParamsToObject = (params: string) => {
}
// generate hooks based on preset config
-type PresetHookResult = [LogsQueryData, LogsQueryHandlers]
+type PresetHookResult = [LogsQueryData | DbQueryData, LogsQueryHandlers | DbQueryHandler]
type PresetHooks = Record<keyof PresetConfig['queries'], () => PresetHookResult>
export const hooksFactory = (projectRef: string, config: PresetConfig) => {
const hooks: PresetHooks = Object.entries(config.queries).reduce(
diff --git a/studio/components/interfaces/Reports/renderers/AuthRenderers.tsx b/studio/components/interfaces/Reports/renderers/AuthRenderers.tsx
index c6a05709fe9..f719a91f1da 100644
--- a/studio/components/interfaces/Reports/renderers/AuthRenderers.tsx
+++ b/studio/components/interfaces/Reports/renderers/AuthRenderers.tsx
@@ -1,7 +1,28 @@
import { BarChart } from 'components/to-be-cleaned/Charts/ChartRenderer'
+import { first } from 'lodash'
import { DATETIME_FORMAT } from '../Reports.constants'
import { ReportWidgetProps } from '../ReportWidget'
+export const renderCumulativeUsers = (
+ props: ReportWidgetProps<{
+ count: number
+ timestamp: string
+ }>
+) => {
+ const total = props.data?.[0]?.count
+ return (
+ <div className="flex w-full flex-col">
+ <BarChart
+ data={props.data}
+ attribute="count"
+ label="Total users"
+ highlightedValue={total}
+ customDateFormat={DATETIME_FORMAT}
+ noDataMessage={'No confirmed users yet'}
+ />
+ </div>
+ )
+}
export const renderDailyActiveUsers = (
props: ReportWidgetProps<{
count: number
diff --git a/studio/hooks/analytics/useDbQuery.tsx b/studio/hooks/analytics/useDbQuery.tsx
index 8bd728de66f..979f30a1e5d 100644
--- a/studio/hooks/analytics/useDbQuery.tsx
+++ b/studio/hooks/analytics/useDbQuery.tsx
@@ -1,37 +1,33 @@
import { DEFAULT_QUERY_PARAMS } from 'components/interfaces/Reports/Reports.constants'
-import { BaseReportParams, PresetSql } from 'components/interfaces/Reports/Reports.types'
+import {
+ BaseReportParams,
+ DbQueryData,
+ DbQueryHandler,
+ MetaQueryResponse,
+ PresetSql,
+} from 'components/interfaces/Reports/Reports.types'
import { useStore } from 'hooks'
import useSWR from 'swr'
-import { ResponseError } from 'types'
-type QueryResponse = any | { error: ResponseError }
-export interface DbQueryData<T = unknown[]> {
- data: T
- params?: never
- logData?: never
- isLoading: boolean
- error: string
-}
-export interface DbQueryHandler {
- runQuery: () => void
- setParams?: never
- changeQuery?: never
-}
-type UseDbQuery<T = unknown[]> = (sql: PresetSql, params?: BaseReportParams) => [DbQueryData<T>, DbQueryHandler]
+type UseDbQuery = (sql: PresetSql, params?: BaseReportParams) => [DbQueryData, DbQueryHandler]
const useDbQuery: UseDbQuery = (sql, params = DEFAULT_QUERY_PARAMS) => {
const { meta } = useStore()
const resolvedSql = typeof sql === 'function' ? sql(params) : sql
-
+ console.log('useDbQuery', 'sql', resolvedSql)
const {
data,
error: swrError,
isValidating: isLoading,
mutate,
- } = useSWR<QueryResponse>(resolvedSql, meta.query, { revalidateOnFocus: false })
+ } = useSWR<MetaQueryResponse>(resolvedSql, async () => await meta.query(resolvedSql), {
+ revalidateOnFocus: false,
+ })
+ console.log('useDbQuery', 'data', data)
+ console.log('useDbQuery', 'isLoading', isLoading)
- const error = swrError || data?.error
- return [{ error, data, isLoading }, { runQuery: () => mutate() }]
+ const error = swrError || (typeof data === 'object' ? data?.error : '')
+ return [{ error, data, isLoading, params }, { runQuery: () => mutate() }]
}
export default useDbQuery
diff --git a/studio/hooks/analytics/useLogsQuery.tsx b/studio/hooks/analytics/useLogsQuery.tsx
index 48721a081ae..be58c480c58 100644
--- a/studio/hooks/analytics/useLogsQuery.tsx
+++ b/studio/hooks/analytics/useLogsQuery.tsx
@@ -12,6 +12,7 @@ export interface LogsQueryData {
params: LogsEndpointParams
isLoading: boolean
logData: LogData[]
+ data?: never
error: string | Object | null
}
export interface LogsQueryHandlers {
diff --git a/studio/pages/project/[ref]/reports/auth.tsx b/studio/pages/project/[ref]/reports/auth.tsx
index 14b871b51b7..1d1557f2444 100644
--- a/studio/pages/project/[ref]/reports/auth.tsx
+++ b/studio/pages/project/[ref]/reports/auth.tsx
@@ -10,6 +10,7 @@ import ReportWidget from 'components/interfaces/Reports/ReportWidget'
import {
renderDailyActiveUsers,
renderNewUsers,
+ renderCumulativeUsers,
} from 'components/interfaces/Reports/renderers/AuthRenderers'
export const ApiBotsPage: NextPageWithLayout = () => {
@@ -19,19 +20,28 @@ export const ApiBotsPage: NextPageWithLayout = () => {
const hooks = hooksFactory(ref as string, config)
const dailyActiveUsers = hooks.dailyActiveUsers()
const newUsers = hooks.newUsers()
- const { isLoading, Layout } = usePresetReport(
- [dailyActiveUsers, newUsers],
- )
+ const cumulativeUsers = hooks.cumulativeUsers()
+
+ const { isLoading, Layout } = usePresetReport([dailyActiveUsers, newUsers, cumulativeUsers])
return (
<Layout title={config.title}>
- <div className="grid lg:grid-cols-4">
+ <div className="grid lg:grid-cols-4 gap-4">
+ <ReportWidget
+ isLoading={isLoading}
+ params={cumulativeUsers[0].params}
+ className="col-span-2 col-start-1"
+ title="Users Over Time"
+ description="Daily count of confirmed users"
+ data={cumulativeUsers[0].data!}
+ renderer={renderCumulativeUsers}
+ />
<ReportWidget
isLoading={isLoading}
params={dailyActiveUsers[0].params}
- className="col-span-4 col-start-1"
+ className="col-span-2 col-start-3"
title="Daily Active Users"
description="The daily count of active authenticated users"
- data={dailyActiveUsers[0].logData}
+ data={dailyActiveUsers[0].logData!}
renderer={renderDailyActiveUsers}
/>
<ReportWidget
@@ -40,7 +50,7 @@ export const ApiBotsPage: NextPageWithLayout = () => {
className="col-span-4 col-start-1"
title="Daily New Users"
description="The daily count of new user signups"
- data={newUsers[0].logData}
+ data={newUsers[0].logData!}
renderer={renderNewUsers}
/>
</div>