Repository navigation
Expand file tree
/
Copy pathexample_latency.py
More file actions
129 lines (102 loc) · 4.34 KB
/
Copy pathexample_latency.py
File metadata and controls
129 lines (102 loc) · 4.34 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
#!/usr/bin/env python3
"""
TESAIoT BDH AI Analytics - Latency Statistics Example
This example demonstrates how to:
1. Get network latency statistics for devices
2. Analyze latency trends over time
3. Identify devices with high latency
API Endpoint: GET /api/v1/bdh-ai/connectivity/latency
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_latency_stats(hours: int = 24) -> dict:
"""
Get network latency statistics.
Args:
hours: Time range in hours (default: 24)
Returns:
dict with latency statistics and per-device breakdown
"""
async with httpx.AsyncClient(verify=False, timeout=30.0) as client:
response = await client.get(
f"{API_BASE_URL}/connectivity/latency",
params={"hours": hours},
headers={"X-API-KEY": API_TOKEN}
)
response.raise_for_status()
return response.json()
def print_latency_report(data: dict):
"""Print formatted latency report"""
print("\n" + "=" * 60)
print(" TESAIoT BDH AI - Network Latency Report")
print("=" * 60)
# Summary statistics
if "summary" in data:
summary = data["summary"]
print("\n📊 Summary Statistics:")
print(f" • Average Latency: {summary.get('avg_latency_ms', 0):.2f} ms")
print(f" • Min Latency: {summary.get('min_latency_ms', 0):.2f} ms")
print(f" • Max Latency: {summary.get('max_latency_ms', 0):.2f} ms")
print(f" • P95 Latency: {summary.get('p95_latency_ms', 0):.2f} ms")
print(f" • P99 Latency: {summary.get('p99_latency_ms', 0):.2f} ms")
print(f" • Total Samples: {summary.get('total_samples', 0)}")
# Per-device breakdown
if "devices" in data and data["devices"]:
print("\n📱 Per-Device Latency:")
print("-" * 60)
print(f"{'Device ID':<40} {'Avg (ms)':<10} {'Status':<10}")
print("-" * 60)
for device in data["devices"][:10]: # Top 10
device_id = device.get("device_id", "Unknown")[:36]
avg_latency = device.get("avg_latency_ms", 0)
status = "🟢 Good" if avg_latency < 100 else "🟡 Fair" if avg_latency < 500 else "🔴 High"
print(f"{device_id:<40} {avg_latency:<10.2f} {status:<10}")
# High latency alerts
if "high_latency_devices" in data and data["high_latency_devices"]:
print("\n⚠️ High Latency Alerts:")
for device in data["high_latency_devices"]:
print(f" • {device['device_id']}: {device['avg_latency_ms']:.2f} ms")
# Latency timeline (if available)
if "timeline" in data and data["timeline"]:
print("\n📈 Latency Timeline (Last 6 hours):")
for point in data["timeline"][-6:]:
timestamp = point.get("timestamp", "")[:16]
avg = point.get("avg_latency_ms", 0)
bar = "█" * int(avg / 10) if avg < 500 else "█" * 50 + "+"
print(f" {timestamp}: {bar} ({avg:.0f} ms)")
print("\n" + "=" * 60)
print(f"Generated at: {datetime.utcnow().isoformat()}Z")
print("=" * 60 + "\n")
async def main():
"""Main function"""
print("Fetching latency statistics from TESAIoT Platform...")
if not API_TOKEN:
print("[ERROR] API token not set. Set TESAIOT_API_TOKEN environment variable.")
return
try:
# Get latency stats for last 24 hours
data = await get_latency_stats(hours=24)
print_latency_report(data)
# Also try 1-hour stats for recent view
print("\nFetching 1-hour stats for recent activity...")
recent_data = await get_latency_stats(hours=1)
if recent_data.get("summary"):
recent_summary = recent_data["summary"]
print(f"📊 Last Hour: Avg={recent_summary.get('avg_latency_ms', 0):.2f}ms, "
f"Samples={recent_summary.get('total_samples', 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())