Skip to content

Commit 200bd0d

Browse files
authored
Merge pull request #1163 from cloudbees-oss/LCHIB-638
Add new command 'inspect model'
2 parents 9e8c334 + 0b170e6 commit 200bd0d

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

launchable/commands/inspect/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from launchable.utils.click import GroupWithAlias
44

5+
from .model import model
56
from .subset import subset
67
from .tests import tests
78

@@ -11,5 +12,6 @@ def inspect():
1112
pass
1213

1314

15+
inspect.add_command(model)
1416
inspect.add_command(subset)
1517
inspect.add_command(tests)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import json
2+
import sys
3+
from http import HTTPStatus
4+
5+
import click
6+
from requests import Response
7+
from tabulate import tabulate
8+
9+
from ...utils.launchable_client import LaunchableClient
10+
11+
12+
@click.command()
13+
@click.option(
14+
'--json',
15+
'is_json_format',
16+
help='display JSON format',
17+
is_flag=True
18+
)
19+
@click.pass_context
20+
def model(context: click.core.Context, is_json_format: bool):
21+
client = LaunchableClient(app=context.obj)
22+
try:
23+
res: Response = client.request("get", "model-metadata")
24+
25+
if res.status_code == HTTPStatus.NOT_FOUND:
26+
click.echo(click.style(
27+
"Model metadata currently not available for this workspace.", 'yellow'), err=True)
28+
sys.exit()
29+
30+
res.raise_for_status()
31+
32+
if is_json_format:
33+
display_as_json(res)
34+
else:
35+
display_as_table(res)
36+
37+
except Exception as e:
38+
client.print_exception_and_recover(e, "Warning: failed to inspect model")
39+
40+
41+
def display_as_json(res: Response):
42+
res_json = res.json()
43+
click.echo(json.dumps(res_json, indent=2))
44+
45+
46+
def display_as_table(res: Response):
47+
headers = ["Metadata", "Value"]
48+
res_json = res.json()
49+
rows = [["Training Cutoff Test Session ID", res_json['training_cutoff_test_session_id']]]
50+
click.echo(tabulate(rows, headers, tablefmt="github"))

tests/cli_test_case.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ def setUp(self):
194194
self.workspace),
195195
json={'isFailFastMode': False, 'isPtsV2Enabled': False},
196196
status=200)
197+
responses.add(
198+
responses.GET,
199+
"{}/intake/organizations/{}/workspaces/{}/model-metadata".format(
200+
get_base_url(),
201+
self.organization,
202+
self.workspace),
203+
json={'training_cutoff_test_session_id': 256},
204+
status=200)
197205

198206
def get_test_files_dir(self):
199207
file_name = Path(inspect.getfile(self.__class__)) # obtain the file of the concrete type
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
from unittest import mock
3+
4+
import responses # type: ignore
5+
6+
from launchable.utils.http_client import get_base_url
7+
from tests.cli_test_case import CliTestCase
8+
9+
10+
class ModelTest(CliTestCase):
11+
mock_json = {
12+
"training_cutoff_test_session_id": 256
13+
}
14+
15+
@responses.activate
16+
@mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": CliTestCase.launchable_token})
17+
def test_model(self):
18+
responses.replace(responses.GET, "{}/intake/organizations/{}/workspaces/{}/model-metadata".format(
19+
get_base_url(), self.organization, self.workspace), json=self.mock_json, status=200)
20+
21+
result = self.cli('inspect', 'model', mix_stderr=False)
22+
expect = """| Metadata | Value |
23+
|---------------------------------|---------|
24+
| Training Cutoff Test Session ID | 256 |
25+
"""
26+
27+
self.assertEqual(result.stdout, expect)
28+
29+
@responses.activate
30+
@mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": CliTestCase.launchable_token})
31+
def test_model_json_format(self):
32+
responses.replace(responses.GET, "{}/intake/organizations/{}/workspaces/{}/model-metadata".format(
33+
get_base_url(), self.organization, self.workspace), json=self.mock_json, status=200)
34+
35+
result = self.cli('inspect', 'model', "--json", mix_stderr=False)
36+
37+
self.assertEqual(result.stdout, """{
38+
"training_cutoff_test_session_id": 256
39+
}
40+
"""
41+
)

0 commit comments

Comments
 (0)