-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improved aggrMethod to accept multiple values #605
Changes from 2 commits
e53b905
ee9cded
c3601a5
e2fcabd
3b51b4c
885c78c
4abf35f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
- Set Nodejs 14 as minimum version in packages.json (effectively removing Nodev12 from supported versions) | ||
- Add: aggrMethod accept multiple values separated by comma (#432, partial) | ||
- Add: aggrMethod 'all' to get all the possible aggregations (#432, partial) | ||
- Set Nodejs 14 as minimum version in packages.json (effectively removing Nodev12 from supported versions) |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -63,6 +63,10 @@ function doStartServer(host, port, callback) { | |||||||||||||
reply({ error: 'BadRequest', description: error.output.payload.message }).code(400); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
const list = ['min', 'max', 'sum', 'sum2', 'occur', 'all']; | ||||||||||||||
const joinedList = '(' + list.join('|') + ')'; | ||||||||||||||
const regex = new RegExp('^' + joinedList + '(,' + joinedList + ')*$'); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use more meaningful names names for the variables. For instance:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 3b51b4c |
||||||||||||||
|
||||||||||||||
const config = { | ||||||||||||||
validate: { | ||||||||||||||
headers: sthHeaderValidator, | ||||||||||||||
|
@@ -74,8 +78,7 @@ function doStartServer(host, port, callback) { | |||||||||||||
// prettier-ignore | ||||||||||||||
hOffset: joi.number().integer().greater(-1).optional(), | ||||||||||||||
// prettier-ignore | ||||||||||||||
aggrMethod: joi.string().valid( | ||||||||||||||
'max', 'min', 'sum', 'sum2', 'occur').optional(), | ||||||||||||||
aggrMethod: joi.string().regex(regex).optional(), | ||||||||||||||
// prettier-ignore | ||||||||||||||
aggrPeriod: joi.string().required().valid( | ||||||||||||||
'month', 'day', 'hour', 'minute', 'second').optional(), | ||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -912,21 +912,22 @@ function aggregatedDataAvailableSinceDateTest(ngsiVersion, params, done) { | |
break; | ||
} | ||
|
||
let value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of modifying existing tests, please add new ones. In particular, I'd suggest two new tests cases:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @fgalan, I have added the new test cases here. Previously, only one aggrMethod was there so switch case was working fine. But in case of multiple aggregation method we need to update the code for multiple values separately. The above code change is for util class to accommodate the multiple aggregation methods. |
||
switch (aggrMethod) { | ||
case 'min': | ||
case 'max': | ||
let value,valueSum,valueSum2,valueOccur; | ||
const aggrMethods=aggrMethod.split(','); | ||
|
||
for (let i=0;i<aggrMethods.length;i++) { | ||
if (aggrMethods[i] === 'min' || aggrMethods[i] === 'max') { | ||
value = parseFloat(theEvent.attrValue).toFixed(2); | ||
break; | ||
case 'sum': | ||
value = (events.length * parseFloat(theEvent.attrValue)).toFixed(2); | ||
break; | ||
case 'sum2': | ||
value = (events.length * Math.pow(parseFloat(theEvent.attrValue), 2)).toFixed(2); | ||
break; | ||
case 'occur': | ||
value = events.length; | ||
break; | ||
} | ||
if (aggrMethods[i] === 'sum') { | ||
valueSum = (events.length * parseFloat(theEvent.attrValue)).toFixed(2); | ||
} | ||
if (aggrMethods[i] === 'sum2') { | ||
valueSum2 = (events.length * Math.pow(parseFloat(theEvent.attrValue), 2)).toFixed(2); | ||
} | ||
if (aggrMethods[i] === 'occur') { | ||
valueOccur = events.length; | ||
} | ||
} | ||
|
||
if (ngsiVersion === 2) { | ||
|
@@ -963,19 +964,37 @@ function aggregatedDataAvailableSinceDateTest(ngsiVersion, params, done) { | |
events.length | ||
); | ||
if (attrType === 'float') { | ||
expect( | ||
parseFloat( | ||
bodyJSON.value[0].points[sthConfig.FILTER_OUT_EMPTY ? 0 : index][aggrMethod] | ||
).toFixed(2) | ||
).to.equal(value); | ||
for (let j=0;j<aggrMethods.length;j++) { | ||
if (aggrMethods[j] === 'min' || aggrMethods[j] === 'max') { | ||
expect( | ||
parseFloat( | ||
bodyJSON.value[0].points[sthConfig.FILTER_OUT_EMPTY ? 0 : index][aggrMethods[j]] | ||
).toFixed(2) | ||
).to.equal(value); | ||
} | ||
if (aggrMethods[j] === 'sum') { | ||
expect( | ||
parseFloat( | ||
bodyJSON.value[0].points[sthConfig.FILTER_OUT_EMPTY ? 0 : index][aggrMethods[j]] | ||
).toFixed(2) | ||
).to.equal(valueSum); | ||
} | ||
if (aggrMethods[j] === 'sum2') { | ||
expect( | ||
parseFloat( | ||
bodyJSON.value[0].points[sthConfig.FILTER_OUT_EMPTY ? 0 : index][aggrMethods[j]] | ||
).toFixed(2) | ||
).to.equal(valueSum2); | ||
} | ||
} | ||
} else if (attrType === 'string') { | ||
expect( | ||
parseFloat( | ||
bodyJSON.value[0].points[sthConfig.FILTER_OUT_EMPTY ? 0 : index][aggrMethod][ | ||
'just a string' | ||
] | ||
) | ||
).to.equal(value); | ||
).to.equal(valueOccur); | ||
} | ||
done(); | ||
} | ||
|
@@ -1025,21 +1044,43 @@ function aggregatedDataAvailableSinceDateTest(ngsiVersion, params, done) { | |
].samples | ||
).to.equal(events.length); | ||
if (attrType === 'float') { | ||
expect( | ||
parseFloat( | ||
bodyJSON.contextResponses[0].contextElement.attributes[0].values[0].points[ | ||
sthConfig.FILTER_OUT_EMPTY ? 0 : index | ||
][aggrMethod] | ||
).toFixed(2) | ||
).to.equal(value); | ||
for (let j=0;j<aggrMethods.length;j++) { | ||
if (aggrMethods[j] === 'min' || aggrMethods[j] === 'max') { | ||
expect( | ||
parseFloat( | ||
bodyJSON.contextResponses[0].contextElement.attributes[0].values[0].points[ | ||
sthConfig.FILTER_OUT_EMPTY ? 0 : index | ||
][aggrMethods[j]] | ||
).toFixed(2) | ||
).to.equal(value); | ||
} | ||
if (aggrMethods[j] === 'sum') { | ||
expect( | ||
parseFloat( | ||
bodyJSON.contextResponses[0].contextElement.attributes[0].values[0].points[ | ||
sthConfig.FILTER_OUT_EMPTY ? 0 : index | ||
][aggrMethods[j]] | ||
).toFixed(2) | ||
).to.equal(valueSum); | ||
} | ||
if (aggrMethods[j] === 'sum2') { | ||
expect( | ||
parseFloat( | ||
bodyJSON.contextResponses[0].contextElement.attributes[0].values[0].points[ | ||
sthConfig.FILTER_OUT_EMPTY ? 0 : index | ||
][aggrMethods[j]] | ||
).toFixed(2) | ||
).to.equal(valueSum2); | ||
} | ||
} | ||
} else if (attrType === 'string') { | ||
expect( | ||
parseFloat( | ||
bodyJSON.contextResponses[0].contextElement.attributes[0].values[0].points[ | ||
sthConfig.FILTER_OUT_EMPTY ? 0 : index | ||
][aggrMethod]['just a string'] | ||
) | ||
).to.equal(value); | ||
).to.equal(valueOccur); | ||
} | ||
expect(bodyJSON.contextResponses[0].statusCode.code).to.equal('200'); | ||
expect(bodyJSON.contextResponses[0].statusCode.reasonPhrase).to.equal('OK'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,13 +324,45 @@ describe('sth tests', function() { | |
}) | ||
); | ||
|
||
it( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest to add a couple of cases:
Btw, what happens if aggMethod mixes all with another method? Eg. aggMethod=all,min? Do we get and error or the "min" part is ignored and that's equivalent to aggMethod=all? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added test cases in 3b51b4c If aggMethod mixes |
||
'should respond with 200 - OK if aggrMethod are multiple and aggrPeriod query params', | ||
sthTestUtils.status200Test.bind(null, 2, { | ||
aggrMethod: 'min,max', | ||
aggrPeriod: 'second' | ||
}) | ||
); | ||
|
||
it( | ||
'should respond with 200 - OK if aggrMethod are multiple and aggrPeriod query params', | ||
sthTestUtils.status200Test.bind(null, 2, { | ||
aggrMethod: 'all', | ||
aggrPeriod: 'second' | ||
}) | ||
); | ||
|
||
it( | ||
'should respond with 200 - OK if aggrMethod and aggrPeriod query params - NGSIv1', | ||
sthTestUtils.status200Test.bind(null, 1, { | ||
aggrMethod: 'min', | ||
aggrPeriod: 'second' | ||
}) | ||
); | ||
|
||
it( | ||
'should respond with 200 - OK if aggrMethod are multiple and aggrPeriod query params - NGSIv1', | ||
sthTestUtils.status200Test.bind(null, 1, { | ||
aggrMethod: 'min,max', | ||
aggrPeriod: 'second' | ||
}) | ||
); | ||
|
||
it( | ||
'should respond with 200 - OK if aggrMethod are multiple and aggrPeriod query params - NGSIv1', | ||
sthTestUtils.status200Test.bind(null, 1, { | ||
aggrMethod: 'all', | ||
aggrPeriod: 'second' | ||
}) | ||
); | ||
}); | ||
|
||
function eachEventTestSuiteContainer(attrName, attrType, includeTimeInstantMetadata) { | ||
|
@@ -390,6 +422,16 @@ describe('sth tests', function() { | |
'aggregated data retrieval', | ||
sthTestUtils.aggregatedDataRetrievalSuite.bind(null, 'attribute-float', 'float', 'sum2') | ||
); | ||
|
||
describe( | ||
'aggregated data retrieval', | ||
sthTestUtils.aggregatedDataRetrievalSuite.bind(null, 'attribute-float', 'float', 'max,sum2') | ||
); | ||
|
||
describe( | ||
'aggregated data retrieval', | ||
sthTestUtils.aggregatedDataRetrievalSuite.bind(null, 'attribute-float', 'float', 'all') | ||
); | ||
} | ||
|
||
for (let j = 0; j < sthTestConfig.SAMPLES; j++) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 3b51b4c