Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add unique index for Device model #1705

Merged
merged 3 commits into from
Apr 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
- Fix: modified JEXL transformations (toisostring, gettime, parseint, etc.) to return null instead of NaN when some unexpected situation occurs (#1701)
- Add: index for Device model based on {service: 1, subservice: 1, id: 1, apikey: 1} (#1576)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have index documented in some .md file (either in this repo or IOTA-JSON)? In that case, documentation should be changed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- Fix: Duplicated Devices when burst measures to non provisioned Device (iotagent-json#865)
- Fix: modified JEXL transformations (toisostring, gettime, parseint, etc.) to return null instead of NaN when some unexpected situation occurs (#1701)
1 change: 1 addition & 0 deletions lib/model/Device.js
Original file line number Diff line number Diff line change
@@ -61,6 +61,7 @@ const Device = new Schema({
});

function load() {
Device.index({ service: 1, subservice: 1, apikey: 1, id: 1 }, { unique: true });
module.exports.model = mongoose.model('Device', Device);
module.exports.internalSchema = Device;
}
2 changes: 1 addition & 1 deletion lib/services/devices/deviceRegistryMongoDB.js
Original file line number Diff line number Diff line change
@@ -90,7 +90,7 @@ function storeDevice(newDevice, callback) {
if (error.code === 11000) {
logger.debug(context, 'Tried to insert a device with duplicate ID in the database: %s', error);

callback(new errors.DuplicateDeviceId(newDevice));
callback(new errors.DuplicateDeviceId(newDevice), newDevice);
} else {
logger.debug(context, 'Error storing device information: %s', error);

13 changes: 11 additions & 2 deletions lib/services/devices/deviceService.js
Original file line number Diff line number Diff line change
@@ -40,6 +40,8 @@ const registrationUtils = require('./registrationUtils');
const subscriptions = require('../ngsi/subscriptionService');
const expressionPlugin = require('./../../plugins/expressionPlugin');
const pluginUtils = require('./../../plugins/pluginUtils');
const alarms = require('../common/alarmManagement');
const constants = require('../../constants');
const _ = require('underscore');
const context = {
op: 'IoTAgentNGSI.DeviceService'
@@ -250,7 +252,7 @@ function registerDevice(deviceObj, callback) {
/* eslint-disable-next-line no-unused-vars */
function (error, device) {
if (!error) {
innerCb(new errors.DuplicateDeviceId(deviceObj));
innerCb(new errors.DuplicateDeviceId(deviceObj), device);
} else {
innerCb();
}
@@ -664,7 +666,14 @@ function findOrCreate(deviceId, apikey, group, callback) {
if (group.autoprovision === undefined || group.autoprovision === true) {
logger.debug(context, 'Registering autoprovision of Device %j for its conf %j', newDevice, group);
registerDevice(newDevice, function (error, device) {
callback(error, device, group);
if (error && error.name === 'DUPLICATE_DEVICE_ID') {
alarms.release(constants.MONGO_ALARM, error);
logger.warn(context, 'Error %j already registered autoprovisioned device: %j ', error, device);
callback(null, device, group);
} else {
logger.debug(context, 'registered autoprovisioned device: %j ', device);
callback(error, device, group);
}
});
} else {
logger.info(