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.

13.6 KB
Raw Blame
import { useActions, useValues } from 'kea'

import { IconSearch } from '@posthog/icons'
import { LemonSkeleton } from '@posthog/lemon-ui'
import {
    Badge,
    Button,
    Card,
    CardFooter,
    CardHeader,
    CardTitle,
    InputGroup,
    InputGroupAddon,
    InputGroupInput,
    InputGroupText,
    Pagination,
    PaginationButton,
    PaginationContent,
    PaginationEllipsis,
    PaginationItem,
    PaginationNext,
    PaginationPrevious,
    Spinner,
    Table,
    TableBody,
    TableCell,
    TableEmpty,
    TableHead,
    TableHeader,
    TableRow,
    Text,
    getPaginationRange,
} from '@posthog/quill-primitives'

import { TZLabel } from 'lib/components/TZLabel'
import { LinkPrimitive } from 'lib/lemon-ui/Link/Link'
import { Tooltip } from 'lib/lemon-ui/Tooltip'
import { formatPercentage } from 'lib/utils/numbers'
import { pluralize } from 'lib/utils/strings'

import { formatMs, formatNumber } from '../dashboard/formatters'
import {
    type SortState,
    type ToolQualitySortColumn,
    TOOL_QUALITY_PAGE_SIZE,
    mcpAnalyticsToolQualityLogic,
    mcpToolReportUrl,
} from '../mcpAnalyticsToolQualityLogic'
import { errorRateChange, p95Change } from './qualityChange'
import { QualityChangeMarker } from './QualityChangeMarker'
import { SessionsCell } from './SessionsCell'
import { TrendCell } from './TrendCell'

const DESTRUCTIVE_ERROR_PCT = 5

interface ColumnSpec {
    key: ToolQualitySortColumn
    label: string
    align?: 'left' | 'right'
    tooltip?: string
}

const SORTABLE_COLUMNS: ColumnSpec[] = [
    { key: 'total_calls', label: 'Calls', align: 'right', tooltip: 'Total number of times this tool was called' },
    {
        key: 'trend_score',
        label: 'Trend',
        align: 'right',
        tooltip:
            "Change in calls versus the previous period of the same length. Sorting ranks by growth relative to volume, so small tools don't dominate.",
    },
    {
        key: 'error_rate_pct',
        label: 'Error rate',
        align: 'right',
        tooltip:
            'Percentage of calls that returned $mcp_is_error = true. An arrow marks a meaningful change versus the previous period, for tools with at least 20 calls in both.',
    },
    {
        key: 'p95_duration_ms',
        label: 'p95',
        align: 'right',
        tooltip:
            '95th-percentile $mcp_duration_ms. An arrow marks a meaningful change versus the previous period, for tools with at least 20 calls in both.',
    },
    { key: 'users', label: 'Users', align: 'right', tooltip: 'Unique users who invoked this tool' },
    {
        key: 'sessions',
        label: 'Sessions',
        align: 'right',
        tooltip: 'Unique sessions where this tool was called, and their share of all sessions in the period',
    },
    { key: 'last_seen', label: 'Last seen' },
]

const COLUMN_COUNT = SORTABLE_COLUMNS.length + 2

function ErrorRateBadge({ pct }: { pct: number }): JSX.Element {
    if (pct <= 0) {
        return <Badge variant="success">0%</Badge>
    }
    return (
        <Badge variant={pct >= DESTRUCTIVE_ERROR_PCT ? 'destructive' : 'warning'}>
            {formatPercentage(pct, { compact: true })}
        </Badge>
    )
}

