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.7 KB
Raw Blame
import type { VariableType } from '../../DashboardSettings/Variables/variableFormModel';
import type {
	SelectedVariableValue,
	VariableSelection,
	VariableSelectionMap,
} from '../selectionTypes';

/** A selection counts as resolved (usable as a parent value) when it's non-empty. */
export function isResolved(selection?: VariableSelection): boolean {
	if (!selection) {
		return false;
	}
	if (selection.allSelected) {
		return true;
	}
	const { value } = selection;
	if (Array.isArray(value)) {
		return value.length > 0;
	}
	return value !== '' && value !== null && value !== undefined;
}

/**
 * Whether a selection carries a value usable when scheduling a dependent
 * variable/panel fetch. Unlike {@link isResolved}, an ALL selection counts only
 * once it holds the concrete option array — for QUERY that's after its fetch, for
 * CUSTOM it's materialized synchronously (no fetch). A DYNAMIC ALL is usable
 * immediately via the `__all__` sentinel.
 */
export function hasUsableValue(
	selection: VariableSelection | undefined,
	type: VariableType | undefined,
): boolean {
	if (!selection) {
		return false;
	}
	if (selection.allSelected) {
		if (type === 'DYNAMIC') {
			return true;
		}
		return Array.isArray(selection.value) && selection.value.length > 0;
	}
	const { value } = selection;
	if (Array.isArray(value)) {
		return value.length > 0;
	}
	return value !== '' && value !== null && value !== undefined;
}

interface CommittedValues {
	values: string[];
	options: string[];
	showAllOption: boolean;
	emptyFallback: VariableSelection;
}

/**
 * The selection a multi-select commit resolves to. Options are known only here, so
 * this is where a value the list never offered is recorded as typed in.
 */
export function selectionFromCommittedValues({
	values,
	options,
	showAllOption,
	emptyFallback,
}: CommittedValues): VariableSelection {
	if (values.length === 0) {
		return emptyFallback;
	}

	const customValues = values.filter((value) => !options.includes(value));
	// ALL re-materializes to the option set, so a set carrying a typed value is not ALL
	// — the next refetch would expand it back and drop what the user typed.
	const allSelected =
		showAllOption &&
		options.length > 0 &&
		customValues.length === 0 &&
		options.every((option) => values.includes(option));

	return {
		value: values,
		allSelected,
		...(customValues.length > 0 && { customValues }),
	};
}

/** Flatten the selection map into the `{ name: value }` payload a query expects. */
export function selectionToPayload(
	selection: VariableSelectionMap,
): Record<string, SelectedVariableValue> {
	const payload: Record<string, SelectedVariableValue> = {};
	Object.entries(selection).forEach(([name, sel]) => {
		payload[name] = sel.value;
	});
	return payload;
}