Repository navigation
Expand file tree
/
Copy pathanalytics_client.py
More file actions
614 lines (503 loc) · 17.1 KB
/
Copy pathanalytics_client.py
File metadata and controls
614 lines (503 loc) · 17.1 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
"""
TESAIoT BDH AI Analytics API Client
A Python client library for accessing the TESAIoT Analytics API.
Usage:
from analytics_client import AnalyticsClient
client = AnalyticsClient(
base_url="https://admin.tesaiot.com/api/v1/bdh-ai",
api_token="your_jwt_token"
)
# Get anomalies
anomalies = await client.get_anomalies()
Author: TESAIoT Team
License: Apache 2.0
"""
import os
import asyncio
from datetime import datetime, timedelta
from typing import Optional, List, Dict, Any
from dataclasses import dataclass, field
import httpx
from pydantic import BaseModel
# ============================================================
# Data Models
# ============================================================
@dataclass
class TimeRange:
"""Time range for queries"""
start: str
end: str
@classmethod
def last_days(cls, days: int) -> "TimeRange":
"""Create time range for last N days"""
end = datetime.utcnow()
start = end - timedelta(days=days)
return cls(
start=start.isoformat() + "Z",
end=end.isoformat() + "Z"
)
@dataclass
class Anomaly:
"""Anomaly data"""
id: str
device_id: str
device_name: str
metric: str
value: float
severity: str
score: float
timestamp: str
acknowledged: bool = False
resolved: bool = False
@dataclass
class Cluster:
"""Device cluster data"""
cluster_id: int
cluster_name: str
device_count: int
characteristics: Dict[str, Any]
devices: List[str]
@dataclass
class Insight:
"""Generated insight"""
id: str
type: str
severity: str
title: str
description: str
confidence: float
actionable: bool
recommended_actions: List[str] = field(default_factory=list)
@dataclass
class DeviceStatus:
"""Device connectivity status"""
device_id: str
device_name: str
status: str # online/offline
last_seen: str
uptime_percent: float
# ============================================================
# Analytics Client
# ============================================================
class AnalyticsClient:
"""
TESAIoT BDH AI Analytics API Client
Example:
client = AnalyticsClient(
base_url="https://admin.tesaiot.com/api/v1/bdh-ai",
api_token="your_token"
)
anomalies = await client.get_anomalies()
"""
def __init__(
self,
base_url: Optional[str] = None,
api_token: Optional[str] = None,
timeout: float = 30.0
):
"""
Initialize the Analytics client.
Args:
base_url: API base URL (default: from TESAIOT_API_URL env)
api_token: JWT authentication token (default: from TESAIOT_API_TOKEN env)
timeout: Request timeout in seconds
"""
self.base_url = base_url or os.getenv(
"TESAIOT_API_URL",
"https://admin.tesaiot.com/api/v1/bdh-ai"
)
self.api_token = api_token or os.getenv("TESAIOT_API_TOKEN")
self.timeout = timeout
if not self.api_token:
raise ValueError("API token required. Set TESAIOT_API_TOKEN or pass api_token")
self._client = httpx.AsyncClient(
base_url=self.base_url,
headers={
"X-API-KEY": self.api_token,
"Content-Type": "application/json"
},
timeout=timeout,
verify=False # Skip TLS verification for testing
)
async def close(self):
"""Close the HTTP client"""
await self._client.aclose()
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.close()
# --------------------------------------------------------
# Anomaly Detection APIs
# --------------------------------------------------------
async def get_anomalies(
self,
start_time: Optional[str] = None,
end_time: Optional[str] = None,
severity_filter: Optional[List[str]] = None,
device_ids: Optional[List[str]] = None,
limit: int = 100,
offset: int = 0
) -> Dict[str, Any]:
"""
Get aggregated anomalies.
Args:
start_time: Start of time range (ISO 8601)
end_time: End of time range (ISO 8601)
severity_filter: Filter by severity levels
device_ids: Filter by specific devices
limit: Maximum results to return
offset: Pagination offset
Returns:
Dict with anomalies data and summary
"""
# Default to last 7 days
if not start_time or not end_time:
time_range = TimeRange.last_days(7)
start_time = start_time or time_range.start
end_time = end_time or time_range.end
payload = {
"time_range": {"start": start_time, "end": end_time},
"limit": limit,
"offset": offset
}
if severity_filter:
payload["severity_filter"] = severity_filter
if device_ids:
payload["device_ids"] = device_ids
response = await self._client.get("/analytics/anomalies", params=payload)
response.raise_for_status()
return response.json()
async def get_anomaly_timeline(
self,
days: int = 30,
group_by: str = "severity"
) -> Dict[str, Any]:
"""
Get anomaly timeline for trend visualization.
Args:
days: Number of days to include
group_by: Grouping field (severity, device, metric)
Returns:
Dict with timeline data and trend info
"""
payload = {"days": days, "group_by": group_by}
response = await self._client.post("/analytics/anomalies/timeline", json=payload)
response.raise_for_status()
return response.json()
async def get_anomaly_heatmap(
self,
start_time: str,
end_time: str,
resolution: str = "hour",
max_devices: int = 50
) -> Dict[str, Any]:
"""
Get device × time heatmap data.
Args:
start_time: Start of time range
end_time: End of time range
resolution: Time resolution (hour, day)
max_devices: Maximum devices to include
Returns:
Dict with heatmap matrix data
"""
payload = {
"time_range": {"start": start_time, "end": end_time},
"resolution": resolution,
"max_devices": max_devices
}
response = await self._client.post("/analytics/anomalies/heatmap", json=payload)
response.raise_for_status()
return response.json()
async def acknowledge_anomaly(
self,
anomaly_id: str,
notes: Optional[str] = None
) -> Dict[str, Any]:
"""
Mark an anomaly as acknowledged.
Args:
anomaly_id: The anomaly ID
notes: Optional notes
Returns:
Updated anomaly data
"""
payload = {"acknowledged": True}
if notes:
payload["notes"] = notes
response = await self._client.put(
f"/analytics/anomalies/{anomaly_id}/acknowledge",
json=payload
)
response.raise_for_status()
return response.json()
async def resolve_anomaly(
self,
anomaly_id: str,
resolution: str = "resolved",
notes: Optional[str] = None
) -> Dict[str, Any]:
"""
Mark an anomaly as resolved.
Args:
anomaly_id: The anomaly ID
resolution: Resolution type (resolved, false_positive, deferred)
notes: Optional notes
Returns:
Updated anomaly data
"""
payload = {"resolved": True, "resolution": resolution}
if notes:
payload["notes"] = notes
response = await self._client.put(
f"/analytics/anomalies/{anomaly_id}/resolve",
json=payload
)
response.raise_for_status()
return response.json()
# --------------------------------------------------------
# Pattern Recognition APIs
# --------------------------------------------------------
async def get_clusters(
self,
metric_name: str,
n_clusters: int = 5,
start_time: Optional[str] = None,
end_time: Optional[str] = None,
include_outliers: bool = True
) -> Dict[str, Any]:
"""
Get K-means clustering of device behavior patterns.
Args:
metric_name: Metric to cluster (e.g., temperature)
n_clusters: Number of clusters
start_time: Start of analysis period
end_time: End of analysis period
include_outliers: Include outlier detection
Returns:
Dict with clusters, silhouette score, and outliers
"""
if not start_time or not end_time:
time_range = TimeRange.last_days(7)
start_time = start_time or time_range.start
end_time = end_time or time_range.end
payload = {
"metric_name": metric_name,
"n_clusters": n_clusters,
"time_range": {"start": start_time, "end": end_time},
"include_outliers": include_outliers
}
response = await self._client.post("/patterns/clusters", json=payload)
response.raise_for_status()
return response.json()
async def get_outliers(
self,
metric_name: str,
threshold: float = 0.7
) -> Dict[str, Any]:
"""
Get outlier devices that don't fit patterns.
Args:
metric_name: Metric to analyze
threshold: Outlier score threshold
Returns:
Dict with outlier devices
"""
payload = {"metric_name": metric_name, "threshold": threshold}
response = await self._client.post("/patterns/outliers", json=payload)
response.raise_for_status()
return response.json()
async def find_similar_devices(
self,
device_id: str,
metric_name: str,
top_k: int = 10
) -> Dict[str, Any]:
"""
Find devices with similar behavior patterns.
Args:
device_id: Source device ID
metric_name: Metric to compare
top_k: Number of similar devices to return
Returns:
Dict with source device features and similar devices
"""
payload = {
"device_id": device_id,
"metric_name": metric_name,
"top_k": top_k
}
response = await self._client.post("/patterns/similar", json=payload)
response.raise_for_status()
return response.json()
# --------------------------------------------------------
# Insights APIs
# --------------------------------------------------------
async def get_insights(
self,
days: int = 7,
insight_types: Optional[List[str]] = None,
min_confidence: float = 0.7
) -> Dict[str, Any]:
"""
Generate AI-powered insights.
Args:
days: Analysis period in days
insight_types: Filter by type (anomaly_pattern, trend, correlation, recommendation)
min_confidence: Minimum confidence score
Returns:
Dict with fleet summary, insights, and recommendations
"""
payload = {
"analysis_period_days": days,
"min_confidence": min_confidence
}
if insight_types:
payload["insight_types"] = insight_types
response = await self._client.post("/insights", json=payload)
response.raise_for_status()
return response.json()
async def get_recommendations(
self,
priority: Optional[str] = None,
limit: int = 10
) -> Dict[str, Any]:
"""
Get actionable recommendations.
Args:
priority: Filter by priority (high, medium, low)
limit: Maximum recommendations
Returns:
Dict with recommendations list
"""
params = {"limit": limit}
if priority:
params["priority"] = priority
response = await self._client.get("/insights/recommendations", params=params)
response.raise_for_status()
return response.json()
# --------------------------------------------------------
# Connectivity APIs
# --------------------------------------------------------
async def get_connectivity_status(
self,
status_filter: Optional[str] = None
) -> Dict[str, Any]:
"""
Get real-time device connectivity status.
Args:
status_filter: Filter by status (online, offline)
Returns:
Dict with device status list and summary
"""
params = {}
if status_filter:
params["status"] = status_filter
response = await self._client.get("/connectivity/status", params=params)
response.raise_for_status()
return response.json()
async def get_latency_stats(
self,
hours: int = 24
) -> Dict[str, Any]:
"""
Get network latency statistics.
Args:
hours: Time range in hours
Returns:
Dict with latency statistics and heatmap
"""
params = {"hours": hours}
response = await self._client.get("/connectivity/latency", params=params)
response.raise_for_status()
return response.json()
async def get_throughput_stats(
self,
hours: int = 24
) -> Dict[str, Any]:
"""
Get message throughput statistics.
Args:
hours: Time range in hours
Returns:
Dict with throughput timeline and by-topic breakdown
"""
params = {"hours": hours}
response = await self._client.get("/connectivity/throughput", params=params)
response.raise_for_status()
return response.json()
async def get_connection_quality(self) -> Dict[str, Any]:
"""
Get connection quality scores for all devices.
Returns:
Dict with quality scores and issues
"""
response = await self._client.get("/connectivity/quality")
response.raise_for_status()
return response.json()
# --------------------------------------------------------
# Fleet Analytics APIs (NEW - 2025-12-30)
# --------------------------------------------------------
async def get_fleet_anomalies(
self,
days: int = 7,
severity: Optional[str] = None,
limit: int = 100
) -> Dict[str, Any]:
"""
Get fleet-wide anomaly overview.
Args:
days: Number of days to analyze (default: 7)
severity: Filter by severity (critical, high, medium, low)
limit: Maximum number of anomalies to return
Returns:
Dict with anomalies, summary, and groupings
"""
params = {"days": days, "limit": limit}
if severity:
params["severity"] = severity
response = await self._client.get("/analytics/anomalies", params=params)
response.raise_for_status()
return response.json()
# ============================================================
# Synchronous Wrapper
# ============================================================
class AnalyticsClientSync:
"""
Synchronous wrapper for AnalyticsClient.
Use this if you don't need async/await.
"""
def __init__(self, *args, **kwargs):
self._async_client = AnalyticsClient(*args, **kwargs)
def _run(self, coro):
return asyncio.get_event_loop().run_until_complete(coro)
def get_anomalies(self, **kwargs):
return self._run(self._async_client.get_anomalies(**kwargs))
def get_anomaly_timeline(self, **kwargs):
return self._run(self._async_client.get_anomaly_timeline(**kwargs))
def get_clusters(self, **kwargs):
return self._run(self._async_client.get_clusters(**kwargs))
def get_insights(self, **kwargs):
return self._run(self._async_client.get_insights(**kwargs))
def get_connectivity_status(self, **kwargs):
return self._run(self._async_client.get_connectivity_status(**kwargs))
def get_latency_stats(self, **kwargs):
return self._run(self._async_client.get_latency_stats(**kwargs))
def get_throughput_stats(self, **kwargs):
return self._run(self._async_client.get_throughput_stats(**kwargs))
def get_fleet_anomalies(self, **kwargs):
return self._run(self._async_client.get_fleet_anomalies(**kwargs))
def close(self):
self._run(self._async_client.close())
# ============================================================
# CLI Usage
# ============================================================
if __name__ == "__main__":
import json
async def main():
async with AnalyticsClient() as client:
# Get anomalies
print("Fetching anomalies...")
anomalies = await client.get_anomalies(limit=5)
print(json.dumps(anomalies, indent=2))
asyncio.run(main())