log api search queries

This commit is contained in:
Andris Reinman 2020-03-19 13:21:05 +02:00
parent aca4c7b1ac
commit a44a946496
No known key found for this signature in database
GPG key ID: 5388A30A31834D83
5 changed files with 84 additions and 3 deletions

View file

@ -313,7 +313,7 @@ indexes:
headers.value: text
text: text
partialFilterExpression:
searchable: true
searchable: true # ignore messages marked with \Deleted flag
- collection: messages
index:
@ -583,3 +583,17 @@ indexes:
key:
metadata.audit: 1
metadata.date: 1
- collection: audit.files
type: gridfs # index applies to gridfs database
index:
name: audit_files_queue
key:
metadata.info.queueId: 1
- collection: audit.files
type: gridfs # index applies to gridfs database
index:
name: audit_files_expire
key:
metadata.info.expires: 1

View file

@ -1064,6 +1064,7 @@ module.exports = (db, server, messageHandler, userHandler, storageHandler) => {
}
let total = await getFilteredMessageCount(filter);
log.debug('API', 'Searching %s', JSON.stringify(filter));
let opts = {
limit,

View file

@ -2,6 +2,7 @@
const ObjectID = require('mongodb').ObjectID;
const GridFSBucket = require('mongodb').GridFSBucket;
const log = require('npmlog');
class AuditHandler {
constructor(options) {
@ -172,6 +173,60 @@ class AuditHandler {
}
});
}
async updateDeliveryStatus(queueId, seq, status, info) {
await this.gridfs.collection('audit.files').update(
{ 'metadata.info.queueId': queueId },
{
$push: {
'metadata.info.delivery': {
seq,
status,
time: new Date(),
info
}
}
}
);
}
async removeAudit(auditData) {
let cursor = await this.gridfs.collection('audit.files').find({
'metadata.audit': auditData._id
});
let messages = 0;
let messageData;
while ((messageData = await cursor.next())) {
try {
await this.gridstore.delete(messageData._id);
messages++;
} catch (err) {
log.error('Audit', 'Failed to delete message %s. %s', messageData._id, err.message);
}
}
await cursor.close();
await this.database.collection('audits').deleteOne({ _id: auditData._id });
log.info('Audit', 'Deleted expired audit %s (%s messages)', auditData._id, messages);
}
async cleanExpired() {
let expiredAudits = await this.database
.collection('audits')
.find({
expires: { $lt: new Date() }
})
.toArray();
for (let auditData of expiredAudits) {
try {
await this.removeAudit(auditData);
} catch (err) {
log.error('Audit', 'Failed to delete expired audit %s. %s', auditData._id, err.message);
}
}
}
}
module.exports = AuditHandler;

View file

@ -235,7 +235,7 @@ class MessageHandler {
if (maildata.attachments && maildata.attachments.length) {
messageData.attachments = maildata.attachments;
messageData.ha = !!maildata.attachments.find(a => !a.related);
messageData.ha = maildata.attachments.some(a => !a.related);
} else {
messageData.ha = false;
}

View file

@ -318,7 +318,18 @@ function clearExpiredMessages() {
if (deleted) {
log.verbose('GC', 'Purged %s messages', deleted);
}
return deleteOrphaned(next);
return deleteOrphaned(() => {
auditHandler
.cleanExpired()
.then(() => {
try {
next();
} catch (err) {
// ignore, only needed to prevent calling next() twice
}
})
.catch(next);
});
});
let processNext = () => {