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.

4.0 KB
Raw Blame
import type {
	DashboardtypesPanelDTO,
	DashboardtypesPanelSpecDTO,
} from 'api/generated/services/sigNoz.schemas';
import { themeColors } from 'constants/theme';
import getLabelName from 'lib/getLabelName';
import { generateColor } from 'lib/uPlotLib/utils/generateColor';
import { preparePieData } from '../kinds/PieChartPanel/prepareData';
import { getColorGroupLabels } from '../kinds/ScatterPlotPanel/utils/prepareData';
import { getJoinedScalarTable } from './getJoinedScalarTable';
import { getBuilderQueries } from './getBuilderQueries';
import { resolveSeriesLabelV5 } from './resolveSeriesLabel';
import { prepareScalarTables } from 'pages/DashboardPage/DashboardContainer/queryV5/prepareScalarTables';
import type { PanelQueryData } from 'pages/DashboardPage/DashboardContainer/queryV5/types';
import {
	flattenTimeSeries,
	getScalarResults,
	getTimeSeriesResults,
} from 'pages/DashboardPage/DashboardContainer/queryV5/v5ResponseData';

export interface LegendSeries {
	/** Resolved display label — the key `legend.customColors` is indexed by. */
	label: string;
	/** The series' auto-assigned color, shown when no override is set. */
	defaultColor: string;
}

type PanelQueries = DashboardtypesPanelDTO['spec']['queries'];

interface LegendSeriesArgs {
	/** The kind's own spec, for kinds whose labels depend on it. */
	spec: DashboardtypesPanelSpecDTO;
	queries: PanelQueries;
	data: PanelQueryData;
	isDarkMode: boolean;
}

/** Resolves a kind's output into the legend entries the colors control keys overrides by. */
export type LegendSeriesResolver = (args: LegendSeriesArgs) => LegendSeries[];

/**
 * Dedupes `labels` (first-seen order, empties dropped) into `{ label, defaultColor }`
 * pairs, resolving each unique label's color lazily via `colorFor` — so a repeated
 * label never resolves a second color.
 */
function buildLegendSeries(
	labels: readonly string[],
	colorFor: (label: string, index: number) => string,
): LegendSeries[] {
	const byLabel = new Map<string, string>();
	labels.forEach((label, index) => {
		if (label && !byLabel.has(label)) {
			byLabel.set(label, colorFor(label, index));
		}
	});
	return Array.from(byLabel, ([label, defaultColor]) => ({
		label,
		defaultColor,
	}));
}

/**
 * Pie is fed by scalar results, not time series. Reuse the exact slices the renderer
 * draws (without overrides, so their colors are the defaults) so the color control keys
 * overrides by the same labels the chart does.
 */
export function resolvePieLegendSeries({
	data,
	isDarkMode,
}: LegendSeriesArgs): LegendSeries[] {
	const slices = preparePieData({
		tables: prepareScalarTables({
			results: getScalarResults(data.response),
			legendMap: data.legendMap,
			requestPayload: data.requestPayload,
		}),
		isDarkMode,
	});
	return buildLegendSeries(
		slices.map((slice) => slice.label),
		(_, index) => slices[index].color,
	);
}

/**
 * Time-series kinds: resolve each flattened series' label the way the renderer does
 * (`getLabelName` → `resolveSeriesLabelV5`) and color it with `generateColor`.
 */
export function resolveTimeSeriesLegendSeries({
	queries,
	data,
	isDarkMode,
}: LegendSeriesArgs): LegendSeries[] {
	const palette = isDarkMode
		? themeColors.chartcolors
		: themeColors.lightModeColor;
	const builderQueries = getBuilderQueries(queries);
	const series = flattenTimeSeries(
		getTimeSeriesResults(data.response),
		data.legendMap,
	);
	return buildLegendSeries(
		series.map((s) =>
			resolveSeriesLabelV5(
				s,
				builderQueries,
				getLabelName(s.labels, s.queryName, s.legend),
			),
		),
		(label) => generateColor(label, palette),
	);
}

/** One entry per colour group, labelled the way the renderer labels its series. */
export function resolveScatterLegendSeries({
	spec,
	data,
	isDarkMode,
}: LegendSeriesArgs): LegendSeries[] {
	if (spec.plugin.kind !== 'signoz/ScatterPlotPanel') {
		return [];
	}
	const palette = isDarkMode
		? themeColors.chartcolors
		: themeColors.lightModeColor;
	return buildLegendSeries(
		getColorGroupLabels(
			getJoinedScalarTable(data),
			spec.plugin.spec.dimensions?.colorBy,
		),
		(label) => generateColor(label, palette),
	);
}