From c26560e3f90a43693b3fa08341f7286d53ef0cc2 Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 27 Feb 2020 15:00:31 -0500 Subject: [PATCH] commit stashed admin-rpc refactor --- lib/commands/admin-rpc.js | 58 +++++++++++++++++++++------------------ lib/historyKeeper.js | 3 ++ 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/lib/commands/admin-rpc.js b/lib/commands/admin-rpc.js index a3bb071ad..13d6a8c36 100644 --- a/lib/commands/admin-rpc.js +++ b/lib/commands/admin-rpc.js @@ -1,8 +1,7 @@ /*jshint esversion: 6 */ -const BatchRead = require("../batch-read"); const nThen = require("nthen"); const getFolderSize = require("get-folder-size"); -//const Util = require("../common-util"); +const Util = require("../common-util"); var Fs = require("fs"); @@ -35,9 +34,8 @@ var shutdown = function (Env, Server, cb) { // and allow system functionality to restart the server }; -const batchRegisteredUsers = BatchRead("GET_REGISTERED_USERS"); -var getRegisteredUsers = function (Env, cb) { - batchRegisteredUsers('', cb, function (done) { +var getRegisteredUsers = function (Env, Server, cb) { + Env.batchRegisteredUsers('', cb, function (done) { var dir = Env.paths.pin; var folders; var users = 0; @@ -63,9 +61,8 @@ var getRegisteredUsers = function (Env, cb) { }); }; -const batchDiskUsage = BatchRead("GET_DISK_USAGE"); -var getDiskUsage = function (Env, cb) { - batchDiskUsage('', cb, function (done) { +var getDiskUsage = function (Env, Server, cb) { + Env.batchDiskUsage('', cb, function (done) { var data = {}; nThen(function (waitFor) { getFolderSize('./', waitFor(function(err, info) { @@ -92,31 +89,40 @@ var getDiskUsage = function (Env, cb) { }); }; -Admin.command = function (Env, safeKey, data, cb, Server) { +var getActiveChannelCount = function (Env, Server, cb) { + cb(void 0, Server.getActiveChannelCount()); +}; + +var flushCache = function (Env, Server, cb) { + Env.flushCache(); + cb(void 0, true); +}; + +var commands = { + ACTIVE_SESSIONS: getActiveSessions, + ACTIVE_PADS: getActiveChannelCount, + REGISTERED_USERS: getRegisteredUsers, + DISK_USAGE: getDiskUsage, + FLUSH_CACHE: flushCache, + SHUTDOWN: shutdown, +}; + +Admin.command = function (Env, safeKey, data, _cb, Server) { + var cb = Util.once(Util.mkAsync(_cb)); + var admins = Env.admins; //var unsafeKey = Util.unescapeKeyCharacters(safeKey); if (admins.indexOf(safeKey) === -1) { return void cb("FORBIDDEN"); } - // Handle commands here - switch (data[0]) { - case 'ACTIVE_SESSIONS': - return getActiveSessions(Env, Server, cb); - case 'ACTIVE_PADS': - return cb(void 0, Server.getActiveChannelCount()); - case 'REGISTERED_USERS': - return getRegisteredUsers(Env, cb); - case 'DISK_USAGE': - return getDiskUsage(Env, cb); - case 'FLUSH_CACHE': - Env.flushCache(); - return cb(void 0, true); - case 'SHUTDOWN': - return shutdown(Env, Server, cb); - default: - return cb('UNHANDLED_ADMIN_COMMAND'); + var command = commands[data[0]]; + + if (typeof(command) === 'function') { + return void command(Env, Server, cb); } + + return void cb('UNHANDLED_ADMIN_COMMAND'); }; diff --git a/lib/historyKeeper.js b/lib/historyKeeper.js index 45668a1b6..ed67602bd 100644 --- a/lib/historyKeeper.js +++ b/lib/historyKeeper.js @@ -37,8 +37,11 @@ module.exports.create = function (config, cb) { metadata_cache: {}, channel_cache: {}, queueStorage: WriteQueue(), + batchIndexReads: BatchRead("HK_GET_INDEX"), batchMetadata: BatchRead('GET_METADATA'), + batchRegisteredUsers: BatchRead("GET_REGISTERED_USERS"), + batchDiskUsage: BatchRead('GET_DISK_USAGE'), //historyKeeper: config.historyKeeper, intervals: config.intervals || {},