Skip to content

Commit

Permalink
sort entities by TimeInstant
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroVega committed May 16, 2024
1 parent 53bc93a commit b6a7d23
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 16 deletions.
17 changes: 16 additions & 1 deletion lib/services/ngsi/entities-NGSI-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,22 @@ function sendUpdateValueNgsi2(entityName, originMeasures, originTypeInformation,
transformedObject.type = entityAttrs.type;
options.json = transformedObject;
options.method = 'POST';
} // else: keep current options object created for a batch update
} else {
// keep current options object created for a batch update

Check failure on line 666 in lib/services/ngsi/entities-NGSI-v2.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

Unexpected if as the only statement in an else block
// but try sort entities by TimeInstant
if (payload.entities.every((entity) => 'TimeInstant' in entity)) {
payload.entities.sort(
(a, b) => new Date(a.TimeInstant.value).getTime() - new Date(b.TimeInstant.value).getTime()
);
options.json = payload;
} else {
logger.debug(
context,
"some entities lack the 'TimeInstant' key. Sorting is not feasible: %j ",
payload.entities
);
}
}

//Send the NGSI request
logger.debug(context, 'Updating device value in the Context Broker at: %j', options.url);
Expand Down
162 changes: 147 additions & 15 deletions test/functional/testCases.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ const testCases = [
]
},
{
describeName: '0021 Simple group with active attributes with metadata',
describeName: '0021 - Simple group with active attributes with metadata',
provision: {
url: 'http://localhost:' + config.iota.server.port + '/iot/services',
method: 'POST',
Expand Down Expand Up @@ -514,7 +514,7 @@ const testCases = [
]
},
{
describeName: '0022 Simple group with active attributes and multimeasures',
describeName: '0022 - Simple group with active attributes and multimeasures',
provision: {
url: 'http://localhost:' + config.iota.server.port + '/iot/services',
method: 'POST',
Expand Down Expand Up @@ -642,7 +642,7 @@ const testCases = [
},
{
shouldName:
'A - WHEN sending defined object_ids (measures) through http IT should send measures with TimeInstant to Context Broker preserving value types and name mappings',
'A - WHEN sending defined object_ids (measures) through http IT should send measures with TimeInstant to Context Broker preserving value types and name mappings and order',
type: 'multimeasure',
measure: {
url: 'http://localhost:' + config.http.port + '/iot/json',
Expand Down Expand Up @@ -771,6 +771,138 @@ const testCases = [
}
]
}
},
{
shouldName:
'A - WHEN sending defined object_ids (measures) through http IT should send measures with TimeInstant to Context Broker preserving value types and name mappings and sorted by TimeInstant',
type: 'multimeasure',
measure: {
url: 'http://localhost:' + config.http.port + '/iot/json',
method: 'POST',
qs: {
i: globalEnv.deviceId,
k: globalEnv.apikey
},
json: [
{
a: 0,
TimeInstant: '2024-04-10T10:15:00Z'
},
{
a: 1,
TimeInstant: '2024-04-10T10:05:00Z'
},
{
a: 2,
TimeInstant: '2024-04-10T10:20:00Z'
},
{
a: 3,
TimeInstant: '2024-04-10T10:00:00Z'
},
{
a: 4,
TimeInstant: '2024-04-10T10:10:00Z'
},
{
a: 5,
TimeInstant: '2024-04-10T10:30:00Z'
},
{
a: 6,
TimeInstant: '2024-04-10T10:25:00Z'
}
]
},
expectation: {
actionType: 'append',
entities: [
{
attr_a: {
type: 'Number',
value: 3
},
TimeInstant: {
type: 'DateTime',
value: '2024-04-10T10:00:00Z'
},
id: globalEnv.entity_name,
type: globalEnv.entity_type
},
{
attr_a: {
type: 'Number',
value: 1
},
TimeInstant: {
type: 'DateTime',
value: '2024-04-10T10:05:00Z'
},
id: globalEnv.entity_name,
type: globalEnv.entity_type
},
{
attr_a: {
type: 'Number',
value: 4
},
TimeInstant: {
type: 'DateTime',
value: '2024-04-10T10:10:00Z'
},
id: globalEnv.entity_name,
type: globalEnv.entity_type
},
{
attr_a: {
type: 'Number',
value: 0
},
TimeInstant: {
type: 'DateTime',
value: '2024-04-10T10:15:00Z'
},
id: globalEnv.entity_name,
type: globalEnv.entity_type
},
{
attr_a: {
type: 'Number',
value: 2
},
TimeInstant: {
type: 'DateTime',
value: '2024-04-10T10:20:00Z'
},
id: globalEnv.entity_name,
type: globalEnv.entity_type
},
{
attr_a: {
type: 'Number',
value: 6
},
TimeInstant: {
type: 'DateTime',
value: '2024-04-10T10:25:00Z'
},
id: globalEnv.entity_name,
type: globalEnv.entity_type
},
{
attr_a: {
type: 'Number',
value: 5
},
TimeInstant: {
type: 'DateTime',
value: '2024-04-10T10:30:00Z'
},
id: globalEnv.entity_name,
type: globalEnv.entity_type
}
]
}
}
]
},
Expand Down Expand Up @@ -2879,56 +3011,56 @@ const testCases = [
actionType: 'append',
entities: [
{
id: globalEnv.entity_name,
id: 'TestType:TestDevice1',
type: globalEnv.entity_type,
a: {
a1: {
value: 23,
type: 'Text',
metadata: {
TimeInstant: {
value: _.isDateString,
value: '2011-01-01T01:11:11.111Z',
type: 'DateTime'
}
}
},
TimeInstant: {
value: _.isDateString,
value: '2011-01-01T01:11:11.111Z',
type: 'DateTime'
}
},
{
id: 'TestType:TestDevice1',
id: 'TestType:TestDevice2',
type: globalEnv.entity_type,
a1: {
a2: {
value: 23,
type: 'Text',
metadata: {
TimeInstant: {
value: '2011-01-01T01:11:11.111Z',
value: '2022-02-02T02:22:22.222Z',
type: 'DateTime'
}
}
},
TimeInstant: {
value: '2011-01-01T01:11:11.111Z',
value: '2022-02-02T02:22:22.222Z',
type: 'DateTime'
}
},
{
id: 'TestType:TestDevice2',
id: globalEnv.entity_name,
type: globalEnv.entity_type,
a2: {
a: {
value: 23,
type: 'Text',
metadata: {
TimeInstant: {
value: '2022-02-02T02:22:22.222Z',
value: _.isDateString,
type: 'DateTime'
}
}
},
TimeInstant: {
value: '2022-02-02T02:22:22.222Z',
value: _.isDateString,
type: 'DateTime'
}
}
Expand Down

0 comments on commit b6a7d23

Please sign in to comment.