Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 27 additions & 0 deletions src/aleph/db/accessors/balances.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,30 @@ def count_address_credit_history(
)

return session.execute(query).scalar_one()


def get_resource_consumed_credits(
session: DbSession,
item_hash: str,
) -> int:
"""
Calculate the total credits consumed by a specific resource.

Aggregates all credit_history entries where:
- payment_method = 'credit_expense'
- origin = item_hash (the resource identifier)

Args:
session: Database session
item_hash: The item hash of the resource (message hash)

Returns:
Total credits consumed by the resource
"""
query = select(func.sum(func.abs(AlephCreditHistoryDb.amount))).where(
(AlephCreditHistoryDb.payment_method == "credit_expense")
& (AlephCreditHistoryDb.origin == item_hash)
)

result = session.execute(query).scalar()
return result or 0
5 changes: 5 additions & 0 deletions src/aleph/schemas/api/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,8 @@ class GetAccountCreditHistoryResponse(BaseModel):
pagination_page: int
pagination_total: int
pagination_per_page: int


class GetResourceConsumedCreditsResponse(BaseModel):
item_hash: str
consumed_credits: int
30 changes: 30 additions & 0 deletions src/aleph/web/controllers/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
get_balances_by_chain,
get_credit_balance,
get_credit_balances,
get_resource_consumed_credits,
get_total_detailed_balance,
)
from aleph.db.accessors.cost import get_total_cost_for_address
Expand All @@ -33,6 +34,7 @@
GetAccountQueryParams,
GetBalancesChainsQueryParams,
GetCreditBalancesQueryParams,
GetResourceConsumedCreditsResponse,
)
from aleph.types.db_session import DbSessionFactory
from aleph.web.controllers.app_state_getters import get_session_factory_from_request
Expand Down Expand Up @@ -84,6 +86,13 @@ def _get_chain_from_request(request: web.Request) -> str:
return chain


def _get_item_hash_from_request(request: web.Request) -> str:
item_hash = request.match_info.get("item_hash")
if item_hash is None:
raise web.HTTPUnprocessableEntity(text="Item hash must be specified.")
return item_hash


async def get_account_balance(request: web.Request):
address = _get_address_from_request(request)

Expand Down Expand Up @@ -268,3 +277,24 @@ async def get_account_credit_history(request: web.Request) -> web.Response:
)

return web.json_response(text=response.model_dump_json())


async def get_resource_consumed_credits_controller(
request: web.Request,
) -> web.Response:
"""Returns the total credits consumed by a specific resource (item_hash)."""
item_hash = _get_item_hash_from_request(request)

session_factory: DbSessionFactory = get_session_factory_from_request(request)

with session_factory() as session:
consumed_credits = get_resource_consumed_credits(
session=session, item_hash=item_hash
)

response = GetResourceConsumedCreditsResponse(
item_hash=item_hash,
consumed_credits=consumed_credits,
)

return web.json_response(text=response.model_dump_json())
4 changes: 4 additions & 0 deletions src/aleph/web/controllers/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def register_routes(app: web.Application):
"/api/v0/addresses/{address}/credit_history",
accounts.get_account_credit_history,
)
app.router.add_get(
"/api/v0/messages/{item_hash}/consumed_credits",
accounts.get_resource_consumed_credits_controller,
)

app.router.add_post("/api/v0/ipfs/add_json", storage.add_ipfs_json_controller)
app.router.add_post("/api/v0/storage/add_json", storage.add_storage_json_controller)
Expand Down
Loading