Path: blob/main/singlestoredb/management/billing_usage.py
469 views
#!/usr/bin/env python1"""SingleStoreDB Cloud Billing Usage."""2import datetime3from typing import Any4from typing import Dict5from typing import List6from typing import Optional78from .manager import Manager9from .utils import camel_to_snake10from .utils import vars_to_str111213class UsageItem(object):14"""Usage statistics."""1516def __init__(17self,18start_time: datetime.datetime,19end_time: datetime.datetime,20owner_id: str,21resource_id: str,22resource_name: str,23resource_type: str,24value: str,25):26#: Starting time for the usage duration27self.start_time = start_time2829#: Ending time for the usage duration30self.end_time = end_time3132#: Owner ID33self.owner_id = owner_id3435#: Resource ID36self.resource_id = resource_id3738#: Resource name39self.resource_name = resource_name4041#: Resource type42self.resource_type = resource_type4344#: Usage statistic value45self.value = value4647self._manager: Optional[Manager] = None4849def __str__(self) -> str:50"""Return string representation."""51return vars_to_str(self)5253def __repr__(self) -> str:54"""Return string representation."""55return str(self)5657@classmethod58def from_dict(59cls,60obj: Dict[str, Any],61manager: Manager,62) -> 'UsageItem':63"""64Convert dictionary to a ``UsageItem`` object.6566Parameters67----------68obj : dict69Key-value pairs to retrieve billling usage information from70manager : WorkspaceManager, optional71The WorkspaceManager the UsageItem belongs to7273Returns74-------75:class:`UsageItem`7677"""78out = cls(79end_time=datetime.datetime.fromisoformat(obj['endTime']),80start_time=datetime.datetime.fromisoformat(obj['startTime']),81owner_id=obj['ownerId'],82resource_id=obj['resourceId'],83resource_name=obj['resourceName'],84resource_type=obj['resource_type'],85value=obj['value'],86)87out._manager = manager88return out899091class BillingUsageItem(object):92"""Billing usage item."""9394def __init__(95self,96description: str,97metric: str,98usage: List[UsageItem],99):100"""Use :attr:`WorkspaceManager.billing.usage` instead."""101#: Description of the usage metric102self.description = description103104#: Name of the usage metric105self.metric = metric106107#: Usage statistics108self.usage = list(usage)109110self._manager: Optional[Manager] = None111112def __str__(self) -> str:113"""Return string representation."""114return vars_to_str(self)115116def __repr__(self) -> str:117"""Return string representation."""118return str(self)119120@ classmethod121def from_dict(122cls,123obj: Dict[str, Any],124manager: Manager,125) -> 'BillingUsageItem':126"""127Convert dictionary to a ``BillingUsageItem`` object.128129Parameters130----------131obj : dict132Key-value pairs to retrieve billling usage information from133manager : WorkspaceManager, optional134The WorkspaceManager the BillingUsageItem belongs to135136Returns137-------138:class:`BillingUsageItem`139140"""141out = cls(142description=obj['description'],143metric=str(camel_to_snake(obj['metric'])),144usage=[UsageItem.from_dict(x, manager) for x in obj['Usage']],145)146out._manager = manager147return out148149150