Fixed default timestamp sort and added tests (#35640)

* Fixed default timestamp sort and added tests

* Removed duplicate functionality

* Fixed unit tests

* Removed debug code

* Update helpers.js
This commit is contained in:
igoristic 2019-05-03 11:32:28 -04:00 committed by GitHub
parent 75043529af
commit 3d3e071c6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 152 additions and 106 deletions

View file

@ -0,0 +1,101 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
export const response = {
hits: {
hits: [
{
_source: {
beats_stats: {
timestamp: 1515541592880,
beat: {
uuid: 'fooUuid',
host: 'beat-listing.test',
name: 'beat-listing.test-0101',
type: 'filebeat',
version: '6.2.0'
},
metrics: {
beat: {
memstats: {
memory_alloc: 2340
}
},
libbeat: {
output: {
type: 'Redis',
write: {
bytes: 140000,
errors: 8,
},
read: {
errors: 3,
}
},
pipeline: {
events: {
total: 23000
}
}
}
}
}
},
inner_hits: {
earliest: {
hits: {
hits: [
{
_source: {
beats_stats: {
timestamp: 1515534342000,
metrics: {
libbeat: {
output: {
write: {
bytes: 4000,
errors: 3,
},
read: {
errors: 1,
}
},
pipeline: {
events: {
total: 2300
}
}
}
}
}
}
}
]
}
}
}
}
]
}
};
export const defaultResponseSort = (handleResponse) => {
const responseMulti = { hits: { hits: [] } };
const hit = response.hits.hits[0];
const version = ['6.6.2', '7.0.0-rc1', '6.7.1'];
for (let i = 0, l = version.length; i < l; ++i) {
// Deep clone the object to preserve the original
const newBeat = JSON.parse(JSON.stringify({ ...hit }));
const { beats_stats: beatsStats } = newBeat._source;
beatsStats.timestamp = `2019-01-0${i + 1}T05:00:00.000Z`;
beatsStats.beat.version = version[i];
beatsStats.beat.uuid = `${i}${beatsStats.beat.uuid}`;
responseMulti.hits.hits.push(newBeat);
}
return { beats: handleResponse(responseMulti, 0, 0), version };
};

View file

@ -0,0 +1,18 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { defaultResponseSort } from '../../__tests__/helpers';
import { handleResponse } from '../get_apms';
import expect from '@kbn/expect';
describe('apm/get_apms', () => {
it('Timestamp is desc', () => {
const { beats, version } = defaultResponseSort(handleResponse);
expect(beats[0].version).to.eql(version[0]);
expect(beats[1].version).to.eql(version[1]);
expect(beats[2].version).to.eql(version[2]);
});
});

View file

@ -14,7 +14,7 @@ import { getDiffCalculation } from './_apm_stats';
export function handleResponse(response, start, end) {
const hits = get(response, 'hits.hits', []);
const initial = { ids: new Set(), beats: [] };
const { beats } = hits.reduceRight((accum, hit) => {
const { beats } = hits.reduce((accum, hit) => {
const stats = get(hit, '_source.beats_stats');
const uuid = get(stats, 'beat.uuid');

View file

@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { response, defaultResponseSort } from '../../__tests__/helpers';
import { handleResponse } from '../get_beats';
import expect from '@kbn/expect';
@ -13,87 +14,7 @@ describe('beats/get_beats', () => {
});
it('Maps hits into a listing', () => {
const start = 1515534342000;
const end = 1515541592880;
const response = {
hits: {
hits: [
{
_source: {
beats_stats: {
timestamp: end,
beat: {
uuid: 'fooUuid',
host: 'beat-listing.test',
name: 'beat-listing.test-0101',
type: 'filebeat',
version: '6.2.0'
},
metrics: {
beat: {
memstats: {
memory_alloc: 2340
}
},
libbeat: {
output: {
type: 'Redis',
write: {
bytes: 140000,
errors: 8,
},
read: {
errors: 3,
}
},
pipeline: {
events: {
total: 23000
}
}
}
}
}
},
inner_hits: {
earliest: {
hits: {
hits: [
{
_source: {
beats_stats: {
timestamp: start,
metrics: {
libbeat: {
output: {
write: {
bytes: 4000,
errors: 3,
},
read: {
errors: 1,
}
},
pipeline: {
events: {
total: 2300
}
}
}
}
}
}
}
]
}
}
}
}
]
}
};
expect(handleResponse(response, start, end)).to.eql([
expect(handleResponse(response, 1515534342000, 1515541592880)).to.eql([
{
bytes_sent_rate: 18.756344057548876,
errors: 7,
@ -107,4 +28,11 @@ describe('beats/get_beats', () => {
}
]);
});
it('Timestamp is desc', () => {
const { beats, version } = defaultResponseSort(handleResponse);
expect(beats[0].version).to.eql(version[0]);
expect(beats[1].version).to.eql(version[1]);
expect(beats[2].version).to.eql(version[2]);
});
});

View file

@ -14,7 +14,7 @@ import { getDiffCalculation } from './_beats_stats';
export function handleResponse(response, start, end) {
const hits = get(response, 'hits.hits', []);
const initial = { ids: new Set(), beats: [] };
const { beats } = hits.reduceRight((accum, hit) => {
const { beats } = hits.reduce((accum, hit) => {
const stats = get(hit, '_source.beats_stats');
const uuid = get(stats, 'beat.uuid');

View file

@ -43,17 +43,6 @@ export default function ({ getService }) {
timeOfLastEvent: '2018-08-31T13:59:21.201Z'
},
apms: [{
uuid: '9b16f434-2092-4983-a401-80a2b61c79d6',
name: '01323afae1fb',
type: 'Apm-server',
output: 'Elasticsearch',
total_events_rate: 0.0016666666666666668,
bytes_sent_rate: 2.9105555555555553,
errors: 0,
memory: 3087640,
version: '7.0.0-alpha1',
time_of_last_event: '2018-08-31T13:59:21.165Z'
}, {
uuid: '55f1089b-43b1-472a-919a-344667bae595',
name: 'd06490170f2b',
type: 'Apm-server',
@ -64,6 +53,17 @@ export default function ({ getService }) {
memory: 3445920,
version: '7.0.0-alpha1',
time_of_last_event: '2018-08-31T13:59:21.201Z'
}, {
uuid: '9b16f434-2092-4983-a401-80a2b61c79d6',
name: '01323afae1fb',
type: 'Apm-server',
output: 'Elasticsearch',
total_events_rate: 0.0016666666666666668,
bytes_sent_rate: 2.9105555555555553,
errors: 0,
memory: 3087640,
version: '7.0.0-alpha1',
time_of_last_event: '2018-08-31T13:59:21.165Z'
}]
};

View file

@ -45,6 +45,17 @@ export default function ({ getService }) {
stats: { totalEvents: 12829, bytesSent: 2040312125 }
},
listing: [
{
uuid: '2736e08b-5830-409b-8169-32aac39c5e55',
name: 'spicy.local',
type: 'Filebeat',
output: 'Elasticsearch',
total_events_rate: 0.018032786885245903,
bytes_sent_rate: 24135.450546448086,
errors: 0,
memory: 30680648,
version: '7.0.0-alpha1',
},
{
uuid: '60599a4f-8139-4251-b0b9-15866df34221',
name: 'spicy.local',
@ -55,21 +66,9 @@ export default function ({ getService }) {
errors: 0,
memory: 7598304,
version: '7.0.0-alpha1',
},
{
uuid: '2736e08b-5830-409b-8169-32aac39c5e55',
name: 'spicy.local',
type: 'Filebeat',
output: 'Elasticsearch',
total_events_rate: 0.35437158469945357,
bytes_sent_rate: 530713.5601092896,
errors: 0,
memory: 27209376,
version: '7.0.0-alpha1',
}
]
};
expect(body).to.eql(expected);
});
});