Skip to content

Commit

Permalink
review on hiding
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Yushkovskiy committed Nov 8, 2019
1 parent d696a91 commit 3f05ebb
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 27 deletions.
8 changes: 2 additions & 6 deletions neuromation/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import aiohttp

from neuromation.api.quota import Quota
from neuromation.api.quota import _Quota

from .config import _Config
from .core import _Core
Expand Down Expand Up @@ -48,7 +48,7 @@ def __init__(
self._storage = Storage._create(self._core, self._config)
self._users = Users._create(self._core)
self._parser = Parser._create(self._config, self.username)
self._quota = Quota._create(self._core, self._config)
self._quota = _Quota._create(self._core, self._config)
self._images: Optional[Images] = None

async def close(self) -> None:
Expand Down Expand Up @@ -101,10 +101,6 @@ def images(self) -> Images:
def parse(self) -> Parser:
return self._parser

@property
def quota(self) -> Quota:
return self._quota

def _get_session_cookie(self) -> Optional["Morsel[str]"]:
for cookie in self._core._session.cookie_jar:
if cookie.key == "NEURO_SESSION":
Expand Down
10 changes: 5 additions & 5 deletions neuromation/api/quota.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


@dataclass(frozen=True)
class QuotaInfo:
class _QuotaInfo:
name: str
cpu_time_spent: float
cpu_time_limit: float
Expand All @@ -30,27 +30,27 @@ def _get_remaining_time(self, time_spent: float, time_limit: float) -> float:
return 0.0


class Quota(metaclass=NoPublicConstructor):
class _Quota(metaclass=NoPublicConstructor):
def __init__(self, core: _Core, config: _Config) -> None:
self._core = core
self._config = config

async def get(self, user: Optional[str] = None) -> QuotaInfo:
async def get(self, user: Optional[str] = None) -> _QuotaInfo:
user = user or self._config.auth_token.username
url = URL(f"stats/users/{user}")
async with self._core.request("GET", url) as resp:
res = await resp.json()
return _quota_info_from_api(res)


def _quota_info_from_api(payload: Dict[str, Any]) -> QuotaInfo:
def _quota_info_from_api(payload: Dict[str, Any]) -> _QuotaInfo:
jobs = payload["jobs"]
spent_gpu = float(int(jobs["total_gpu_run_time_minutes"]) * 60)
spent_cpu = float(int(jobs["total_non_gpu_run_time_minutes"]) * 60)
quota = payload["quota"]
limit_gpu = float(quota.get("total_gpu_run_time_minutes", "inf")) * 60
limit_cpu = float(quota.get("total_non_gpu_run_time_minutes", "inf")) * 60
return QuotaInfo(
return _QuotaInfo(
name=payload["name"],
gpu_time_spent=spent_gpu,
gpu_time_limit=limit_gpu,
Expand Down
2 changes: 1 addition & 1 deletion neuromation/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def show_quota(root: Root, user: Optional[str]) -> None:
"""
Print quota and remaining computation time.
"""
quota = await root.client.quota.get(user)
quota = await root.client._quota.get(user)
fmt = QuotaInfoFormatter()
click.echo(fmt(quota))

Expand Down
4 changes: 2 additions & 2 deletions neuromation/cli/formatters/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from click import style

from neuromation.api import Preset
from neuromation.api.quota import QuotaInfo
from neuromation.api.quota import _QuotaInfo
from neuromation.cli.root import Root
from neuromation.cli.utils import format_size

Expand Down Expand Up @@ -72,7 +72,7 @@ def _format_presets(self, presets: Dict[str, Preset]) -> Iterator[str]:
class QuotaInfoFormatter:
QUOTA_NOT_SET = "infinity"

def __call__(self, quota: QuotaInfo) -> str:
def __call__(self, quota: _QuotaInfo) -> str:
gpu_details = self._format_quota_details(
quota.gpu_time_spent, quota.gpu_time_limit, quota.gpu_time_left
)
Expand Down
16 changes: 8 additions & 8 deletions tests/api/test_quota.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from aiohttp import web

from neuromation.api import AuthorizationError, Client
from neuromation.api.quota import QuotaInfo
from neuromation.api.quota import _QuotaInfo
from tests import _TestServerFactory


Expand Down Expand Up @@ -34,8 +34,8 @@ async def handle_stats(request: web.Request) -> web.StreamResponse:
srv = await aiohttp_server(app)

async with make_client(srv.make_url("/")) as client:
quota = await client.quota.get()
assert quota == QuotaInfo(
quota = await client._quota.get()
assert quota == _QuotaInfo(
name=client.username,
gpu_time_spent=float(101 * 60),
gpu_time_limit=float(201 * 60),
Expand Down Expand Up @@ -67,8 +67,8 @@ async def handle_stats(request: web.Request) -> web.StreamResponse:
srv = await aiohttp_server(app)

async with make_client(srv.make_url("/")) as client:
quota = await client.quota.get("another-user")
assert quota == QuotaInfo(
quota = await client._quota.get("another-user")
assert quota == _QuotaInfo(
name="another-user",
gpu_time_spent=float(101 * 60),
gpu_time_limit=float(201 * 60),
Expand Down Expand Up @@ -97,8 +97,8 @@ async def handle_stats(request: web.Request) -> web.StreamResponse:
srv = await aiohttp_server(app)

async with make_client(srv.make_url("/")) as client:
quota = await client.quota.get()
assert quota == QuotaInfo(
quota = await client._quota.get()
assert quota == _QuotaInfo(
name=client.username,
gpu_time_spent=float(101 * 60),
gpu_time_limit=float("inf"),
Expand All @@ -124,4 +124,4 @@ async def handle_stats(request: web.Request) -> web.StreamResponse:
with pytest.raises(
AuthorizationError, match='"uri": "user://another-user", "action": "read"'
):
await client.quota.get("another-user")
await client._quota.get("another-user")
10 changes: 5 additions & 5 deletions tests/cli/test_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
ImageProgressSave,
)
from neuromation.api.parsing_utils import _ImageNameParser
from neuromation.api.quota import QuotaInfo
from neuromation.api.quota import _QuotaInfo
from neuromation.cli.formatters import (
BaseFilesFormatter,
ConfigFormatter,
Expand Down Expand Up @@ -1481,7 +1481,7 @@ async def test_output_for_tpu_presets(self, root: Root, monkeypatch: Any) -> Non

class TestQuotaInfoFormatter:
def test_output(self) -> None:
quota = QuotaInfo(
quota = _QuotaInfo(
name="user",
gpu_time_spent=0.0,
gpu_time_limit=0.0,
Expand All @@ -1499,7 +1499,7 @@ def test_output(self) -> None:
)

def test_output_no_quota(self) -> None:
quota = QuotaInfo(
quota = _QuotaInfo(
name="user",
gpu_time_spent=0.0,
gpu_time_limit=float("inf"),
Expand All @@ -1515,7 +1515,7 @@ def test_output_no_quota(self) -> None:
)

def test_output_too_many_hours(self) -> None:
quota = QuotaInfo(
quota = _QuotaInfo(
name="user",
gpu_time_spent=float((1 * 60 + 29) * 60),
gpu_time_limit=float((9 * 60 + 59) * 60),
Expand All @@ -1533,7 +1533,7 @@ def test_output_too_many_hours(self) -> None:
)

def test_output_spent_more_than_quota_left_zero(self) -> None:
quota = QuotaInfo(
quota = _QuotaInfo(
name="user",
gpu_time_spent=float(9 * 60 * 60),
gpu_time_limit=float(1 * 60 * 60),
Expand Down

0 comments on commit 3f05ebb

Please sign in to comment.