Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

3.0 KB
Raw Blame
import { useMemo } from 'react';
import type { DashboardtypesPanelDTO } from 'api/generated/services/sigNoz.schemas';
import { getQueryPanelDefinition } from 'pages/DashboardPage/DashboardContainer/Panels/capabilities';
import type { PanelFormattingSlice } from 'pages/DashboardPage/DashboardContainer/Panels/types/sections';
import { getColumnUnit } from 'pages/DashboardPage/DashboardContainer/Panels/utils/getColumnUnit';
import { getValueColumnLabels } from 'pages/DashboardPage/DashboardContainer/Panels/utils/getValueColumnLabels';
import { prepareScalarTables } from 'pages/DashboardPage/DashboardContainer/queryV5/prepareScalarTables';
import type { PanelQueryData } from 'pages/DashboardPage/DashboardContainer/queryV5/types';
import { getScalarResults } from 'pages/DashboardPage/DashboardContainer/queryV5/v5ResponseData';

export interface TableColumnOption {
	/**
	 * Key the column's unit / threshold is stored under — the query identifier
	 * (`queryName`, or `queryName.expression` for multi-aggregation queries). Matches
	 * `PanelTableColumn.id` and the rendered column's `dataIndex` (V1 parity: column
	 * units and thresholds are keyed by the query, not the display name).
	 */
	key: string;
	/** Display label shown in the editor — the resolved column name. */
	label: string;
	/** The column's own name (alias/legend/expression), without the query prefix. */
	name: string;
	/**
	 * The column's configured unit (`formatting.columnUnits[key]`), if any. The
	 * per-column threshold editor scopes its unit picker to this unit's category
	 * (V1 parity), since Table panels have no single panel-wide unit.
	 */
	unit?: string;
}

/**
 * Resolves a Table panel's value (aggregation) columns into `{ key, label }`
 * options, so the table-only config editors (column units, per-column thresholds)
 * store the query-keyed value the renderer looks up by while showing the readable
 * column name. Empty for kinds that don't join scalar rows, or before data arrives.
 */
export function useTableColumns(
	panel: DashboardtypesPanelDTO,
	data: PanelQueryData,
): TableColumnOption[] {
	return useMemo(() => {
		if (
			!getQueryPanelDefinition(panel.spec.plugin.kind)?.queryCapabilities
				.formatTableResultForUI
		) {
			return [];
		}
		const table = prepareScalarTables({
			results: getScalarResults(data.response),
			legendMap: data.legendMap,
			requestPayload: data.requestPayload,
		}).find((candidate) => candidate.columns.length > 0);
		if (!table) {
			return [];
		}
		const columnUnits =
			(panel.spec.plugin.spec as { formatting?: PanelFormattingSlice }).formatting
				?.columnUnits ?? {};
		const labels = getValueColumnLabels(table.columns);
		return table.columns
			.filter((column) => column.isValueColumn)
			.map((column) => {
				const key = column.id || column.name;
				return {
					key,
					label: labels[key] ?? column.name,
					name: column.name,
					unit: getColumnUnit(key, columnUnits),
				};
			});
	}, [
		panel.spec.plugin.kind,
		panel.spec.plugin.spec,
		data.response,
		data.legendMap,
		data.requestPayload,
	]);
}