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.

3.4 KB
Raw Blame
import { Tooltip } from '@posthog/lemon-ui'

import { ProbabilityBucket } from './autoresearchPipelineLogic'
import { PredictionSegmentThresholds, predictionSegmentForRange } from './predictionSegments'

// The h-40 track minus room for the count label above the tallest bar.
const BAR_MAX_HEIGHT_PX = 136

/**
 * Fixed-decile histogram of a scoring run's predicted probabilities, colored by segment. A bar that a cut
 * point splits, or every bar before the cut points load, is neutral. No chart deps, like MetricSparkline.
 */
export function ProbabilityHistogram({
    buckets,
    thresholds,
}: {
    buckets: ProbabilityBucket[]
    thresholds: PredictionSegmentThresholds | null
}): JSX.Element {
    const totalUsers = buckets.reduce((sum, bucket) => sum + bucket.users, 0)
    const maxUsers = Math.max(...buckets.map((bucket) => bucket.users), 1)
    return (
        <div className="flex items-end gap-2 max-w-4xl">
            {buckets.map((bucket) => {
                const label = `${Math.round(bucket.lower * 100)}-${Math.round((bucket.lower + 0.1) * 100)}%`
                const share = totalUsers > 0 ? (100 * bucket.users) / totalUsers : 0
                const segment = thresholds
                    ? predictionSegmentForRange(bucket.lower, bucket.lower + 0.1, thresholds)
                    : null
                const segmentLabel = segment ? segment.label.toLowerCase() : thresholds ? 'more than one segment' : null
                return (
                    <Tooltip
                        key={bucket.lower}
                        title={`${label}${segmentLabel ? ` (${segmentLabel})` : ''}: ${bucket.users.toLocaleString()} users (${share.toFixed(1)}% of ${totalUsers.toLocaleString()} scored)`}
                    >
                        <div className="flex-1 min-w-0 flex flex-col items-center gap-1">
                            <div className="w-full h-40 flex flex-col items-center justify-end gap-0.5">
                                <span
                                    className={`text-xs tabular-nums ${bucket.users > 0 ? 'text-secondary' : 'text-muted'}`}
                                >
                                    {bucket.users.toLocaleString()}
                                </span>
                                <div
                                    className={`w-full rounded-t hover:opacity-80 ${segment?.colorClassName ?? 'bg-border-primary'}`}
                                    style={{
                                        height: Math.round((BAR_MAX_HEIGHT_PX * bucket.users) / maxUsers),
                                        minHeight: bucket.users > 0 ? 3 : 0,
                                    }}
                                />
                            </div>
                            {/* A narrow scene leaves each bucket too little room for the full range, so show its lower bound. */}
                            <span className="text-xs text-muted whitespace-nowrap @min-[48rem]/main-content:hidden">
                                {Math.round(bucket.lower * 100)}%
                            </span>
                            <span className="text-xs text-muted whitespace-nowrap hidden @min-[48rem]/main-content:inline">
                                {label}
                            </span>
                        </div>
                    </Tooltip>
                )
            })}
        </div>
    )
}