Skip to content

Commit

Permalink
fix(client): fix broken resource ls cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
jialeicui committed Nov 13, 2023
1 parent 0208100 commit a496800
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 16 deletions.
16 changes: 7 additions & 9 deletions client/starwhale/base/uri/project.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import os
import urllib
from typing import Any, Optional
from functools import lru_cache

import requests

from starwhale.consts.env import SWEnv
from starwhale.utils.retry import http_retry
from starwhale.utils.config import SWCliConfigMixed
from starwhale.base.uri.instance import Instance
from starwhale.base.uri.exceptions import UriTooShortException

Expand Down Expand Up @@ -60,7 +57,11 @@ def __init__(
self.id = (
self.name
if self.name.isdigit()
else str(get_remote_project_id(self.instance.url, self.name))
else str(
get_remote_project_id(
self.instance.url, self.instance.token, self.name
)
)
)
else:
self.id = self.name
Expand Down Expand Up @@ -112,15 +113,12 @@ def __eq__(self, other: object) -> bool:

@lru_cache(maxsize=None)
@http_retry
def get_remote_project_id(instance_uri: str, project: str) -> Any:
def get_remote_project_id(instance_uri: str, token: str, project: str) -> Any:
resp = requests.get(
urllib.parse.urljoin(instance_uri, f"/api/v1/project/{project}"),
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": (
SWCliConfigMixed().get_sw_token(instance=instance_uri)
or os.getenv(SWEnv.instance_token, "")
),
"Authorization": token,
},
timeout=60,
)
Expand Down
13 changes: 12 additions & 1 deletion client/starwhale/base/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ def print_table(
custom_table: t.Optional[t.Dict[str, t.Any]] = None,
allowed_keys: t.Optional[t.List[str]] = None,
) -> None:
custom_column = custom_column or {}
default_attr = {
"title": title,
"box": box.SIMPLE,
Expand All @@ -295,6 +296,14 @@ def init_header(row: t.List[str]) -> None:
if custom_header and idx in custom_header:
extra = custom_header[idx]
table.add_column(snake_to_camel(field), **extra)
for custom_column_key in custom_column:
if custom_column_key not in row:
table.add_column(
snake_to_camel(custom_column_key),
justify="left",
style="cyan",
no_wrap=True,
)

header_inited = False
for row in data:
Expand All @@ -311,7 +320,9 @@ def init_header(row: t.List[str]) -> None:
if not is_renderable(col):
col = pretty.Pretty(col)
rendered_row.append(col)

for custom_column_key in custom_column:
if custom_column_key not in row:
rendered_row.append(custom_column[custom_column_key](row))
row_ext: t.Dict[str, t.Any] = {}
if custom_row:
row_ext = custom_row(row) or {}
Expand Down
1 change: 0 additions & 1 deletion client/starwhale/core/dataset/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ def list(
custom_column: t.Dict[str, t.Callable[[t.Any], str]] = {
"tags": lambda x: ",".join(x),
"size": lambda x: pretty_bytes(x),
"runtime": cls.place_holder_for_empty(),
"rows": lambda x: x and str(x) or "",
}

Expand Down
26 changes: 21 additions & 5 deletions client/starwhale/core/model/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from starwhale.base.uri.resource import Resource, ResourceType
from starwhale.core.runtime.model import StandaloneRuntime
from starwhale.core.runtime.process import Process as RuntimeProcess
from starwhale.base.client.models.models import ModelVo


class ModelTermView(BaseTermView, TagViewMixin):
Expand Down Expand Up @@ -437,11 +438,26 @@ def list(
_models, _pager = super().list(
project_uri, fullname, show_removed, page, size, filters
)
custom_column: t.Dict[str, t.Callable[[t.Any], str]] = {
"tags": lambda x: ",".join(x),
"size": lambda x: pretty_bytes(x),
"runtime": cls.place_holder_for_empty(""),
}
custom_column: t.Dict[str, t.Callable[[t.Any], str]] = dict()
if _models and isinstance(_models[0], ModelVo):
custom_column.update(
{
"tags": lambda x: ",".join(
[x["version"]["alias"]] + (x["version"]["tags"] or [])
),
"size": lambda x: pretty_bytes(x["version"]["size"]),
"owner": lambda x: x["name"],
"version": lambda x: x["name"],
"shared": lambda x: str(x["version"]["shared"]),
}
)
else:
custom_column.update(
{
"tags": lambda x: ",".join(x),
"size": lambda x: pretty_bytes(x or 0),
}
)

cls.print_header(project_uri)
cls.print_table("Model List", _models, custom_column=custom_column)
Expand Down
1 change: 1 addition & 0 deletions client/starwhale/core/runtime/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ def list(
"tags": [i.version.alias] + (i.version.tags or []),
"shared": i.version.shared != 0,
"image": i.version.image,
"size": 0,
"created_at": CloudRequestMixed.fmt_timestamp(
i.version.created_time
),
Expand Down
11 changes: 11 additions & 0 deletions scripts/client_test/cli_exec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -e

for ins in "local" "server"; do
swcli instance select $ins
for rc in "model" "runtime" "dataset"; do
swcli $rc ls
swcli -o json $rc ls
done
done
2 changes: 2 additions & 0 deletions scripts/client_test/cli_test.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ bash "$SCRIPT_DIR"/update_controller_setting.sh
for i in $@; do
python3 "$SCRIPT_DIR"/cli_test.py --sw_repo_path "$REPO_PATH" --case $i --work_dir "$WORK_DIR" --server_url "$CONTROLLER_URL" || exit 1
done

. "$SCRIPT_DIR"/cli_exec.sh
Empty file modified scripts/client_test/update_controller_setting.sh
100644 → 100755
Empty file.

0 comments on commit a496800

Please sign in to comment.