mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-18 15:28:28 +01:00
Machine: table output for machine status(#6892)
Change the output format of `dvc machine status` command to tables for a better readability.
This commit is contained in:
+25
-10
@@ -5,6 +5,7 @@ from dvc.command.config import CmdConfig
|
||||
from dvc.compare import TabularData
|
||||
from dvc.config import ConfigError
|
||||
from dvc.exceptions import DvcException
|
||||
from dvc.types import Dict, List
|
||||
from dvc.ui import ui
|
||||
from dvc.utils import format_link
|
||||
|
||||
@@ -232,8 +233,8 @@ class CmdMachineCreate(CmdBase):
|
||||
|
||||
|
||||
class CmdMachineStatus(CmdBase):
|
||||
INSTANCE_FIELD = ["name", "instance", "status"]
|
||||
SHOWN_FIELD = [
|
||||
"name",
|
||||
"cloud",
|
||||
"instance_ip",
|
||||
"instance_type",
|
||||
@@ -241,23 +242,34 @@ class CmdMachineStatus(CmdBase):
|
||||
"instance_gpu",
|
||||
]
|
||||
|
||||
def _show_machine_status(self, name: str):
|
||||
ui.write(f"machine '{name}':")
|
||||
all_status = list(self.repo.machine.status(name))
|
||||
def _add_row(
|
||||
self,
|
||||
name: str,
|
||||
all_status: List[Dict],
|
||||
td: TabularData,
|
||||
):
|
||||
|
||||
if not all_status:
|
||||
ui.write("\toffline")
|
||||
row = [name, None, "offline"]
|
||||
td.append(row)
|
||||
for i, status in enumerate(all_status, start=1):
|
||||
ui.write(f"\tinstance_num_{i}:")
|
||||
row = [name, f"num_{i}", "running" if status else "offline"]
|
||||
for field in self.SHOWN_FIELD:
|
||||
value = status.get(field, None)
|
||||
ui.write(f"\t\t{field:20}: {value}")
|
||||
value = str(status.get(field, ""))
|
||||
row.append(value)
|
||||
td.append(row)
|
||||
|
||||
def run(self):
|
||||
if self.repo.machine is None:
|
||||
raise MachineDisabledError
|
||||
|
||||
td = TabularData(
|
||||
self.INSTANCE_FIELD + self.SHOWN_FIELD, fill_value="-"
|
||||
)
|
||||
|
||||
if self.args.name:
|
||||
self._show_machine_status(self.args.name)
|
||||
all_status = list(self.repo.machine.status(self.args.name))
|
||||
self._add_row(self.args.name, all_status, td)
|
||||
else:
|
||||
name_set = set()
|
||||
for level in self.repo.config.LEVELS:
|
||||
@@ -265,8 +277,11 @@ class CmdMachineStatus(CmdBase):
|
||||
name_set.update(conf.keys())
|
||||
name_list = list(name_set)
|
||||
for name in sorted(name_list):
|
||||
self._show_machine_status(name)
|
||||
all_status = list(self.repo.machine.status(name))
|
||||
self._add_row(name, all_status, td)
|
||||
|
||||
td.dropna("cols", "all")
|
||||
td.render()
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
@@ -116,6 +116,8 @@ class TabularData(MutableSequence[Sequence["CellT"]]):
|
||||
del col[item]
|
||||
|
||||
def __len__(self) -> int:
|
||||
if not self._columns:
|
||||
return 0
|
||||
return len(self.columns[0])
|
||||
|
||||
@property
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ if TYPE_CHECKING:
|
||||
SHOW_MAX_WIDTH = 1024
|
||||
|
||||
|
||||
CellT = Union[str, "RichText"] # RichText is mostly compatible with str
|
||||
CellT = Union[str, "RichText", None] # RichText is mostly compatible with str
|
||||
Row = Sequence[CellT]
|
||||
TableData = Sequence[Row]
|
||||
Headers = Sequence[str]
|
||||
|
||||
@@ -40,14 +40,16 @@ def machine_config(tmp_dir):
|
||||
|
||||
@pytest.fixture
|
||||
def machine_instance(tmp_dir, dvc, mocker):
|
||||
from tpi.terraform import TerraformBackend
|
||||
|
||||
with dvc.config.edit() as conf:
|
||||
conf["machine"]["foo"] = {"cloud": "aws"}
|
||||
mocker.patch.object(
|
||||
TerraformBackend,
|
||||
"instances",
|
||||
autospec=True,
|
||||
return_value=[TEST_INSTANCE],
|
||||
|
||||
def mock_instances(name=None, **kwargs):
|
||||
if name == "foo":
|
||||
return iter([TEST_INSTANCE])
|
||||
return iter([])
|
||||
|
||||
mocker.patch(
|
||||
"tpi.terraform.TerraformBackend.instances",
|
||||
mocker.MagicMock(side_effect=mock_instances),
|
||||
)
|
||||
yield TEST_INSTANCE
|
||||
|
||||
@@ -1,14 +1,23 @@
|
||||
from dvc.command.machine import CmdMachineStatus
|
||||
from dvc.main import main
|
||||
from dvc.ui import ui
|
||||
from tests.utils import console_width
|
||||
|
||||
|
||||
def test_status(
|
||||
tmp_dir, scm, dvc, machine_config, machine_instance, mocker, capsys
|
||||
):
|
||||
status = machine_instance
|
||||
assert main(["machine", "status", "foo"]) == 0
|
||||
def test_status(tmp_dir, scm, dvc, machine_config, machine_instance, capsys):
|
||||
|
||||
assert main(["machine", "add", "bar", "aws"]) == 0
|
||||
with console_width(ui.rich_console, 255):
|
||||
assert main(["machine", "status"]) == 0
|
||||
cap = capsys.readouterr()
|
||||
assert "machine 'foo':\n" in cap.out
|
||||
assert "\tinstance_num_1:\n" in cap.out
|
||||
for key in CmdMachineStatus.SHOWN_FIELD:
|
||||
assert f"\t\t{key:20}: {status[key]}\n" in cap.out
|
||||
assert (
|
||||
"name instance status cloud instance_ip "
|
||||
"instance_type instance_hdd_size instance_gpu"
|
||||
) in cap.out
|
||||
assert (
|
||||
"bar - offline - - "
|
||||
"- - -"
|
||||
) in cap.out
|
||||
assert (
|
||||
"foo num_1 running aws 123.123.123.123 "
|
||||
"m 35 None"
|
||||
) in cap.out
|
||||
|
||||
Reference in New Issue
Block a user