From ec97b3e8d405ea62b1f862be9b6b9c88da015ebc Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Sun, 25 Nov 2018 19:05:16 -0700 Subject: [PATCH] More user/system stat constants & usage --- core/bbs.js | 7 ++++--- core/config.js | 2 +- core/database.js | 8 -------- core/file_area_web.js | 11 +++++++---- core/file_base_area.js | 3 ++- core/file_base_area_select.js | 3 ++- core/file_transfer.js | 20 ++++++++++++-------- core/last_callers.js | 3 ++- core/predefined_mci.js | 29 +++++++++++++++-------------- core/rumorz.js | 21 +++++++++++++-------- core/stat_log.js | 3 +-- core/system_log.js | 11 +++++++++++ core/system_property.js | 25 +++++++++++++++++++++++++ core/user_login.js | 6 ++++-- 14 files changed, 99 insertions(+), 53 deletions(-) create mode 100644 core/system_log.js create mode 100644 core/system_property.js diff --git a/core/bbs.js b/core/bbs.js index ede2b82e..000b19a8 100644 --- a/core/bbs.js +++ b/core/bbs.js @@ -11,6 +11,7 @@ const logger = require('./logger.js'); const database = require('./database.js'); const resolvePath = require('./misc_util.js').resolvePath; const UserProps = require('./user_property.js'); +const SysProps = require('./system_property.js'); // deps const async = require('async'); @@ -246,13 +247,13 @@ function initialize(cb) { if(err) { propLoadOpts.names.concat('username').forEach(v => { - StatLog.setNonPeristentSystemStat(`sysop_${v}`, 'N/A'); + StatLog.setNonPersistentSystemStat(`sysop_${v}`, 'N/A'); }); } else { opProps.username = opUserName; _.each(opProps, (v, k) => { - StatLog.setNonPeristentSystemStat(`sysop_${k}`, v); + StatLog.setNonPersistentSystemStat(`sysop_${k}`, v); }); } @@ -265,7 +266,7 @@ function initialize(cb) { getAreaStats( (err, stats) => { if(!err) { const StatLog = require('./stat_log.js'); - StatLog.setNonPeristentSystemStat('file_base_area_stats', stats); + StatLog.setNonPersistentSystemStat(SysProps.FileBaseAreaStats, stats); } return callback(null); diff --git a/core/config.js b/core/config.js index 701c8494..a90ff50d 100644 --- a/core/config.js +++ b/core/config.js @@ -969,7 +969,7 @@ function getDefaultConfig() { statLog : { systemEvents : { - loginHistoryMax: -1 // forever + loginHistoryMax: 5000, // set to -1 for forever } } }; diff --git a/core/database.js b/core/database.js index 017aa949..90f15692 100644 --- a/core/database.js +++ b/core/database.js @@ -189,14 +189,6 @@ const DB_INIT_TABLE = { );` ); - dbs.user.run( - `CREATE TABLE IF NOT EXISTS user_login_history ( - user_id INTEGER NOT NULL, - user_name VARCHAR NOT NULL, - timestamp DATETIME NOT NULL - );` - ); - return cb(null); }, diff --git a/core/file_area_web.js b/core/file_area_web.js index a1d12989..b36c8b72 100644 --- a/core/file_area_web.js +++ b/core/file_area_web.js @@ -15,6 +15,8 @@ const Log = require('./logger.js').log; const getConnectionByUserId = require('./client_connections.js').getConnectionByUserId; const webServerPackageName = require('./servers/content/web.js').moduleInfo.packageName; const Events = require('./events.js'); +const UserProps = require('./user_property.js'); +const SysProps = require('./system_menu_method.js'); // deps const hashids = require('hashids'); @@ -470,10 +472,11 @@ class FileAreaWebAccess { }); }, function updateStats(user, callback) { - StatLog.incrementUserStat(user, 'dl_total_count', 1); - StatLog.incrementUserStat(user, 'dl_total_bytes', dlBytes); - StatLog.incrementSystemStat('dl_total_count', 1); - StatLog.incrementSystemStat('dl_total_bytes', dlBytes); + StatLog.incrementUserStat(user, UserProps.FileDlTotalCount, 1); + StatLog.incrementUserStat(user, UserProps.FileDlTotalBytes, dlBytes); + + StatLog.incrementSystemStat(SysProps.FileDlTotalCount, 1); + StatLog.incrementSystemStat(SysProps.FileDlTotalBytes, dlBytes); return callback(null, user); }, diff --git a/core/file_base_area.js b/core/file_base_area.js index 760eb87c..f699a543 100644 --- a/core/file_base_area.js +++ b/core/file_base_area.js @@ -15,6 +15,7 @@ const stringFormat = require('./string_format.js'); const wordWrapText = require('./word_wrap.js').wordWrapText; const StatLog = require('./stat_log.js'); const UserProps = require('./user_property.js'); +const SysProps = require('./system_property.js'); // deps const _ = require('lodash'); @@ -1012,7 +1013,7 @@ function getAreaStats(cb) { function updateAreaStatsScheduledEvent(args, cb) { getAreaStats( (err, stats) => { if(!err) { - StatLog.setNonPeristentSystemStat('file_base_area_stats', stats); + StatLog.setNonPersistentSystemStat(SysProps.FileBaseAreaStats, stats); } return cb(err); diff --git a/core/file_base_area_select.js b/core/file_base_area_select.js index 1b77eb21..a8f74322 100644 --- a/core/file_base_area_select.js +++ b/core/file_base_area_select.js @@ -5,6 +5,7 @@ const MenuModule = require('./menu_module.js').MenuModule; const { getSortedAvailableFileAreas } = require('./file_base_area.js'); const StatLog = require('./stat_log.js'); +const SysProps = require('./system_property.js'); // deps const async = require('async'); @@ -52,7 +53,7 @@ exports.getModule = class FileAreaSelectModule extends MenuModule { async.waterfall( [ function mergeAreaStats(callback) { - const areaStats = StatLog.getSystemStat('file_base_area_stats') || { areas : {} }; + const areaStats = StatLog.getSystemStat(SysProps.FileBaseAreaStats) || { areas : {} }; // we could use 'sort' alone, but area/conf sorting has some special properties; user can still override const availAreas = getSortedAvailableFileAreas(self.client); diff --git a/core/file_transfer.js b/core/file_transfer.js index 340ca6b9..83dedc13 100644 --- a/core/file_transfer.js +++ b/core/file_transfer.js @@ -11,6 +11,8 @@ const StatLog = require('./stat_log.js'); const FileEntry = require('./file_entry.js'); const Log = require('./logger.js').log; const Events = require('./events.js'); +const UserProps = require('./user_property.js'); +const SysProps = require('./system_property.js'); // deps const async = require('async'); @@ -479,10 +481,11 @@ exports.getModule = class TransferFileModule extends MenuModule { }); }, () => { // All stats/meta currently updated via fire & forget - if this is ever a issue, we can wait for callbacks - StatLog.incrementUserStat(this.client.user, 'dl_total_count', downloadCount); - StatLog.incrementUserStat(this.client.user, 'dl_total_bytes', downloadBytes); - StatLog.incrementSystemStat('dl_total_count', downloadCount); - StatLog.incrementSystemStat('dl_total_bytes', downloadBytes); + StatLog.incrementUserStat(this.client.user, UserProps.FileDlTotalCount, downloadCount); + StatLog.incrementUserStat(this.client.user, UserProps.FileDlTotalBytes, downloadBytes); + + StatLog.incrementSystemStat(SysProps.FileDlTotalCount, downloadCount); + StatLog.incrementSystemStat(SysProps.FileDlTotalBytes, downloadBytes); fileIds.forEach(fileId => { FileEntry.incrementAndPersistMetaValue(fileId, 'dl_count', 1); @@ -509,10 +512,11 @@ exports.getModule = class TransferFileModule extends MenuModule { return next(null); }); }, () => { - StatLog.incrementUserStat(this.client.user, 'ul_total_count', uploadCount); - StatLog.incrementUserStat(this.client.user, 'ul_total_bytes', uploadBytes); - StatLog.incrementSystemStat('ul_total_count', uploadCount); - StatLog.incrementSystemStat('ul_total_bytes', uploadBytes); + StatLog.incrementUserStat(this.client.user, UserProps.FileUlTotalCount, uploadCount); + StatLog.incrementUserStat(this.client.user, UserProps.FileUlTotalBytes, uploadBytes); + + StatLog.incrementSystemStat(SysProps.FileUlTotalCount, uploadCount); + StatLog.incrementSystemStat(SysProps.FileUlTotalBytes, uploadBytes); return cb(null); }); diff --git a/core/last_callers.js b/core/last_callers.js index 6b25caf6..c80ce336 100644 --- a/core/last_callers.js +++ b/core/last_callers.js @@ -8,6 +8,7 @@ const User = require('./user.js'); const sysDb = require('./database.js').dbs.system; const { Errors } = require('./enig_error.js'); const UserProps = require('./user_property.js'); +const SysLogKeys = require('./system_log.js'); // deps const moment = require('moment'); @@ -91,7 +92,7 @@ exports.getModule = class LastCallersModule extends MenuModule { } StatLog.getSystemLogEntries( - 'user_login_history', + SysLogKeys.UserLoginHistory, StatLog.Order.TimestampDesc, 200, // max items to fetch - we need more than max displayed for filtering/etc. (err, loginHistory) => { diff --git a/core/predefined_mci.js b/core/predefined_mci.js index cda9f5b4..5b92e0e3 100644 --- a/core/predefined_mci.js +++ b/core/predefined_mci.js @@ -14,6 +14,7 @@ const FileBaseFilters = require('./file_base_filter.js'); const { formatByteSize } = require('./string_util.js'); const ANSI = require('./ansi_term.js'); const UserProps = require('./user_property.js'); +const SysProps = require('./system_property.js'); // deps const packageJson = require('../package.json'); @@ -34,7 +35,7 @@ function setNextRandomRumor(cb) { entry = entry[0]; } const randRumor = entry && entry.log_value ? entry.log_value : ''; - StatLog.setNonPeristentSystemStat('random_rumor', randRumor); + StatLog.setNonPersistentSystemStat(SysProps.NextRandomRumor, randRumor); if(cb) { return cb(null); } @@ -67,12 +68,12 @@ const PREDEFINED_MCI_GENERATORS = { VN : function version() { return packageJson.version; }, // +op info - SN : function opUserName() { return StatLog.getSystemStat('sysop_username'); }, - SR : function opRealName() { return StatLog.getSystemStat('sysop_real_name'); }, - SL : function opLocation() { return StatLog.getSystemStat('sysop_location'); }, - SA : function opAffils() { return StatLog.getSystemStat('sysop_affiliation'); }, - SS : function opSex() { return StatLog.getSystemStat('sysop_sex'); }, - SE : function opEmail() { return StatLog.getSystemStat('sysop_email_address'); }, + SN : function opUserName() { return StatLog.getSystemStat(SysProps.SysOpUsername); }, + SR : function opRealName() { return StatLog.getSystemStat(SysProps.SysOpRealName); }, + SL : function opLocation() { return StatLog.getSystemStat(SysProps.SysOpLocation); }, + SA : function opAffils() { return StatLog.getSystemStat(SysProps.SysOpAffiliations); }, + SS : function opSex() { return StatLog.getSystemStat(SysProps.SysOpSex); }, + SE : function opEmail() { return StatLog.getSystemStat(SysProps.SysOpEmailAddress); }, // :TODO: op age, web, ????? // @@ -189,7 +190,7 @@ const PREDEFINED_MCI_GENERATORS = { AN : function activeNodes() { return clientConnections.getActiveConnections().length.toString(); }, - TC : function totalCalls() { return StatLog.getSystemStat('login_count').toLocaleString(); }, + TC : function totalCalls() { return StatLog.getSystemStat(SysProps.LoginCount).toLocaleString(); }, RR : function randomRumor() { // start the process of picking another random one @@ -203,22 +204,22 @@ const PREDEFINED_MCI_GENERATORS = { // // :TODO: DD - Today's # of downloads (iNiQUiTY) // - SD : function systemNumDownloads() { return sysStatAsString('dl_total_count', 0); }, + SD : function systemNumDownloads() { return sysStatAsString(SysProps.FileDlTotalCount, 0); }, SO : function systemByteDownload() { - const byteSize = StatLog.getSystemStatNum('dl_total_bytes'); + const byteSize = StatLog.getSystemStatNum(SysProps.FileDlTotalBytes); return formatByteSize(byteSize, true); // true=withAbbr }, - SU : function systemNumUploads() { return sysStatAsString('ul_total_count', 0); }, + SU : function systemNumUploads() { return sysStatAsString(SysProps.FileUlTotalCount, 0); }, SP : function systemByteUpload() { - const byteSize = StatLog.getSystemStatNum('ul_total_bytes'); + const byteSize = StatLog.getSystemStatNum(SysProps.FileUlTotalBytes); return formatByteSize(byteSize, true); // true=withAbbr }, TF : function totalFilesOnSystem() { - const areaStats = StatLog.getSystemStat('file_base_area_stats'); + const areaStats = StatLog.getSystemStat(SysProps.FileBaseAreaStats); return _.get(areaStats, 'totalFiles', 0).toLocaleString(); }, TB : function totalBytesOnSystem() { - const areaStats = StatLog.getSystemStat('file_base_area_stats'); + const areaStats = StatLog.getSystemStat(SysProps.FileBaseAreaStats); const totalBytes = parseInt(_.get(areaStats, 'totalBytes', 0)); return formatByteSize(totalBytes, true); // true=withAbbr }, diff --git a/core/rumorz.js b/core/rumorz.js index 31aea104..51e58d53 100644 --- a/core/rumorz.js +++ b/core/rumorz.js @@ -8,6 +8,7 @@ const theme = require('./theme.js'); const resetScreen = require('./ansi_term.js').resetScreen; const StatLog = require('./stat_log.js'); const renderStringLength = require('./string_util.js').renderStringLength; +const SystemLogKeys = require('./system_log.js'); // deps const async = require('async'); @@ -20,8 +21,6 @@ exports.moduleInfo = { packageName : 'codes.l33t.enigma.rumorz', }; -const STATLOG_KEY_RUMORZ = 'system_rumorz'; - const FormIds = { View : 0, Add : 1, @@ -52,10 +51,16 @@ exports.getModule = class RumorzModule extends MenuModule { if(_.isString(formData.value.rumor) && renderStringLength(formData.value.rumor) > 0) { const rumor = formData.value.rumor.trim(); // remove any trailing ws - StatLog.appendSystemLogEntry(STATLOG_KEY_RUMORZ, rumor, StatLog.KeepDays.Forever, StatLog.KeepType.Forever, () => { - this.clearAddForm(); - return this.displayViewScreen(true, cb); // true=cls - }); + StatLog.appendSystemLogEntry( + SystemLogKeys.UserAddedRumorz, + rumor, + StatLog.KeepDays.Forever, + StatLog.KeepType.Forever, + () => { + this.clearAddForm(); + return this.displayViewScreen(true, cb); // true=cls + } + ); } else { // empty message - treat as if cancel was hit return this.displayViewScreen(true, cb); // true=cls @@ -149,7 +154,7 @@ exports.getModule = class RumorzModule extends MenuModule { function fetchEntries(callback) { const entriesView = self.viewControllers.view.getView(MciCodeIds.ViewForm.Entries); - StatLog.getSystemLogEntries(STATLOG_KEY_RUMORZ, StatLog.Order.Timestamp, (err, entries) => { + StatLog.getSystemLogEntries(SystemLogKeys.UserAddedRumorz, StatLog.Order.Timestamp, (err, entries) => { return callback(err, entriesView, entries); }); }, @@ -158,7 +163,7 @@ exports.getModule = class RumorzModule extends MenuModule { return { text : e.log_value, // standard rumor : e.log_value, - } + }; })); entriesView.redraw(); diff --git a/core/stat_log.js b/core/stat_log.js index 173222b9..80840ddb 100644 --- a/core/stat_log.js +++ b/core/stat_log.js @@ -70,8 +70,7 @@ class StatLog { }; } - // :TODO: fix spelling :) - setNonPeristentSystemStat(statName, statValue) { + setNonPersistentSystemStat(statName, statValue) { this.systemStats[statName] = statValue; } diff --git a/core/system_log.js b/core/system_log.js new file mode 100644 index 00000000..e753c68b --- /dev/null +++ b/core/system_log.js @@ -0,0 +1,11 @@ +/* jslint node: true */ +'use strict'; + +// +// Common SYSTEM/global log keys +// +module.exports = { + UserAddedRumorz : 'system_rumorz', + UserLoginHistory : 'user_login_history', +}; + diff --git a/core/system_property.js b/core/system_property.js new file mode 100644 index 00000000..f2743088 --- /dev/null +++ b/core/system_property.js @@ -0,0 +1,25 @@ +/* jslint node: true */ +'use strict'; + +// +// Common SYSTEM/global properties/stats used throughout the system. +// +// This IS NOT a full list. Custom modules & the like can create +// their own! +// +module.exports = { + LoginCount : 'login_count', + + FileBaseAreaStats : 'file_base_area_stats', // object - see file_base_area.js::getAreaStats + FileUlTotalCount : 'ul_total_count', + FileUlTotalBytes : 'ul_total_bytes', + + SysOpUsername : 'sysop_username', + SysOpRealName : 'sysop_real_name', + SysOpLocation : 'sysop_location', + SysOpAffiliations : 'sysop_affiliation', + SysOpSex : 'sysop_sex', + SysOpEmailAddress : 'sysop_email_address', + + NextRandomRumor : 'random_rumor', +}; diff --git a/core/user_login.js b/core/user_login.js index a46be5b1..b02066a1 100644 --- a/core/user_login.js +++ b/core/user_login.js @@ -13,6 +13,8 @@ const { ErrorReasons } = require('./enig_error.js'); const UserProps = require('./user_property.js'); +const SysProps = require('./system_property.js'); +const SystemLogKeys = require('./system_log.js'); // deps const async = require('async'); @@ -86,7 +88,7 @@ function userLogin(client, username, password, cb) { return callback(null); }, function updateSystemLoginCount(callback) { - return StatLog.incrementSystemStat('login_count', 1, callback); // :TODO: create system_property.js + return StatLog.incrementSystemStat(SysProps.LoginCount, 1, callback); }, function recordLastLogin(callback) { return StatLog.setUserStat(user, UserProps.LastLoginTs, StatLog.now, callback); @@ -102,7 +104,7 @@ function userLogin(client, username, password, cb) { }); return StatLog.appendSystemLogEntry( - 'user_login_history', + SystemLogKeys.UserLoginHistory, historyItem, loginHistoryMax, StatLog.KeepType.Max,