Skip to content

Commit

Permalink
Changed human friendly output in web interface network monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
bufgix committed Aug 30, 2019
1 parent 42ff3b9 commit 5fc1a5c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 34 additions & 9 deletions graphenex/core/utils/sysinfo.py
Expand Up @@ -5,20 +5,44 @@
import psutil
import re


class SysInformation:

UNITS_MAPPING = [
(1 << 50, ' PB'),
(1 << 40, ' TB'),
(1 << 30, ' GB'),
(1 << 20, ' MB'),
(1 << 10, ' KB'),
(1, (' byte', ' bytes')),
]

@staticmethod
def pretty_size(bytes, units=UNITS_MAPPING):
for factor, suffix in units:
if bytes >= factor:
break
amount = int(bytes / factor)

if isinstance(suffix, tuple):
singular, multiple = suffix
if amount == 1:
suffix = singular
else:
suffix = multiple
return str(amount) + suffix

@staticmethod
def get_network_info():
masks = list()
MaskResult = namedtuple('MaskResult', ['name', 'recv', 'sent', 'slug'])
for mask, data in OrderedDict(psutil.net_io_counters(pernic=True)).items():
if data.packets_recv > 0 or data.packets_sent > 0:
res = MaskResult(mask, data.packets_recv, data.packets_sent,
SysInformation.slugify(mask))
res = MaskResult(mask, SysInformation.pretty_size(data.packets_recv), SysInformation.pretty_size(data.packets_sent),
SysInformation.slugify(mask))
masks.append(res)

return masks

@staticmethod
def get_disk_info():
disks = list()
Expand All @@ -30,9 +54,9 @@ def get_disk_info():
disks.append(res)
except PermissionError:
pass

return disks

@staticmethod
def get_general_info():
uname = platform.uname()
Expand All @@ -52,11 +76,12 @@ def get_all_info():
return info_dict

@staticmethod
def slugify(text, delim='-'):
def slugify(text, delim='-'):
"""
Generate an ASCII-only slug.
"""
_punctuation_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
_punctuation_re = re.compile(
r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
result = []
for word in _punctuation_re.split(text.lower()):
word = normalize('NFKD', word) \
Expand All @@ -66,4 +91,4 @@ def slugify(text, delim='-'):
if word:
result.append(word)

return delim.join(result)
return delim.join(result)
2 changes: 2 additions & 0 deletions graphenex/core/web/__init__.py
Expand Up @@ -6,6 +6,8 @@
import webbrowser
import secrets



logger = GraphenexLogger(__name__)
app = Flask(__name__)
app.config['SECRET_KEY'] = '77a98d7971ec94c8aae6dd2d'
Expand Down
3 changes: 1 addition & 2 deletions graphenex/core/web/views.py
Expand Up @@ -33,8 +33,7 @@ def decorated_function(*args, **kwargs):
return f(*args, **kwargs)
else:
emit('auth', {'text': 'Not authenticated'})
disconnect()


return decorated_function


Expand Down

0 comments on commit 5fc1a5c

Please sign in to comment.