function SortableHead({
    column,
    sort,
    loading,
    onSort,
}: {
    column: ColumnSpec
    sort: SortState
    loading: boolean
    onSort: (column: ToolQualitySortColumn, direction: 'ASC' | 'DESC') => void
}): JSX.Element {
    const isSorted = sort.column === column.key
    const nextDirection = isSorted && sort.direction === 'DESC' ? 'ASC' : 'DESC'
    const head = (
        <button
            type="button"
            disabled={loading}
            onClick={() => onSort(column.key, nextDirection)}
            className="inline-flex cursor-pointer select-none items-center gap-1"
        >
            {column.label}
            {isSorted ? <span className="text-[9px]">{sort.direction === 'DESC' ? '▼' : '▲'}</span> : null}
        </button>
    )
    return (
        <TableHead
            align={column.align}
            aria-sort={isSorted ? (sort.direction === 'DESC' ? 'descending' : 'ascending') : 'none'}
        >
            {column.tooltip ? <Tooltip title={column.tooltip}>{head}</Tooltip> : head}
        </TableHead>
    )
}

function ToolRows(): JSX.Element {
    const {
        toolRows,
        toolRowsTotalSessions,
        toolRowsPreviousTotalSessions,
        toolRowsPageLoading,
        selectedTool,
        dateFilter,
        pinnedInterval,
    } = useValues(mcpAnalyticsToolQualityLogic)
    const { setSelectedTool } = useActions(mcpAnalyticsToolQualityLogic)

    if (toolRowsPageLoading && toolRows.length === 0) {
        return (
            <TableBody>
                <TableRow>
                    <TableCell colSpan={COLUMN_COUNT}>
                        <div className="space-y-2 py-1">
                            {Array.from({ length: 6 }).map((_, i) => (
                                <LemonSkeleton key={i} className="h-3.5 w-full" />
                            ))}
                        </div>
                    </TableCell>
                </TableRow>
            </TableBody>
        )
    }
    if (toolRows.length === 0) {
        return <TableEmpty className="py-6 text-secondary">No tool calls match the current filters.</TableEmpty>
    }
    return (
        <TableBody>
            {toolRows.map((row) => (
                <TableRow
                    key={row.tool}
                    data-state={row.tool === selectedTool ? 'selected' : undefined}
                    className="cursor-pointer"
                    onClick={() => setSelectedTool(row.tool === selectedTool ? null : row.tool)}
                    data-attr="mcp-tool-quality-row"
                >
                    <TableCell expand>
                        <span className="font-mono">{row.tool}</span>
                    </TableCell>
                    <TableCell align="right">{formatNumber(row.total_calls)}</TableCell>
                    <TableCell align="right">
                        <TrendCell totalCalls={row.total_calls} previousCalls={row.previous_calls} />
                    </TableCell>
                    <TableCell align="right">
                        <span className="inline-flex items-center whitespace-nowrap">
                            <ErrorRateBadge pct={row.error_rate_pct} />
                            <QualityChangeMarker change={errorRateChange(row)} />
                        </span>
                    </TableCell>
                    <TableCell align="right">
                        <span className="inline-flex items-center whitespace-nowrap">
                            <span className="tabular-nums">{formatMs(row.p95_duration_ms)}</span>
                            <QualityChangeMarker change={p95Change(row)} />
                        </span>
                    </TableCell>
                    <TableCell align="right">{formatNumber(row.users)}</TableCell>
                    <TableCell align="right">
                        <SessionsCell
                            sessions={row.sessions}
                            totalSessions={toolRowsTotalSessions}
                            previousSessions={row.previous_sessions}
                            previousTotalSessions={toolRowsPreviousTotalSessions}
                        />
                    </TableCell>
                    <TableCell className="whitespace-nowrap">
                        <TZLabel time={row.last_seen} />
                    </TableCell>
                    <TableCell align="right">
                        <Button
                            variant="outline"
                            size="sm"
                            render={<LinkPrimitive to={mcpToolReportUrl(row.tool, dateFilter, pinnedInterval)} />}
                            onClick={(e) => e.stopPropagation()}
                            data-attr="mcp-tool-quality-full-report"
                        >
                            Full report
                        </Button>
                    </TableCell>
                </TableRow>
            ))}
        </TableBody>
    )
}

