mirror of
https://github.com/supabase/supabase.git
synced 2026-09-06 18:11:51 +08:00
## Summary - define application-owned database and logs source contracts, defaults, validation, labels, and execution endpoints - extract controlled database and logs parameter controls for reuse outside SQL snippets - adapt the SQL editor to the shared source model without changing snippet behavior - standardize source icons at 16px with a 2px stroke - keep relative logs ranges aligned with the existing date picker units ## To test 1. Open an existing query in the SQL Editor and run it against the database. 2. Switch the query source to Logs, change the time range, and confirm the query still runs as expected. ## Why Explorer queries and notebook query cells need to select an execution source without coupling that source to SQL snippets. This provides the shared registry and controlled UI foundation for those consumers. ## Impact Existing SQL snippets retain their current database/logs routing and session behavior. The registry documents the SQL editor legacy database-selector adapter while new consumers own their identifier inline. The shared Logs date picker remains unchanged; query ranges support its existing minute, hour, and day units. This PR does not add the Explorer query tab itself. ## Validation - pnpm --filter studio typecheck - focused Vitest coverage for the registry, canonical log-range utilities, SQL execution adapters, source filtering, retention locking, custom ranges, and preset selection - pnpm --filter studio run lint:ratchet Component and state tests cover this change per the Studio testing guidance; no E2E test is added. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added a unified query-source menu for database queries and logs. * Added custom log time-range selection with calendar support and retention-aware upgrade prompts. * Added consistent source icons and improved database selection handling. * Added support for relative and absolute log time ranges. * **Bug Fixes** * Improved log-range validation, defaults, and current-time handling. * Updated query execution to use the correct source-specific endpoints. * **Tests** * Expanded coverage for query sources, log ranges, menus, and retention behavior. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
65 lines
2.2 KiB
TypeScript
65 lines
2.2 KiB
TypeScript
import { useFlag, useParams } from 'common'
|
|
import { useCallback } from 'react'
|
|
|
|
import { resolveLogTimeRange } from '@/components/interfaces/QuerySources/LogTimeRange.utils'
|
|
import { useExecuteLogsSqlMutation } from '@/data/logs/execute-logs-sql-mutation'
|
|
import { type SafeLogSqlFragment } from '@/data/logs/safe-analytics-sql'
|
|
import {
|
|
DEFAULT_LOG_TIME_RANGE,
|
|
QUERY_SOURCE_REGISTRY,
|
|
} from '@/data/query-sources/query-source-registry'
|
|
import { useTrack } from '@/lib/telemetry/track'
|
|
import {
|
|
getSqlEditorSessionSnapshot,
|
|
useSqlEditorSessionSnapshot,
|
|
} from '@/state/sql-editor/sql-editor-session-state'
|
|
|
|
type UseLogsSqlExecutionArgs = { id: string }
|
|
|
|
/**
|
|
* Logs counterpart to `useSqlEditorExecution`. Runs a promoted
|
|
* `SafeLogSqlFragment` against the analytics OTEL endpoint with the snippet's
|
|
* active time range attached as request params.
|
|
*/
|
|
export function useLogsSqlExecution({ id }: UseLogsSqlExecutionArgs) {
|
|
const { ref: projectRef } = useParams()
|
|
const isOtelLogsEnabled = useFlag('otelLegacyLogs')
|
|
const track = useTrack()
|
|
const sessionSnap = useSqlEditorSessionSnapshot()
|
|
|
|
const { mutate, isPending: isExecuting } = useExecuteLogsSqlMutation({
|
|
onSuccess: (data) => {
|
|
sessionSnap.addResult(id, data.rows)
|
|
},
|
|
onError: (error) => {
|
|
sessionSnap.addResultError(id, error)
|
|
},
|
|
})
|
|
|
|
const executeLogsQuery = useCallback(
|
|
(sql: SafeLogSqlFragment) => {
|
|
if (isExecuting || projectRef === undefined) return
|
|
|
|
if (!isOtelLogsEnabled) {
|
|
getSqlEditorSessionSnapshot().addResultError(id, {
|
|
message: "Querying logs from the SQL editor isn't available for this project yet.",
|
|
})
|
|
return
|
|
}
|
|
|
|
// Re-read imperatively so a range picked immediately before the run is
|
|
// honored; relative ranges re-resolve against `now` here.
|
|
const range = resolveLogTimeRange(
|
|
getSqlEditorSessionSnapshot().logRange[id] ?? DEFAULT_LOG_TIME_RANGE
|
|
)
|
|
|
|
mutate({ projectRef, sql, range, endpoint: QUERY_SOURCE_REGISTRY.logs.endpoint })
|
|
|
|
track('sql_editor_query_run_button_clicked', { source: 'logs' })
|
|
},
|
|
[id, isExecuting, isOtelLogsEnabled, mutate, projectRef, track]
|
|
)
|
|
|
|
return { executeLogsQuery, isExecuting }
|
|
}
|