Skip to content

Commit

Permalink
now returns a proper AnyUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderegg committed Nov 1, 2024
1 parent f534e35 commit 4686d42
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from models_library.services import ServiceKey, ServiceVersion
from models_library.users import UserID
from models_library.wallets import WalletID
from pydantic import AnyUrl
from servicelib.rabbitmq import RPCRouter
from servicelib.rabbitmq.rpc_interfaces.resource_usage_tracker.errors import (
CustomResourceUsageTrackerError,
Expand Down Expand Up @@ -78,7 +79,7 @@ async def export_service_runs(
access_all_wallet_usage: bool = False,
order_by: OrderBy | None = None,
filters: ServiceResourceUsagesFilters | None = None,
) -> str:
) -> AnyUrl:
app_settings: ApplicationSettings = app.state.settings
s3_settings = app_settings.RESOURCE_USAGE_TRACKER_S3
assert s3_settings # nosec
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta, timezone
from datetime import UTC, datetime, timedelta, timezone

import shortuuid
from aws_library.s3 import SimcoreS3API
Expand All @@ -18,7 +18,7 @@
from models_library.rest_ordering import OrderBy
from models_library.users import UserID
from models_library.wallets import WalletID
from pydantic import PositiveInt, TypeAdapter
from pydantic import AnyUrl, PositiveInt, TypeAdapter
from servicelib.rabbitmq.rpc_interfaces.resource_usage_tracker.errors import (
CustomResourceUsageTrackerError,
)
Expand Down Expand Up @@ -144,6 +144,7 @@ async def list_service_runs(

async def export_service_runs(
s3_client: SimcoreS3API,
*,
bucket_name: str,
s3_region: str,
user_id: UserID,
Expand All @@ -153,15 +154,17 @@ async def export_service_runs(
access_all_wallet_usage: bool = False,
order_by: OrderBy | None = None,
filters: ServiceResourceUsagesFilters | None = None,
) -> str:
) -> AnyUrl:
started_from = filters.started_at.from_ if filters else None
started_until = filters.started_at.until if filters else None

# Create S3 key name
s3_bucket_name = TypeAdapter(S3BucketName).validate_python(bucket_name)
# NOTE: su stands for "service usage"
file_name = f"su_{shortuuid.uuid()}.csv"
s3_object_key = f"resource-usage-tracker-service-runs/{datetime.now(tz=timezone.utc).date()}/{file_name}"
s3_object_key = (
f"resource-usage-tracker-service-runs/{datetime.now(tz=UTC).date()}/{file_name}"
)

# Export CSV to S3
await resource_tracker_repo.export_service_runs_table_to_s3(
Expand All @@ -177,12 +180,11 @@ async def export_service_runs(
)

# Create presigned S3 link
generated_url = await s3_client.create_single_presigned_download_link(
return await s3_client.create_single_presigned_download_link(
bucket=s3_bucket_name,
object_key=s3_object_key,
expiration_secs=_PRESIGNED_LINK_EXPIRATION_SEC,
)
return f"{generated_url}"


async def get_osparc_credits_aggregated_usages_page(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# pylint:disable=unused-variable
# pylint:disable=unused-argument
# pylint:disable=redefined-outer-name
# pylint:disable=too-many-arguments

import os
from unittest.mock import Mock

Expand Down Expand Up @@ -25,25 +30,21 @@

@pytest.fixture
async def mocked_export(mocker: MockerFixture):
mock_export = mocker.patch(
return mocker.patch(
"simcore_service_resource_usage_tracker.services.resource_tracker_service_runs.ResourceTrackerRepository.export_service_runs_table_to_s3",
autospec=True,
)

return mock_export


@pytest.fixture
async def mocked_presigned_link(mocker: MockerFixture):
mock_presigned_link = mocker.patch(
return mocker.patch(
"simcore_service_resource_usage_tracker.services.resource_tracker_service_runs.SimcoreS3API.create_single_presigned_download_link",
return_value=TypeAdapter(AnyUrl).validate_python(
"https://www.testing.com/",
),
)

return mock_presigned_link


@pytest.fixture
async def enable_resource_usage_tracker_s3(
Expand Down Expand Up @@ -76,6 +77,6 @@ async def test_rpc_list_service_runs_which_was_billed(
user_id=_USER_ID,
product_name="osparc",
)
assert isinstance(download_url, AnyUrl)
assert isinstance(download_url, AnyUrl) # nosec
assert mocked_export.called
assert mocked_presigned_link.called

0 comments on commit 4686d42

Please sign in to comment.