Prometheus: fix rules expansion

Rules expansion (available via query hints in explore) was broken for expressions that contained selectors.

- fix replacing regexp to recognize `{` and `[` as the end of a rule name
- moved logic to language utils
- added tests
This commit is contained in:
David Kaltschmidt
2018-11-16 14:31:51 +00:00
parent ac8731b9fb
commit e3e8be16b3
3 changed files with 32 additions and 6 deletions

View File

@@ -83,3 +83,9 @@ export function parseSelector(query: string, cursorOffset = 1): { labelKeys: any
return { labelKeys, selector: selectorString };
}
export function expandRecordingRules(query: string, mapping: { [name: string]: string }): string {
const ruleNames = Object.keys(mapping);
const rulesRegex = new RegExp(`(\\s|^)(${ruleNames.join('|')})(\\s|$|\\(|\\[|\\{)`, 'ig');
return query.replace(rulesRegex, (match, pre, name, post) => `${pre}${mapping[name]}${post}`);
}