Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: updated the logic for variable update queue (#6586) #6788

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,17 @@ function DashboardVariableSelection(): JSX.Element | null {
const depGrp = buildDependencies(variablesTableData);
const { order, graph } = buildDependencyGraph(depGrp);
const parentDependencyGraph = buildParentDependencyGraph(graph);

// cleanup order to only include variables that are of type 'QUERY'
const cleanedOrder = order.filter((variable) => {
SagarRajput-7 marked this conversation as resolved.
Show resolved Hide resolved
const variableData = variablesTableData.find(
(v: IDashboardVariable) => v.name === variable,
);
return variableData?.type === 'QUERY';
});

setDependencyData({
order,
order: cleanedOrder,
graph,
parentDependencyGraph,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEmpty } from 'lodash-es';
import { isEmpty, isNull } from 'lodash-es';
import { Dashboard, IDashboardVariable } from 'types/api/dashboard/getAll';

export function areArraysEqual(
Expand Down Expand Up @@ -31,29 +31,39 @@ export const convertVariablesToDbFormat = (
return result;
}, {});

const getDependentVariables = (queryValue: string): string[] => {
// Combined pattern for all formats:
// {{.variable_name}} - original format
// $variable_name - dollar prefix format
// [[variable_name]] - square bracket format
// {{variable_name}} - without dot format
const variableRegexPattern = /(?:\{\{\s*\.?([^\s}]+)\s*\}\}|\$([^\s\W]+)|\[\[([^\]]+)\]\])/g;

const matches = queryValue.match(variableRegexPattern);

// Extract variable names from the matches array, handling all formats
return matches
? matches.map((match) => {
if (match.startsWith('$')) {
return match.slice(1); // Remove $ prefix
}
if (match.startsWith('[[')) {
return match.slice(2, -2); // Remove [[ and ]]
const getDependentVariablesBasedOnVariableName = (
variableName: string,
variables: IDashboardVariable[],
): string[] => {
if (!variables || !Array.isArray(variables)) {
return [];
}

return variables
?.map((variable: any) => {
if (variable.type === 'QUERY') {
// Combined pattern for all formats
// {{.variable_name}} - original format
// $variable_name - dollar prefix format
// [[variable_name]] - square bracket format
// {{variable_name}} - without dot format
const patterns = [
`\\{\\{\\s*?\\.${variableName}\\s*?\\}\\}`, // {{.var}}
`\\{\\{\\s*${variableName}\\s*\\}\\}`, // {{var}}
`\\$${variableName}\\b`, // $var
`\\[\\[\\s*${variableName}\\s*\\]\\]`, // [[var]]
];
const combinedRegex = new RegExp(patterns.join('|'));

const queryValue = variable.queryValue || '';
const dependVarReMatch = queryValue.match(combinedRegex);
if (dependVarReMatch !== null && dependVarReMatch.length > 0) {
return variable.name;
}
// Handle both {{.var}} and {{var}} formats
return match.replace(/\{\{\s*\.?([^\s}]+)\s*\}\}/, '$1');
})
: [];
}
return null;
})
.filter((val: string | null) => !isNull(val));
};
export type VariableGraph = Record<string, string[]>;

Expand All @@ -64,24 +74,21 @@ export const buildDependencies = (

// Initialize empty arrays for all variables first
variables.forEach((variable) => {
if (variable.name && variable.type === 'QUERY') {
if (variable.name) {
graph[variable.name] = [];
}
});

// For each QUERY variable, add it as a dependent to its referenced variables
variables.forEach((variable) => {
if (variable.type === 'QUERY' && variable.name) {
const dependentVariables = getDependentVariables(variable.queryValue || '');
if (variable.name) {
const dependentVariables = getDependentVariablesBasedOnVariableName(
variable.name,
variables,
);

// For each referenced variable, add the current query as a dependent
dependentVariables.forEach((referencedVar) => {
if (graph[referencedVar]) {
graph[referencedVar].push(variable.name as string);
} else {
graph[referencedVar] = [variable.name as string];
}
});
graph[variable.name] = dependentVariables;
}
});

Expand Down
Loading