Prometheus: Query variable editor persist query type on clicking run query (#74045)

run query to save query type selection
This commit is contained in:
Brendan O'Handley 2023-08-30 09:52:23 -04:00 committed by GitHub
parent 2a835301c3
commit 42662667d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 23 deletions

View File

@ -170,7 +170,7 @@ describe('PromVariableQueryEditor', () => {
});
});
test('Calls onChange for label_names() query', async () => {
test('Calls onChange for label_names, label_values, metrics, and query result queries', async () => {
const onChange = jest.fn();
props.query = {
@ -181,21 +181,18 @@ describe('PromVariableQueryEditor', () => {
render(<PromVariableQueryEditor {...props} onChange={onChange} />);
await selectOptionInTest(screen.getByLabelText('Query type'), 'Label names');
await selectOptionInTest(screen.getByLabelText('Query type'), 'Label values');
await selectOptionInTest(screen.getByLabelText('Query type'), 'Metrics');
await selectOptionInTest(screen.getByLabelText('Query type'), 'Query result');
expect(onChange).toHaveBeenCalledWith({
query: 'label_names()',
refId,
});
expect(onChange).toHaveBeenCalledTimes(4);
});
test('Does not call onChange for other queries', async () => {
test('Does not call onChange for series query', async () => {
const onChange = jest.fn();
render(<PromVariableQueryEditor {...props} onChange={onChange} />);
await selectOptionInTest(screen.getByLabelText('Query type'), 'Label values');
await selectOptionInTest(screen.getByLabelText('Query type'), 'Metrics');
await selectOptionInTest(screen.getByLabelText('Query type'), 'Query result');
await selectOptionInTest(screen.getByLabelText('Query type'), 'Series query');
expect(onChange).not.toHaveBeenCalled();

View File

@ -135,8 +135,8 @@ export const PromVariableQueryEditor = ({ onChange, query, datasource }: Props)
/** Call onchange for label names query type change */
const onQueryTypeChange = (newType: SelectableValue<QueryType>) => {
setQryType(newType.value);
if (newType.value === QueryType.LabelNames) {
onChangeWithVariableString({ qryType: newType.value });
if (newType.value !== QueryType.SeriesQuery) {
onChangeWithVariableString({ qryType: newType.value ?? 0 });
}
};

View File

@ -38,11 +38,11 @@ export function migrateVariableQueryToEditor(rawQuery: string | PromVariableQuer
};
}
const labelValues = rawQuery.match(PrometheusLabelValuesRegex);
if (labelValues) {
const label = labelValues[2];
const metric = labelValues[1];
const labelValuesCheck = rawQuery.match(/^label_values\(/);
if (labelValuesCheck) {
const labelValues = rawQuery.match(PrometheusLabelValuesRegex);
const label = labelValues ? labelValues[2] : '';
const metric = labelValues ? labelValues[1] : '';
if (metric) {
const visQuery = buildVisualQueryFromString(metric);
@ -62,26 +62,30 @@ export function migrateVariableQueryToEditor(rawQuery: string | PromVariableQuer
}
}
const metricNames = rawQuery.match(PrometheusMetricNamesRegex);
if (metricNames) {
const metricNamesCheck = rawQuery.match(/^metrics\(/);
if (metricNamesCheck) {
const metricNames = rawQuery.match(PrometheusMetricNamesRegex);
const metric = metricNames ? metricNames[1] : '';
return {
...queryBase,
qryType: QueryType.MetricNames,
metric: metricNames[1],
metric,
};
}
const queryResult = rawQuery.match(PrometheusQueryResultRegex);
if (queryResult) {
const queryResultCheck = rawQuery.match(/^query_result\(/);
if (queryResultCheck) {
const queryResult = rawQuery.match(PrometheusQueryResultRegex);
const varQuery = queryResult ? queryResult[1] : '';
return {
...queryBase,
qryType: QueryType.VarQueryResult,
varQuery: queryResult[1],
varQuery,
};
}
// seriesQuery does not have a function and no regex above
if (!labelNames && !labelValues && !metricNames && !queryResult) {
if (!labelNames && !labelValuesCheck && !metricNamesCheck && !queryResultCheck) {
return {
...queryBase,
qryType: QueryType.SeriesQuery,