Files
grafana/public/app/plugins/datasource/testdata/metricTree.ts
Torkel Ödegaard 832b67db38 TestData: Query variable support (nested + glob queries) (#18413)
* TestData: added support for nested data source variable queries, and test dashboard

* Added drilldown dashboards

* Fixed typescript issue
2019-08-06 18:17:12 +02:00

66 lines
1.4 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 (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 (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);
}