: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
chore(metrics): adopt generated API functions in the metrics frontend (#69191)
Frank Hamand5c2630d656aa
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.tsindex 2b703ba29739..799a6fa6e012 100644--- a/frontend/src/lib/api.ts+++ b/frontend/src/lib/api.ts@@ -752,19 +752,6 @@ export class ApiRequest {return this.logs(projectId).addPathComponent('export')}- // # Metrics- public metrics(projectId?: ProjectType['id']): ApiRequest {- return this.environmentsDetail(projectId).addPathComponent('metrics')- }-- public metricsHasMetrics(projectId?: ProjectType['id']): ApiRequest {- return this.metrics(projectId).addPathComponent('has_metrics')- }-- public metricsValues(projectId?: ProjectType['id']): ApiRequest {- return this.metrics(projectId).addPathComponent('values')- }-// # Tracingpublic tracingSpans(): ApiRequest {return this.environmentsDetail().addPathComponent('tracing').addPathComponent('spans')@@ -2901,29 +2888,6 @@ const api = {},},- metrics: {- async hasMetrics(): Promise<boolean> {- return new ApiRequest()- .metricsHasMetrics()- .get()- .then((response) => Boolean(response.hasMetrics))- },- async values({- search,- limit,- signal,- }: {- search?: string- limit?: number- signal?: AbortSignal- } = {}): Promise<{ results: { name: string; metric_type: string }[] }> {- return new ApiRequest()- .metricsValues()- .withQueryString({ value: search ?? '', limit: limit ?? 100 })- .get({ signal })- },- },-tracing: {async hasSpans(): Promise<boolean> {return new ApiRequest()diff --git a/products/metrics/frontend/components/metricNamePickerLogic.ts b/products/metrics/frontend/components/metricNamePickerLogic.tsindex c26585fd99a2..38e818041eb5 100644--- a/products/metrics/frontend/components/metricNamePickerLogic.ts+++ b/products/metrics/frontend/components/metricNamePickerLogic.ts@@ -1,17 +1,20 @@-import { actions, afterMount, kea, listeners, path, reducers } from 'kea'+import { actions, afterMount, connect, kea, listeners, path, reducers } from 'kea'import { loaders } from 'kea-loaders'-import api from 'lib/api'+import { teamLogic } from 'scenes/teamLogic'++import { metricsValuesRetrieve } from 'products/metrics/frontend/generated/api'+import type { _MetricNameApi } from 'products/metrics/frontend/generated/api.schemas'import type { metricNamePickerLogicType } from './metricNamePickerLogicType'-export interface MetricNameItem {- name: string- metric_type: string-}+export type MetricNameItem = _MetricNameApiexport const metricNamePickerLogic = kea<metricNamePickerLogicType>([path(['products', 'metrics', 'frontend', 'components', 'metricNamePickerLogic']),+ connect(() => ({+ values: [teamLogic, ['currentTeamId']],+ })),actions({setSearch: (search: string) => ({ search }),}),@@ -26,7 +29,10 @@ export const metricNamePickerLogic = kea<metricNamePickerLogicType>([// Debounce — match the 300ms cadence used in the viewer logic so// both fetches feel cohesive.await breakpoint(300)- const response = await api.metrics.values({ search: values.search, limit: 100 })+ const response = await metricsValuesRetrieve(String(values.currentTeamId), {+ value: values.search,+ limit: 100,+ })breakpoint()return response.results},diff --git a/products/metrics/frontend/components/metricsViewerLogic.test.ts b/products/metrics/frontend/components/metricsViewerLogic.test.tsindex 1d265c3810c1..b833db0d1fe0 100644--- a/products/metrics/frontend/components/metricsViewerLogic.test.ts+++ b/products/metrics/frontend/components/metricsViewerLogic.test.ts@@ -1,10 +1,15 @@-import api from 'lib/api'-import { initKeaTests } from '~/test/init'+import { metricsValuesRetrieve } from 'products/metrics/frontend/generated/api'+import { metricNamePickerLogic } from './metricNamePickerLogic'import { metricsViewerLogic, NEW_QUERY_STARTED_ERROR_MESSAGE } from './metricsViewerLogic'+jest.mock('products/metrics/frontend/generated/api', () => ({+ ...jest.requireActual('products/metrics/frontend/generated/api'),+ metricsValuesRetrieve: jest.fn(),+}))+const PICKER_ITEMS = [{ name: 'requests_total', metric_type: 'sum' },{ name: 'queue_depth', metric_type: 'gauge' },@@ -17,7 +22,7 @@ describe('metricsViewerLogic', () => {beforeEach(() => {initKeaTests()- jest.spyOn(api.metrics, 'values').mockResolvedValue({ results: PICKER_ITEMS })+ jest.mocked(metricsValuesRetrieve).mockResolvedValue({ results: PICKER_ITEMS })logic = metricsViewerLogic()logic.mount()metricNamePickerLogic.actions.loadItemsSuccess(PICKER_ITEMS)diff --git a/products/metrics/frontend/metricsIngestionLogic.tsx b/products/metrics/frontend/metricsIngestionLogic.tsxindex 5c6b05d63e4f..1ae19691dc34 100644--- a/products/metrics/frontend/metricsIngestionLogic.tsx+++ b/products/metrics/frontend/metricsIngestionLogic.tsx@@ -1,23 +1,30 @@-import { afterMount, kea, path, reducers, selectors } from 'kea'+import { afterMount, connect, kea, path, reducers, selectors } from 'kea'import { loaders } from 'kea-loaders'-import api from 'lib/api'import { retryWithBackoff } from 'lib/utils/async'+import { teamLogic } from 'scenes/teamLogic'+import { metricsHasMetricsRetrieve } from './generated/api'import type { metricsIngestionLogicType } from './metricsIngestionLogicType'const teamId = window.POSTHOG_APP_CONTEXT?.current_team?.idexport const metricsIngestionLogic = kea<metricsIngestionLogicType>([path(['products', 'metrics', 'frontend', 'metricsIngestionLogic']),- loaders({+ connect(() => ({+ values: [teamLogic, ['currentTeamId']],+ })),+ loaders(({ values }) => ({teamHasMetrics: {__default: undefined as boolean | undefined,loadTeamHasMetrics: async (): Promise<boolean> => {- return await retryWithBackoff(() => api.metrics.hasMetrics(), { maxAttempts: 3 })+ const response = await retryWithBackoff(() => metricsHasMetricsRetrieve(String(values.currentTeamId)), {+ maxAttempts: 3,+ })+ return Boolean(response.hasMetrics)},},- }),+ })),reducers({teamHasMetricsCheckFailed: [