: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 posthog from 'posthog-js'
import { LemonDialog } from '@posthog/lemon-ui'
import type { MetricsQuery, MetricsQueryLanguage } from '~/queries/schema/schema-general'
import { convertMetricsQuery, queryLanguage } from '../queryLanguages/convert'
export const METRICS_QUERY_LANGUAGE_LABELS: Record<MetricsQueryLanguage, string> = {
builder: 'Builder',
promql: 'PromQL',
sql: 'SQL',
}
/** Converts a metrics query to another language. When the conversion loses something, it asks first. */
export function switchMetricsQueryLanguage(
query: MetricsQuery,
to: MetricsQueryLanguage,
apply: (query: MetricsQuery) => void
): void {
const from = queryLanguage(query)
if (from === to) {
return
}
const { query: converted, issues } = convertMetricsQuery(query, to)
const track = (confirmed: boolean): void => {
posthog.capture('metrics query language switched', {
from,
to,
lossy: issues.length > 0,
issue_count: issues.length,
confirmed,
})
}
if (!issues.length) {
track(true)
apply(converted)
return
}
const label = METRICS_QUERY_LANGUAGE_LABELS[to]
LemonDialog.open({
title: `Switch to ${label}?`,
description: (
<ul className="list-disc pl-5 mt-2 flex flex-col gap-1">
{issues.map((issue) => (
<li key={issue}>{issue}</li>
))}
</ul>
),
primaryButton: {
children: 'Switch',
onClick: () => {
track(true)
apply(converted)
},
},
secondaryButton: { children: 'Cancel', onClick: () => track(false) },
})
}