meter-lib 0.0.3__py3-none-any.whl → 0.0.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of meter-lib might be problematic. Click here for more details.
meter_lib/__init__.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
from .core import
|
|
1
|
+
from .core import post_meter_usage, post_ai_meter_usage
|
meter_lib/core.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
from typing import Optional
|
|
1
3
|
import requests
|
|
2
4
|
|
|
3
5
|
def fetch_customer_id(tenant_id: str):
|
|
@@ -5,7 +7,7 @@ def fetch_customer_id(tenant_id: str):
|
|
|
5
7
|
Fetch the customer account for a given tenant_id.
|
|
6
8
|
Returns a dict with customer info, or None if not found/error.
|
|
7
9
|
"""
|
|
8
|
-
url = f"http://metering.metering.svc.cluster.local:8000/
|
|
10
|
+
url = f"http://metering.metering.svc.cluster.local:8000/v1alpha1/customer-accounts/tenant/{tenant_id}"
|
|
9
11
|
try:
|
|
10
12
|
response = requests.get(url, timeout=10)
|
|
11
13
|
response.raise_for_status()
|
|
@@ -35,16 +37,20 @@ def post_meter_usage(tenant_id: str, device_id: str, meter_id: str, total_usage:
|
|
|
35
37
|
print("No customer account available, skipping meter usage post")
|
|
36
38
|
return None
|
|
37
39
|
|
|
38
|
-
url = "
|
|
40
|
+
url = "https://stream.app.litewave.ai/topics/usage-events"
|
|
39
41
|
headers = {
|
|
40
|
-
"
|
|
41
|
-
"x-device-id": device_id,
|
|
42
|
-
"Content-Type": "application/json"
|
|
42
|
+
"Content-Type": "application/vnd.kafka.json.v2+json",
|
|
43
43
|
}
|
|
44
44
|
payload = {
|
|
45
|
-
"
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
"records": [
|
|
46
|
+
{
|
|
47
|
+
"value": {
|
|
48
|
+
"meter_id": meter_id,
|
|
49
|
+
"customer_id": customer_account["id"],
|
|
50
|
+
"total_usage": total_usage
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
]
|
|
48
54
|
}
|
|
49
55
|
|
|
50
56
|
try:
|
|
@@ -54,3 +60,53 @@ def post_meter_usage(tenant_id: str, device_id: str, meter_id: str, total_usage:
|
|
|
54
60
|
except requests.RequestException as e:
|
|
55
61
|
print(f" Error posting to {url}: {e}")
|
|
56
62
|
return None
|
|
63
|
+
|
|
64
|
+
def post_ai_meter_usage(tenant_id: str, device_id: str, meter_id: str, start_time:Optional[datetime]= None, end_time:Optional[datetime]=None) :
|
|
65
|
+
"""
|
|
66
|
+
Posts meter usage to the ai events API.
|
|
67
|
+
Uses tenant_id + device_id in headers and includes customer_id in payload.
|
|
68
|
+
"""
|
|
69
|
+
customer_account = fetch_customer_id(tenant_id)
|
|
70
|
+
if not customer_account:
|
|
71
|
+
print("No customer account available, skipping meter usage post")
|
|
72
|
+
return None
|
|
73
|
+
|
|
74
|
+
url = "https://stream.app.litewave.ai/topics/usage-events"
|
|
75
|
+
headers = {
|
|
76
|
+
"Content-Type": "application/vnd.kafka.json.v2+json",
|
|
77
|
+
}
|
|
78
|
+
if start_time:
|
|
79
|
+
payload = {
|
|
80
|
+
"records": [
|
|
81
|
+
{
|
|
82
|
+
"value": {
|
|
83
|
+
"ai_event": True,
|
|
84
|
+
"meter_id": meter_id,
|
|
85
|
+
"customer_id": customer_account["id"],
|
|
86
|
+
"start_time": start_time
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
]
|
|
90
|
+
}
|
|
91
|
+
else:
|
|
92
|
+
payload = {
|
|
93
|
+
"records": [
|
|
94
|
+
{
|
|
95
|
+
"value": {
|
|
96
|
+
"ai_event": True,
|
|
97
|
+
"meter_id": meter_id,
|
|
98
|
+
"customer_id": customer_account["id"],
|
|
99
|
+
"end_time": end_time
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
]
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
try:
|
|
106
|
+
response = requests.post(url, headers=headers, json=payload, timeout=10)
|
|
107
|
+
response.raise_for_status()
|
|
108
|
+
return response.json()
|
|
109
|
+
except requests.RequestException as e:
|
|
110
|
+
print(f" Error posting to {url}: {e}")
|
|
111
|
+
return None
|
|
112
|
+
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: meter-lib
|
|
3
|
+
Version: 0.0.4
|
|
4
|
+
Summary: A litewave library to collect the customer credit usage
|
|
5
|
+
Project-URL: Homepage, https://github.com/aiorch/meter-lib
|
|
6
|
+
Project-URL: Repository, https://github.com/aiorch/meter-lib
|
|
7
|
+
Project-URL: Issues, https://github.com/aiorch/meter-lib/issues
|
|
8
|
+
Author-email: Sruthi <sruthi@litewave.ai>
|
|
9
|
+
License: MIT
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Requires-Dist: requests>=2.28
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
## meter-lib — Usage Guide
|
|
20
|
+
|
|
21
|
+
### Overview
|
|
22
|
+
|
|
23
|
+
`meter-lib` is a lightweight helper library for sending metering events to the Litewave backend and for looking up a customer account by `tenant_id`.
|
|
24
|
+
|
|
25
|
+
### Requirements
|
|
26
|
+
|
|
27
|
+
- Python 3.10+
|
|
28
|
+
|
|
29
|
+
### Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install meter-lib
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Parameters Required
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
tenant_id,
|
|
39
|
+
device_id,
|
|
40
|
+
meter_id,
|
|
41
|
+
total_usage,
|
|
42
|
+
start_time,
|
|
43
|
+
end_time
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Quickstart
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from meter_lib import post_meter_usage
|
|
50
|
+
|
|
51
|
+
tenant_id = "tenant_123"
|
|
52
|
+
device_id = "us-east-ing1"
|
|
53
|
+
meter_id = "document.basic.page"
|
|
54
|
+
|
|
55
|
+
result = post_meter_usage(
|
|
56
|
+
tenant_id=tenant_id,
|
|
57
|
+
device_id=device_id,
|
|
58
|
+
meter_id=meter_id,
|
|
59
|
+
total_usage=24, # integer units as defined by your meter
|
|
60
|
+
)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### For AI enabled
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from meter_lib import post_meter_usage
|
|
67
|
+
|
|
68
|
+
tenant_id = "tenant_123"
|
|
69
|
+
device_id = "us-east-ing1"
|
|
70
|
+
meter_id = "chat.on.time_hours"
|
|
71
|
+
start_time = 1759791552000 # Timestamp in milliseconds
|
|
72
|
+
|
|
73
|
+
result = post_meter_usage(
|
|
74
|
+
tenant_id=tenant_id,
|
|
75
|
+
device_id=device_id,
|
|
76
|
+
meter_id=meter_id,
|
|
77
|
+
start_time= start_time # Timestamp in milliseconds
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### For AI disabled
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from meter_lib import post_meter_usage
|
|
86
|
+
|
|
87
|
+
tenant_id = "tenant_123"
|
|
88
|
+
device_id = "us-east-ing1"
|
|
89
|
+
meter_id = "chat.on.time_hours"
|
|
90
|
+
end_time = 1779799552000 # Timestamp in milliseconds
|
|
91
|
+
|
|
92
|
+
result = post_meter_usage(
|
|
93
|
+
tenant_id=tenant_id,
|
|
94
|
+
device_id=device_id,
|
|
95
|
+
meter_id=meter_id,
|
|
96
|
+
end_time= end_time # Timestamp in milliseconds
|
|
97
|
+
)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
if result is None:
|
|
102
|
+
print("Failed to post meter usage event")
|
|
103
|
+
else:
|
|
104
|
+
print("Event accepted:", result)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Error Handling
|
|
108
|
+
|
|
109
|
+
- `post_meter_usage` returns `None` for network errors or non-success HTTP statuses.
|
|
110
|
+
- Prefer explicit checks for `None` and add retries or backoff in your application layer if needed.
|
|
111
|
+
|
|
112
|
+
### API Reference
|
|
113
|
+
|
|
114
|
+
#### post_meter_usage(tenant_id: str, device_id: str, meter_id: str, total_usage: int) -> dict | None
|
|
115
|
+
|
|
116
|
+
- **Description**: Posts a metering event for a device and meter under a given tenant.
|
|
117
|
+
- **Headers**:
|
|
118
|
+
- `x-tenant-id`: the tenant identifier (string)
|
|
119
|
+
- `x-device-id`: the device identifier (string)
|
|
120
|
+
- **Payload (JSON)**:
|
|
121
|
+
- `meter_id` (string)
|
|
122
|
+
- `total_usage` (integer)
|
|
123
|
+
- `customer_id` (string) — auto-filled by the library.`
|
|
124
|
+
- **Returns**: The backend JSON response (`dict`) on success, otherwise `None`.
|
|
125
|
+
- **Timeout**: 10 seconds.
|
|
126
|
+
- **Notes**:
|
|
127
|
+
- If the customer lookup fails, the call is skipped and `None` is returned.
|
|
128
|
+
- This function is synchronous and will block until the request completes or times out.
|
|
129
|
+
|
|
130
|
+
### List Of Meters:
|
|
131
|
+
- meter_id: "page.processed.basic"
|
|
132
|
+
name: "Basic Document Scanning"
|
|
133
|
+
type: "volume"
|
|
134
|
+
description: "Total number of basic pages processed"
|
|
135
|
+
|
|
136
|
+
- meter_id: "page.processed.advanced"
|
|
137
|
+
name: "Advanced Document Scanning"
|
|
138
|
+
type: "volume"
|
|
139
|
+
description: "Total number of advanced pages processed"
|
|
140
|
+
|
|
141
|
+
- meter_id: "report.generated.small"
|
|
142
|
+
name: "Small Report Generation"
|
|
143
|
+
type: "volume"
|
|
144
|
+
description: "Total number of small reports generated"
|
|
145
|
+
|
|
146
|
+
- meter_id: "report.generated.medium"
|
|
147
|
+
name: "Medium Report Generation"
|
|
148
|
+
type: "volume"
|
|
149
|
+
description: "Total number of medium reports generated"
|
|
150
|
+
|
|
151
|
+
- meter_id: "report.generated.large"
|
|
152
|
+
name: "Large Report Generation"
|
|
153
|
+
type: "volume"
|
|
154
|
+
description: "Total number of large reports generated"
|
|
155
|
+
|
|
156
|
+
- meter_id: "report.generated.dataquery"
|
|
157
|
+
name: "Data Query Report Generation"
|
|
158
|
+
type: "volume"
|
|
159
|
+
description: "Total number of data query reports generated"
|
|
160
|
+
|
|
161
|
+
- meter_id: "report.generated.insights"
|
|
162
|
+
name: "Insights Report Generation"
|
|
163
|
+
type: "volume"
|
|
164
|
+
description: "Total number of insights reports generated"
|
|
165
|
+
|
|
166
|
+
- meter_id: "rule.executed.small"
|
|
167
|
+
name: "Small Rule Execution"
|
|
168
|
+
type: "volume"
|
|
169
|
+
description: "Total number of rules executed with runtime less than 1 minute"
|
|
170
|
+
|
|
171
|
+
- meter_id: "rule.executed.medium"
|
|
172
|
+
name: "Medium Rule Execution"
|
|
173
|
+
type: "volume"
|
|
174
|
+
description: "Total number of rules executed with runtime between 1 and 3 minutes"
|
|
175
|
+
|
|
176
|
+
- meter_id: "rule.executed.large"
|
|
177
|
+
name: "Large Rule Execution"
|
|
178
|
+
type: "volume"
|
|
179
|
+
description: "Total number of rules executed with runtime greater than 4 minutes"
|
|
180
|
+
|
|
181
|
+
- meter_id: "chat.on.time_hours"
|
|
182
|
+
name: "Litewave AI Assistant Usage (Hours)"
|
|
183
|
+
type: "performance"
|
|
184
|
+
description: "Total active chat usage time in hours"
|
|
185
|
+
|
|
186
|
+
- meter_id: "chat.query.time_secs"
|
|
187
|
+
name: "Litewave AI Assistant Query Time (Seconds)"
|
|
188
|
+
type: "performance"
|
|
189
|
+
description: "Total time spent per query in seconds"
|
|
190
|
+
|
|
191
|
+
- meter_id: "document.storage.size_gb"
|
|
192
|
+
name: "Document Storage Size"
|
|
193
|
+
type: "volume"
|
|
194
|
+
description: "Total document storage consumed in GB"
|
|
195
|
+
|
|
196
|
+
- meter_id: "template.processed.small"
|
|
197
|
+
name: "Small Template Processing"
|
|
198
|
+
type: "volume"
|
|
199
|
+
description: "Total number of small templates processed"
|
|
200
|
+
|
|
201
|
+
- meter_id: "template.processed.medium"
|
|
202
|
+
name: "Medium Template Processing"
|
|
203
|
+
type: "volume"
|
|
204
|
+
description: "Total number of medium templates processed"
|
|
205
|
+
|
|
206
|
+
- meter_id: "template.processed.large"
|
|
207
|
+
name: "Large Template Processing"
|
|
208
|
+
type: "volume"
|
|
209
|
+
description: "Total number of large templates processed"
|
|
210
|
+
|
|
211
|
+
- meter_id: "template.processed.count"
|
|
212
|
+
name: "Template Licensing Count"
|
|
213
|
+
type: "volume"
|
|
214
|
+
description: "Total number of licensed templates processed"
|
|
215
|
+
|
|
216
|
+
- meter_id: "template.licensed.total"
|
|
217
|
+
name: "Yearly Template Setup"
|
|
218
|
+
type: "volume"
|
|
219
|
+
description: "Total yearly template setup count"
|
|
220
|
+
|
|
221
|
+
### Troubleshooting
|
|
222
|
+
|
|
223
|
+
- Confirm your `tenant_id`, `device_id`, and `meter_id` values are correct.
|
|
224
|
+
|
|
225
|
+
### Support
|
|
226
|
+
|
|
227
|
+
- Homepage: `https://github.com/aiorch/meter-lib`
|
|
228
|
+
- Issues: `https://github.com/aiorch/meter-lib/issues`
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
meter_lib/__init__.py,sha256=cDltzQPWJ6jywyuwjevINEFQZ4AE9_ovQ5C2XGtNxgo,55
|
|
2
|
+
meter_lib/core.py,sha256=X_sKWoRfo0jzGTfIDvelBqTveYYcJaWw1QzvjUx7FZs,3585
|
|
3
|
+
meter_lib-0.0.4.dist-info/METADATA,sha256=2bMH7PbFPtQKX9F4f62OdPuqVg_ZEmjN9t0KtRJgcs8,6211
|
|
4
|
+
meter_lib-0.0.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
5
|
+
meter_lib-0.0.4.dist-info/RECORD,,
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: meter-lib
|
|
3
|
-
Version: 0.0.3
|
|
4
|
-
Summary: A litewave library to collect the customer credit usage
|
|
5
|
-
Project-URL: Homepage, https://github.com/aiorch/meter-lib
|
|
6
|
-
Project-URL: Repository, https://github.com/aiorch/meter-lib
|
|
7
|
-
Project-URL: Issues, https://github.com/aiorch/meter-lib/issues
|
|
8
|
-
Author-email: Sruthi <sruthi@litewave.ai>
|
|
9
|
-
License: MIT
|
|
10
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
-
Classifier: Programming Language :: Python :: 3
|
|
12
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
-
Requires-Python: >=3.10
|
|
16
|
-
Requires-Dist: requests>=2.28
|
|
17
|
-
Description-Content-Type: text/markdown
|
|
18
|
-
|
|
19
|
-
## meter-lib — Usage Guide
|
|
20
|
-
|
|
21
|
-
### Overview
|
|
22
|
-
|
|
23
|
-
`meter-lib` is a lightweight helper library for sending metering events to the Litewave backend and for looking up a customer account by `tenant_id`.
|
|
24
|
-
|
|
25
|
-
### Requirements
|
|
26
|
-
|
|
27
|
-
- Python 3.10+
|
|
28
|
-
|
|
29
|
-
### Installation
|
|
30
|
-
|
|
31
|
-
```bash
|
|
32
|
-
pip install meter-lib
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
### Parameters Required
|
|
36
|
-
|
|
37
|
-
```
|
|
38
|
-
tenant_id,
|
|
39
|
-
device_id,
|
|
40
|
-
meter_id,
|
|
41
|
-
total_usage
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
### Quickstart
|
|
45
|
-
|
|
46
|
-
```python
|
|
47
|
-
from meter_lib import post_meter_usage
|
|
48
|
-
|
|
49
|
-
tenant_id = "tenant_123"
|
|
50
|
-
device_id = "device_456"
|
|
51
|
-
meter_id = "pages.processed.rate.hourly"
|
|
52
|
-
|
|
53
|
-
result = post_meter_usage(
|
|
54
|
-
tenant_id=tenant_id,
|
|
55
|
-
device_id=device_id,
|
|
56
|
-
meter_id=meter_id,
|
|
57
|
-
total_usage=24, # integer units as defined by your meter
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
if result is None:
|
|
61
|
-
# Handle network error or non-2xx response
|
|
62
|
-
print("Failed to post meter usage event")
|
|
63
|
-
else:
|
|
64
|
-
print("Event accepted:", result)
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
### Error Handling
|
|
68
|
-
|
|
69
|
-
- `post_meter_usage` returns `None` for network errors or non-success HTTP statuses.
|
|
70
|
-
- Prefer explicit checks for `None` and add retries or backoff in your application layer if needed.
|
|
71
|
-
|
|
72
|
-
### API Reference
|
|
73
|
-
|
|
74
|
-
#### post_meter_usage(tenant_id: str, device_id: str, meter_id: str, total_usage: int) -> dict | None
|
|
75
|
-
|
|
76
|
-
- **Description**: Posts a metering event for a device and meter under a given tenant.
|
|
77
|
-
- **Headers**:
|
|
78
|
-
- `x-tenant-id`: the tenant identifier (string)
|
|
79
|
-
- `x-device-id`: the device identifier (string)
|
|
80
|
-
- **Payload (JSON)**:
|
|
81
|
-
- `meter_id` (string)
|
|
82
|
-
- `total_usage` (integer)
|
|
83
|
-
- `customer_id` (string) — auto-filled by the library.`
|
|
84
|
-
- **Returns**: The backend JSON response (`dict`) on success, otherwise `None`.
|
|
85
|
-
- **Timeout**: 10 seconds.
|
|
86
|
-
- **Notes**:
|
|
87
|
-
- If the customer lookup fails, the call is skipped and `None` is returned.
|
|
88
|
-
- This function is synchronous and will block until the request completes or times out.
|
|
89
|
-
|
|
90
|
-
### Troubleshooting
|
|
91
|
-
|
|
92
|
-
- Confirm your `tenant_id`, `device_id`, and `meter_id` values are correct.
|
|
93
|
-
|
|
94
|
-
### Support
|
|
95
|
-
|
|
96
|
-
- Homepage: `https://github.com/aiorch/meter-lib`
|
|
97
|
-
- Issues: `https://github.com/aiorch/meter-lib/issues`
|
meter_lib-0.0.3.dist-info/RECORD
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
meter_lib/__init__.py,sha256=3iRDH_E4qSKZVLaiJAs4YjoFC8jPIvH4sbgL6x4pAT8,53
|
|
2
|
-
meter_lib/core.py,sha256=Qi5OL4rg4iwweDTeBOljYrnSwU19SgU8naIA2cflGSo,1824
|
|
3
|
-
meter_lib-0.0.3.dist-info/METADATA,sha256=4GpY9I59GEyveMX3r7GLbzuFHBvOxDBWZk234C00M88,2716
|
|
4
|
-
meter_lib-0.0.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
5
|
-
meter_lib-0.0.3.dist-info/RECORD,,
|
|
File without changes
|