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.

2.9 KB
Raw Blame
/* eslint-disable sonarjs/cognitive-complexity */
import { PANEL_TYPES } from 'constants/queryBuilder';
import { themeColors } from 'constants/theme';
import { getLegend } from 'lib/dashboard/getQueryResults';
import getLabelName from 'lib/getLabelName';
import { isUndefined } from 'lodash-es';
import { Query } from 'types/api/queryBuilder/queryBuilderData';
import { QueryData } from 'types/api/widgets/getQuery';

import { drawStyles, lineInterpolations } from './constants';
import { generateColor } from './generateColor';
import getRenderer from './getRenderer';

const paths = (
	u: any,
	seriesIdx: number,
	idx0: number,
	idx1: number,
	extendGap: boolean,
	buildClip: boolean,
): any => {
	const s = u.series[seriesIdx];
	const style = s.drawStyle;
	const interp = s.lineInterpolation;

	const renderer = getRenderer(style, interp);

	return renderer(u, seriesIdx, idx0, idx1, extendGap, buildClip);
};

const getSeries = ({
	series,
	widgetMetaData,
	graphsVisibilityStates,
	panelType,
	hiddenGraph,
	isDarkMode,
	colorMapping,
	currentQuery,
}: GetSeriesProps): uPlot.Options['series'] => {
	const configurations: uPlot.Series[] = [
		{ label: 'Timestamp', stroke: 'purple' },
	];

	const seriesList = series || [];

	const newGraphVisibilityStates = graphsVisibilityStates?.slice(1);

	for (let i = 0; i < seriesList?.length; i += 1) {
		const { metric = {}, queryName = '', legend = '' } = widgetMetaData[i] || {};

		const baseLabelName = getLabelName(
			metric,
			queryName || '', // query
			legend || '',
		);

		const label = currentQuery
			? getLegend(widgetMetaData[i], currentQuery, baseLabelName)
			: baseLabelName;

		const color =
			colorMapping?.[label || ''] ||
			generateColor(
				label || '',
				isDarkMode ? themeColors.chartcolors : themeColors.lightModeColor,
			);

		const pointSize = seriesList[i]?.values?.length > 1 ? 5 : 10;
		const showPoints = !(seriesList[i]?.values?.length > 1);

		const seriesObj: any = {
			paths,
			drawStyle:
				panelType && panelType === PANEL_TYPES.BAR
					? drawStyles.bars
					: drawStyles.line,
			lineInterpolation:
				panelType && panelType === PANEL_TYPES.BAR
					? null
					: lineInterpolations.spline,
			show: newGraphVisibilityStates
				? newGraphVisibilityStates[i]
				: !isUndefined(hiddenGraph)
					? hiddenGraph[i]
					: true,
			label,
			fill: panelType && panelType === PANEL_TYPES.BAR ? `${color}` : undefined,
			stroke: color,
			width: 2,
			spanGaps: true,
			points: {
				size: pointSize,
				show: showPoints,
				stroke: color,
			},
		};

		configurations.push(seriesObj);
	}

	return configurations;
};

export type GetSeriesProps = {
	series?: QueryData[];
	widgetMetaData: QueryData[];
	isDarkMode: boolean;
	graphsVisibilityStates?: boolean[];
	panelType?: PANEL_TYPES;
	currentQuery?: Query;
	stackBarChart?: boolean;
	hiddenGraph?: {
		[key: string]: boolean;
	};
	colorMapping?: Record<string, string>;
};

export default getSeries;