Skip to content

Commit

Permalink
devmate/cli: Refactor list_devices output to tabular format.
Browse files Browse the repository at this point in the history
Enhance the readability of the list_devices function output by presenting it in
a structured tabular format.

- Replaced the previous list output with a table-like structure using
  PrettyTable.
- Updated dependencies to include PrettyTable.
- Modified the unit tests to match the new table output.

Signed-off-by: Nikolay Martyanov <[email protected]>
  • Loading branch information
OhmSpectator committed Sep 19, 2023
1 parent 7e8b37b commit 0b6517f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
19 changes: 14 additions & 5 deletions client/devmatecli/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
from datetime import datetime, timezone
import humanize
from prettytable import PrettyTable

DEVMATE_ADDRESS = os.environ.get('DEVMATE_ADDRESS', 'devmate.zededa.net')
DEVMATE_PORT = os.environ.get('DEVMATE_PORT', '8000')
Expand Down Expand Up @@ -39,10 +40,16 @@ def list_devices():
response = requests.get(f"{BASE_URL}/devices/list")
if response.status_code == 200:
devices = response.json().get('devices', [])
print(f"Found {len(devices)} device{'s' if len(devices) != 1 else ''}:")

# Create a table object
table = PrettyTable()
table.field_names = ["Name", "Model", "Status", "Reserved By", "Reserved At", "Reserved For"]

for device in devices:
status = device.get('status')
reserved_by = device.get('user')
reserved_at = ""
reserved_for = ""
if status == 'reserved' and reserved_by:
# Assuming that the reservation time is in UTC
utc_time = datetime.fromisoformat(device.get('reservation_time')).replace(tzinfo=timezone.utc)
Expand All @@ -52,10 +59,12 @@ def list_devices():
reserved_at = local_time.strftime("%d.%m.%Y %H:%M:%S")
# For how long the device is reserved
reserved_for = datetime_now() - utc_time
reserved_for = humanize.naturaldelta(reserved_for)
print(f" {device.get('name')} ({device.get('model')}) is {status} by {reserved_by} {reserved_at} ({reserved_for} by now)")
else:
print(f" {device.get('name')} ({device.get('model')}) is {status}")
reserved_for = humanize.naturaldelta(reserved_for) + " by now"

table.add_row(
[device.get('name'), device.get('model'), status, reserved_by or "", reserved_at, reserved_for])

print(table)
elif response.status_code == 204:
print("No devices found.")
else:
Expand Down
3 changes: 2 additions & 1 deletion client/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pyinstaller
requests
humanize
humanize
prettytable
18 changes: 14 additions & 4 deletions client/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ def test_list_devices_found(self, mock_stdout, mock_get):
mock_get.return_value.status_code = HTTPStatus.OK
mock_get.return_value.json.return_value = {'devices': [{'name': 'Device1', 'model': 'Model1', 'status': 'free'}]}
device_management.list_devices()
expected_output = "Found 1 device:\n Device1 (Model1) is free\n"
expected_output = (
"+---------+--------+--------+-------------+-------------+--------------+\n"
"| Name | Model | Status | Reserved By | Reserved At | Reserved For |\n"
"+---------+--------+--------+-------------+-------------+--------------+\n"
"| Device1 | Model1 | free | | | |\n"
"+---------+--------+--------+-------------+-------------+--------------+\n"
)
self.assertEqual(mock_stdout.getvalue(), expected_output)

@patch('requests.get')
Expand All @@ -65,9 +71,13 @@ def test_list_devices_reserved_by(self, mock_stdout, mock_get, mock_datetime_now
]
}
device_management.list_devices()

# Match the output including the date and duration strings
expected_output = "Found 1 device:\n Device1 (Model1) is reserved by User1 01.01.2021 01:00:00 (an hour by now)\n"
expected_output = (
"+---------+--------+----------+-------------+---------------------+----------------+\n"
"| Name | Model | Status | Reserved By | Reserved At | Reserved For |\n"
"+---------+--------+----------+-------------+---------------------+----------------+\n"
"| Device1 | Model1 | reserved | User1 | 01.01.2021 01:00:00 | an hour by now |\n"
"+---------+--------+----------+-------------+---------------------+----------------+\n"
)
self.assertEqual(mock_stdout.getvalue(), expected_output)

@patch('requests.get')
Expand Down

0 comments on commit 0b6517f

Please sign in to comment.