export function ToolQualityTable(): JSX.Element {
    const {
        toolQualitySort,
        toolQualityPageIndex,
        loadedToolQualityPageIndex,
        toolRows,
        toolRowsPageLoading,
        toolRowsTotalCount,
        searchTerm,
    } = useValues(mcpAnalyticsToolQualityLogic)
    const { setToolQualitySort, setToolQualityPageIndex, setSearchTerm } = useActions(mcpAnalyticsToolQualityLogic)
    const pageCount = Math.max(Math.ceil(toolRowsTotalCount / TOOL_QUALITY_PAGE_SIZE), 1)
    const pageRange = getPaginationRange(pageCount, toolQualityPageIndex)
    const firstRow = toolRowsTotalCount === 0 ? 0 : loadedToolQualityPageIndex * TOOL_QUALITY_PAGE_SIZE + 1
    const lastRow = Math.min(firstRow + toolRows.length - 1, toolRowsTotalCount)

    return (
        <Card size="sm" className="gap-0">
            <CardHeader className="flex flex-row flex-wrap items-center justify-between gap-2 border-b border-border pb-3">
                <CardTitle>All tools</CardTitle>
                <InputGroup className="w-[220px]">
                    <InputGroupAddon align="inline-start">
                        <InputGroupText>
                            <IconSearch />
                        </InputGroupText>
                    </InputGroupAddon>
                    <InputGroupInput
                        type="search"
                        placeholder="Search tools"
                        value={searchTerm}
                        onChange={(e) => setSearchTerm(e.target.value)}
                        data-attr="mcp-tool-quality-search"
                    />
                </InputGroup>
            </CardHeader>
            <div className="relative">
                <Table fullWidth stickyHeader className="max-h-[44rem]" aria-busy={toolRowsPageLoading}>
                    <TableHeader>
                        <TableRow>
                            <TableHead expand>Tool</TableHead>
                            {SORTABLE_COLUMNS.map((column) => (
                                <SortableHead
                                    key={column.key}
                                    column={column}
                                    sort={toolQualitySort}
                                    loading={toolRowsPageLoading}
                                    onSort={setToolQualitySort}
                                />
                            ))}
                            <TableHead />
                        </TableRow>
                    </TableHeader>
                    <ToolRows />
                </Table>
                {toolRowsPageLoading ? (
                    <div className="absolute inset-0 z-10 flex items-center justify-center bg-background/60">
                        <Spinner className="size-5" />
                    </div>
                ) : null}
            </div>
            {toolRowsTotalCount > 0 && (
                <CardFooter className="flex flex-row flex-wrap items-center justify-between gap-2 border-t border-border">
                    <Text size="xs" variant="muted" render={<span />} className="tabular-nums">
                        {firstRow}-{lastRow} of {pluralize(toolRowsTotalCount, 'tool')}
                    </Text>
                    {pageCount > 1 ? (
                        <Pagination className="w-auto">
                            <PaginationContent>
                                <PaginationItem>
                                    <PaginationPrevious
                                        disabled={toolRowsPageLoading || toolQualityPageIndex === 0}
                                        onClick={() => setToolQualityPageIndex(toolQualityPageIndex - 1)}
                                        data-attr="mcp-tool-quality-page-previous"
                                    />
                                </PaginationItem>
                                {pageRange.map((item, index) =>
                                    item === 'ellipsis' ? (
                                        <PaginationItem key={`ellipsis-${index}`}>
                                            <PaginationEllipsis />
                                        </PaginationItem>
                                    ) : (
                                        <PaginationItem key={item}>
                                            <PaginationButton
                                                isActive={item === toolQualityPageIndex}
                                                disabled={toolRowsPageLoading}
                                                aria-label={`Go to page ${item + 1}`}
                                                onClick={() => setToolQualityPageIndex(item)}
                                                data-attr="mcp-tool-quality-page"
                                            >
                                                {item + 1}
                                            </PaginationButton>
                                        </PaginationItem>
                                    )
                                )}
                                <PaginationItem>
                                    <PaginationNext
                                        disabled={toolRowsPageLoading || toolQualityPageIndex === pageCount - 1}
                                        onClick={() => setToolQualityPageIndex(toolQualityPageIndex + 1)}
                                        data-attr="mcp-tool-quality-page-next"
                                    />
                                </PaginationItem>
                            </PaginationContent>
                        </Pagination>
                    ) : null}
                </CardFooter>
            )}
        </Card>
    )
}