: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 { MakeLogicType, connect, kea, path, selectors } from 'kea'
import { FEATURE_FLAGS } from 'lib/constants'
import { FeatureFlagsSet, featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { toParams } from 'lib/utils/url'
import {
billingProjectList,
billingSpendRetrieve,
billingSpendTimeseriesRetrieve,
billingUsageRetrieve,
billingUsageTeamOptionsRetrieve,
billingUsageTimeseriesRetrieve,
getBillingSpendExportDownloadUrl,
getBillingUsageExportDownloadUrl,
} from 'products/billing/frontend/generated/api'
import type {
BillingSpendExportDownloadParams,
BillingSpendTimeseriesRetrieveParams,
BillingTimeSeriesPointApi,
BillingUsageExportDownloadParams,
BillingUsageTimeseriesRetrieveParams,
} from 'products/billing/frontend/generated/api.schemas'
import type { BillingSpendResponse } from './billingSpendLogic'
import type { BillingUsageResponse } from './billingUsageLogic'
/**
* The billing reads the usage and spend pages make, one set per source. The pages never name a route.
*
* Each method returns what the pages consume, and each source maps its own answer into it, so neither
* source takes the other's shape.
*/
export interface BillingReads {
usageSeries(params: BillingUsageTimeseriesRetrieveParams): Promise<BillingUsageResponse>
spendSeries(params: BillingSpendTimeseriesRetrieveParams): Promise<BillingSpendResponse>
usageExportUrl(params: BillingUsageExportDownloadParams): string
spendExportUrl(params: BillingSpendExportDownloadParams): string
/** The ids of the projects that reported usage, live and deleted. */
reportedProjectIds(): Promise<number[]>
}
// Both sources describe a series with every field optional, and the charts draw only complete ones.
// The pages' breakdown enums hold the same strings as the generated one.
function chartSeries<Response extends BillingUsageResponse | BillingSpendResponse>(
points: BillingTimeSeriesPointApi[]
): Response['results'] {
return points.map((point) => ({
id: point.id ?? 0,
label: point.label ?? '',
data: point.data ?? [],
dates: point.dates ?? [],
breakdown_type: point.breakdown_type ?? null,
breakdown_value: (point.breakdown_value as string | string[] | null | undefined) ?? null,
})) as Response['results']
}
export const organizationBillingReads: BillingReads = {
usageSeries: async (params) => {
const { count, next, previous, results } = await billingUsageTimeseriesRetrieve('@current', params)
return { count, next, previous, results: chartSeries<BillingUsageResponse>(results) }
},
spendSeries: async (params) => {
const { count, next, previous, results } = await billingSpendTimeseriesRetrieve('@current', params)
return { count, next, previous, results: chartSeries<BillingSpendResponse>(results) }
},
usageExportUrl: (params) => getBillingUsageExportDownloadUrl('@current', params),
spendExportUrl: (params) => getBillingSpendExportDownloadUrl('@current', params),
reportedProjectIds: async () => (await billingProjectList('@current')).results.map((project) => project.id),
}
// The legacy routes, for organizations the organization billing API is not on for yet. Delete this
// and the switch below when the flag is at 100%.
export const legacyBillingReads: BillingReads = {
usageSeries: async (params) => {
const { results, next } = await billingUsageRetrieve(params)
return {
count: results.length,
next: next ?? null,
previous: null,
results: chartSeries<BillingUsageResponse>(results),
}
},
spendSeries: async (params) => {
const { results, next } = await billingSpendRetrieve(params)
return {
count: results.length,
next: next ?? null,
previous: null,
results: chartSeries<BillingSpendResponse>(results),
}
},
usageExportUrl: (params) => `/api/billing/usage/export/?${toParams(params)}`,
spendExportUrl: (params) => `/api/billing/spend/export/?${toParams(params)}`,
reportedProjectIds: async () => (await billingUsageTeamOptionsRetrieve()).team_id_options ?? [],
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface billingReadsLogicValues {
featureFlags: FeatureFlagsSet // featureFlagLogic
billingReads: BillingReads
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface billingReadsLogicMeta {
__keaTypeGenInternalSelectorTypes: {
billingReads: (featureFlags: FeatureFlagsSet) => BillingReads
}
}
export type billingReadsLogicType = MakeLogicType<
billingReadsLogicValues,
{},
Record<string, any>,
billingReadsLogicMeta
>
export const billingReadsLogic = kea<billingReadsLogicType>([
path(['scenes', 'billing', 'billingReadsLogic']),
connect(() => ({ values: [featureFlagLogic, ['featureFlags']] })),
selectors({
billingReads: [
(s) => [s.featureFlags],
(featureFlags: FeatureFlagsSet): BillingReads =>
featureFlags[FEATURE_FLAGS.ORGANIZATION_BILLING_API] ? organizationBillingReads : legacyBillingReads,
],
}),
])