feat: #7743 meta/errors

v1.18.x
Barış Soner Uşaklı 6 years ago
parent 99cf47ee08
commit 97d7a85060

@ -1,67 +1,57 @@
'use strict';
var async = require('async');
var winston = require('winston');
var validator = require('validator');
var cronJob = require('cron').CronJob;
const _ = require('lodash');
const winston = require('winston');
const validator = require('validator');
const cronJob = require('cron').CronJob;
var db = require('../database');
var analytics = require('../analytics');
const db = require('../database');
const analytics = require('../analytics');
var Errors = module.exports;
const Errors = module.exports;
var counters = {};
let counters = {};
new cronJob('0 * * * * *', function () {
Errors.writeData();
}, null, true);
Errors.writeData = function () {
var dbQueue = [];
if (Object.keys(counters).length > 0) {
for (var key in counters) {
if (counters.hasOwnProperty(key)) {
dbQueue.push(async.apply(db.sortedSetIncrBy, 'errors:404', counters[key], key));
}
}
Errors.writeData = async function () {
try {
const _counters = _.clone(counters);
counters = {};
async.series(dbQueue, function (err) {
if (err) {
winston.error(err);
}
});
const keys = Object.keys(_counters);
if (!keys.length) {
return;
}
for (const key of keys) {
/* eslint-disable no-await-in-loop */
await db.sortedSetIncrBy('errors:404', _counters[key], key);
}
} catch (err) {
winston.error(err);
}
};
Errors.log404 = function (route, callback) {
callback = callback || function () {};
Errors.log404 = function (route) {
if (!route) {
return setImmediate(callback);
return;
}
route = route.slice(0, 512);
route = route.replace(/\/$/, ''); // remove trailing slashes
route = route.slice(0, 512).replace(/\/$/, ''); // remove trailing slashes
analytics.increment('errors:404');
counters[route] = counters[route] || 0;
counters[route] += 1;
setImmediate(callback);
};
Errors.get = function (escape, callback) {
async.waterfall([
function (next) {
db.getSortedSetRevRangeWithScores('errors:404', 0, 199, next);
},
function (data, next) {
data = data.map(function (nfObject) {
nfObject.value = escape ? validator.escape(String(nfObject.value || '')) : nfObject.value;
return nfObject;
});
next(null, data);
},
], callback);
Errors.get = async function (escape) {
const data = await db.getSortedSetRevRangeWithScores('errors:404', 0, 199);
data.forEach(function (nfObject) {
nfObject.value = escape ? validator.escape(String(nfObject.value || '')) : nfObject.value;
});
return data;
};
Errors.clear = function (callback) {
db.delete('errors:404', callback);
Errors.clear = async function () {
await db.delete('errors:404');
};

Loading…
Cancel
Save