Skip to content

Commit

Permalink
Add unit test & clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeelmers committed Jan 23, 2019
1 parent caebdd0 commit 9e613b3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,7 @@ import expect from 'expect.js';
import { addToSiri } from '../_add_to_siri';

describe('addToSiri', function () {
const xKeys = {
ordered: ['', 'foo', 'bar', 'baz'],
indexMap: {
'': 0,
foo: 1,
bar: 2,
baz: 3,
}
};
const xKeys = ['', 'foo', 'bar', 'baz'];

it('creates a new series the first time it sees an id', function () {
const series = new Map();
Expand Down
39 changes: 39 additions & 0 deletions src/ui/public/agg_response/point_series/__tests__/_get_series.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,43 @@ describe('getSeries', function () {
expect(series2[2]).to.have.property('label', '1: 1');
expect(series2[3]).to.have.property('label', '1: 0');
});

it('produces series values with x values in the same order as the provided rows', function () {
const rows = [
[1, 4],
[1, 3],
[1, 4],
[1, 4],
[1, 2],
[1, 1],
[1, 1],
[1, 2],
[1, 3],
].map(wrapRows);

const chart = {
aspects: {
x: { i: 1 },
y: { i: 0, title: 'y', aggConfig: { id: 'id' } },
}
};

const series = getSeries(rows, chart);

expect(series)
.to.be.an('array')
.and.to.have.length(1);

const siri = series[0];
expect(siri)
.to.be.an('object')
.and.have.property('label', chart.aspects.y.title)
.and.have.property('values');

expect(siri.values)
.to.be.an('array')
.and.have.length(9);

expect(siri.values.map(v => v.x)).to.eql([4, 4, 4, 3, 3, 2, 2, 1, 1]);
});
});
13 changes: 6 additions & 7 deletions src/ui/public/agg_response/point_series/_add_to_siri.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { findLastIndex } from 'lodash';

function instantiateZeroFilledArray({ xKeys, series }) {
// borrowed from `ui/public/vislib/components/zero_injection/zero_filled_array`
Expand All @@ -36,19 +37,17 @@ export function addToSiri(xKeys, series, point, id, label, agg) {
aggLabel: agg.type ? agg.type.makeLabel(agg) : label,
aggId: agg.parentId ? agg.parentId : agg.id,
count: 0,
values: instantiateZeroFilledArray({ xKeys: xKeys.ordered, series: label }),
values: instantiateZeroFilledArray({ xKeys, series: label }),
});
}

const seriesValues = series.get(id).values;
const xIndex = xKeys.indexMap[point.x];
if (seriesValues[xIndex].xi) {
const lastXIndex = findLastIndex(seriesValues, v => v.x === point.x);
if (seriesValues[lastXIndex].xi) {
// update the ordered, zero-filled array with the "real" value
seriesValues[xIndex] = point;
seriesValues[lastXIndex] = point;
} else {
// add the point to the list of values at the correct position
seriesValues.splice(xIndex, 0, point);
// TODO: recalculate values in xKeys.indexMap
seriesValues.splice(lastXIndex + 1, 0, point);
}
return;
}
10 changes: 3 additions & 7 deletions src/ui/public/agg_response/point_series/_get_series.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,15 @@ export function getSeries(rows, chart) {
const multiY = Array.isArray(aspects.y);
const yScale = chart.yScale;
const partGetPoint = _.partial(getPoint, aspects.x, aspects.series, yScale);
const orderedXKeys = aspects.x.i > -1 ? _.uniq(rows.map(r => r[aspects.x.i].value)) : ['_all'];
const orderedXKeysMap = orderedXKeys.reduce((acc, cur, i) => {
acc[cur] = i;
return acc;
}, {}); // maps x key values to their ordered array index, e.g. { foo: 0, bar: 1 }
const xKeys = aspects.x.i > -1 ? _.uniq(rows.map(r => r[aspects.x.i].value)) : ['_all'];

let series = _(rows)
.transform(function (series, row) {
if (!multiY) {
const point = partGetPoint(row, aspects.y, aspects.z);
if (point) {
addToSiri(
{ ordered: orderedXKeys, indexMap: orderedXKeysMap },
xKeys,
series,
point,
point.series,
Expand Down Expand Up @@ -66,7 +62,7 @@ export function getSeries(rows, chart) {
}

addToSiri(
{ ordered: orderedXKeys, indexMap: orderedXKeysMap },
xKeys,
series,
point,
seriesId,
Expand Down

0 comments on commit 9e613b3

Please sign in to comment.