Improve statistics command

master
Arinerron 2018-05-10 19:05:56 -07:00
parent 193845104e
commit 2f7002fc67
No known key found for this signature in database
GPG Key ID: 99383627861C62F0
2 changed files with 69 additions and 4 deletions

View File

@ -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 import os, base64, random, getpass, shutil, subprocess, requests, time, platform, datetime, re, json
from threading import Thread from threading import Thread
import api, core, config, logger, onionrplugins as plugins, onionrevents as events import api, core, config, logger, onionrplugins as plugins, onionrevents as events
import onionrutils
from onionrutils import OnionrUtils from onionrutils import OnionrUtils
from netcontroller import NetController from netcontroller import NetController
@ -554,8 +555,43 @@ class Onionr:
Displays statistics and exits Displays statistics and exits
''' '''
logger.info('Our pubkey: ' + self.onionrCore._crypto.pubKey) try:
logger.info('Our address: ' + self.get_hostname()) # 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 return
def showHelp(self, command = None): def showHelp(self, command = None):

View File

@ -422,10 +422,15 @@ class OnionrUtils:
return False return False
def token(self, size = 32): def token(self, size = 32):
'''
Generates a secure random hex encoded token
'''
return binascii.hexlify(os.urandom(size)) return binascii.hexlify(os.urandom(size))
def importNewBlocks(self, scanDir=''): 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() blockList = self._core.getBlockList()
if scanDir == '': if scanDir == '':
scanDir = self._core.blockDataLocation scanDir = self._core.blockDataLocation
@ -440,4 +445,28 @@ class OnionrUtils:
self._core.addToBlockDB(block.replace('.dat', ''), dataSaved=True) self._core.addToBlockDB(block.replace('.dat', ''), dataSaved=True)
logger.info('Imported block.') logger.info('Imported block.')
else: else:
logger.warn('Failed to verify hash for ' + block) 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)