-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added logic for trying to get the field value from the source or fr…
…om a doc_values field. - added onlyAggregatable option for a field agg param to decide whether or not to retain only aggregatable fields.
- Loading branch information
Showing
5 changed files
with
278 additions
and
7 deletions.
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
src/ui/public/agg_types/__tests__/metrics/get_values_at_path.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import getValuesAtPath from 'ui/agg_types/metrics/_get_values_at_path'; | ||
import expect from 'expect.js'; | ||
|
||
describe('getValuesAtPath', function () { | ||
it('non existing path', function () { | ||
const values = getValuesAtPath({ aaa: 'bbb' }, [ 'not', 'in', 'there' ]); | ||
expect(values).to.have.length(0); | ||
}); | ||
|
||
it('non existing path in nested object', function () { | ||
const json = { | ||
aaa: { | ||
bbb: 123 | ||
} | ||
}; | ||
const values = getValuesAtPath(json, [ 'aaa', 'ccc' ]); | ||
expect(values).to.have.length(0); | ||
}); | ||
|
||
it('get value at level one', function () { | ||
const values = getValuesAtPath({ aaa: 'bbb' }, [ 'aaa' ]); | ||
expect(values).to.eql([ 'bbb' ]); | ||
}); | ||
|
||
it('get nested value', function () { | ||
const json = { | ||
aaa: { | ||
bbb: 123 | ||
} | ||
}; | ||
const values = getValuesAtPath(json, [ 'aaa', 'bbb' ]); | ||
expect(values).to.eql([ 123 ]); | ||
}); | ||
|
||
it('value is an array', function () { | ||
const json = { | ||
aaa: [ 123, 456 ] | ||
}; | ||
const values = getValuesAtPath(json, [ 'aaa' ]); | ||
expect(values).to.eql([ 123, 456 ]); | ||
}); | ||
|
||
it('nested value is an array', function () { | ||
const json = { | ||
aaa: { | ||
bbb: [ 123, 456 ] | ||
} | ||
}; | ||
const values = getValuesAtPath(json, [ 'aaa', 'bbb' ]); | ||
expect(values).to.eql([ 123, 456 ]); | ||
}); | ||
|
||
it('multiple values are reachable via path', function () { | ||
const json = { | ||
aaa: [ | ||
{ | ||
bbb: 123 | ||
}, | ||
{ | ||
bbb: 456 | ||
} | ||
] | ||
}; | ||
const values = getValuesAtPath(json, [ 'aaa', 'bbb' ]); | ||
expect(values).to.eql([ 123, 456 ]); | ||
}); | ||
|
||
it('multiple values with some that are arrays are reachable via path', function () { | ||
const json = { | ||
aaa: [ | ||
{ | ||
bbb: [ 123, 456 ] | ||
}, | ||
{ | ||
bbb: 789 | ||
} | ||
] | ||
}; | ||
const values = getValuesAtPath(json, [ 'aaa', 'bbb' ]); | ||
expect(values).to.eql([ 123, 456, 789 ]); | ||
}); | ||
|
||
it('nested array mix', function () { | ||
const json = { | ||
aaa: [ | ||
{ | ||
bbb: [ | ||
{ | ||
ccc: 123 | ||
}, | ||
{ | ||
ccc: 456 | ||
} | ||
] | ||
}, | ||
{ | ||
bbb: { | ||
ccc: 789 | ||
} | ||
} | ||
] | ||
}; | ||
const values = getValuesAtPath(json, [ 'aaa', 'bbb', 'ccc' ]); | ||
expect(values).to.eql([ 123, 456, 789 ]); | ||
}); | ||
|
||
describe('nulls', function () { | ||
it('on level 1', function () { | ||
const json = { | ||
aaa: null | ||
}; | ||
expect(getValuesAtPath(json, [ 'aaa' ])).to.have.length(0); | ||
expect(getValuesAtPath(json, [ 'aaa', 'bbb' ])).to.have.length(0); | ||
}); | ||
|
||
it('on level 2', function () { | ||
const json = { | ||
aaa: { | ||
bbb: null | ||
} | ||
}; | ||
expect(getValuesAtPath(json, [ 'aaa', 'bbb' ])).to.have.length(0); | ||
expect(getValuesAtPath(json, [ 'aaa', 'bbb', 'ccc' ])).to.have.length(0); | ||
}); | ||
|
||
it('in array', function () { | ||
const json = { | ||
aaa: [ | ||
123, | ||
null | ||
] | ||
}; | ||
expect(getValuesAtPath(json, [ 'aaa' ])).to.eql([ 123 ]); | ||
}); | ||
|
||
it('nested in array', function () { | ||
const json = { | ||
aaa: [ | ||
{ | ||
bbb: 123 | ||
}, | ||
{ | ||
bbb: null | ||
} | ||
] | ||
}; | ||
expect(getValuesAtPath(json, [ 'aaa', 'bbb' ])).to.eql([ 123 ]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Returns the values at path, regardless if there are arrays on the way. | ||
* Therefore, there is no need to specify the offset in an array. | ||
* For example, for the path aaa.bbb and a JSON object like: | ||
* | ||
* { | ||
* "aaa": [ | ||
* { | ||
* "bbb": 123 | ||
* }, | ||
* { | ||
* "bbb": 456 | ||
* } | ||
* ] | ||
* } | ||
* | ||
* the values returned are 123 and 456. | ||
* | ||
* | ||
* @param json the JSON object | ||
* @param path the path as an array | ||
* @returns an array with all the values reachable from path | ||
*/ | ||
export default function (json, path) { | ||
if (!path || !path.length) { | ||
return []; | ||
} | ||
|
||
const values = []; | ||
|
||
const getValues = function (element, pathIndex) { | ||
if (!element) { | ||
return; | ||
} | ||
|
||
if (pathIndex >= path.length) { | ||
if (element) { | ||
if (element.constructor === Array) { | ||
for (let i = 0; i < element.length; i++) { | ||
if (element[i]) { | ||
values.push(element[i]); | ||
} | ||
} | ||
} else { | ||
values.push(element); | ||
} | ||
} | ||
} else if (element.constructor === Object) { | ||
if (element.hasOwnProperty(path[pathIndex])) { | ||
getValues(element[path[pathIndex]], pathIndex + 1); | ||
} | ||
} else if (element.constructor === Array) { | ||
for (let childi = 0; childi < element.length; childi++) { | ||
getValues(element[childi], pathIndex); | ||
} | ||
} | ||
}; | ||
|
||
getValues(json, 0); | ||
return values; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters