datatrails: allow multiple search terms to help select metric names (#81032)

* datatrails: allow multiple search terms help select metric names
This commit is contained in:
Darren Janeczek 2024-01-23 09:54:19 -05:00 committed by GitHub
parent d8630bf9e4
commit 52550966af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -115,7 +115,7 @@ export class MetricSelectScene extends SceneObjectBase<MetricSelectSceneState> {
return;
}
const searchRegex = new RegExp(this.state.searchQuery ?? '.*');
const searchRegex = createSearchRegExp(this.state.searchQuery);
const metricNames = variable.state.options;
const sortedMetricNames =
trail.state.metric !== undefined ? sortRelatedMetrics(metricNames, trail.state.metric) : metricNames;
@ -126,7 +126,8 @@ export class MetricSelectScene extends SceneObjectBase<MetricSelectSceneState> {
const metric = sortedMetricNames[index];
const metricName = String(metric.value);
if (!metricName.match(searchRegex)) {
if (searchRegex && !searchRegex.test(metricName)) {
continue;
}
@ -319,3 +320,26 @@ function getStyles(theme: GrafanaTheme2) {
}),
};
}
// Consider any sequence of characters not permitted for metric names as a sepratator
const splitSeparator = /[^a-z0-9_:]+/;
function createSearchRegExp(spaceSeparatedMetricNames?: string) {
if (!spaceSeparatedMetricNames) {
return null;
}
const searchParts = spaceSeparatedMetricNames
?.toLowerCase()
.split(splitSeparator)
.filter((part) => part.length > 0)
.map((part) => `(?=(.*${part}.*))`);
if (searchParts.length === 0) {
return null;
}
const regex = searchParts.join('');
// (?=(.*expr1.*))(?=().*expr2.*))...
// The ?=(...) lookahead allows us to match these in any order.
return new RegExp(regex, 'igy');
}