diff --git a/frontend/src/container/NewDashboard/DashboardVariablesSelection/__test__/dashboardVariables.test.tsx b/frontend/src/container/NewDashboard/DashboardVariablesSelection/__test__/dashboardVariables.test.tsx new file mode 100644 index 00000000000..f1270631e02 --- /dev/null +++ b/frontend/src/container/NewDashboard/DashboardVariablesSelection/__test__/dashboardVariables.test.tsx @@ -0,0 +1,60 @@ +import { onUpdateVariableNode } from '../util'; +import { onUpdateVariableNodeMock } from './mock'; + +describe('dashboardVariables - utilities and processors', () => { + const { graph, topologicalOrder } = onUpdateVariableNodeMock; + + test.each([ + { + scenario: 'root element', + nodeToUpdate: 'deployment_environment', + expected: [ + 'deployment_environment', + 'service_name', + 'endpoint', + 'http_status_code', + ], + }, + { + scenario: 'middle child', + nodeToUpdate: 'k8s_node_name', + expected: ['k8s_node_name', 'k8s_namespace_name'], + }, + { + scenario: 'leaf element', + nodeToUpdate: 'http_status_code', + expected: ['http_status_code'], + }, + { + scenario: 'node not in graph', + nodeToUpdate: 'unknown', + expected: [], + }, + { + scenario: 'node not in topological order', + nodeToUpdate: 'unknown', + expected: [], + }, + ])( + 'should update variable node when $scenario', + ({ nodeToUpdate, expected }) => { + const updatedVariables: string[] = []; + + onUpdateVariableNode(nodeToUpdate, graph, topologicalOrder, (node) => + updatedVariables.push(node), + ); + + expect(updatedVariables).toEqual(expected); + }, + ); + + it('should return empty array when topological order is empty', () => { + const updatedVariables: string[] = []; + + onUpdateVariableNode('http_status_code', graph, [], (node) => + updatedVariables.push(node), + ); + + expect(updatedVariables).toEqual([]); + }); +}); diff --git a/frontend/src/container/NewDashboard/DashboardVariablesSelection/__test__/mock.ts b/frontend/src/container/NewDashboard/DashboardVariablesSelection/__test__/mock.ts new file mode 100644 index 00000000000..2ce9ee9d301 --- /dev/null +++ b/frontend/src/container/NewDashboard/DashboardVariablesSelection/__test__/mock.ts @@ -0,0 +1,56 @@ +export const checkAPIInvocationMock = { + variablesToGetUpdated: [], + variableData: { + name: 'k8s_namespace_name', + key: '937ecbae-b24b-4d6d-8cc4-5d5b8d53569b', + customValue: '', + description: '', + id: '937ecbae-b24b-4d6d-8cc4-5d5b8d53569b', + modificationUUID: '8ad2442d-8b4d-4c64-848e-af847d1d0eec', + multiSelect: false, + order: 7, + queryValue: + "SELECT JSONExtractString(labels, 'k8s_namespace_name') AS k8s_namespace_name\nFROM signoz_metrics.distributed_time_series_v4_1day\nWHERE metric_name = 'k8s_pod_cpu_time' AND JSONExtractString(labels, 'k8s_cluster_name') = {{.k8s_cluster_name}} AND JSONExtractString(labels, 'k8s_node_name') IN {{.k8s_node_name}}\nGROUP BY k8s_namespace_name", + showALLOption: false, + sort: 'DISABLED', + textboxValue: '', + type: 'QUERY', + selectedValue: 'saasmonitor', + allSelected: false, + }, + parentDependencyGraph: { + deployment_environment: [], + service_name: ['deployment_environment'], + endpoint: ['deployment_environment', 'service_name'], + http_status_code: ['endpoint'], + k8s_cluster_name: [], + environment: [], + k8s_node_name: ['k8s_cluster_name'], + k8s_namespace_name: ['k8s_cluster_name', 'k8s_node_name'], + }, +}; + +export const onUpdateVariableNodeMock = { + nodeToUpdate: 'deployment_environment', + graph: { + deployment_environment: ['service_name', 'endpoint'], + service_name: ['endpoint'], + endpoint: ['http_status_code'], + http_status_code: [], + k8s_cluster_name: ['k8s_node_name', 'k8s_namespace_name'], + environment: [], + k8s_node_name: ['k8s_namespace_name'], + k8s_namespace_name: [], + }, + topologicalOrder: [ + 'deployment_environment', + 'k8s_cluster_name', + 'environment', + 'service_name', + 'k8s_node_name', + 'endpoint', + 'k8s_namespace_name', + 'http_status_code', + ], + callback: jest.fn(), +}; diff --git a/frontend/src/container/NewDashboard/DashboardVariablesSelection/util.ts b/frontend/src/container/NewDashboard/DashboardVariablesSelection/util.ts index 9a2c62f395f..470c0bc3d86 100644 --- a/frontend/src/container/NewDashboard/DashboardVariablesSelection/util.ts +++ b/frontend/src/container/NewDashboard/DashboardVariablesSelection/util.ts @@ -32,6 +32,7 @@ export const convertVariablesToDbFormat = ( }, {}); const getDependentVariables = (queryValue: string): string[] => { + console.log('getDependentVariables', queryValue); const variableRegexPattern = /\{\{\s*?\.([^\s}]+)\s*?\}\}/g; const matches = queryValue.match(variableRegexPattern); @@ -46,6 +47,7 @@ export type VariableGraph = Record; export const buildDependencies = ( variables: IDashboardVariable[], ): VariableGraph => { + console.log('buildDependencies', variables); const graph: VariableGraph = {}; // Initialize empty arrays for all variables first @@ -78,6 +80,7 @@ export const buildDependencies = ( export const buildDependencyGraph = ( dependencies: VariableGraph, ): { order: string[]; graph: VariableGraph } => { + console.log('buildDependencyGraph', dependencies); const inDegree: Record = {}; const adjList: VariableGraph = {}; @@ -124,6 +127,7 @@ export const onUpdateVariableNode = ( topologicalOrder: string[], callback: (node: string) => void, ): void => { + console.log('onUpdateVariableNode', nodeToUpdate, graph, topologicalOrder); const visited = new Set(); // Start processing from the node to update @@ -141,6 +145,7 @@ export const onUpdateVariableNode = ( export const buildParentDependencyGraph = ( graph: VariableGraph, ): VariableGraph => { + console.log('buildParentDependencyGraph', graph); const parentGraph: VariableGraph = {}; // Initialize empty arrays for all nodes