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.

2.1 KB
Raw Blame
import { escapeHogQLString } from 'lib/utils/hogql'
import type { NotebookFrameNodeSummary } from 'scenes/notebooks/Nodes/notebookNodeContent'

import type { WidgetInputContractColumnApi } from '../generated/api.schemas'

export function widgetInputMatches(columns: WidgetInputContractColumnApi[], frame: NotebookFrameNodeSummary): boolean {
    return (
        columns.length === frame.columns.length &&
        columns.every(
            (column, index) => column.name === frame.columns[index][0] && column.type === frame.columns[index][1]
        )
    )
}

export function defaultColumnMapping(
    columns: WidgetInputContractColumnApi[],
    frame: NotebookFrameNodeSummary | undefined
): Record<string, string> {
    return Object.fromEntries(
        columns.map(({ name, type }) => [
            name,
            frame?.columns.some(([source, sourceType]) => source === name && sourceType === type) ? name : '',
        ])
    )
}

export function columnMappingHog(columns: WidgetInputContractColumnApi[], mapping: Record<string, string>): string {
    if (!columns.length) {
        return 'return rows'
    }
    if (columns.some(({ name }) => !mapping[name])) {
        return ''
    }
    const fields = columns
        .map(({ name }) => `${escapeHogQLString(name)}: row[${escapeHogQLString(mapping[name])}]`)
        .join(', ')
    return `let mapped := [];\nfor (let row in rows) {\n    mapped := arrayPushBack(mapped, {${fields}});\n}\nreturn mapped`
}

export function widgetMappingIssues(
    columns: WidgetInputContractColumnApi[],
    frame: NotebookFrameNodeSummary
): string[] {
    const issues = columns.flatMap(({ name, type }) => {
        const source = frame.columns.find(([sourceName]) => sourceName === name)
        return !source
            ? [`Missing column: ${name}`]
            : source[1] !== type
              ? [`${name}: expected ${type}, found ${source[1]}`]
              : []
    })
    if (!issues.length && !widgetInputMatches(columns, frame)) {
        issues.push('The columns need to be reordered or extra columns removed.')
    }
    return issues
}