Skip to content

: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.

7.0 KB
Raw Blame
import { IconPlusSmall, IconX } from '@posthog/icons'
import { LemonButton, LemonMenu, LemonMenuItem } from '@posthog/lemon-ui'

import {
    MetricAggregation,
    MetricRangeFunction,
    MetricsViewerClause,
    isHistogramQuantileAggregation,
    supportsHistogramQuantile,
} from './metricsViewerLogic'

interface OperationOption<T extends string> {
    value: T
    label: string
    description: string
}

const RANGE_FUNCTION_OPTIONS: OperationOption<MetricRangeFunction>[] = [
    {
        value: 'rate',
        label: 'Rate (/s)',
        description: 'Per-second rate of increase of each series. Use it for counters.',
    },
    {
        value: 'increase',
        label: 'Increase',
        description: 'Total increase of each series per time bucket. Use it for counters.',
    },
]

const AGGREGATION_OPTIONS: OperationOption<MetricAggregation>[] = [
    { value: 'sum', label: 'Sum', description: 'Add the series together.' },
    { value: 'avg', label: 'Average', description: 'Average of the series.' },
    { value: 'count', label: 'Series count', description: 'Number of series that reported.' },
    { value: 'min', label: 'Min', description: 'Lowest value across the series.' },
    { value: 'max', label: 'Max', description: 'Highest value across the series.' },
    { value: 'p95', label: 'p95', description: '95th percentile across the series.' },
]

const HISTOGRAM_AGGREGATION_OPTIONS: OperationOption<MetricAggregation>[] = [
    {
        value: 'histogram_p50',
        label: 'Histogram p50',
        description: 'Median of the recorded values, read from the histogram buckets.',
    },
    {
        value: 'histogram_p95',
        label: 'Histogram p95',
        description: '95th percentile of the recorded values, read from the histogram buckets.',
    },
    {
        value: 'histogram_p99',
        label: 'Histogram p99',
        description: '99th percentile of the recorded values, read from the histogram buckets.',
    },
]

// A clause converted from a PromQL histogram percentile has no metric type, so its current
// aggregation also unlocks the other percentiles.
const aggregationOptionsFor = (
    metricType: MetricsViewerClause['selectedMetricType'],
    aggregation: MetricAggregation | null
): OperationOption<MetricAggregation>[] =>
    supportsHistogramQuantile(metricType) || isHistogramQuantileAggregation(aggregation)
        ? [...HISTOGRAM_AGGREGATION_OPTIONS, ...AGGREGATION_OPTIONS]
        : AGGREGATION_OPTIONS

function operationMenuItems<T extends string>(
    options: OperationOption<T>[],
    current: T | null,
    onPick: (value: T) => void
): LemonMenuItem[] {
    return options.map((option) => ({
        label: option.label,
        tooltip: option.description,
        active: option.value === current,
        onClick: () => onPick(option.value),
        'data-attr': `metrics-operation-option-${option.value}`,
    }))
}

function OperationChip<T extends string>({
    label,
    options,
    current,
    onPick,
    onRemove,
    removeTooltip,
    disabledReason,
    dataAttr,
}: {
    label: string
    options: OperationOption<T>[]
    current: T
    onPick: (value: T) => void
    onRemove: () => void
    removeTooltip: string
    disabledReason: string | null
    dataAttr: string
}): JSX.Element {
    return (
        <LemonMenu items={operationMenuItems(options, current, onPick)}>
            <LemonButton
                size="small"
                type="secondary"
                active
                disabledReason={disabledReason}
                sideAction={{
                    icon: <IconX />,
                    onClick: onRemove,
                    tooltip: removeTooltip,
                    'data-attr': `${dataAttr}-remove`,
                }}
                data-attr={dataAttr}
            >
                {label}
            </LemonButton>
        </LemonMenu>
    )
}

/** The operations applied to a clause's series, in the order they run: a range function on each
 * series first, then an aggregation across series. Mirrors the operation chips of a PromQL builder. */
export function MetricsOperations({
    clause,
    onRangeFunctionChange,
    onAggregationChange,
    disabledReason,
}: {
    clause: Pick<MetricsViewerClause, 'rangeFunction' | 'aggregation' | 'selectedMetricType'>
    onRangeFunctionChange: (rangeFunction: MetricRangeFunction | null) => void
    onAggregationChange: (aggregation: MetricAggregation | null) => void
    disabledReason: string | null
}): JSX.Element {
    const { rangeFunction, aggregation, selectedMetricType } = clause
    const aggregationOptions = aggregationOptionsFor(selectedMetricType, aggregation)
    const rangeFunctionOption = RANGE_FUNCTION_OPTIONS.find((option) => option.value === rangeFunction)
    // A saved or linked histogram percentile still shows its chip while the type is not known yet.
    const aggregationOption = [...HISTOGRAM_AGGREGATION_OPTIONS, ...AGGREGATION_OPTIONS].find(
        (option) => option.value === aggregation
    )

    return (
        <div className="flex flex-wrap items-center gap-1" data-attr="metrics-operations">
            {rangeFunctionOption && (
                <OperationChip
                    label={rangeFunctionOption.label}
                    options={RANGE_FUNCTION_OPTIONS}
                    current={rangeFunctionOption.value}
                    onPick={onRangeFunctionChange}
                    onRemove={() => onRangeFunctionChange(null)}
                    removeTooltip="Remove range function"
                    disabledReason={disabledReason}
                    dataAttr="metrics-operation-range-function"
                />
            )}
            {aggregationOption && (
                <OperationChip
                    label={aggregationOption.label}
                    options={aggregationOptions}
                    current={aggregationOption.value}
                    onPick={onAggregationChange}
                    onRemove={() => onAggregationChange(null)}
                    removeTooltip="Remove aggregation"
                    disabledReason={disabledReason}
                    dataAttr="metrics-operation-aggregation"
                />
            )}
            <LemonMenu
                items={[
                    {
                        title: 'Range functions',
                        items: operationMenuItems(RANGE_FUNCTION_OPTIONS, rangeFunction, onRangeFunctionChange),
                    },
                    {
                        title: 'Aggregations',
                        items: operationMenuItems(aggregationOptions, aggregation, onAggregationChange),
                    },
                ]}
            >
                <LemonButton
                    size="small"
                    type="secondary"
                    icon={<IconPlusSmall />}
                    disabledReason={disabledReason}
                    tooltip="Add an operation. Without an aggregation, each series gets its own line."
                    data-attr="metrics-operation-add"
                >
                    Operation
                </LemonButton>
            </LemonMenu>
        </div>
    )
}