Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 add running_time_in_hours to /services/-/aggregated-usages #6350

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion api/specs/web-server/_resource_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

from _common import assert_handler_signature_against_model
from fastapi import APIRouter, Query, status
from models_library.api_schemas_resource_usage_tracker.service_runs import (
OsparcCreditsAggregatedByServiceGet,
)
from models_library.api_schemas_webserver.resource_usage import (
ConnectServiceToPricingPlanBodyParams,
CreatePricingPlanBodyParams,
Expand Down Expand Up @@ -93,7 +96,7 @@ async def list_resource_usage_services(

@router.get(
"/services/-/aggregated-usages",
response_model=Envelope[list[ServiceRunGet]],
response_model=Envelope[list[OsparcCreditsAggregatedByServiceGet]],
summary="Used credits based on aggregate by type, currently supported `services`. (user and product are taken from context, optionally wallet_id parameter might be provided).",
tags=["usage"],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ServiceRunPage(NamedTuple):
class OsparcCreditsAggregatedByServiceGet(BaseModel):
osparc_credits: Decimal
service_key: ServiceKey
running_time_in_hours: Decimal


class OsparcCreditsAggregatedUsagesPage(NamedTuple):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class Config:
class OsparcCreditsAggregatedByServiceKeyDB(BaseModel):
osparc_credits: Decimal
service_key: ServiceKey
running_time_in_hours: Decimal

class Config:
orm_mode = True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,22 @@ async def get_osparc_credits_aggregated_by_service(
sa.func.SUM(
resource_tracker_credit_transactions.c.osparc_credits
).label("osparc_credits"),
sa.func.SUM(
sa.func.round(
(
sa.func.extract(
"epoch",
resource_tracker_service_runs.c.stopped_at,
)
- sa.func.extract(
"epoch",
resource_tracker_service_runs.c.started_at,
)
)
/ 3600,
2,
)
).label("running_time_in_hours"),
)
.select_from(
resource_tracker_service_runs.join(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ async def get_osparc_credits_aggregated_usages_page(
OsparcCreditsAggregatedByServiceGet.construct(
osparc_credits=item.osparc_credits,
service_key=item.service_key,
running_time_in_hours=item.running_time_in_hours,
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,49 @@ def resource_tracker_setup_db(
random_resource_tracker_service_run(
user_id=_USER_ID_1,
service_run_id=_SERVICE_RUN_ID_1,
started_at=datetime.now(tz=timezone.utc),
started_at=datetime.now(tz=timezone.utc) - timedelta(hours=1),
stopped_at=datetime.now(tz=timezone.utc),
service_run_status="SUCCESS",
service_key="simcore/services/dynamic/jupyter-smash",
),
random_resource_tracker_service_run(
user_id=_USER_ID_2,
service_run_id=_SERVICE_RUN_ID_2,
started_at=datetime.now(tz=timezone.utc),
started_at=datetime.now(tz=timezone.utc) - timedelta(hours=1),
stopped_at=datetime.now(tz=timezone.utc),
service_run_status="SUCCESS",
service_key="simcore/services/dynamic/jupyter-smash",
),
random_resource_tracker_service_run(
user_id=_USER_ID_1,
service_run_id=_SERVICE_RUN_ID_3,
started_at=datetime.now(tz=timezone.utc),
started_at=datetime.now(tz=timezone.utc) - timedelta(hours=1),
stopped_at=datetime.now(tz=timezone.utc),
service_run_status="SUCCESS",
service_key="simcore/services/dynamic/jupyter-smash",
),
random_resource_tracker_service_run(
user_id=_USER_ID_1,
service_run_id=_SERVICE_RUN_ID_4,
started_at=datetime.now(tz=timezone.utc),
started_at=datetime.now(tz=timezone.utc) - timedelta(hours=1),
stopped_at=datetime.now(tz=timezone.utc),
service_run_status="SUCCESS",
service_key="simcore/services/dynamic/jupyter-smash",
),
random_resource_tracker_service_run(
user_id=_USER_ID_1,
service_run_id=_SERVICE_RUN_ID_5,
started_at=datetime.now(tz=timezone.utc) - timedelta(days=3),
stopped_at=datetime.now(tz=timezone.utc),
service_run_status="SUCCESS",
service_key="simcore/services/dynamic/jupyter-smash",
),
random_resource_tracker_service_run(
user_id=_USER_ID_1,
service_run_id=_SERVICE_RUN_ID_6,
started_at=datetime.now(tz=timezone.utc) - timedelta(days=10),
stopped_at=datetime.now(tz=timezone.utc),
service_run_status="SUCCESS",
service_key="simcore/services/dynamic/sim4life",
),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4258,7 +4258,7 @@ paths:
'403':
description: ProjectInvalidRightsError
'404':
description: ProjectNotFoundError, UserDefaultWalletNotFoundError
description: UserDefaultWalletNotFoundError, ProjectNotFoundError
'409':
description: ProjectTooManyProjectOpenedError
'422':
Expand Down Expand Up @@ -4579,7 +4579,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/Envelope_list_models_library.api_schemas_webserver.resource_usage.ServiceRunGet__'
$ref: '#/components/schemas/Envelope_list_models_library.api_schemas_resource_usage_tracker.service_runs.OsparcCreditsAggregatedByServiceGet__'
/v0/services/-/usage-report:
get:
tags:
Expand Down Expand Up @@ -7641,6 +7641,17 @@ components:
$ref: '#/components/schemas/TaskGet'
error:
title: Error
Envelope_list_models_library.api_schemas_resource_usage_tracker.service_runs.OsparcCreditsAggregatedByServiceGet__:
title: Envelope[list[models_library.api_schemas_resource_usage_tracker.service_runs.OsparcCreditsAggregatedByServiceGet]]
type: object
properties:
data:
title: Data
type: array
items:
$ref: '#/components/schemas/OsparcCreditsAggregatedByServiceGet'
error:
title: Error
Envelope_list_models_library.api_schemas_storage.FileMetaDataGet__:
title: Envelope[list[models_library.api_schemas_storage.FileMetaDataGet]]
type: object
Expand Down Expand Up @@ -9339,6 +9350,24 @@ components:
- WALLET_SHARED
type: string
description: An enumeration.
OsparcCreditsAggregatedByServiceGet:
title: OsparcCreditsAggregatedByServiceGet
required:
- osparc_credits
- service_key
- running_time_in_hours
type: object
properties:
osparc_credits:
title: Osparc Credits
type: number
service_key:
title: Service Key
pattern: ^simcore/services/((comp|dynamic|frontend))/([a-z0-9][a-z0-9_.-]*/)*([a-z0-9-_]+[a-z0-9])$
type: string
running_time_in_hours:
title: Running Time In Hours
type: number
Owner:
title: Owner
required:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
**{
"osparc_credits": Decimal(-50),
"service_key": "simcore/services/comp/itis/sleeper",
"running_time_in_hours": Decimal(0.5),
}
)
],
Expand Down
Loading