-
Notifications
You must be signed in to change notification settings - Fork 29
/
entitiesStore.js
203 lines (184 loc) · 7.46 KB
/
entitiesStore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright 2015 Telefonica Investigación y Desarrollo, S.A.U
*
* This file is part of perseo-fe
*
* perseo-fe is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* perseo-fe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with perseo-fe.
* If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License
* please contact with::[[email protected]]
*/
'use strict';
var async = require('async'),
appContext = require('../appContext'),
config = require('../../config'),
entitiesCollectionName = require('../../config').orionDb.collection,
myutils = require('../myutils'),
constants = require('../constants'),
logger = require('logops'),
ngsi = require('ngsijs'),
context = { op: 'entitiesStore', comp: constants.COMPONENT_NAME };
function orionServiceDb(service) {
return appContext.OrionDb(config.orionDb.prefix + '-' + service);
}
function createFilter(ruleData, service, subservice, limit, offset) {
var filter = {
service: service,
servicepath: subservice,
type: ruleData.type,
mq: ruleData.attribute + '.dateModified<' + (Date.now() / 1000 - ruleData.reportInterval).toString(),
limit: limit,
offset: offset
};
if (ruleData.id) {
filter.id = ruleData.id;
} else if (ruleData.idRegexp) {
filter.idPattern = ruleData.idRegexp;
}
return filter;
}
function createConnection(service, subservice) {
var options = {
service: service,
servicepath: subservice
};
options.headers = {};
// Add correlator
var domain = process.domain;
if (domain && domain.context) {
options.headers[constants.CORRELATOR_HEADER] = domain.context.corr;
// Add other headers
if (domain.context.srv && options.headers[constants.SERVICE_HEADER] === undefined) {
options.headers[constants.SERVICE_HEADER] = domain.context.srv;
}
if (domain.context.subsrv && options.headers[constants.SUBSERVICE_HEADER] === undefined) {
options.headers[constants.SUBSERVICE_HEADER] = domain.context.subsrv;
}
if (domain.context.from && options.headers[constants.REALIP_HEADER] === undefined) {
options.headers[constants.REALIP_HEADER] = domain.context.from;
}
}
return new ngsi.Connection(config.orion.URL, options);
}
function findSilentEntitiesByAPIWithPagination(connection, filter, alterFunc, accumulatedResults, callback) {
connection.v2.listEntities(filter).then(
(response) => {
if (response.results) {
response.results.forEach((entity) => {
logger.debug(context, 'silent entity %j', entity);
alterFunc(entity);
accumulatedResults.push(entity);
});
}
logger.debug(context, 'findSilentEntities %s', myutils.firstChars(response.results));
if (response.count > filter.limit + filter.offset) {
filter.offset += filter.limit;
findSilentEntitiesByAPIWithPagination(connection, filter, alterFunc, accumulatedResults, callback);
} else {
// Pass the count of entities to the callback
callback(null, accumulatedResults, accumulatedResults.length);
}
},
(error) => {
logger.warn(context, ' error v2.listEntities: %j trying list entities using filter %j', error, filter);
callback(error, null, null);
}
);
}
function findSilentEntitiesByAPI(service, subservice, ruleData, alterFunc, callback) {
var limit = 20;
var offset = 0;
var connection = createConnection(service, subservice);
var filter = createFilter(ruleData, service, subservice, limit, offset);
logger.info(
context,
' find silent entities by API ngsi using options %j and filter %j and rule %j',
connection,
filter,
ruleData
);
var accumulatedResults = [];
findSilentEntitiesByAPIWithPagination(connection, filter, alterFunc, accumulatedResults, callback);
}
function findSilentEntitiesByMongo(service, subservice, ruleData, alterFunc, callback) {
var db,
criterion = {};
db = orionServiceDb(service);
criterion['attrs.' + ruleData.attribute + '.modDate'] = {
$lt: Date.now() / 1000 - ruleData.reportInterval
};
criterion['_id.servicePath'] = subservice;
if (ruleData.id) {
criterion['_id.id'] = ruleData.id;
} else if (ruleData.idRegexp) {
try {
criterion['_id.id'] = new RegExp(ruleData.idRegexp);
} catch (e) {
return callback(e, null);
}
}
if (ruleData.type) {
criterion['_id.type'] = ruleData.type;
}
logger.debug(context, 'findSilentEntities criterion %j', criterion);
// Variable to store the count of entities
var entityCount = 0;
async.waterfall(
[
db.collection.bind(db, entitiesCollectionName, { strict: true }),
function(col, cb) {
col.find(criterion)
.batchSize(config.orionDb.batchSize)
.each(function(err, one) {
if (err) {
return cb(err, null);
}
if (one === null) {
// Cursor exhausted
return cb(err, 'silent ones count ' + entityCount);
}
logger.debug(context, 'silent entity %j', one._id);
alterFunc(one);
// Increment the count of entities
entityCount++;
});
}
],
function(err, result) {
logger.debug(context, 'findSilentEntities %s', myutils.firstChars(result));
return callback(err, result, entityCount);
}
);
}
function findSilentEntities(service, subservice, ruleData, alterFunc, callback) {
var hrstart = process.hrtime();
var method = !config.nonSignalByAPI ? 'findSilentEntitiesByMongo' : 'findSilentEntitiesByAPI';
var timedCallback = function(err, result, entityCount) {
var hrend = process.hrtime(hrstart);
logger.info(context, ' %s has found %d entities in (hr): %d ms', method, entityCount, hrend[1] / 1000000);
callback(err, result, entityCount);
};
if (!config.nonSignalByAPI) {
return findSilentEntitiesByMongo(service, subservice, ruleData, alterFunc, timedCallback);
} else {
return findSilentEntitiesByAPI(service, subservice, ruleData, alterFunc, timedCallback);
}
}
module.exports.FindSilentEntities = findSilentEntities;
module.exports.findSilentEntitiesByAPI = findSilentEntitiesByAPI;
module.exports.findSilentEntitiesByMongo = findSilentEntitiesByMongo;
module.exports.findSilentEntitiesByAPIWithPagination = findSilentEntitiesByAPIWithPagination;
module.exports.createConnection = createConnection;
module.exports.createFilter = createFilter;