Repository navigation
Expand file tree
/
Copy pathexample_insights.py
More file actions
364 lines (285 loc) · 11.9 KB
/
Copy pathexample_insights.py
File metadata and controls
364 lines (285 loc) · 11.9 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
"""
AI Insights Examples for TESAIoT Analytics API
This example demonstrates:
- Generating AI-powered insights
- Filtering by insight type and confidence
- Actionable recommendations
- Fleet health summary
- Trend analysis and correlations
Author: TESAIoT Team
License: Apache 2.0
"""
import asyncio
import os
import json
from datetime import datetime
from analytics_client import AnalyticsClient
async def get_all_insights():
"""Get all AI-generated insights"""
print("\n" + "=" * 60)
print("All AI Insights (Last 7 Days)")
print("=" * 60)
async with AnalyticsClient() as client:
result = await client.get_insights(days=7)
# Fleet summary
summary = result.get("fleet_summary", {})
print("\n--- Fleet Summary ---")
print(f"Total Devices: {summary.get('total_devices', 0)}")
print(f"Active Devices: {summary.get('active_devices', 0)}")
print(f"Anomaly Rate: {summary.get('anomaly_rate', 0):.2%}")
print(f"Health Score: {summary.get('health_score', 0):.1f}/100")
# All insights
insights = result.get("insights", [])
print(f"\n--- {len(insights)} Insights Generated ---")
for insight in insights:
severity_icon = {
"critical": "!",
"warning": "!",
"info": "i"
}.get(insight.get("severity", "info"), "*")
print(f"\n[{severity_icon}] {insight['title']}")
print(f" Type: {insight['type']}")
print(f" Severity: {insight['severity']}")
print(f" Confidence: {insight['confidence']:.0%}")
print(f" {insight['description']}")
if insight.get("actionable"):
print(f" Recommended Actions:")
for action in insight.get("recommended_actions", []):
print(f" - {action}")
async def get_insights_by_type():
"""Filter insights by type"""
print("\n" + "=" * 60)
print("Insights by Type")
print("=" * 60)
insight_types = [
("anomaly_pattern", "Anomaly Patterns"),
("trend", "Trend Analysis"),
("correlation", "Correlations"),
("recommendation", "Recommendations")
]
async with AnalyticsClient() as client:
for type_id, type_name in insight_types:
result = await client.get_insights(
days=7,
insight_types=[type_id]
)
insights = result.get("insights", [])
print(f"\n{type_name}: {len(insights)} insights")
for insight in insights[:3]:
print(f" - {insight['title']}")
print(f" Confidence: {insight['confidence']:.0%}")
async def get_high_confidence_insights():
"""Get only high-confidence insights"""
print("\n" + "=" * 60)
print("High Confidence Insights (>80%)")
print("=" * 60)
async with AnalyticsClient() as client:
result = await client.get_insights(
days=7,
min_confidence=0.8
)
insights = result.get("insights", [])
print(f"\nFound {len(insights)} high-confidence insights")
for insight in insights:
print(f"\n[{insight['confidence']:.0%}] {insight['title']}")
print(f" {insight['description']}")
async def get_actionable_recommendations():
"""Get actionable recommendations"""
print("\n" + "=" * 60)
print("Actionable Recommendations")
print("=" * 60)
async with AnalyticsClient() as client:
# Get from insights endpoint
result = await client.get_insights(days=7)
actionable = [i for i in result.get("insights", [])
if i.get("actionable", False)]
print(f"\n{len(actionable)} Actionable Insights:")
for insight in actionable:
print(f"\n{insight['title']}")
print(f" Severity: {insight['severity']}")
print(f" Confidence: {insight['confidence']:.0%}")
print(f" Actions:")
for action in insight.get("recommended_actions", []):
print(f" [ ] {action}")
# Also get dedicated recommendations endpoint
print("\n--- Priority Recommendations ---")
recs = await client.get_recommendations(priority="high", limit=5)
for rec in recs.get("recommendations", []):
print(f"\n[HIGH] {rec.get('title', 'Untitled')}")
print(f" Impact: {rec.get('impact', 'N/A')}")
print(f" Effort: {rec.get('effort', 'N/A')}")
async def analyze_trends():
"""Analyze trends from insights"""
print("\n" + "=" * 60)
print("Trend Analysis")
print("=" * 60)
async with AnalyticsClient() as client:
result = await client.get_insights(
days=30,
insight_types=["trend"]
)
trends = result.get("insights", [])
# Categorize trends
increasing = [t for t in trends if "increasing" in t.get("description", "").lower()]
decreasing = [t for t in trends if "decreasing" in t.get("description", "").lower()]
stable = [t for t in trends if t not in increasing and t not in decreasing]
print(f"\nTrend Summary:")
print(f" Increasing trends: {len(increasing)}")
print(f" Decreasing trends: {len(decreasing)}")
print(f" Stable trends: {len(stable)}")
if increasing:
print(f"\n! Increasing Trends (Attention Needed):")
for t in increasing[:5]:
print(f" - {t['title']}")
if decreasing:
print(f"\n Good News - Decreasing Trends:")
for t in decreasing[:5]:
print(f" - {t['title']}")
async def analyze_correlations():
"""Analyze correlations from insights"""
print("\n" + "=" * 60)
print("Correlation Analysis")
print("=" * 60)
async with AnalyticsClient() as client:
result = await client.get_insights(
days=7,
insight_types=["correlation"]
)
correlations = result.get("insights", [])
print(f"\nFound {len(correlations)} correlations")
# Strong correlations (high confidence)
strong = [c for c in correlations if c.get("confidence", 0) > 0.8]
if strong:
print(f"\nStrong Correlations (>80% confidence):")
for corr in strong:
print(f"\n {corr['title']}")
print(f" {corr['description']}")
print(f" Confidence: {corr['confidence']:.0%}")
async def fleet_health_dashboard():
"""Generate fleet health dashboard data"""
print("\n" + "=" * 60)
print("Fleet Health Dashboard")
print("=" * 60)
print(f"Generated: {datetime.utcnow().isoformat()}Z")
async with AnalyticsClient() as client:
result = await client.get_insights(days=7)
summary = result.get("fleet_summary", {})
insights = result.get("insights", [])
# Health score visualization
health_score = summary.get("health_score", 0)
bar_length = int(health_score / 5)
health_bar = "" * bar_length + "" * (20 - bar_length)
print(f"\n Health Score: [{health_bar}] {health_score:.0f}/100")
# Status indicators
print(f"\n--- Status Indicators ---")
# Device status
total = summary.get("total_devices", 0)
active = summary.get("active_devices", 0)
device_pct = (active / total * 100) if total > 0 else 0
status = "OK" if device_pct > 90 else "WARN" if device_pct > 70 else "ALERT"
print(f"[{status}] Devices Online: {active}/{total} ({device_pct:.0f}%)")
# Anomaly rate
anomaly_rate = summary.get("anomaly_rate", 0)
status = "OK" if anomaly_rate < 0.05 else "WARN" if anomaly_rate < 0.1 else "ALERT"
print(f"[{status}] Anomaly Rate: {anomaly_rate:.1%}")
# Critical insights count
critical = len([i for i in insights if i.get("severity") == "critical"])
status = "OK" if critical == 0 else "ALERT"
print(f"[{status}] Critical Issues: {critical}")
# Key metrics
print(f"\n--- Key Metrics ---")
metrics = summary.get("metrics", {})
for key, value in metrics.items():
if isinstance(value, float):
print(f" {key}: {value:.2f}")
else:
print(f" {key}: {value}")
# Top priorities
print(f"\n--- Top Priorities ---")
critical_insights = [i for i in insights if i.get("severity") == "critical"][:3]
warning_insights = [i for i in insights if i.get("severity") == "warning"][:3]
for i, insight in enumerate(critical_insights + warning_insights, 1):
print(f"{i}. [{insight['severity'].upper()}] {insight['title']}")
async def insights_report():
"""Generate comprehensive insights report"""
print("\n" + "=" * 60)
print("AI Insights Report")
print("=" * 60)
print(f"Generated: {datetime.utcnow().isoformat()}Z")
print(f"Analysis Period: Last 7 days")
async with AnalyticsClient() as client:
result = await client.get_insights(days=7)
summary = result.get("fleet_summary", {})
insights = result.get("insights", [])
print("\n" + "=" * 60)
print("EXECUTIVE SUMMARY")
print("=" * 60)
health = summary.get("health_score", 0)
print(f"Fleet Health: {health:.0f}/100 ", end="")
if health >= 90:
print("(Excellent)")
elif health >= 70:
print("(Good)")
elif health >= 50:
print("(Fair)")
else:
print("(Needs Attention)")
print(f"Total Insights: {len(insights)}")
# Count by severity
by_severity = {}
for i in insights:
sev = i.get("severity", "info")
by_severity[sev] = by_severity.get(sev, 0) + 1
for sev in ["critical", "warning", "info"]:
if sev in by_severity:
print(f" {sev.capitalize()}: {by_severity[sev]}")
# Count by type
print("\nInsights by Type:")
by_type = {}
for i in insights:
t = i.get("type", "other")
by_type[t] = by_type.get(t, 0) + 1
for t, count in sorted(by_type.items(), key=lambda x: x[1], reverse=True):
print(f" {t}: {count}")
print("\n" + "=" * 60)
print("DETAILED FINDINGS")
print("=" * 60)
# Group insights by type
for insight_type in ["anomaly_pattern", "trend", "correlation", "recommendation"]:
type_insights = [i for i in insights if i.get("type") == insight_type]
if type_insights:
print(f"\n--- {insight_type.replace('_', ' ').title()} ---")
for i in type_insights[:5]:
print(f"\n {i['title']}")
print(f" Severity: {i['severity']} | Confidence: {i['confidence']:.0%}")
print(f" {i['description'][:200]}...")
print("\n" + "=" * 60)
print("ACTION ITEMS")
print("=" * 60)
actionable = [i for i in insights if i.get("actionable")]
actionable.sort(key=lambda x: (
{"critical": 0, "warning": 1, "info": 2}.get(x.get("severity"), 3),
-x.get("confidence", 0)
))
for i, insight in enumerate(actionable[:10], 1):
print(f"\n{i}. {insight['title']}")
print(f" Priority: {insight['severity'].upper()}")
for action in insight.get("recommended_actions", [])[:2]:
print(f" - {action}")
async def main():
"""Run all insights examples"""
print("=" * 60)
print("TESAIoT Analytics API - AI Insights Examples")
print("=" * 60)
await get_all_insights()
await get_insights_by_type()
await get_high_confidence_insights()
await get_actionable_recommendations()
await analyze_trends()
await analyze_correlations()
await fleet_health_dashboard()
await insights_report()
print("\n" + "=" * 60)
print("All examples completed!")
if __name__ == "__main__":
asyncio.run(main())