InfluxDB: Fix the ordering of results for variable queries (#33314)

This commit is contained in:
Gábor Farkas 2021-05-05 15:35:48 +02:00 committed by GitHub
parent 88497305a9
commit 8f210db17a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 9 deletions

View File

@ -1,4 +1,4 @@
import { each, isArray, map } from 'lodash';
import { each, isArray } from 'lodash';
export default class ResponseParser {
parse(query: string, results: { results: any }) {
@ -15,7 +15,7 @@ export default class ResponseParser {
const isValueFirst =
normalizedQuery.indexOf('show field keys') >= 0 || normalizedQuery.indexOf('show retention policies') >= 0;
const res = {};
const res = new Set<string>();
each(influxResults.series, (serie) => {
each(serie.values, (value) => {
if (isArray(value)) {
@ -44,14 +44,14 @@ export default class ResponseParser {
});
});
// @ts-ignore problems with typings for this map only accepts [] but this needs to be object
return map(res, (value) => {
// @ts-ignore
return { text: value.toString() };
});
// NOTE: it is important to keep the order of items in the parsed output
// the same as it was in the influxdb-response.
// we use a `Set` to collect the unique-results, and `Set` iteration
// order is insertion-order, so this should be ok.
return Array.from(res).map((v) => ({ text: v }));
}
}
function addUnique(arr: { [x: string]: any }, value: string | number) {
arr[value] = value;
function addUnique(s: Set<string>, value: string | number) {
s.add(value.toString());
}

View File

@ -121,6 +121,39 @@ describe('influxdb response parser', () => {
});
});
describe('SELECT response where ordering matters', () => {
const query = 'SELECT "val" from "num"';
const response = {
results: [
{
series: [
{
name: 'num',
columns: ['time', 'val'],
values: [
[1620041231000, 2],
[1620041233000, 3],
[1620041235000, 4],
[1620041238000, 5],
[1620041239000, 1],
],
},
],
},
],
};
it('should keep the order returned by influxdb, even for numbers', () => {
expect(parser.parse(query, response)).toStrictEqual([
{ text: '2' },
{ text: '3' },
{ text: '4' },
{ text: '5' },
{ text: '1' },
]);
});
});
describe('SHOW FIELD response', () => {
const query = 'SHOW FIELD KEYS FROM "cpu"';