Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
singlestore-labs
GitHub Repository: singlestore-labs/singlestoredb-python
Path: blob/main/singlestoredb/management/billing_usage.py
469 views
1
#!/usr/bin/env python
2
"""SingleStoreDB Cloud Billing Usage."""
3
import datetime
4
from typing import Any
5
from typing import Dict
6
from typing import List
7
from typing import Optional
8
9
from .manager import Manager
10
from .utils import camel_to_snake
11
from .utils import vars_to_str
12
13
14
class UsageItem(object):
15
"""Usage statistics."""
16
17
def __init__(
18
self,
19
start_time: datetime.datetime,
20
end_time: datetime.datetime,
21
owner_id: str,
22
resource_id: str,
23
resource_name: str,
24
resource_type: str,
25
value: str,
26
):
27
#: Starting time for the usage duration
28
self.start_time = start_time
29
30
#: Ending time for the usage duration
31
self.end_time = end_time
32
33
#: Owner ID
34
self.owner_id = owner_id
35
36
#: Resource ID
37
self.resource_id = resource_id
38
39
#: Resource name
40
self.resource_name = resource_name
41
42
#: Resource type
43
self.resource_type = resource_type
44
45
#: Usage statistic value
46
self.value = value
47
48
self._manager: Optional[Manager] = None
49
50
def __str__(self) -> str:
51
"""Return string representation."""
52
return vars_to_str(self)
53
54
def __repr__(self) -> str:
55
"""Return string representation."""
56
return str(self)
57
58
@classmethod
59
def from_dict(
60
cls,
61
obj: Dict[str, Any],
62
manager: Manager,
63
) -> 'UsageItem':
64
"""
65
Convert dictionary to a ``UsageItem`` object.
66
67
Parameters
68
----------
69
obj : dict
70
Key-value pairs to retrieve billling usage information from
71
manager : WorkspaceManager, optional
72
The WorkspaceManager the UsageItem belongs to
73
74
Returns
75
-------
76
:class:`UsageItem`
77
78
"""
79
out = cls(
80
end_time=datetime.datetime.fromisoformat(obj['endTime']),
81
start_time=datetime.datetime.fromisoformat(obj['startTime']),
82
owner_id=obj['ownerId'],
83
resource_id=obj['resourceId'],
84
resource_name=obj['resourceName'],
85
resource_type=obj['resource_type'],
86
value=obj['value'],
87
)
88
out._manager = manager
89
return out
90
91
92
class BillingUsageItem(object):
93
"""Billing usage item."""
94
95
def __init__(
96
self,
97
description: str,
98
metric: str,
99
usage: List[UsageItem],
100
):
101
"""Use :attr:`WorkspaceManager.billing.usage` instead."""
102
#: Description of the usage metric
103
self.description = description
104
105
#: Name of the usage metric
106
self.metric = metric
107
108
#: Usage statistics
109
self.usage = list(usage)
110
111
self._manager: Optional[Manager] = None
112
113
def __str__(self) -> str:
114
"""Return string representation."""
115
return vars_to_str(self)
116
117
def __repr__(self) -> str:
118
"""Return string representation."""
119
return str(self)
120
121
@ classmethod
122
def from_dict(
123
cls,
124
obj: Dict[str, Any],
125
manager: Manager,
126
) -> 'BillingUsageItem':
127
"""
128
Convert dictionary to a ``BillingUsageItem`` object.
129
130
Parameters
131
----------
132
obj : dict
133
Key-value pairs to retrieve billling usage information from
134
manager : WorkspaceManager, optional
135
The WorkspaceManager the BillingUsageItem belongs to
136
137
Returns
138
-------
139
:class:`BillingUsageItem`
140
141
"""
142
out = cls(
143
description=obj['description'],
144
metric=str(camel_to_snake(obj['metric'])),
145
usage=[UsageItem.from_dict(x, manager) for x in obj['Usage']],
146
)
147
out._manager = manager
148
return out
149
150