Repository navigation
Expand file tree
/
Copy pathexample_throughput.py
More file actions
150 lines (119 loc) · 4.96 KB
/
Copy pathexample_throughput.py
File metadata and controls
150 lines (119 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
"""
TESAIoT BDH AI Analytics - Throughput Statistics Example
This example demonstrates how to:
1. Get message throughput statistics
2. Analyze throughput by topic
3. Identify traffic patterns over time
API Endpoint: GET /api/v1/bdh-ai/connectivity/throughput
Author: TESAIoT Team
License: Apache 2.0
"""
import os
import asyncio
import httpx
from dotenv import load_dotenv
from datetime import datetime
# Load environment
load_dotenv()
# Configuration
API_BASE_URL = os.getenv("TESAIOT_API_URL", "https://admin.tesaiot.com/api/v1/bdh-ai")
API_TOKEN = os.getenv("TESAIOT_API_TOKEN", "")
async def get_throughput_stats(hours: int = 24) -> dict:
"""
Get message throughput statistics.
Args:
hours: Time range in hours (default: 24)
Returns:
dict with throughput statistics, timeline, and topic breakdown
"""
async with httpx.AsyncClient(verify=False, timeout=30.0) as client:
response = await client.get(
f"{API_BASE_URL}/connectivity/throughput",
params={"hours": hours},
headers={"X-API-KEY": API_TOKEN}
)
response.raise_for_status()
return response.json()
def format_bytes(bytes_value: int) -> str:
"""Format bytes to human-readable string"""
for unit in ['B', 'KB', 'MB', 'GB']:
if bytes_value < 1024:
return f"{bytes_value:.2f} {unit}"
bytes_value /= 1024
return f"{bytes_value:.2f} TB"
def print_throughput_report(data: dict):
"""Print formatted throughput report"""
print("\n" + "=" * 60)
print(" TESAIoT BDH AI - Message Throughput Report")
print("=" * 60)
# Summary statistics
if "summary" in data:
summary = data["summary"]
print("\n📊 Summary Statistics:")
print(f" • Total Messages: {summary.get('total_messages', 0):,}")
print(f" • Total Bytes: {format_bytes(summary.get('total_bytes', 0))}")
print(f" • Messages/Hour: {summary.get('messages_per_hour', 0):,.0f}")
print(f" • Bytes/Hour: {format_bytes(summary.get('bytes_per_hour', 0))}")
print(f" • Peak Messages/Min: {summary.get('peak_messages_per_min', 0):,}")
print(f" • Active Devices: {summary.get('active_devices', 0)}")
# By topic breakdown
if "by_topic" in data and data["by_topic"]:
print("\n📬 Throughput by Topic:")
print("-" * 60)
print(f"{'Topic':<30} {'Messages':<12} {'Bytes':<15}")
print("-" * 60)
for topic in data["by_topic"][:10]: # Top 10 topics
topic_name = topic.get("topic", "Unknown")
# Truncate long topic names
if len(topic_name) > 28:
topic_name = topic_name[:25] + "..."
messages = topic.get("message_count", 0)
bytes_val = topic.get("total_bytes", 0)
print(f"{topic_name:<30} {messages:<12,} {format_bytes(bytes_val):<15}")
# By device breakdown
if "by_device" in data and data["by_device"]:
print("\n📱 Top 5 Active Devices:")
print("-" * 60)
print(f"{'Device ID':<40} {'Messages':<12}")
print("-" * 60)
for device in data["by_device"][:5]:
device_id = device.get("device_id", "Unknown")[:36]
messages = device.get("message_count", 0)
print(f"{device_id:<40} {messages:<12,}")
# Timeline visualization
if "timeline" in data and data["timeline"]:
print("\n📈 Throughput Timeline (Last 6 hours):")
max_msgs = max(p.get("message_count", 1) for p in data["timeline"][-6:])
for point in data["timeline"][-6:]:
timestamp = point.get("timestamp", "")[:16]
msgs = point.get("message_count", 0)
bar_len = int((msgs / max_msgs) * 40) if max_msgs > 0 else 0
bar = "█" * bar_len
print(f" {timestamp}: {bar} ({msgs:,} msgs)")
print("\n" + "=" * 60)
print(f"Generated at: {datetime.utcnow().isoformat()}Z")
print("=" * 60 + "\n")
async def main():
"""Main function"""
print("Fetching throughput statistics from TESAIoT Platform...")
if not API_TOKEN:
print("[ERROR] API token not set. Set TESAIOT_API_TOKEN environment variable.")
return
try:
# Get throughput stats for last 24 hours
data = await get_throughput_stats(hours=24)
print_throughput_report(data)
# Compare with 1-hour stats
print("\nFetching 1-hour stats for recent activity...")
recent_data = await get_throughput_stats(hours=1)
if recent_data.get("summary"):
recent = recent_data["summary"]
print(f"📊 Last Hour: {recent.get('total_messages', 0):,} messages, "
f"{format_bytes(recent.get('total_bytes', 0))}")
except httpx.HTTPStatusError as e:
print(f"[ERROR] HTTP {e.response.status_code}: {e.response.text}")
except Exception as e:
print(f"[ERROR] {type(e).__name__}: {e}")
if __name__ == "__main__":
asyncio.run(main())