Skip to content

Commit

Permalink
Merge pull request #1344 from telefonicaid/fix/propage_timestamp_as_m…
Browse files Browse the repository at this point in the history
…easure_all_metadata_all_cases_master

 Fix/propage timestamp as measure all metadata all cases [master]
  • Loading branch information
fgalan authored Apr 24, 2023
2 parents 7b7c950 + e1a8377 commit 9d4c6ed
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 36 deletions.
2 changes: 2 additions & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
- Add support to run tests with node 18
- Set Nodejs 16 as minimum version in packages.json (effectively removing Nodev14 from supported versions)
- Fix: propagate TimeInstant to all metadata attributes when TimeInstant is provided as measure

18 changes: 2 additions & 16 deletions lib/services/ngsi/entities-NGSI-LD.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,23 +973,9 @@ function sendUpdateValueNgsiLD(entityName, attributes, typeInformation, token, c
) {
if (timestampValue) {
// timeInstant is provided as measure
if (Object.keys(payload[0]).length > 3) {
if (Object.keys(payload[0]).length > 1) {
// include metadata with TimeInstant in attrs when TimeInstant is provided as measure in all entities
payload[0] = NGSIv2.addTimestamp(
payload[0],
typeInformation.timezone,
timestampValue,
false // skipMetadataAtt
);
} else {
// Do not include metadata with TimeInstant in attrs when TimeInstant is provided as measure
// and no more entities
payload[0] = NGSIv2.addTimestamp(
payload[0],
typeInformation.timezone,
timestampValue,
true // skipMetadataAtt
);
payload[0] = NGSIv2.addTimestamp(payload[0], typeInformation.timezone, timestampValue);
}
} else {
// jshint maxdepth:5
Expand Down
20 changes: 4 additions & 16 deletions lib/services/ngsi/entities-NGSI-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function formatGeoAttrs(attr) {
* @param Boolean skipMetadataAtt An optional flag to indicate if timestamp should be added to each metadata attribute. Default is false
* @return {Object} NGSIv2 payload entities with timestamp
*/
function addTimestampNgsi2(payload, timezone, timestampValue, skipMetadataAtt) {
function addTimestampNgsi2(payload, timezone, timestampValue) {
function addTimestampEntity(entity, timezone, timestampValue) {
const timestamp = {
type: constants.TIMESTAMP_TYPE_NGSI2
Expand Down Expand Up @@ -148,9 +148,7 @@ function addTimestampNgsi2(payload, timezone, timestampValue, skipMetadataAtt) {
for (const key in entity) {
/* eslint-disable-next-line no-prototype-builtins */
if (entity.hasOwnProperty(key) && key !== 'id' && key !== 'type') {
if (!skipMetadataAtt) {
addMetadata(entity[key]);
}
addMetadata(entity[key]);
keyCount += 1;
}
}
Expand Down Expand Up @@ -885,25 +883,15 @@ function sendUpdateValueNgsi2(entityName, attributes, typeInformation, token, ca
) {
if (timestampValue) {
// timeInstant is provided as measure
if (payload.entities.length > 1) {
if (payload.entities.length > 0) {
for (let n = 0; n < payload.entities.length; n++) {
// include metadata with TimeInstant in attrs when TimeInstant is provided as measure in all entities
payload.entities[n] = addTimestampNgsi2(
payload.entities[n],
typeInformation.timezone,
timestampValue,
false // skipMetadataAtt
timestampValue
);
}
} else {
// Do not include metadata with TimeInstant in attrs when TimeInstant is provided as measure
// and no more entities
payload.entities[0] = addTimestampNgsi2(
payload.entities[0],
typeInformation.timezone,
timestampValue,
true // skipMetadataAtt
);
}
} else {
// jshint maxdepth:5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
52
],
"type": "Point"
},
"metadata": {
"TimeInstant": {
"type": "DateTime",
"value": "1970-01-01T00:00:00.001Z"
}
}
},
"TimeInstant": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"state":{
"type": "Boolean",
"value": true
"value": true,
"metadata": {
"TimeInstant": {
"type": "DateTime",
"value": "2016-05-30T16:25:22.304Z"
}
}
},
"TimeInstant":
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"state": {
"value": true,
"type": "boolean"
"type": "boolean",
"metadata": {
"TimeInstant":{
"type": "DateTime",
"value": "2015-12-14T08:06:01.468Z"
}
}
},
"TimeInstant":{
"type": "DateTime",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"state": {
"value": true,
"type": "boolean"
"type": "boolean",
"metadata": {
"TimeInstant":{
"type": "DateTime",
"value": "2022-10-22T22:22:22Z"
}
}
},
"TimeInstant":{
"type": "DateTime",
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ngsiv2/ngsiService/active-devices-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ describe('NGSI-v2 - Active attributes test', function () {
done();
});

it('should not override the received instant and should not add metadatas for this request', function (done) {
it('should not override the received instant and should add metadatas for this request', function (done) {
iotAgentLib.update('light1', 'Light', '', modifiedValues, function (error) {
should.not.exist(error);
contextBrokerMock.done();
Expand Down

0 comments on commit 9d4c6ed

Please sign in to comment.