Files
grafana/public/app/plugins/datasource/testdata/metricTree.ts
Hugo Häggmark cb0e80e7b9 TemplateVariables: Introduces $__searchFilter to Query Variables (#19858)
* WIP: Initial hardcoded version

* Feature: Introduces SearchFiltering to Graphite

* Feature: Adds searchFiltering to MySql

* Tests: Adds tests to Graphite and MySql

* Feature: Adds $__searchFilter to TestData

* Refactor: Adds searchFilter to Postgres and extracts function

* Tests: Adds tests to variable

* Refactor: Adds debounce and lodash import optimization

* Docs: Adds documentation

* Refactor: Removes unused function and fixes typo

* Docs: Updates docs

* Fixed issue with UI not updating when no  was used due to async func and no .apply in the non lazy path
2019-10-18 11:40:08 +02:00

76 lines
1.7 KiB
TypeScript

export interface TreeNode {
name: string;
children: TreeNode[];
}
/*
* Builds a nested tree like
* [
* {
* name: 'A',
* children: [
* { name: 'AA', children: [] },
* { name: 'AB', children: [] },
* ]
* }
* ]
*/
function buildMetricTree(parent: string, depth: number): TreeNode[] {
const chars = ['A', 'B', 'C'];
const children: TreeNode[] = [];
if (depth > 3) {
return [];
}
for (const letter of chars) {
const nodeName = `${parent}${letter}`;
children.push({
name: nodeName,
children: buildMetricTree(nodeName, depth + 1),
});
}
return children;
}
function queryTree(children: TreeNode[], query: string[], queryIndex: number): TreeNode[] {
if (queryIndex >= query.length) {
return children;
}
if (query[queryIndex] === '*') {
return children;
}
const nodeQuery = query[queryIndex];
let result: TreeNode[] = [];
let namesToMatch = [nodeQuery];
// handle glob queries
if (nodeQuery.startsWith('{')) {
namesToMatch = nodeQuery.replace(/\{|\}/g, '').split(',');
}
for (const node of children) {
for (const nameToMatch of namesToMatch) {
if (nameToMatch.indexOf('*') !== -1) {
const pattern = nameToMatch.replace('*', '');
const regex = new RegExp(`^${pattern}.*`, 'gi');
if (regex.test(node.name)) {
result = result.concat(queryTree([node], query, queryIndex + 1));
}
} else if (node.name === nameToMatch) {
result = result.concat(queryTree(node.children, query, queryIndex + 1));
}
}
}
return result;
}
export function queryMetricTree(query: string): TreeNode[] {
const children = buildMetricTree('', 0);
return queryTree(children, query.split('.'), 0);
}