Skip to content

Commit

Permalink
Improve system-info command output
Browse files Browse the repository at this point in the history
  • Loading branch information
thatmattlove committed Jul 4, 2021
1 parent c1e1d7c commit f235e29
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 55 deletions.
46 changes: 14 additions & 32 deletions hyperglass/cli/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,52 +148,34 @@ def write_to_file(file, data) -> bool:
return True


def system_info() -> bool:
def system_info() -> None:
"""Create a markdown table of various system information."""
# Project
from hyperglass.util.system_info import get_system_info

values = get_system_info()
data = get_system_info()

def _code(val):
return f"`{str(val)}`"

def _bold(val):
return f"**{str(val)}**"

def _suffix(val, suffix):
return f"{str(val)}{str(suffix)}"

columns = (
("hyperglass Version", _bold),
("hyperglass Path", _code),
("Python Version", _code),
("Platform Info", _code),
("CPU Info", None),
("Logical Cores", _code),
("Physical Cores", _code),
("Processor Speed", "GHz"),
("Total Memory", " GB"),
("Memory Utilization", "%"),
("Total Disk Space", " GB"),
("Disk Utilization", "%"),
)
md_table_lines = ("| Metric | Value |", "| ------ | ----- |")

for i, metric in enumerate(values):
title, mod = columns[i]
value = metric

if isinstance(mod, str):
value = _suffix(value, mod)
elif callable(mod):
value = mod(value)

md_table_lines += (f"| **{title}** | {value} |",)
md_table_lines = ("| Metric | Value |", "| :----- | :---- |")

for title, metric in data.items():
value, mod = metric

title = _bold(title)

if mod == "code":
value = _code(value)

md_table_lines += (f"| {title} | {value} |",)

md_table = "\n".join(md_table_lines)

info("Please copy & paste this table in your bug report:\n")
echo(md_table + "\n")

return True
return None
4 changes: 2 additions & 2 deletions hyperglass/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
raise RuntimeError(f"Python {pretty_version}+ is required.")

# Ensure the NodeJS version meets the minimum requirements.
node_version = get_node_version()
node_major, _, __ = get_node_version()

if node_version != MIN_NODE_VERSION:
if node_major != MIN_NODE_VERSION:
raise RuntimeError(f"NodeJS {MIN_NODE_VERSION}+ is required.")


Expand Down
8 changes: 3 additions & 5 deletions hyperglass/util/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import shutil
import asyncio
import subprocess
from typing import Dict, Optional
from typing import Dict, Tuple, Optional
from pathlib import Path

# Project
Expand All @@ -17,7 +17,7 @@
from .files import copyfiles, check_path


def get_node_version() -> int:
def get_node_version() -> Tuple[int, int, int]:
"""Get the system's NodeJS version."""
node_path = shutil.which("node")

Expand All @@ -28,9 +28,7 @@ def get_node_version() -> int:
# Node returns the version as 'v14.5.0', for example. Remove the v.
version = raw_version.replace("v", "")
# Parse the version parts.
major, minor, patch = version.split(".")

return int(major)
return tuple((int(v) for v in version.split(".")))


def get_ui_build_timeout() -> Optional[int]:
Expand Down
44 changes: 28 additions & 16 deletions hyperglass/util/system_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Standard Library
import os
import platform
from typing import Dict, Tuple, Union

# Third Party
import psutil as _psutil
Expand All @@ -11,8 +12,13 @@
# Project
from hyperglass.constants import __version__

# Local
from .frontend import get_node_version

def _cpu():
SystemData = Dict[str, Tuple[Union[str, int], str]]


def _cpu() -> SystemData:
"""Construct CPU Information."""
cpu_info = _get_cpu_info()
brand = cpu_info.get("brand_raw", "")
Expand All @@ -22,35 +28,41 @@ def _cpu():
return (brand, cores_logical, cores_raw, cpu_ghz)


def _memory():
def _memory() -> SystemData:
"""Construct RAM Information."""
mem_info = _psutil.virtual_memory()
total_gb = round(mem_info.total / 1e9, 2)
usage_percent = mem_info.percent
return (total_gb, usage_percent)


def _disk():
def _disk() -> SystemData:
"""Construct Disk Information."""
disk_info = _psutil.disk_usage("/")
total_gb = round(disk_info.total / 1e9, 2)
usage_percent = disk_info.percent
return (total_gb, usage_percent)


def get_system_info():
def get_system_info() -> SystemData:
"""Get system info."""
yield __version__

yield os.environ["hyperglass_directory"]

yield platform.python_version()

yield platform.platform()
cpu_info, cpu_logical, cpu_physical, cpu_speed = _cpu()
mem_total, mem_usage = _memory()
disk_total, disk_usage = _disk()

for c in _cpu():
yield c
for m in _memory():
yield m
for d in _disk():
yield d
return {
"hyperglass Version": (__version__, "text"),
"hyperglass Path": (os.environ["hyperglass_directory"], "code"),
"Python Version": (platform.python_version(), "code"),
"Node Version": (".".join(str(v) for v in get_node_version()), "code"),
"Platform Info": (platform.platform(), "code"),
"CPU Info": (cpu_info, "text"),
"Logical Cores": (cpu_logical, "code"),
"Physical Cores": (cpu_physical, "code"),
"Processor Speed": (f"{cpu_speed}GHz", "code"),
"Total Memory": (f"{mem_total} GB", "text"),
"Memory Utilization": (f"{mem_usage}%", "text"),
"Total Disk Space": (f"{disk_total} GB", "text"),
"Disk Utilization": (f"{disk_usage}%", "text"),
}

0 comments on commit f235e29

Please sign in to comment.