feat: #7743 meta/errors

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

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

Loading…
Cancel
Save