[TSVB] Gauge/Top N/Metric panels show wrong data (#36502) (#36969)

Fix: #36501
This commit is contained in:
Alexey Antonov 2019-05-23 20:36:54 +03:00 committed by GitHub
parent 4f7fc12f6b
commit ea7faad921
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 26 deletions

View file

@ -17,20 +17,15 @@
* under the License.
*/
import { isArray, findLast } from 'lodash';
import { isArray, last } from 'lodash';
const DEFAULT_VALUE = 0;
const extractValue = data => data && data[1] || null;
export default (data, defaultValue = DEFAULT_VALUE) => {
if (!isArray(data)) {
return data;
return data || defaultValue;
}
const extractValue = data => data && data[1] || null;
// If the last value is zero or null because of a partial bucket or
// some kind of timeshift weirdness we will show the second to last.
const lastValid = findLast(data, item => extractValue(item));
return extractValue(lastValid) || defaultValue;
return extractValue(last(data)) || defaultValue;
};

View file

@ -17,33 +17,27 @@
* under the License.
*/
import { expect } from 'chai';
import getLastValue from '../get_last_value';
import getLastValue from './get_last_value';
describe('getLastValue(data)', () => {
it('returns data if data is not array', () => {
expect(getLastValue('foo')).to.equal('foo');
test('should returns data if data is not array', () => {
expect(getLastValue('foo')).toBe('foo');
});
it('returns the last value', () => {
const data = [[1, 1]];
expect(getLastValue(data)).to.equal(1);
test('should returns the last value', () => {
expect(getLastValue([[1, 2]])).toBe(2);
});
it('returns the second to last value if the last value is null (default)', () => {
const data = [[1, 4], [2, null]];
expect(getLastValue(data)).to.equal(4);
test('should returns the default value ', () => {
expect(getLastValue()).toBe(0);
});
it('returns 0 if second to last is not defined (default)', () => {
const data = [[1, null], [2, null]];
expect(getLastValue(data)).to.equal(0);
test('should returns 0 if second to last is not defined (default)', () => {
expect(getLastValue([[1, null], [2, null]])).toBe(0);
});
it('returns the N to last value if the last N-1 values are null (default)', () => {
const data = [[1, 4], [2, null], [3, null]];
expect(getLastValue(data, 3)).to.equal(4);
test('should allows to override the default value', () => {
expect(getLastValue(null, '-')).toBe('-');
});
});