Skip to content

Commit

Permalink
fix multiple agg functions handling, closes #530
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderzobnin committed Mar 7, 2019
1 parent 3428137 commit e4bbecb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/datasource-zabbix/dataProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,22 @@ function groupByWrapper(interval, groupFunc, datapoints) {

function aggregateByWrapper(interval, aggregateFunc, datapoints) {
// Flatten all points in frame and then just use groupBy()
const flattenedPoints = _.flatten(datapoints, true);
const flattenedPoints = ts.flattenDatapoints(datapoints);
// groupBy_perf works with sorted series only
const sortedPoints = ts.sortByTime(flattenedPoints);
let groupByCallback = aggregationFunctions[aggregateFunc];
return groupBy(sortedPoints, interval, groupByCallback);
}

function aggregateWrapper(groupByCallback, interval, datapoints) {
var flattenedPoints = _.flatten(datapoints, true);
var flattenedPoints = ts.flattenDatapoints(datapoints);
// groupBy_perf works with sorted series only
const sortedPoints = ts.sortByTime(flattenedPoints);
return groupBy(sortedPoints, interval, groupByCallback);
}

function percentil(interval, n, datapoints) {
var flattenedPoints = _.flatten(datapoints, true);
var flattenedPoints = ts.flattenDatapoints(datapoints);
var groupByCallback = _.partial(PERCENTIL, n);
return groupBy(flattenedPoints, interval, groupByCallback);
}
Expand Down
27 changes: 27 additions & 0 deletions src/datasource-zabbix/specs/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,31 @@ describe('Utils', () => {
done();
});
});

describe('getArrayDepth()', () => {
it('should calculate proper array depth', () => {
const test_cases = [
{
array: [],
depth: 1
},
{
array: [1, 2, 3],
depth: 1
},
{
array: [[1, 2], [3, 4]],
depth: 2
},
{
array: [ [[1, 2], [3, 4]], [[1, 2], [3, 4]] ],
depth: 3
},
];

for (const test_case of test_cases) {
expect(utils.getArrayDepth(test_case.array)).toBe(test_case.depth);
}
});
});
});
12 changes: 11 additions & 1 deletion src/datasource-zabbix/timeseries.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,15 @@ function findNearestLeft(series, pointIndex) {
return null;
}

function flattenDatapoints(datapoints) {
const depth = utils.getArrayDepth(datapoints);
if (depth <= 2) {
// Don't process if datapoints already flattened
return datapoints;
}
return _.flatten(datapoints);
}

////////////
// Export //
////////////
Expand All @@ -501,7 +510,8 @@ const exportedFunctions = {
MAX,
MEDIAN,
PERCENTIL,
sortByTime
sortByTime,
flattenDatapoints,
};

export default exportedFunctions;
11 changes: 11 additions & 0 deletions src/datasource-zabbix/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ export function compactQuery(query) {
return query.replace(/\s+/g, ' ').trim();
}

export function getArrayDepth(a, level = 0) {
if (a.length === 0) {
return 1;
}
const elem = a[0];
if (_.isArray(elem)) {
return getArrayDepth(elem, level + 1);
}
return level + 1;
}

// Fix for backward compatibility with lodash 2.4
if (!_.includes) {
_.includes = _.contains;
Expand Down

0 comments on commit e4bbecb

Please sign in to comment.