Skip to content

Commit

Permalink
Merge pull request #34 from aarranz/feature/filter-attributes-metadata
Browse files Browse the repository at this point in the history
Allow to configure what attributes and what metadata to request to the context broker
  • Loading branch information
aarranz authored May 13, 2021
2 parents 82f3622 + fc48f52 commit ec50e78
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 19 deletions.
14 changes: 13 additions & 1 deletion src/config.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<operator xmlns="http://wirecloud.conwet.fi.upm.es/ns/macdescription/1" vendor="CoNWeT" name="ngsi-source" version="4.2.0rc1">
<operator xmlns="http://wirecloud.conwet.fi.upm.es/ns/macdescription/1" vendor="CoNWeT" name="ngsi-source" version="4.2.0rc2">
<details>
<title>NGSI source</title>
<homepage>https://github.com/wirecloud-fiware/ngsi-source</homepage>
Expand Down Expand Up @@ -72,6 +72,18 @@
label="Query"
description="Filter entities by providing a query using the Simple Query Language."
default="" />
<preference
name="ngsi_attributes"
type="text"
label="Requested Attributes"
description="A comma separated list of attribute names or the `*` wildcard. Use `*` or an empty value to request all attributes except the builtin ones (`dateCreated`, `dateModified`, ...)."
default="*" />
<preference
name="ngsi_metadata"
type="text"
label="Requested Metadata"
description="A comma separated list of attribute metadata names or the `*` wildcard. Use `*` or an empty value to request all attribute matadata except the builtin ones (`dateCreated`, `dateModified`, ...)."
default="*" />
<preference
name="ngsi_update_attributes"
type="text"
Expand Down
2 changes: 2 additions & 0 deletions src/doc/changelog.es.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- Añadida la opción de subscribirse a los cambios en cualquiera de los
atributos mediante el uso de comodín `*` en la preferencia **Monitored NGSI
Attributes**.
- Soporte para poder configurar que atributos y que metadatos van a ser pedidos
al context broker.


## v4.0.0 (2017-12-07)
Expand Down
2 changes: 2 additions & 0 deletions src/doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
to take effect.
- Allow to monitor all entity attributes by providing the `*` value on the
**Monitored NGSI Attributes** setting.
- Allow to configure what attributes and metadata values are going to be
requested to the context broker.


## v4.0.0 (2017-12-07)
Expand Down
9 changes: 6 additions & 3 deletions src/doc/userguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ Settings
empty, in that case, entities won't be filtered by id.
- **Query:** Filter entities by providing a query using the Simple Query
Language.
- **Requested Attributes:** A comma separated list of attribute names or the `*`
wildcard. Use `*` or an empty value to request all attributes except the
builtin ones (`dateCreated`, `dateModified`, ...).
- **Requested Metadata:** A comma separated list of attribute metadata names or
the `*` wildcard. Use `*` or an empty value to request all attribute matadata
except the builtin ones (`dateCreated`, `dateModified`, ...).
- **Monitored NGSI Attributes:** Attributes to monitor for updates. Those
changes are tracked by creating a subscription inside the context broker. If
this list is empty, that subscription won't be created. Use `*` to subscribe
to changes on any attribute.
- **attributes format ('keyValues', 'normalized' or 'values'):** Specifies how
the entities are represented in notifications. Accepted values are 'normalized'
(default), 'keyValues' or 'values'.


Wiring
Expand Down
46 changes: 32 additions & 14 deletions src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
/* ******************************** PRIVATE ************************************/
/* *****************************************************************************/

