feat: #7743 meta/errors
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) {
|
|
||||||
if (counters.hasOwnProperty(key)) {
|
|
||||||
dbQueue.push(async.apply(db.sortedSetIncrBy, 'errors:404', counters[key], key));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
counters = {};
|
counters = {};
|
||||||
async.series(dbQueue, function (err) {
|
const keys = Object.keys(_counters);
|
||||||
if (err) {
|
if (!keys.length) {
|
||||||
winston.error(err);
|
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) {
|
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);
|
nfObject.value = escape ? validator.escape(String(nfObject.value || '')) : nfObject.value;
|
||||||
},
|
});
|
||||||
function (data, next) {
|
return data;
|
||||||
data = data.map(function (nfObject) {
|
|
||||||
nfObject.value = escape ? validator.escape(String(nfObject.value || '')) : nfObject.value;
|
|
||||||
return nfObject;
|
|
||||||
});
|
|
||||||
|
|
||||||
next(null, data);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Errors.clear = function (callback) {
|
Errors.clear = async function () {
|
||||||
db.delete('errors:404', callback);
|
await db.delete('errors:404');
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue