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

🎨 improvements: return user email in licensed items, error msg, expose pricing plans πŸ—ƒοΈ #7068

Merged
merged 28 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a3d0eb8
first commit
matusdrobuliak66 Jan 22, 2025
393cd5c
Merge branch 'master' into fix/debt-msg
matusdrobuliak66 Jan 22, 2025
c1e21d3
openapi specs
matusdrobuliak66 Jan 22, 2025
99242cc
add migration db add user email col
matusdrobuliak66 Jan 22, 2025
250f41a
openapi specs
matusdrobuliak66 Jan 22, 2025
0165cde
add unit test
matusdrobuliak66 Jan 22, 2025
03306d5
Merge branch 'master' into fix/debt-msg
matusdrobuliak66 Jan 22, 2025
662a796
fix
matusdrobuliak66 Jan 22, 2025
e26e28d
fix
matusdrobuliak66 Jan 22, 2025
afbe8ec
Merge branch 'master' into fix/debt-msg
matusdrobuliak66 Jan 27, 2025
938d267
review @sanderegg
matusdrobuliak66 Jan 27, 2025
4963ec8
review @pcrespov
matusdrobuliak66 Jan 27, 2025
86f75c0
add comment
matusdrobuliak66 Jan 27, 2025
4a685d1
openapi specs
matusdrobuliak66 Jan 27, 2025
92ca3a0
fix tests
matusdrobuliak66 Jan 27, 2025
6e402f9
Merge branch 'master' into fix/debt-msg
matusdrobuliak66 Jan 28, 2025
21caad7
update regex
odeimaiz Jan 28, 2025
3588ae6
getAllPages
odeimaiz Jan 28, 2025
2ff9021
adminPricingPlans & pricingPlans
odeimaiz Jan 28, 2025
8e35c0d
minor
odeimaiz Jan 28, 2025
b2c0daa
Merge pull request #5 from odeimaiz/matusdrobuliak66-fix/debt-msg
matusdrobuliak66 Jan 28, 2025
45be582
Merge branch 'master' into fix/debt-msg
matusdrobuliak66 Jan 28, 2025
cddb7cc
fix
matusdrobuliak66 Jan 28, 2025
dca94a8
Merge branch 'master' into fix/debt-msg
matusdrobuliak66 Jan 28, 2025
ee53789
Merge branch 'master' into fix/debt-msg
matusdrobuliak66 Jan 28, 2025
493e24f
fix issue with publishing
matusdrobuliak66 Jan 28, 2025
2c97c52
Merge branch 'fix/debt-msg' of github.com:matusdrobuliak66/osparc-sim…
matusdrobuliak66 Jan 28, 2025
d911b4c
Merge branch 'master' into fix/debt-msg
matusdrobuliak66 Jan 28, 2025
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
28 changes: 26 additions & 2 deletions api/specs/web-server/_resource_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
CreatePricingPlanBodyParams,
CreatePricingUnitBodyParams,
PricingPlanAdminGet,
PricingPlanGet,
PricingPlanToServiceAdminGet,
PricingUnitAdminGet,
PricingUnitGet,
Expand Down Expand Up @@ -102,6 +103,29 @@ async def get_pricing_plan_unit(
...


@router.get(
"/pricing-plans",
response_model=Envelope[list[PricingPlanGet]],
summary="List pricing plans",
tags=["pricing-plans"],
description="To keep the listing lightweight, the pricingUnits field is None.",
)
async def list_pricing_plans():
...


@router.get(
"/pricing-plans/{pricing_plan_id}",
response_model=Envelope[PricingPlanGet],
summary="Retrieve detail information about pricing plan",
tags=["pricing-plans"],
)
async def get_pricing_plan(
_path: Annotated[PricingPlanGetPathParams, Depends()],
):
...


## Pricing plans for Admin panel


Expand All @@ -112,7 +136,7 @@ async def get_pricing_plan_unit(
tags=["admin"],
description="To keep the listing lightweight, the pricingUnits field is None.",
)
async def list_pricing_plans():
async def list_pricing_plans_for_admin_user():
...


Expand All @@ -122,7 +146,7 @@ async def list_pricing_plans():
summary="Retrieve detail information about pricing plan",
tags=["admin"],
)
async def get_pricing_plan(
async def get_pricing_plan_for_admin_user(
_path: Annotated[PricingPlanGetPathParams, Depends()],
):
...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class LicensedItemCheckoutGet(BaseModel):
licensed_item_id: LicensedItemID
wallet_id: WalletID
user_id: UserID
user_email: str
product_name: ProductName
service_run_id: ServiceRunID
started_at: datetime
Expand All @@ -31,6 +32,7 @@ class LicensedItemCheckoutGet(BaseModel):
"licensed_item_id": "303942ef-6d31-4ba8-afbe-dbb1fce2a953",
"wallet_id": 1,
"user_id": 1,
"user_email": "[email protected]",
"product_name": "osparc",
"service_run_id": "run_1",
"started_at": "2023-01-11 13:11:47.293595",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class LicensedItemPurchaseGet(BaseModel):
expire_at: datetime
num_of_seats: int
purchased_by_user: UserID
user_email: str
purchased_at: datetime
modified: datetime

Expand All @@ -43,6 +44,7 @@ class LicensedItemPurchaseGet(BaseModel):
"expire_at": "2023-01-11 13:11:47.293595",
"num_of_seats": 1,
"purchased_by_user": 1,
"user_email": "[email protected]",
"purchased_at": "2023-01-11 13:11:47.293595",
"modified": "2023-01-11 13:11:47.293595",
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class LicensedItemCheckoutRestGet(OutputSchema):
licensed_item_id: LicensedItemID
wallet_id: WalletID
user_id: UserID
user_email: str
product_name: ProductName
started_at: datetime
stopped_at: datetime | None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class LicensedItemPurchaseGet(OutputSchema):
expire_at: datetime
num_of_seats: int
purchased_by_user: UserID
user_email: str
purchased_at: datetime
modified_at: datetime

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class PricingPlanGet(OutputSchema):
classification: PricingPlanClassification
created_at: datetime
pricing_plan_key: str
pricing_units: list[PricingUnitGet]
pricing_units: list[PricingUnitGet] | None
is_active: bool


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""add user email col to purchases

Revision ID: 5f88b513cd4c
Revises: ecd4eadaa781
Create Date: 2025-01-22 15:08:17.729337+00:00

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "5f88b513cd4c"
down_revision = "ecd4eadaa781"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"resource_tracker_licensed_items_purchases",
sa.Column("user_email", sa.String(), nullable=True),
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("resource_tracker_licensed_items_purchases", "user_email")
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
sa.BigInteger,
nullable=False,
),
sa.Column(
"user_email",
sa.String,
nullable=True,
),
sa.Column(
"purchased_at",
sa.DateTime(timezone=True),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class LicensedItemsPurchasesDB(BaseModel):
expire_at: datetime
num_of_seats: int
purchased_by_user: UserID
user_email: str
purchased_at: datetime
modified: datetime

Expand All @@ -41,6 +42,7 @@ class CreateLicensedItemsPurchasesDB(BaseModel):
expire_at: datetime
num_of_seats: int
purchased_by_user: UserID
user_email: str
purchased_at: datetime

model_config = ConfigDict(from_attributes=True)
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ async def _close_unhealthy_service(
service_run_id: ServiceRunID,
base_start_timestamp: datetime,
):

# 1. Close the service_run
update_service_run_stopped_at = ServiceRunStoppedAtUpdate(
service_run_id=service_run_id,
Expand All @@ -106,21 +107,39 @@ async def _close_unhealthy_service(
running_service.last_heartbeat_at,
running_service.pricing_unit_cost,
)
_transaction_status = (
CreditTransactionStatus.NOT_BILLED
if running_service.service_type
== ResourceTrackerServiceType.COMPUTATIONAL_SERVICE
else CreditTransactionStatus.BILLED
)
update_credit_transaction = CreditTransactionCreditsAndStatusUpdate(
service_run_id=service_run_id,
osparc_credits=make_negative(computed_credits),
transaction_status=(
CreditTransactionStatus.NOT_BILLED
if running_service.service_type
== ResourceTrackerServiceType.COMPUTATIONAL_SERVICE
else CreditTransactionStatus.BILLED
),
transaction_status=_transaction_status,
)
await credit_transactions_db.update_credit_transaction_credits_and_status(
db_engine, data=update_credit_transaction
)

# 3. Release license seats in case some were checked out but not properly released.
# 3. If the credit transaction status is considered "NOT_BILLED", this might return
# the wallet to positive numbers. If, in the meantime, some transactions were marked as DEBT,
# we need to update them back to the BILLED state.
if _transaction_status == CreditTransactionStatus.NOT_BILLED:
wallet_total_credits = await credit_transactions_db.sum_wallet_credits(
db_engine,
product_name=running_service.product_name,
wallet_id=running_service.wallet_id,
)
if wallet_total_credits.available_osparc_credits >= 0:
await credit_transactions_db.batch_update_credit_transaction_status_for_in_debt_transactions(
db_engine,
project_id=None,
wallet_id=running_service.wallet_id,
transaction_status=CreditTransactionStatus.BILLED,
)

# 4. Release license seats in case some were checked out but not properly released.
await licensed_items_checkouts_db.force_release_license_seats_by_run_id(
db_engine, service_run_id=service_run_id
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ async def list_licensed_items_checkouts(
licensed_item_id=licensed_item_checkout_db.licensed_item_id,
wallet_id=licensed_item_checkout_db.wallet_id,
user_id=licensed_item_checkout_db.user_id,
user_email=licensed_item_checkout_db.user_email,
product_name=licensed_item_checkout_db.product_name,
service_run_id=licensed_item_checkout_db.service_run_id,
started_at=licensed_item_checkout_db.started_at,
Expand Down Expand Up @@ -90,6 +91,7 @@ async def get_licensed_item_checkout(
licensed_item_id=licensed_item_checkout_db.licensed_item_id,
wallet_id=licensed_item_checkout_db.wallet_id,
user_id=licensed_item_checkout_db.user_id,
user_email=licensed_item_checkout_db.user_email,
product_name=licensed_item_checkout_db.product_name,
service_run_id=licensed_item_checkout_db.service_run_id,
started_at=licensed_item_checkout_db.started_at,
Expand Down Expand Up @@ -171,6 +173,7 @@ async def checkout_licensed_item(
licensed_item_id=licensed_item_checkout_db.licensed_item_id,
wallet_id=licensed_item_checkout_db.wallet_id,
user_id=licensed_item_checkout_db.user_id,
user_email=licensed_item_checkout_db.user_email,
product_name=licensed_item_checkout_db.product_name,
service_run_id=licensed_item_checkout_db.service_run_id,
started_at=licensed_item_checkout_db.started_at,
Expand Down Expand Up @@ -200,6 +203,7 @@ async def release_licensed_item(
licensed_item_id=licensed_item_checkout_db.licensed_item_id,
wallet_id=licensed_item_checkout_db.wallet_id,
user_id=licensed_item_checkout_db.user_id,
user_email=licensed_item_checkout_db.user_email,
product_name=licensed_item_checkout_db.product_name,
service_run_id=licensed_item_checkout_db.service_run_id,
started_at=licensed_item_checkout_db.started_at,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ async def list_licensed_items_purchases(
expire_at=licensed_item_purchase_db.expire_at,
num_of_seats=licensed_item_purchase_db.num_of_seats,
purchased_by_user=licensed_item_purchase_db.purchased_by_user,
user_email=licensed_item_purchase_db.user_email,
purchased_at=licensed_item_purchase_db.purchased_at,
modified=licensed_item_purchase_db.modified,
)
Expand Down Expand Up @@ -96,6 +97,7 @@ async def get_licensed_item_purchase(
expire_at=licensed_item_purchase_db.expire_at,
num_of_seats=licensed_item_purchase_db.num_of_seats,
purchased_by_user=licensed_item_purchase_db.purchased_by_user,
user_email=licensed_item_purchase_db.user_email,
purchased_at=licensed_item_purchase_db.purchased_at,
modified=licensed_item_purchase_db.modified,
)
Expand All @@ -120,6 +122,7 @@ async def create_licensed_item_purchase(
expire_at=data.expire_at,
num_of_seats=data.num_of_seats,
purchased_by_user=data.purchased_by_user,
user_email=data.user_email,
purchased_at=data.purchased_at,
)

Expand Down Expand Up @@ -169,6 +172,7 @@ async def create_licensed_item_purchase(
expire_at=licensed_item_purchase_db.expire_at,
num_of_seats=licensed_item_purchase_db.num_of_seats,
purchased_by_user=licensed_item_purchase_db.purchased_by_user,
user_email=licensed_item_purchase_db.user_email,
purchased_at=licensed_item_purchase_db.purchased_at,
modified=licensed_item_purchase_db.modified,
)
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ async def batch_update_credit_transaction_status_for_in_debt_transactions(
)
async with transaction_context(engine, connection) as conn:
result = await conn.execute(update_stmt)
print(result)
if result.rowcount:
_logger.info(
f"Wallet {wallet_id} and project {project_id} transactions in DEBT were changed to BILLED. Num. of transaction {result.rowcount}"
)


async def sum_wallet_credits(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
resource_tracker_licensed_items_purchases.c.expire_at,
resource_tracker_licensed_items_purchases.c.num_of_seats,
resource_tracker_licensed_items_purchases.c.purchased_by_user,
resource_tracker_licensed_items_purchases.c.user_email,
resource_tracker_licensed_items_purchases.c.purchased_at,
resource_tracker_licensed_items_purchases.c.modified,
)
Expand Down Expand Up @@ -66,6 +67,7 @@ async def create(
expire_at=data.expire_at,
num_of_seats=data.num_of_seats,
purchased_by_user=data.purchased_by_user,
user_email=data.user_email,
purchased_at=data.purchased_at,
modified=sa.func.now(),
)
Expand Down
Loading
Loading