const doInitialQueries = function doInitialQueries(idPattern, types, filter) {
const doInitialQueries = function doInitialQueries(idPattern, types, filter, attributes, metadata) {
const attrsFormat = MashupPlatform.operator.outputs.normalizedOutput.connected ? "normalized" : "keyValues";
this.query_task = requestInitialData.call(this, idPattern, types, filter, attrsFormat, 0);
this.query_task = requestInitialData.call(this, idPattern, types, filter, attributes, metadata, attrsFormat, 0);
};

const refreshNGSISubscription = function refreshNGSISubscription() {
Expand Down Expand Up @@ -103,28 +103,42 @@
id_pattern = '.*';
}

// Filter using the Simple Query Language supported by NGSIv2
let filter = MashupPlatform.prefs.get('query').trim();
let attrs = MashupPlatform.prefs.get('ngsi_update_attributes').trim();
if (filter === '') {
if (filter === "") {
filter = undefined;
}

// Filter entity attributes
let attributes = MashupPlatform.prefs.get("ngsi_attributes").trim();
if (attributes === "" || attributes === "*") {
attributes = undefined;
}

// Filter attribute metadata
let metadata = MashupPlatform.prefs.get("ngsi_metadata").trim();
if (metadata === "" || metadata === "*") {
metadata = undefined;
}

// Monitored attributes
let monitored_attrs = MashupPlatform.prefs.get('ngsi_update_attributes').trim();
let condition = undefined;
if (filter != null || attrs !== "") {
if (filter != null || monitored_attrs !== "") {
condition = {};
}
if (attrs !== "") {
attrs = attrs.split(/,\s*/);
condition.attrs = attrs.includes("*") ? [] : attrs;
if (monitored_attrs !== "") {
monitored_attrs = monitored_attrs.split(/,\s*/);
condition.attrs = monitored_attrs.includes("*") ? [] : monitored_attrs;
}
if (filter != null) {
condition.expression = {
q: filter
};
}

if (attrs === "") {
doInitialQueries.call(this, id_pattern, types, filter);
if (monitored_attrs === "") {
doInitialQueries.call(this, id_pattern, types, filter, attributes, metadata);
} else {
let entities = [];
if (types != null) {
Expand All @@ -146,6 +160,8 @@
condition: condition
},
notification: {
attrs: attributes != null ? attributes.split(/,\s*/) : undefined,
metadata: metadata != null ? metadata.split(/,\s*/) : undefined,
attrsFormat: attrsFormat,
callback: (notification) => {
handlerReceiveEntities.call(this, attrsFormat, notification.data);
Expand All @@ -159,7 +175,7 @@
MashupPlatform.operator.log("Subscription created successfully (id: " + response.subscription.id + ")", MashupPlatform.log.INFO);
this.subscriptionId = response.subscription.id;
this.refresh_interval = setInterval(refreshNGSISubscription.bind(this), 1000 * 60 * 60 * 2); // each 2 hours
doInitialQueries.call(this, id_pattern, types, filter);
doInitialQueries.call(this, id_pattern, types, filter, attributes, metadata);
},
(e) => {
if (e instanceof NGSI.ProxyConnectionError) {
Expand All @@ -172,7 +188,7 @@
}
};

const requestInitialData = function requestInitialData(idPattern, types, filter, attrsFormat, page) {
const requestInitialData = function requestInitialData(idPattern, types, filter, attributes, metadata, attrsFormat, page) {
return this.connection.v2.listEntities(
{
idPattern: idPattern,
Expand All @@ -181,13 +197,15 @@
keyValues: attrsFormat === "keyValues",
limit: 100,
offset: page * 100,
q: filter
q: filter,
attrs: attributes,
metadata: metadata
}
).then(
(response) => {
handlerReceiveEntities.call(this, attrsFormat, response.results);
if (page < 100 && (page + 1) * 100 < response.count) {
return requestInitialData.call(this, idPattern, types, filter, attrsFormat, page + 1);
return requestInitialData.call(this, idPattern, types, filter, attributes, metadata, attrsFormat, page + 1);
}
},
() => {
Expand Down
44 changes: 43 additions & 1 deletion tests/js/NGSISourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
type: 'operator',
prefs: {
'query': '',
'ngsi_attributes': '*',
'ngsi_entities': '',
'ngsi_id_filter': '',
'ngsi_metadata': '*',
'ngsi_proxy': 'https://ngsiproxy.example.com',
'ngsi_server': 'https://orion.example.com',
'ngsi_tenant': 'Tenant',
'ngsi_service_path': '/Spain/Madrid',
'ngsi_tenant': 'Tenant',
'ngsi_update_attributes': '',
'use_owner_credentials': false,
'use_user_fiware_token': false
Expand Down Expand Up @@ -333,6 +335,46 @@
}, 0);
});

it("connect (custom attributes)", (done) => {
MashupPlatform.prefs.set('ngsi_attributes', 'speed,location');
MashupPlatform.prefs.set('ngsi_update_attributes', 'location');
MashupPlatform.operator.outputs.entityOutput.connect(true);

operator.init();

setTimeout(() => {
// List Entities Options
const leo = operator.connection.v2.listEntities.calls.mostRecent().args[0];
expect(leo.attrs).toEqual("speed,location");

// Create Subscription Options
const cso = operator.connection.v2.createSubscription.calls.mostRecent().args[0];

expect(cso.notification.attrs).toEqual(["speed", "location"]);
done();
});
});

it("connect (custom metadata)", (done) => {
MashupPlatform.prefs.set('ngsi_metadata', 'unitCode,timestamp');
MashupPlatform.prefs.set('ngsi_update_attributes', 'location');
MashupPlatform.operator.outputs.entityOutput.connect(true);

operator.init();

setTimeout(() => {
// List Entities Options
const leo = operator.connection.v2.listEntities.calls.mostRecent().args[0];
expect(leo.metadata).toEqual("unitCode,timestamp");

// Create Subscription Options
const cso = operator.connection.v2.createSubscription.calls.mostRecent().args[0];

expect(cso.notification.metadata).toEqual(["unitCode", "timestamp"]);
done();
});
});

it("connect (types + subscription)", (done) => {
MashupPlatform.operator.outputs.entityOutput.connect(true);
MashupPlatform.prefs.set('ngsi_entities', 'AirQualityObserved, WeatherForecast');
Expand Down

0 comments on commit ec50e78

Please sign in to comment.