Fixed counters

This commit is contained in:
Andris Reinman 2017-07-20 16:42:08 +03:00
parent a1936db9e2
commit fc1c23676f
2 changed files with 29 additions and 6 deletions

15
api.js
View file

@ -1398,7 +1398,10 @@ server.get('/users/:user/updates', (req, res, next) => {
function formatJournalData(e) {
let data = {};
Object.keys(e).forEach(key => {
if (!['_id', 'ignore', 'user'].includes(key)) {
if (!['_id', 'ignore', 'user', 'modseq', 'unseenChange', 'created'].includes(key)) {
if (e.command !== 'COUNTERS' && key === 'unseen') {
return;
}
data[key] = e[key];
}
});
@ -1413,7 +1416,6 @@ function formatJournalData(e) {
}
function loadJournalStream(req, res, user, lastEventId, done) {
console.log('READ');
let query = { user };
if (lastEventId) {
query._id = { $gt: lastEventId };
@ -1446,7 +1448,7 @@ function loadJournalStream(req, res, user, lastEventId, done) {
processed
});
}
let mailbox = mailboxes[mailboxPos++];
let mailbox = new ObjectID(mailboxes[mailboxPos++]);
getMailboxCounter(mailbox, false, (err, total) => {
if (err) {
// ignore
@ -1502,12 +1504,14 @@ function loadJournalStream(req, res, user, lastEventId, done) {
}
function getMailboxCounter(mailbox, type, done) {
let prefix = type ? type : 'sum';
let prefix = type ? type : 'total';
db.redis.get(prefix + ':' + mailbox.toString(), (err, sum) => {
if (err) {
return done(err);
}
console.log(prefix + ':' + mailbox.toString(), err, sum);
if (sum !== null) {
return done(null, Number(sum));
}
@ -1517,11 +1521,14 @@ function getMailboxCounter(mailbox, type, done) {
if (type) {
query[type] = true;
}
db.database.collection('messages').count(query, (err, sum) => {
if (err) {
return done(err);
}
console.log(query, err, sum);
// cache calculated sum in redis
db.redis.multi().set(prefix + ':' + mailbox.toString(), sum).expire(prefix + ':' + mailbox.toString(), consts.MAILBOX_COUNTER_TTL).exec(() => {
done(null, sum);

View file

@ -30,9 +30,10 @@ local ttl = tonumber(ARGV[2]) or 0;
if redis.call("EXISTS", key) == 1 then
redis.call("INCRBY", key, increment);
local sum = tonumber(redis.call("GET", key)) or 0;
-- extend the life of this counter by ttl seconds
redis.call("EXPIRE", key, ttl);
return redis.call("GET", key);
return sum;
else
return nil;
end
@ -65,7 +66,22 @@ module.exports = redis => {
if (err) {
return callback(err);
}
script.run(1, key, count, ttl, callback);
script.run(
1,
key,
count,
ttl,
(
err,
res => {
if (err) {
return callback(err);
}
callback(null, res);
}
)
);
});
}
};