From 97d7a8506062b78833e60868f9cb6a364f380d71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Tue, 23 Jul 2019 22:20:39 -0400 Subject: [PATCH] feat: #7743 meta/errors --- src/meta/errors.js | 76 ++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 43 deletions(-) diff --git a/src/meta/errors.js b/src/meta/errors.js index 6eb0d9a309..43b92ba4b1 100644 --- a/src/meta/errors.js +++ b/src/meta/errors.js @@ -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'); };