Skip to content
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Upgrade: mongodb dependency from 3.6.12 to 6.19.0
- Add PERSEO_SMTP_TLS_CA, PERSEO_SMTP_TLS_KEY, PERSEO_SMTP_TLS_CERT, PERSEO_SMTP_TLS_MIN_VERSION and PERSEO_SMTP_TLS_SERVER_NAME env vars for global config (#841)
- Add: allow send email bodies as HTML (keeping also text) when template is expanded to html (#837)
- Upgrade NodeJS version from 16-slim to 24-bullseye-slim in Dockerfile
45 changes: 18 additions & 27 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var async = require('async'),
myutils = require('./myutils'),
constants = require('./constants'),
alarm = require('./alarm'),
mongodb = require('mongodb'),
database,
orionDb,
delay = config.checkDB.delay,
Expand All @@ -46,16 +47,19 @@ function pingAux(db, component, callback) {
}

function getDbAux(url, component, callback) {
var client = require('mongodb').MongoClient,
var client = mongodb.MongoClient,
checkDbHealthFunc;

client.connect(
url,
{
bufferMaxEntries: config.checkDB.bufferMaxEntries,
domainsEnabled: true,
reconnectTries: config.checkDB.reconnectTries,
reconnectInterval: config.checkDB.reconnectInterval
//useUnifiedTopology: true
// connectTimeoutMS is no longer supported in MongoDB 4.x
// (see https://stackoverflow.com/q/72699235/1485926)
// we keep the existing checkDB configuration parameters, but
// the driver parameters are now socketTimeoutMS and
// serverSelectionTimeoutMS
socketTimeoutMS: config.checkDB.reconnectInterval,
serverSelectionTimeoutMS: config.checkDB.reconnectInterval
},
function(err, client) {
if (err) {
Expand All @@ -65,15 +69,6 @@ function getDbAux(url, component, callback) {

const db = client.db();

// This event is emitted only by Server topology (standalone)
// The driver has given up getting a connection, so we will die (restart perseo usually)
// and re-try from scratch.
// The ReplSet does not emit 'reconnectFailed'
db.serverConfig.on('reconnectFailed', function() {
logger.fatal('too many tries to reconnect to database, dying ...');
process.exit(-2);
});

checkDbHealthFunc = function checkDbHealth() {
pingAux(db, component, function(err, result) {
logger.debug('ping (%s) %j', component, err || result);
Expand All @@ -98,21 +93,17 @@ function getOrionDb(callback) {
}

function ensureIndex(collection, fields, callback) {
database.collection(collection, function(err, collection) {
myutils.logErrorIf(err, collection, context);
collection.createIndex(fields, { unique: true }, function(err, indexName) {
myutils.logErrorIf(err, 'ensureIndex ' + collection, context);
callback(err, indexName);
});
var col = database.collection(collection);
col.createIndex(fields, { unique: true }, function(err, indexName) {
myutils.logErrorIf(err, 'ensureIndex ' + collection, context);
callback(err, indexName);
});
}
function ensureIndexTTL(collection, fields, ttl, callback) {
database.collection(collection, function(err, collection) {
myutils.logErrorIf(err, collection);
collection.createIndex(fields, { expireAfterSeconds: ttl }, function(err, indexName) {
myutils.logErrorIf(err, 'ensureIndex ' + collection, context);
callback(err, indexName);
});
var col = database.collection(collection);
col.createIndex(fields, { expireAfterSeconds: ttl }, function(err, indexName) {
myutils.logErrorIf(err, 'ensureIndex ' + collection, context);
callback(err, indexName);
});
}

Expand Down
178 changes: 95 additions & 83 deletions lib/models/entitiesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
*/
'use strict';

var async = require('async'),
appContext = require('../appContext'),
var appContext = require('../appContext'),
config = require('../../config'),
entitiesCollectionName = require('../../config').orionDb.collection,
myutils = require('../myutils'),
Expand Down Expand Up @@ -138,6 +137,10 @@ function findSilentEntitiesByMongo(service, subservice, ruleData, alterFunc, cal
var db,
criterion = {};

var cb = function(err, result, entityCount) {
logger.debug(context, 'findSilentEntitiesByMongo %s', myutils.firstChars(result));
return callback(err, result, entityCount);
};
db = orionServiceDb(service);
criterion['_id.servicePath'] = subservice;
if (ruleData.id) {
Expand All @@ -152,94 +155,96 @@ function findSilentEntitiesByMongo(service, subservice, ruleData, alterFunc, cal
if (ruleData.type) {
criterion['_id.type'] = ruleData.type;
}
// Variable to store the count of entities
var entityCount = 0;

async.waterfall(
[
db.collection.bind(db, entitiesCollectionName, { strict: false }),
function(col, cb) {
var pipeline = [
{
$match: criterion
},
{
$addFields: {
currentTime: { $divide: [Date.now(), 1000] },
maxTimeDetection: {
$convert: {
input: {
$ifNull: [
'$attrs.' + ruleData.maxTimeDetectionAttr + '.value',
ruleData.maxTimeDetection !== undefined
? ruleData.maxTimeDetection // jshint ignore: line
: config.nonSignalMaxTimeDetection
]
},
to: 'double',
onError: config.nonSignalMaxTimeDetection,
onNull: config.nonSignalMaxTimeDetection
}
logger.debug(context, 'findSilentEntities criterion %j', criterion);

myutils.collectionExists(db, entitiesCollectionName, function(exists) {
if (!exists) {
return cb('collection ' + entitiesCollectionName + ' does not exist');
}

var col = db.collection(entitiesCollectionName);

// Variable to store the count of entities
var entityCount = 0;

var pipeline = [
{
$match: criterion
},
{
$addFields: {
currentTime: { $divide: [Date.now(), 1000] },
maxTimeDetection: {
$convert: {
input: {
$ifNull: [
'$attrs.' + ruleData.maxTimeDetectionAttr + '.value',
ruleData.maxTimeDetection !== undefined
? ruleData.maxTimeDetection // jshint ignore: line
: config.nonSignalMaxTimeDetection
]
},
reportInterval: {
$convert: {
input: {
$ifNull: [
'$attrs.' + ruleData.reportIntervalAttr + '.value',
ruleData.reportInterval
]
},
to: 'double',
onError: ruleData.reportInterval,
onNull: ruleData.reportInterval
}
}
to: 'double',
onError: config.nonSignalMaxTimeDetection,
onNull: config.nonSignalMaxTimeDetection
}
},
{
$match: {
$expr: {
$and: [
{
$lt: [
'$attrs.' + ruleData.attribute + '.modDate',
{ $subtract: ['$currentTime', '$reportInterval'] }
]
},
{
$gt: [
'$attrs.' + ruleData.attribute + '.modDate',
{ $subtract: ['$currentTime', '$maxTimeDetection'] }
]
},
{
$gt: ['$attrs.' + ruleData.attribute + '.modDate', 0]
} // exclude invalid dates
]
}
reportInterval: {
$convert: {
input: {
$ifNull: ['$attrs.' + ruleData.reportIntervalAttr + '.value', ruleData.reportInterval]
},
to: 'double',
onError: ruleData.reportInterval,
onNull: ruleData.reportInterval
}
}
];
logger.debug(context, 'findSilentEntities service %s pipeline: %j ', service, pipeline);
col.aggregate(pipeline).toArray(function(err, results) {
if (err) {
return cb(err, null);
}
},
{
$match: {
$expr: {
$and: [
{
$lt: [
'$attrs.' + ruleData.attribute + '.modDate',
{ $subtract: ['$currentTime', '$reportInterval'] }
]
},
{
$gt: [
'$attrs.' + ruleData.attribute + '.modDate',
{ $subtract: ['$currentTime', '$maxTimeDetection'] }
]
},
{
$gt: ['$attrs.' + ruleData.attribute + '.modDate', 0]
} // exclude invalid dates
]
}
results.forEach(function(one) {
logger.debug(context, 'silent entity %j', one._id);
alterFunc(one);
// Increment the count of entities
entityCount++;
});
cb(null, 'silent ones count ' + entityCount);
});
}
}
],
function(err, result) {
logger.debug(context, 'findSilentEntities %s', myutils.firstChars(result));
return callback(err, result, entityCount);
}
);
];

logger.debug(context, 'findSilentEntities service %s pipeline: %j ', service, pipeline);
var cursor = col.aggregate(pipeline);

cursor.toArray(function(err, results) {
//myutils.logErrorIf(err);
if (err) {
return cb(err, null);
}
results.forEach(function(one) {
logger.debug(context, 'silent entity %j', one._id);
alterFunc(one);
// Increment the count of entities
entityCount++;
});
//cb(null, 'silent ones count ' + entityCount);
return cb(null, results, entityCount);
});
});
}

function findSilentEntities(service, subservice, ruleData, alterFunc, callback) {
Expand All @@ -249,7 +254,14 @@ function findSilentEntities(service, subservice, ruleData, alterFunc, callback)
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);
logger.info(
context,
' %s has found %d entities in (hr): %d ms result %j',
method,
entityCount,
hrend[1] / 1000000,
result
);

callback(err, result, entityCount);
};
Expand Down
Loading
Loading