mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
d8630bf9e4
commit
52550966af
@ -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');
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user