Use MongoDB text index in Logs
MODIFY
src/locking/LockingManager.ts
src/locking/LockingManager.ts
@@ -53,7 +53,7 @@ export default class LockingManager {
message: `Cannot acquire the lock entity '${lock.entity}' ('${lock.key}') of type '${lock.type}'`,
detailedMessages: { lock, error: error.message, stack: error.stack }
});
- Utils.isDevelopmentEnv() && console.debug(`Cannot acquire the lock entity '${lock.entity}' ('${lock.key}') of type '${lock.type}'`);
+ Utils.isDevelopmentEnv() && console.error(`>>>>> Cannot acquire the lock entity '${lock.entity}' ('${lock.key}') of type '${lock.type}'`);
return false;
}
}

MODIFY
src/storage/mongodb/LoggingStorage.ts
src/storage/mongodb/LoggingStorage.ts
@@ -113,6 +113,10 @@ export default class LoggingStorage {
dbParams.skip = Utils.checkRecordSkip(dbParams.skip);
// Set the filters
const filters: FilterParams = {};
+ // Search
+ if (params.search) {
+ filters.$text = { $search: params.search };
+ }
// Date provided?
if (params.startDateTime || params.endDateTime) {
filters.timestamp = {};
@@ -158,24 +162,6 @@ export default class LoggingStorage {
$in: params.logIDs.map((logID) => Utils.convertToObjectID(logID))
};
}
- // Search
- if (params.search) {
- const searchArray = [
- { 'source': { $regex: params.search, $options: 'i' } },
- { 'host': { $regex: params.search, $options: 'i' } },
- { 'message': { $regex: params.search, $options: 'i' } },
- { 'action': { $regex: params.search, $options: 'i' } }
- ];
- // Already exists?
- if (filters.$or) {
- filters.$and = [
- { $or: [...filters.$or] },
- { $or: [...searchArray] },
- ];
- } else {
- filters.$or = searchArray;
- }
- }
// Create Aggregation
const aggregation = [];
// Filters
@@ -199,7 +185,7 @@ export default class LoggingStorage {
.toArray();
// Check if only the total count is requested
if (dbParams.onlyRecordCount) {
- Logging.traceEnd(tenantID, MODULE_NAME, 'getLogs', uniqueTimerID);
+ Logging.traceEnd(tenantID, MODULE_NAME, 'getLogs', uniqueTimerID, loggingsCountMDB);
return {
count: (loggingsCountMDB.length > 0 ? loggingsCountMDB[0].count : 0),
result: []

MODIFY
src/storage/mongodb/MongoDBStorage.ts
src/storage/mongodb/MongoDBStorage.ts
@@ -76,6 +76,7 @@ export default class MongoDBStorage {
{ fields: { level: 1, timestamp: 1 } },
{ fields: { source: 1, timestamp: 1 } },
{ fields: { host: 1, timestamp: 1 } },
+ { fields: { message: 'text', source: 'text', host: 'text', action: 'text' } },
]);
// MeterValues
await this.handleIndexesInCollection(tenantID, 'metervalues', [
@@ -276,8 +277,7 @@ export default class MongoDBStorage {
}
}
const tenantsMDB = await this.db.collection(DatabaseUtils.getCollectionName(Constants.DEFAULT_TENANT, 'tenants'))
- .find({})
- .toArray();
+ .find({}).toArray();
const tenantIds = tenantsMDB.map((t): string => t._id.toString());
for (const tenantId of tenantIds) {
// Database creation Lock
@@ -319,41 +319,43 @@ export default class MongoDBStorage {
try {
await this.db.createCollection(tenantCollectionName);
} catch (error) {
- console.log(`Error in creating collection '${tenantCollectionName}': ${error.message}`);
+ console.error(`>>>>> Error in creating collection '${tenantCollectionName}': ${error.message}`);
}
}
// Indexes?
if (indexes) {
// Get current indexes
const databaseIndexes = await this.db.collection(tenantCollectionName).listIndexes().toArray();
- // Check each index that should be dropped
+ // Drop indexes
for (const databaseIndex of databaseIndexes) {
- // Bypass ID
if (databaseIndex.key._id) {
continue;
}
- // Exists?
const foundIndex = indexes.find((index) => (JSON.stringify(index.fields) === JSON.stringify(databaseIndex.key)));
if (!foundIndex) {
- // Drop indexes
if (Utils.isDevelopmentEnv()) {
console.log(`Drop index ${JSON.stringify(databaseIndex.key)} on collection ${tenantID}.${name}`);
}
+ try {
await this.db.collection(tenantCollectionName).dropIndex(databaseIndex.key);
+ } catch (error) {
+ console.error(`>>>>> Error in dropping index '${JSON.stringify(databaseIndex.key)}' in '${tenantCollectionName}': ${error.message}`);
+ }
}
}
- // Check each index that should be created
+ // Create indexes
for (const index of indexes) {
- // Create
- // Check if it exists
const foundIndex = databaseIndexes.find((existingIndex) => (JSON.stringify(existingIndex.key) === JSON.stringify(index.fields)));
if (!foundIndex) {
- // Create Indexes
if (Utils.isDevelopmentEnv()) {
console.log(`Create index ${JSON.stringify(index)} on collection ${tenantID}.${name}`);
}
+ try {
// eslint-disable-next-line @typescript-eslint/await-thenable
await this.db.collection(tenantCollectionName).createIndex(index.fields, index.options);
+ } catch (error) {
+ console.error(`>>>>> Error in creating index '${JSON.stringify(index.fields)}' with options '${JSON.stringify(index.options)}' in '${tenantCollectionName}': ${error.message}`);
+ }
}
}
}

MODIFY
src/utils/Logging.ts
src/utils/Logging.ts
@@ -55,7 +55,7 @@ export default class Logging {
delete Logging.traceCalls[key];
found = true;
}
- console.log(`${module}.${method} ${found ? '- ' + executionDurationSecs.toString() + 'ms' : ''} ${!Utils.isEmptyJSon(data) ? '- ' + (sizeof(data) / 1000) + 'Kb' : ''}`);
+ console.debug(`${module}.${method} ${found ? '- ' + executionDurationSecs.toString() + 'ms' : ''} ${!Utils.isEmptyJSon(data) ? '- ' + (sizeof(data) / 1000) + 'Kb' : ''}`);
}
}
@@ -160,7 +160,7 @@ export default class Logging {
contentLengthKB = Utils.roundTo(res.getHeader('content-length') as number / 1024, 2);
}
if (Utils.isDevelopmentEnv()) {
- console.log(`Express HTTP Response - ${(durationSecs > 0) ? durationSecs : '?'}s - ${(contentLengthKB > 0) ? contentLengthKB : '?'}kB >> ${req.method}/${res.statusCode} '${req.url}'`);
+ console.debug(`Express HTTP Response - ${(durationSecs > 0) ? durationSecs : '?'}s - ${(contentLengthKB > 0) ? contentLengthKB : '?'}kB >> ${req.method}/${res.statusCode} '${req.url}'`);
}
Logging.logSecurityDebug({
tenantID: tenantID,
@@ -756,7 +756,7 @@ export default class Logging {
found = true;
}
if (Utils.isDevelopmentEnv()) {
- console.log(`${direction} OCPP Request '${action}' on '${chargeBoxID}' has been processed ${found ? 'in ' + executionDurationSecs.toString() + ' secs' : ''}`);
+ console.debug(`${direction} OCPP Request '${action}' on '${chargeBoxID}' has been processed ${found ? 'in ' + executionDurationSecs.toString() + ' secs' : ''}`);
}
if (detailedMessages && detailedMessages['status'] && detailedMessages['status'] === 'Rejected') {
Logging.logError({

Use MongoDB text index in Logs