From 2f7002fc67b78f91b3ba393b3966d472bd6b06ac Mon Sep 17 00:00:00 2001 From: Arinerron Date: Thu, 10 May 2018 19:05:56 -0700 Subject: [PATCH] Improve statistics command --- onionr/onionr.py | 40 ++++++++++++++++++++++++++++++++++++++-- onionr/onionrutils.py | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/onionr/onionr.py b/onionr/onionr.py index 2143c320..13c2d0c2 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -28,6 +28,7 @@ if sys.version_info[0] == 2 or sys.version_info[1] < 5: import os, base64, random, getpass, shutil, subprocess, requests, time, platform, datetime, re, json from threading import Thread import api, core, config, logger, onionrplugins as plugins, onionrevents as events +import onionrutils from onionrutils import OnionrUtils from netcontroller import NetController @@ -554,8 +555,43 @@ class Onionr: Displays statistics and exits ''' - logger.info('Our pubkey: ' + self.onionrCore._crypto.pubKey) - logger.info('Our address: ' + self.get_hostname()) + try: + # define stats messages here + messages = { + # info about local client + 'Public Key' : self.onionrCore._crypto.pubKey, + 'Address' : self.get_hostname(), + + # file and folder size stats + 'div1' : True, # this creates a solid line across the screen, a div + 'Total Block Size' : onionrutils.humanSize(onionrutils.size('data/blocks/')), + 'Total Plugin Size' : onionrutils.humanSize(onionrutils.size('data/plugins/')), + 'Log File Size' : onionrutils.humanSize(onionrutils.size('data/output.log')), + + # count stats + 'div2' : True, + 'Known Peers Count' : str(len(self.onionrCore.listPeers())), + 'Enabled Plugins Count' : str(len(config.get('plugins')['enabled'])) + ' / ' + str(len(os.listdir('data/plugins/'))) + } + + # pre-processing + maxlength = 0 + for key, val in messages.items(): + if not (type(val) is bool and val is True): + maxlength = max(len(key), maxlength) + + # generate stats table + logger.info(logger.colors.bold + 'Onionr v%s Statistics' % ONIONR_VERSION + logger.colors.reset) + logger.info('─' * (maxlength + 1) + '┐') + for key, val in messages.items(): + if not (type(val) is bool and val is True): + logger.info(str(key).rjust(maxlength) + ' │ ' + str(val)) + else: + logger.info('─' * (maxlength + 1) + '┤') + logger.info('─' * (maxlength + 1) + '┘') + except Exception as e: + logger.error('Failed to generate statistics table.', error = e, timestamp = False) + return def showHelp(self, command = None): diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index 27f7b67d..8ef0d28e 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -422,10 +422,15 @@ class OnionrUtils: return False def token(self, size = 32): + ''' + Generates a secure random hex encoded token + ''' return binascii.hexlify(os.urandom(size)) def importNewBlocks(self, scanDir=''): - '''This function is intended to scan for new blocks ON THE DISK and import them''' + ''' + This function is intended to scan for new blocks ON THE DISK and import them + ''' blockList = self._core.getBlockList() if scanDir == '': scanDir = self._core.blockDataLocation @@ -440,4 +445,28 @@ class OnionrUtils: self._core.addToBlockDB(block.replace('.dat', ''), dataSaved=True) logger.info('Imported block.') else: - logger.warn('Failed to verify hash for ' + block) \ No newline at end of file + logger.warn('Failed to verify hash for ' + block) + + +def size(path='.'): + ''' + Returns the size of a folder's contents in bytes + ''' + total = 0 + if os.path.exists(path): + if os.path.isfile(path): + total = os.path.getsize(path) + else: + for entry in os.scandir(path): + if entry.is_file(): + total += entry.stat().st_size + elif entry.is_dir(): + total += size(entry.path) + return total + +def humanSize(num, suffix='B'): + for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']: + if abs(num) < 1024.0: + return "%.1f %s%s" % (num, unit, suffix) + num /= 1024.0 + return "%.1f %s%s" % (num, 'Yi', suffix)