Skip to content

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

fix: delayed metadata loading only on log focus

TzeYiing37d6a6a19968
diff --git a/studio/components/interfaces/Settings/Logs/LogSelection.tsx b/studio/components/interfaces/Settings/Logs/LogSelection.tsx
index cbbcbd29373..46f73a971f4 100644
--- a/studio/components/interfaces/Settings/Logs/LogSelection.tsx
+++ b/studio/components/interfaces/Settings/Logs/LogSelection.tsx
@@ -13,52 +13,42 @@ import FunctionInvocationSelectionRender, {
import FunctionLogsSelectionRender from './LogSelectionRenderers/FunctionLogsSelectionRender'
import DefaultExplorerSelectionRenderer from './LogSelectionRenderers/DefaultExplorerSelectionRenderer'
import DefaultPreviewSelectionRenderer from './LogSelectionRenderers/DefaultPreviewSelectionRenderer'
-import { isDefaultLogPreviewFormat } from '.'
+import { isDefaultLogPreviewFormat, LogsTableName } from '.'
+import useSingleLog from 'hooks/analytics/useSingleLog'
interface Props {
log: LogData | null
onClose: () => void
queryType?: QueryType
- isLoading?: boolean
+ projectRef: string
}
-const LogSelection: FC<Props> = ({ log, onClose, queryType, isLoading }) => {
+const LogSelection: FC<Props> = ({ projectRef, log: partialLog, onClose, queryType }) => {
+ const [{ logData: fullLog, isLoading }] = useSingleLog(projectRef, queryType, partialLog?.id)
const Formatter = () => {
switch (queryType) {
case 'api':
- return <DatabaseApiSelectionRender log={log} />
+ if (!fullLog) return null
+ return <DatabaseApiSelectionRender log={fullLog} />
case 'database':
- return <DatabasePostgresSelectionRender log={log} />
+ if (!fullLog) return null
+ return <DatabasePostgresSelectionRender log={fullLog} />
case 'fn_edge':
- return <FunctionInvocationSelectionRender log={log} />
- break
+ if (!fullLog) return null
+ return <FunctionInvocationSelectionRender log={fullLog} />
case 'functions':
- return <FunctionLogsSelectionRender log={log} />
- break
+ if (!fullLog) return null
+ return <FunctionLogsSelectionRender log={fullLog} />
default:
- if (log && isDefaultLogPreviewFormat(log)) {
- return <DefaultPreviewSelectionRenderer log={log} />
+ if (fullLog && isDefaultLogPreviewFormat(fullLog)) {
+ return <DefaultPreviewSelectionRenderer log={fullLog} />
}
- return <DefaultExplorerSelectionRenderer log={log} />
- }
- }
-
- function header() {
- switch (queryType) {
- case 'api':
- return DatabaseApiSelectionHeaderRender(log)
-
- break
- case 'fn_edge':
- return FunctionInvocationHeaderRender(log)
-
- default:
- return null
+ return <DefaultExplorerSelectionRenderer log={partialLog} />
}
}
@@ -74,7 +64,7 @@ const LogSelection: FC<Props> = ({ log, onClose, queryType, isLoading }) => {
className={
`overflow-y-scroll transition-all
bg-scale-200 absolute w-full h-full text-center flex-col gap-2 flex items-center justify-center opacity-0 ` +
- (log ? 'opacity-0 z-0' : 'opacity-100 z-10')
+ (partialLog ? 'opacity-0 z-0' : 'opacity-100 z-10')
}
>
<div
@@ -90,7 +80,7 @@ const LogSelection: FC<Props> = ({ log, onClose, queryType, isLoading }) => {
gap-6
max-w-sm
text-center scale-95 opacity-0 ` +
- (log || isLoading ? 'mt-0 opacity-0 scale-95' : 'mt-8 opacity-100 scale-100')
+ (partialLog || isLoading ? 'mt-0 opacity-0 scale-95' : 'mt-8 opacity-100 scale-100')
}
>
<div className="relative border border-scale-600 dark:border-scale-400 w-32 h-4 rounded px-2 flex items-center">
@@ -135,7 +125,7 @@ const LogSelection: FC<Props> = ({ log, onClose, queryType, isLoading }) => {
>
<IconX size={14} strokeWidth={2} />
</div>
- <div className="bg-scale-300 py-8 flex flex-col space-y-6">{log && <Formatter />}</div>
+ <div className="bg-scale-300 py-8 flex flex-col space-y-6">{fullLog && <Formatter />}</div>
</div>
</div>
)
diff --git a/studio/components/interfaces/Settings/Logs/LogTable.tsx b/studio/components/interfaces/Settings/Logs/LogTable.tsx
index d317b28f12f..d2385516f4f 100644
--- a/studio/components/interfaces/Settings/Logs/LogTable.tsx
+++ b/studio/components/interfaces/Settings/Logs/LogTable.tsx
@@ -21,6 +21,7 @@ interface Props {
isLoading?: boolean
error?: any
showDownload?: boolean
+ projectRef: string
}
type LogMap = { [id: string]: LogData }
@@ -55,6 +56,7 @@ const LogTable = ({
isLoading,
showDownload,
error,
+ projectRef,
}: Props) => {
const [focusedLog, setFocusedLog] = useState<LogData | null>(null)
const firstRow: LogData | undefined = data?.[0] as LogData
@@ -390,7 +392,7 @@ const LogTable = ({
}
>
<LogSelection
- isLoading={isLoading}
+ projectRef={projectRef}
onClose={() => setFocusedLog(null)}
log={focusedLog}
queryType={queryType}
diff --git a/studio/components/interfaces/Settings/Logs/Logs.utils.ts b/studio/components/interfaces/Settings/Logs/Logs.utils.ts
index 791f3866a4a..445aa67227f 100644
--- a/studio/components/interfaces/Settings/Logs/Logs.utils.ts
+++ b/studio/components/interfaces/Settings/Logs/Logs.utils.ts
@@ -125,7 +125,7 @@ export const genDefaultQuery = (table: LogsTableName, filters: Filters) => {
switch (table) {
case 'edge_logs':
- return `select id, timestamp, event_message, metadata, request, response, request.method, request.path, response.status_code
+ return `select id, timestamp, event_message, request, response, request.method, request.path, response.status_code
from ${table}
cross join unnest(metadata) as m
cross join unnest(m.request) as request
@@ -133,24 +133,21 @@ export const genDefaultQuery = (table: LogsTableName, filters: Filters) => {
${where}
limit 100
`
- break
case 'postgres_logs':
- return `select postgres_logs.timestamp, id, event_message, metadata, metadataparsed.error_severity from ${table}
+ return `select postgres_logs.timestamp, id, event_message, parsed.error_severity from ${table}
cross join unnest(metadata) as m
- cross join unnest(m.parsed) as metadataparsed
+ cross join unnest(m.parsed) as parsed
${where}
limit 100
`
- break
case 'function_logs':
- return `select id, ${table}.timestamp, event_message, metadata.event_type, metadata.function_id, metadata.level, metadata from ${table}
+ return `select id, ${table}.timestamp, event_message, metadata.event_type, metadata.function_id, metadata.level from ${table}
cross join unnest(metadata) as metadata
${where}
limit 100
`
- break
case 'function_edge_logs':
return `select id, ${table}.timestamp, event_message, response.status_code, response, request, request.method, m.function_id, m.execution_time_ms, m.deployment_id, m.version from ${table}
@@ -162,10 +159,14 @@ export const genDefaultQuery = (table: LogsTableName, filters: Filters) => {
`
default:
- return `select id, ${table}.timestamp, event_message, metadata from ${table}
+ return `select id, ${table}.timestamp, event_message from ${table}
${where}
limit 100
`
- break
}
}
+
+/**
+ * SQL query to retrieve only one log
+ */
+export const genSingleLogQuery = (table: LogsTableName, id: string) => `select id, timestamp, event_message, metadata from ${table} where id = '${id}'' limit 1`
\ No newline at end of file
diff --git a/studio/components/interfaces/Settings/Logs/LogsPreviewer.tsx b/studio/components/interfaces/Settings/Logs/LogsPreviewer.tsx
index 6eb2977f558..95d0761a75b 100644
--- a/studio/components/interfaces/Settings/Logs/LogsPreviewer.tsx
+++ b/studio/components/interfaces/Settings/Logs/LogsPreviewer.tsx
@@ -156,6 +156,7 @@ export const LogsPreviewer: React.FC<Props> = ({
<ShimmerLine active={isLoading} />
<LoadingOpacity active={isLoading}>
<LogTable
+ projectRef={projectRef}
isLoading={isLoading}
data={logData}
queryType={queryType}
diff --git a/studio/hooks/analytics/useSingleLog.tsx b/studio/hooks/analytics/useSingleLog.tsx
new file mode 100644
index 00000000000..86ecb4af618
--- /dev/null
+++ b/studio/hooks/analytics/useSingleLog.tsx
@@ -0,0 +1,56 @@
+import {
+ genQueryParams,
+ genSingleLogQuery,
+ LogData,
+ Logs,
+ LogsEndpointParams,
+ LOGS_TABLES,
+ QueryType,
+} from 'components/interfaces/Settings/Logs'
+import useSWR from 'swr'
+import { API_URL } from 'lib/constants'
+import { get } from 'lib/common/fetch'
+
+interface Data {
+ logData: LogData | undefined
+ error: string | Object | null
+ isLoading: boolean
+}
+interface Handlers {
+ refresh: () => void
+}
+function useSingleLog(
+ project: string,
+ queryType?: QueryType,
+ id?: string | null
+): [Data, Handlers] {
+ const table = queryType ? LOGS_TABLES[queryType] : undefined
+ const sql = id && table ? genSingleLogQuery(table, id) : ''
+ const params: LogsEndpointParams = { project, sql }
+ const endpointUrl = `${API_URL}/projects/${project}/analytics/endpoints/logs.all?${genQueryParams(
+ params as any
+ )}`
+ const {
+ data,
+ error: swrError,
+ isValidating,
+ mutate,
+ } = useSWR<Logs>(id && table ? endpointUrl : null, get, {
+ revalidateOnFocus: false,
+ dedupingInterval: 3000,
+ })
+
+ let error: null | string | object = swrError ? swrError.message : null
+
+ return [
+ {
+ logData: data ? data.result[0] : undefined,
+ isLoading: isValidating,
+ error,
+ },
+ {
+ refresh: () => mutate(),
+ },
+ ]
+}
+export default useSingleLog
diff --git a/studio/pages/project/[ref]/logs-explorer/index.tsx b/studio/pages/project/[ref]/logs-explorer/index.tsx
index 710ed9f8e99..496b932ad79 100644
--- a/studio/pages/project/[ref]/logs-explorer/index.tsx
+++ b/studio/pages/project/[ref]/logs-explorer/index.tsx
@@ -150,7 +150,7 @@ export const LogsExplorerPage: NextPageWithLayout = () => {
<div className="relative flex flex-grow flex-col">
<LoadingOpacity active={isLoading}>
<div className="flex h-full flex-grow">
- <LogTable data={logData} error={error} />
+ <LogTable data={logData} error={error} projectRef={ref as string} />
</div>
</LoadingOpacity>
<div className="mt-2 flex flex-row justify-end">