Skip to content

Commit

Permalink
rename credits consumption to usage (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
zubenkoivan authored Jul 21, 2024
1 parent 349de6f commit b11004d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 79 deletions.
24 changes: 12 additions & 12 deletions src/platform_reports/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@
PodCreditsCollector,
Price,
)
from .metrics_service import CreditsConsumptionRequest, MetricsService
from .metrics_service import GetCreditsUsageRequest, MetricsService
from .prometheus_client import PrometheusClient
from .schema import (
ClientErrorSchema,
PostCreditsConsumptionRequest,
PostCreditsConsumptionRequestSchema,
PostCreditsConsumptionResponseSchema,
PostCreditsUsageRequest,
PostCreditsUsageRequestSchema,
PostCreditsUsageResponseSchema,
)


Expand Down Expand Up @@ -302,7 +302,7 @@ def __init__(self, app: aiohttp.web.Application) -> None:

def register(self) -> None:
self._app.router.add_post(
"/v1/metrics/credits/consumption", self.handle_post_credits_consumption
"/v1/metrics/credits/usage", self.handle_post_credits_usage
)

@property
Expand All @@ -324,23 +324,23 @@ def _metrics_service(self) -> MetricsService:
},
},
)
@request_schema(PostCreditsConsumptionRequestSchema())
@response_schema(PostCreditsConsumptionResponseSchema(many=True))
async def handle_post_credits_consumption(self, request: Request) -> Response:
@request_schema(PostCreditsUsageRequestSchema())
@response_schema(PostCreditsUsageResponseSchema(many=True))
async def handle_post_credits_usage(self, request: Request) -> Response:
await check_permissions(
request, [Permission(f"cluster://{self._config.cluster_name}", "read")]
)
request_data: PostCreditsConsumptionRequest = request["data"]
consumptions = await self._metrics_service.get_credits_consumption(
CreditsConsumptionRequest(
request_data: PostCreditsUsageRequest = request["data"]
consumptions = await self._metrics_service.get_credits_usage(
GetCreditsUsageRequest(
category_name=request_data.category_name,
org_name=request_data.org_name,
project_name=request_data.project_name,
start_date=request_data.start_date,
end_date=request_data.end_date,
)
)
response_schema = PostCreditsConsumptionResponseSchema(many=True)
response_schema = PostCreditsUsageResponseSchema(many=True)
return json_response(
response_schema.dump(consumptions), status=HTTPOk.status_code
)
Expand Down
34 changes: 17 additions & 17 deletions src/platform_reports/metrics_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


@dataclass(frozen=True)
class CreditsConsumptionRequest:
class GetCreditsUsageRequest:
start_date: datetime
end_date: datetime
category_name: CategoryName | None = None
Expand All @@ -23,7 +23,7 @@ class CreditsConsumptionRequest:


@dataclass(frozen=True)
class CreditsConsumption:
class CreditsUsage:
category_name: CategoryName
project_name: str
resource_id: str
Expand Down Expand Up @@ -88,9 +88,9 @@ def _get_apps_label_matchers(
return ",".join(label_matchers)


class CreditsConsumptionFactory:
class CreditsUsageFactory:
@classmethod
def create_for_compute(cls, metric: Metric) -> CreditsConsumption | None:
def create_for_compute(cls, metric: Metric) -> CreditsUsage | None:
if len(metric.values) < 2:
return None
if job_id := metric.labels.get(PrometheusLabel.NEURO_JOB_KEY):
Expand All @@ -103,8 +103,8 @@ def create_for_compute(cls, metric: Metric) -> CreditsConsumption | None:
return None

@classmethod
def _create_for_job(cls, metric: Metric, *, job_id: str) -> CreditsConsumption:
return CreditsConsumption(
def _create_for_job(cls, metric: Metric, *, job_id: str) -> CreditsUsage:
return CreditsUsage(
category_name=CategoryName.JOBS,
org_name=cls._get_org_name(metric),
project_name=cls._get_project_name(metric),
Expand All @@ -113,8 +113,8 @@ def _create_for_job(cls, metric: Metric, *, job_id: str) -> CreditsConsumption:
)

@classmethod
def _create_for_app(cls, metric: Metric, *, app_id: str) -> CreditsConsumption:
return CreditsConsumption(
def _create_for_app(cls, metric: Metric, *, app_id: str) -> CreditsUsage:
return CreditsUsage(
category_name=CategoryName.APPS,
org_name=cls._get_org_name(metric),
project_name=cls._get_project_name(metric),
Expand All @@ -141,22 +141,22 @@ class MetricsService:
def __init__(self, *, prometheus_client: PrometheusClient) -> None:
self._prometheus_client = prometheus_client
self._prometheus_query_factory = PrometheusQueryFactory()
self._credits_consumption_factory = CreditsConsumptionFactory()
self._credits_usage_factory = CreditsUsageFactory()

async def get_credits_consumption(
self, request: CreditsConsumptionRequest
) -> list[CreditsConsumption]:
async def get_credits_usage(
self, request: GetCreditsUsageRequest
) -> list[CreditsUsage]:
async with asyncio.TaskGroup() as tg:
tasks = []
if request.category_name in (None, CategoryName.JOBS, CategoryName.APPS):
tasks.append(
tg.create_task(self._get_compute_credits_consumption(request))
tg.create_task(self._get_compute_credits_usage(request))
)
return list(itertools.chain.from_iterable(t.result() for t in tasks))

async def _get_compute_credits_consumption(
self, request: CreditsConsumptionRequest
) -> list[CreditsConsumption]:
async def _get_compute_credits_usage(
self, request: GetCreditsUsageRequest
) -> list[CreditsUsage]:
LOGGER.debug("Requesting compute credits consumption: %s", request)
query = self._prometheus_query_factory.create_compute_credits(
org_name=request.org_name, project_name=request.project_name
Expand All @@ -166,7 +166,7 @@ async def _get_compute_credits_consumption(
)
consumptions = []
for metric in metrics:
if c := self._credits_consumption_factory.create_for_compute(metric):
if c := self._credits_usage_factory.create_for_compute(metric):
LOGGER.debug("Compute consumption: %s", c)
consumptions.append(c)
return consumptions
10 changes: 5 additions & 5 deletions src/platform_reports/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class CategoryName(StrEnum):


@dataclass(frozen=True)
class PostCreditsConsumptionRequest:
class PostCreditsUsageRequest:
start_date: datetime
end_date: datetime
category_name: CategoryName | None = None
org_name: str | None = None
project_name: str | None = None


class PostCreditsConsumptionRequestSchema(Schema):
class PostCreditsUsageRequestSchema(Schema):
category_name = fields.Enum(CategoryName, by_value=True)
org_name = fields.String(validate=[Length(min=1)])
project_name = fields.String(validate=[Length(min=1)])
Expand All @@ -44,11 +44,11 @@ def validate_dates(self, data: Mapping[str, Any], **kwargs: Any) -> None:
@post_load
def make_object(
self, data: Mapping[str, Any], **kwargs: Any
) -> PostCreditsConsumptionRequest:
return PostCreditsConsumptionRequest(**data)
) -> PostCreditsUsageRequest:
return PostCreditsUsageRequest(**data)


class PostCreditsConsumptionResponseSchema(Schema):
class PostCreditsUsageResponseSchema(Schema):
category_name = fields.Enum(CategoryName, by_value=True)
org_name = fields.String()
project_name = fields.String()
Expand Down
30 changes: 15 additions & 15 deletions tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from platform_reports.api import create_metrics_api_app
from platform_reports.config import MetricsApiConfig, MetricsExporterConfig
from platform_reports.kube_client import Node
from platform_reports.metrics_service import CreditsConsumption, MetricsService
from platform_reports.metrics_service import CreditsUsage, MetricsService
from platform_reports.schema import CategoryName

from .conftest import create_local_app_server
Expand Down Expand Up @@ -345,11 +345,11 @@ async def test_ping(
async with client.get(metrics_api_server / "ping") as response:
assert response.status == HTTPOk.status_code, await response.text()

async def test_post_credits_consumption__unauthorized(
async def test_post_credits_usage__unauthorized(
self, client: aiohttp.ClientSession, metrics_api_server: URL
) -> None:
async with client.post(
metrics_api_server / "api/v1/metrics/credits/consumption",
metrics_api_server / "api/v1/metrics/credits/usage",
json={
"start_date": (datetime.now() - timedelta(hours=1)).isoformat(),
"end_date": datetime.now().isoformat(),
Expand All @@ -359,15 +359,15 @@ async def test_post_credits_consumption__unauthorized(
response.status == HTTPUnauthorized.status_code
), await response.text()

async def test_post_credits_consumption__forbidden(
async def test_post_credits_usage__forbidden(
self,
client: aiohttp.ClientSession,
metrics_api_server: URL,
user_factory: UserFactory,
) -> None:
user = await user_factory(str(uuid.uuid4()), [])
async with client.post(
metrics_api_server / "api/v1/metrics/credits/consumption",
metrics_api_server / "api/v1/metrics/credits/usage",
headers={"Authorization": f"Bearer {user.token}"},
json={
"start_date": (datetime.now() - timedelta(hours=1)).isoformat(),
Expand All @@ -376,23 +376,23 @@ async def test_post_credits_consumption__forbidden(
) as response:
assert response.status == HTTPForbidden.status_code, await response.text()

async def test_post_credits_consumption__bad_request(
async def test_post_credits_usage__bad_request(
self, client: aiohttp.ClientSession, user: User, metrics_api_server: URL
) -> None:
async with client.post(
metrics_api_server / "api/v1/metrics/credits/consumption",
metrics_api_server / "api/v1/metrics/credits/usage",
headers={"Authorization": f"Bearer {user.token}"},
json={},
) as response:
assert (
response.status == HTTPUnprocessableEntity.status_code
), await response.text()

async def test_post_credits_consumption(
async def test_post_credits_usage(
self, client: aiohttp.ClientSession, user: User, metrics_api_server: URL
) -> None:
async with client.post(
metrics_api_server / "api/v1/metrics/credits/consumption",
metrics_api_server / "api/v1/metrics/credits/usage",
headers={"Authorization": f"Bearer {user.token}"},
json={
"start_date": (datetime.now() - timedelta(hours=1)).isoformat(),
Expand All @@ -401,11 +401,11 @@ async def test_post_credits_consumption(
) as response:
assert response.status == HTTPOk.status_code, await response.text()

async def test_post_credits_consumption__with_org_and_project(
async def test_post_credits_usage__with_org_and_project(
self, client: aiohttp.ClientSession, user: User, metrics_api_server: URL
) -> None:
async with client.post(
metrics_api_server / "api/v1/metrics/credits/consumption",
metrics_api_server / "api/v1/metrics/credits/usage",
headers={"Authorization": f"Bearer {user.token}"},
json={
"org_name": "test-org",
Expand All @@ -416,7 +416,7 @@ async def test_post_credits_consumption__with_org_and_project(
) as response:
assert response.status == HTTPOk.status_code, await response.text()

async def test_post_credits_consumption__mocked(
async def test_post_credits_usage__mocked(
self,
client: aiohttp.ClientSession,
user: User,
Expand All @@ -427,8 +427,8 @@ async def test_post_credits_consumption__mocked(
mock.patch("platform_reports.api.MetricsService", spec=MetricsService)
)
mocked_service = mocked_service_cls.return_value
mocked_service.get_credits_consumption.return_value = [
CreditsConsumption(
mocked_service.get_credits_usage.return_value = [
CreditsUsage(
category_name=CategoryName.JOBS,
project_name="test-project",
resource_id="test-job",
Expand All @@ -444,7 +444,7 @@ async def test_post_credits_consumption__mocked(
)

async with client.post(
server_address.http_url / "api/v1/metrics/credits/consumption",
server_address.http_url / "api/v1/metrics/credits/usage",
headers={"Authorization": f"Bearer {user.token}"},
json={
"start_date": (datetime.now() - timedelta(hours=1)).isoformat(),
Expand Down
14 changes: 6 additions & 8 deletions tests/integration/test_metrics_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from aiohttp.test_utils import TestClient

from platform_reports.metrics_service import (
CreditsConsumption,
CreditsConsumptionRequest,
CreditsUsage,
GetCreditsUsageRequest,
MetricsService,
)
from platform_reports.prometheus_client import PrometheusClient
Expand Down Expand Up @@ -66,10 +66,10 @@ class TestMetricsService:
def metrics_service(self, prometheus_client: PrometheusClient) -> MetricsService:
return MetricsService(prometheus_client=prometheus_client)

async def test_get_credits_consumption(
async def test_get_credits_usage(
self, metrics_service: MetricsService, prometheus_handler: PrometheusHandler
) -> None:
consumption_request = CreditsConsumptionRequest(
consumption_request = GetCreditsUsageRequest(
start_date=datetime.now(UTC) - timedelta(hours=1),
end_date=datetime.now(UTC),
)
Expand Down Expand Up @@ -109,12 +109,10 @@ async def post_query_range(

prometheus_handler.post_query_range = post_query_range

consumptions = await metrics_service.get_credits_consumption(
consumption_request
)
consumptions = await metrics_service.get_credits_usage(consumption_request)

assert consumptions == [
CreditsConsumption(
CreditsUsage(
category_name=CategoryName.JOBS,
project_name="test-project",
resource_id="test-job",
Expand Down
Loading

0 comments on commit b11004d

Please sign in to comment.