:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { useActions, useMountedLogic, useValues } from 'kea'
import { useEffect } from 'react'
import { LemonBanner } from '@posthog/lemon-ui'
import { getAccessControlDisabledReason } from 'lib/utils/accessControlUtils'
import { SQLEditor } from 'scenes/data-warehouse/editor/SQLEditor'
import { sqlEditorLogic } from 'scenes/data-warehouse/editor/sqlEditorLogic'
import { SQLEditorMode } from 'scenes/data-warehouse/editor/sqlEditorModes'
import { NodeKind } from '~/queries/schema/schema-general'
import { AccessControlLevel, AccessControlResourceType, ChartDisplayType } from '~/types'
import { METRICS_SQL_EDITOR_TAB_ID, metricsSceneLogic } from '../metricsSceneLogic'
import { metricsSqlEditorTrackingLogic } from './metricsSqlEditorTrackingLogic'
// `metrics` is only registered under the `posthog.` HogQL namespace
// (posthog/hogql/database/database.py), so unlike `logs` it must be
// referenced fully qualified.
const DEFAULT_METRICS_QUERY = 'SELECT * FROM posthog.metrics LIMIT 10'
export const MetricsSqlEditor = (): JSX.Element => {
const sqlEditorTabId = METRICS_SQL_EDITOR_TAB_ID
const { keepSqlEditorMounted } = useActions(metricsSceneLogic)
const logic = sqlEditorLogic({ tabId: sqlEditorTabId, mode: SQLEditorMode.Embedded })
const { queryInput } = useValues(logic)
const { setQueryInput, setSourceQuery, runQuery } = useActions(logic)
const warehouseViewerDisabledReason = getAccessControlDisabledReason(
AccessControlResourceType.WarehouseObjects,
AccessControlLevel.Viewer
)
useMountedLogic(metricsSqlEditorTrackingLogic({ sqlEditorTabId }))
useEffect(() => {
if (!warehouseViewerDisabledReason) {
keepSqlEditorMounted(sqlEditorTabId)
}
}, [sqlEditorTabId, warehouseViewerDisabledReason]) // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
if (queryInput === null && !warehouseViewerDisabledReason) {
setQueryInput(DEFAULT_METRICS_QUERY)
setSourceQuery({
kind: NodeKind.DataVisualizationNode,
source: {
kind: NodeKind.HogQLQuery,
query: DEFAULT_METRICS_QUERY,
},
display: ChartDisplayType.ActionsLineGraph,
})
runQuery(DEFAULT_METRICS_QUERY)
}
}, [queryInput]) // eslint-disable-line react-hooks/exhaustive-deps
if (warehouseViewerDisabledReason) {
return <LemonBanner type="warning">{warehouseViewerDisabledReason}</LemonBanner>
}
return (
<div className="flex flex-col flex-1 min-h-0 min-w-0 border rounded overflow-hidden">
<SQLEditor tabId={sqlEditorTabId} mode={SQLEditorMode.Embedded} defaultShowDatabaseTree={false} />
</div>
)
}