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.7 KB
Raw Blame
import {
	DashboardtypesVariableEnvelopeGithubComSigNozSignozPkgTypesDashboardtypesTextVariableSpecDTOKind as TextEnvelopeKind,
	DashboardtypesVariableEnvelopeGithubComSigNozSignozPkgTypesDashboardtypesListVariableSpecDTOKind as ListEnvelopeKind,
	DashboardtypesVariablePluginVariantGithubComSigNozSignozPkgTypesDashboardtypesCustomVariableSpecDTOKind as CustomPluginKind,
	DashboardtypesVariablePluginVariantGithubComSigNozSignozPkgTypesDashboardtypesDynamicVariableSpecDTOKind as DynamicPluginKind,
	DashboardtypesVariablePluginVariantGithubComSigNozSignozPkgTypesDashboardtypesQueryVariableSpecDTOKind as QueryPluginKind,
} from 'api/generated/services/sigNoz.schemas';
import type {
	DashboardtypesListVariableSpecDTO,
	DashboardtypesVariableDTO,
	DashboardtypesVariablePluginDTO,
	DashboardtypesTextVariableSpecDTO,
} from 'api/generated/services/sigNoz.schemas';

import {
	emptyVariableFormModel,
	VARIABLE_SORT_DISABLED,
	type VariableFormModel,
} from './variableFormModel';

/** DTO envelope → flat form model (for display / editing). */
export function dtoToFormModel(
	dto: DashboardtypesVariableDTO,
): VariableFormModel {
	const base = emptyVariableFormModel();
	const display = dto.spec.display;
	const common: VariableFormModel = {
		...base,
		// TODO
		name: dto.spec?.name ?? display?.name ?? '',
		description: display?.description ?? '',
	};

	// Text variable — a distinct envelope (no list plugin).
	if (dto.kind === TextEnvelopeKind.TextVariable) {
		const spec = dto.spec as DashboardtypesTextVariableSpecDTO;
		return {
			...common,
			type: 'TEXT',
			textValue: spec.value ?? '',
			textConstant: spec.constant ?? false,
		};
	}

	// List variable — Query / Custom / Dynamic, distinguished by plugin.kind.
	const spec = dto.spec as DashboardtypesListVariableSpecDTO;
	const listCommon: VariableFormModel = {
		...common,
		multiSelect: spec.allowMultiple ?? false,
		showAllOption: spec.allowAllValue ?? false,
		sort: spec.sort ?? VARIABLE_SORT_DISABLED,
		defaultValue: spec.defaultValue,
	};
	const plugin = spec.plugin;

	if (plugin?.kind === CustomPluginKind['signoz/CustomVariable']) {
		return {
			...listCommon,
			type: 'CUSTOM',
			customValue: plugin.spec.customValue ?? '',
		};
	}
	if (plugin?.kind === DynamicPluginKind['signoz/DynamicVariable']) {
		return {
			...listCommon,
			type: 'DYNAMIC',
			dynamicAttribute: plugin.spec.name ?? '',
			// signal is a required wire field (`all` = every telemetry signal), used as-is.
			dynamicSignal: plugin.spec.signal,
		};
	}
	// Default to Query (also covers a query plugin or a missing/unknown plugin).
	return {
		...listCommon,
		type: 'QUERY',
		queryValue:
			plugin?.kind === QueryPluginKind['signoz/QueryVariable']
				? (plugin.spec.queryValue ?? '')
				: '',
	};
}

function buildPlugin(
	model: VariableFormModel,
): DashboardtypesVariablePluginDTO {
	switch (model.type) {
		case 'CUSTOM':
			return {
				kind: CustomPluginKind['signoz/CustomVariable'],
				spec: { customValue: model.customValue },
			};
		case 'DYNAMIC':
			return {
				kind: DynamicPluginKind['signoz/DynamicVariable'],
				spec: {
					name: model.dynamicAttribute,
					signal: model.dynamicSignal,
				},
			};
		case 'QUERY':
		default:
			return {
				kind: QueryPluginKind['signoz/QueryVariable'],
				spec: { queryValue: model.queryValue },
			};
	}
}

/** Flat form model → DTO envelope (for persistence). */
export function formModelToDto(
	model: VariableFormModel,
): DashboardtypesVariableDTO {
	const display = {
		name: model.name,
		description: model.description,
	};

	if (model.type === 'TEXT') {
		return {
			kind: TextEnvelopeKind.TextVariable,
			spec: {
				name: model.name,
				display,
				value: model.textValue,
				constant: model.textConstant,
			},
		};
	}

	return {
		kind: ListEnvelopeKind.ListVariable,
		spec: {
			name: model.name,
			display,
			allowMultiple: model.multiSelect,
			// Dynamic variables always expose the aggregate "ALL" entry (matches V1, which
			// forced showALLOption true on save); other types respect the toggle. Either
			// way it needs multi-select — ALL is a set of values, and the API rejects the
			// flag without it, which used to make a single-select dynamic variable
			// unsaveable and blocked every other edit to the dashboard with it.
			allowAllValue:
				model.multiSelect &&
				(model.type === 'DYNAMIC' ? true : model.showAllOption),
			// model.sort is already a Perses sort token (`none` / `alphabetical-*`).
			sort: model.sort,
			defaultValue: model.defaultValue,
			plugin: buildPlugin(model),
		},
	};
}

/** Maps the V2 plugin/envelope to the four UI-facing variable types. */
export function variableTypeOf(
	dto: DashboardtypesVariableDTO,
): VariableFormModel['type'] {
	return dtoToFormModel(dto).type;
}