Handle scalar and string resultType separately (#51411)

This commit is contained in:
ismail simsek 2022-06-29 16:43:33 +02:00 committed by GitHub
parent f654152dc8
commit 0de544f3a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 25 deletions

View File

@ -10318,7 +10318,7 @@ exports[`better eslint`] = {
[327, 27, 45, "Do not use any type assertions.", "881765776"],
[330, 29, 45, "Do not use any type assertions.", "881765776"]
],
"public/app/plugins/datasource/prometheus/metric_find_query.test.ts:142842024": [
"public/app/plugins/datasource/prometheus/metric_find_query.test.ts:1350978765": [
[12, 6, 59, "Do not use any type assertions.", "3685154675"],
[12, 6, 49, "Do not use any type assertions.", "1184085652"],
[18, 25, 178, "Do not use any type assertions.", "2743844758"],
@ -10330,18 +10330,15 @@ exports[`better eslint`] = {
[47, 5, 3, "Unexpected any. Specify a different type.", "193409811"],
[59, 38, 3, "Unexpected any. Specify a different type.", "193409811"],
[60, 42, 70, "Do not use any type assertions.", "164910658"],
[60, 42, 53, "Do not use any type assertions.", "1065771343"],
[165, 21, 3, "Unexpected any. Specify a different type.", "193409811"],
[216, 21, 3, "Unexpected any. Specify a different type.", "193409811"],
[240, 21, 3, "Unexpected any. Specify a different type.", "193409811"]
[60, 42, 53, "Do not use any type assertions.", "1065771343"]
],
"public/app/plugins/datasource/prometheus/metric_find_query.ts:3246701167": [
"public/app/plugins/datasource/prometheus/metric_find_query.ts:1417569751": [
[62, 70, 3, "Unexpected any. Specify a different type.", "193409811"],
[83, 72, 3, "Unexpected any. Specify a different type.", "193409811"],
[96, 72, 3, "Unexpected any. Specify a different type.", "193409811"],
[122, 70, 3, "Unexpected any. Specify a different type.", "193409811"],
[140, 43, 35, "Do not use any type assertions.", "2712117061"],
[175, 70, 3, "Unexpected any. Specify a different type.", "193409811"]
[188, 70, 3, "Unexpected any. Specify a different type.", "193409811"]
],
"public/app/plugins/datasource/prometheus/query_hints.test.ts:3821515673": [
[45, 23, 28, "Do not use any type assertions.", "252196522"]

View File

@ -163,7 +163,7 @@ describe('PrometheusMetricFindQuery', () => {
],
},
});
const results: any = await query.process();
const results = await query.process();
expect(results).toHaveLength(2);
expect(results[0].text).toBe('value1');
@ -214,7 +214,7 @@ describe('PrometheusMetricFindQuery', () => {
},
},
});
const results: any = await query.process();
const results = await query.process();
expect(results).toHaveLength(1);
expect(results[0].text).toBe('metric{job="testjob"} 3846 1443454528000');
@ -227,6 +227,28 @@ describe('PrometheusMetricFindQuery', () => {
});
});
it('query_result(metric) should handle scalar resultTypes separately', async () => {
const query = setupMetricFindQuery({
query: 'query_result(1+1)',
response: {
data: {
resultType: 'scalar',
result: [1443454528.0, '2'],
},
},
});
const results = await query.process();
expect(results).toHaveLength(1);
expect(results[0].text).toBe('2');
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({
method: 'GET',
url: `proxied/api/v1/query?query=1%2B1&time=${raw.to.unix()}`,
requestId: undefined,
headers: {},
});
});
it('up{job="job1"} should fallback using generate series query', async () => {
const query = setupMetricFindQuery({
query: 'up{job="job1"}',
@ -238,7 +260,7 @@ describe('PrometheusMetricFindQuery', () => {
],
},
});
const results: any = await query.process();
const results = await query.process();
expect(results).toHaveLength(3);
expect(results[0].text).toBe('up{instance="127.0.0.1:1234",job="job1"}');

View File

@ -141,22 +141,35 @@ export default class PrometheusMetricFindQuery {
const instantQuery: PromQueryRequest = { expr: query } as PromQueryRequest;
return this.datasource.performInstantQuery(instantQuery, end).pipe(
map((result) => {
return _map(result.data.data.result, (metricData) => {
let text = metricData.metric.__name__ || '';
delete metricData.metric.__name__;
text +=
'{' +
_map(metricData.metric, (v, k) => {
return k + '="' + v + '"';
}).join(',') +
'}';
text += ' ' + metricData.value[1] + ' ' + metricData.value[0] * 1000;
switch (result.data.data.resultType) {
case 'scalar': // [ <unix_time>, "<scalar_value>" ]
case 'string': // [ <unix_time>, "<string_value>" ]
return [
{
text: result.data.data.result[1] || '',
expandable: false,
},
];
case 'vector':
return _map(result.data.data.result, (metricData) => {
let text = metricData.metric.__name__ || '';
delete metricData.metric.__name__;
text +=
'{' +
_map(metricData.metric, (v, k) => {
return k + '="' + v + '"';
}).join(',') +
'}';
text += ' ' + metricData.value[1] + ' ' + metricData.value[0] * 1000;
return {
text: text,
expandable: true,
};
});
return {
text: text,
expandable: true,
};
});
default:
throw Error(`Unknown/Unhandled result type: [${result.data.data.resultType}]`);
}
})
);
}