-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathapi_usage_examples.py
More file actions
299 lines (235 loc) · 9.13 KB
/
api_usage_examples.py
File metadata and controls
299 lines (235 loc) · 9.13 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
#!/usr/bin/env python3
"""
AstraGuard AI REST API Usage Examples
Examples of how to interact with the REST API programmatically.
"""
import requests
import json
from datetime import datetime
# Base URL (adjust if running on different host/port)
BASE_URL = "http://localhost:8000"
def example_health_check():
"""Example: Check API health."""
print("\n=== Health Check ===")
response = requests.get(f"{BASE_URL}/health")
print(f"Status Code: {response.status_code}")
print(f"Response: {json.dumps(response.json(), indent=2)}")
def example_submit_telemetry():
"""Example: Submit single telemetry point."""
print("\n=== Submit Telemetry ===")
telemetry = {
"voltage": 7.2,
"temperature": 35.5,
"gyro": 0.08,
"current": 1.5,
"wheel_speed": 4200
}
response = requests.post(
f"{BASE_URL}/api/v1/telemetry",
json=telemetry
)
print(f"Status Code: {response.status_code}")
result = response.json()
print(f"Is Anomaly: {result['is_anomaly']}")
print(f"Anomaly Score: {result['anomaly_score']:.3f}")
print(f"Anomaly Type: {result['anomaly_type']}")
print(f"Recommended Action: {result['recommended_action']}")
print(f"Reasoning: {result['reasoning']}")
def example_submit_anomalous_telemetry():
"""Example: Submit anomalous telemetry."""
print("\n=== Submit Anomalous Telemetry ===")
# Low voltage + high temperature = anomaly
telemetry = {
"voltage": 6.5, # Below threshold
"temperature": 50.0, # High temperature
"gyro": 0.2, # High gyro
"current": 2.0,
"wheel_speed": 5000
}
response = requests.post(
f"{BASE_URL}/api/v1/telemetry",
json=telemetry
)
result = response.json()
print(f"Is Anomaly: {result['is_anomaly']}")
print(f"Anomaly Type: {result['anomaly_type']}")
print(f"Severity: {result['severity_level']} ({result['severity_score']:.2f})")
print(f"Escalate to Safe Mode: {result['should_escalate_to_safe_mode']}")
print(f"Recommended Action: {result['recommended_action']}")
print(f"Allowed Actions: {', '.join(result['allowed_actions'])}")
print(f"Reasoning: {result['reasoning']}")
def example_batch_telemetry():
"""Example: Submit batch of telemetry points."""
print("\n=== Batch Telemetry Submission ===")
batch = {
"telemetry": [
{"voltage": 8.0, "temperature": 25.0, "gyro": 0.01},
{"voltage": 7.5, "temperature": 30.0, "gyro": 0.02},
{"voltage": 6.5, "temperature": 50.0, "gyro": 0.2}, # Anomaly
{"voltage": 8.2, "temperature": 28.0, "gyro": 0.015},
]
}
response = requests.post(
f"{BASE_URL}/api/v1/telemetry/batch",
json=batch
)
result = response.json()
print(f"Total Processed: {result['total_processed']}")
print(f"Anomalies Detected: {result['anomalies_detected']}")
print("\nResults:")
for i, r in enumerate(result['results']):
print(f" {i+1}. Anomaly: {r['is_anomaly']}, Type: {r['anomaly_type']}, Score: {r['anomaly_score']:.3f}")
def example_get_system_status():
"""Example: Get system status."""
print("\n=== System Status ===")
response = requests.get(f"{BASE_URL}/api/v1/status")
result = response.json()
print(f"Overall Status: {result['status']}")
print(f"Mission Phase: {result['mission_phase']}")
print(f"Uptime: {result['uptime_seconds']:.2f} seconds")
print(f"Components:")
for name, health in result['components'].items():
print(f" - {name}: {health.get('status', 'UNKNOWN')}")
def example_get_phase():
"""Example: Get current mission phase."""
print("\n=== Current Mission Phase ===")
response = requests.get(f"{BASE_URL}/api/v1/phase")
result = response.json()
print(f"Phase: {result['phase']}")
print(f"Description: {result['description']}")
print(f"Allowed Actions: {', '.join(result['constraints']['allowed_actions'])}")
print(f"Forbidden Actions: {', '.join(result['constraints']['forbidden_actions'])}")
print(f"Threshold Multiplier: {result['constraints']['threshold_multiplier']}x")
def example_update_phase():
"""Example: Update mission phase."""
print("\n=== Update Mission Phase ===")
# Transition to SAFE_MODE (force=True for guaranteed transition)
request = {
"phase": "SAFE_MODE",
"force": True
}
response = requests.post(
f"{BASE_URL}/api/v1/phase",
json=request
)
result = response.json()
print(f"Success: {result['success']}")
print(f"Previous Phase: {result['previous_phase']}")
print(f"New Phase: {result['new_phase']}")
print(f"Message: {result['message']}")
def example_get_memory_stats():
"""Example: Get memory store statistics."""
print("\n=== Memory Statistics ===")
response = requests.get(f"{BASE_URL}/api/v1/memory/stats")
result = response.json()
print(f"Total Events: {result['total_events']}")
print(f"Critical Events: {result['critical_events']}")
print(f"Average Age: {result['avg_age_hours']:.2f} hours")
print(f"Max Recurrence: {result['max_recurrence']}")
def example_get_anomaly_history():
"""Example: Get anomaly history."""
print("\n=== Anomaly History ===")
# Query with limit
response = requests.get(
f"{BASE_URL}/api/v1/history/anomalies",
params={"limit": 5}
)
result = response.json()
print(f"Total Anomalies: {result['count']}")
print(f"\nLast {min(5, result['count'])} Anomalies:")
for anomaly in result['anomalies']:
print(f" - {anomaly['timestamp']}: {anomaly['anomaly_type']} "
f"(severity: {anomaly['severity_score']:.2f}, "
f"action: {anomaly['recommended_action']})")
def example_filter_anomaly_history():
"""Example: Filter anomaly history by severity."""
print("\n=== Filtered Anomaly History (High Severity) ===")
response = requests.get(
f"{BASE_URL}/api/v1/history/anomalies",
params={
"limit": 10,
"severity_min": 0.7 # Only high severity
}
)
result = response.json()
print(f"High Severity Anomalies: {result['count']}")
for anomaly in result['anomalies']:
print(f" - {anomaly['anomaly_type']}: {anomaly['severity_score']:.2f}")
def example_integration_workflow():
"""Example: Complete integration workflow."""
print("\n=== Complete Integration Workflow ===")
# 1. Check health
print("\n1. Checking API health...")
health = requests.get(f"{BASE_URL}/health").json()
print(f" API Status: {health['status']}")
# 2. Get current phase
print("\n2. Getting current mission phase...")
phase = requests.get(f"{BASE_URL}/api/v1/phase").json()
print(f" Current Phase: {phase['phase']}")
# 3. Submit telemetry
print("\n3. Submitting telemetry...")
telemetry = {
"voltage": 6.0,
"temperature": 55.0,
"gyro": 0.3
}
detection = requests.post(
f"{BASE_URL}/api/v1/telemetry",
json=telemetry
).json()
print(f" Anomaly Detected: {detection['is_anomaly']}")
print(f" Recommended Action: {detection['recommended_action']}")
# 4. Check updated phase (if escalated)
print("\n4. Checking if phase changed...")
new_phase = requests.get(f"{BASE_URL}/api/v1/phase").json()
print(f" New Phase: {new_phase['phase']}")
# 5. Get anomaly history
print("\n5. Retrieving anomaly history...")
history = requests.get(
f"{BASE_URL}/api/v1/history/anomalies",
params={"limit": 3}
).json()
print(f" Total Anomalies in History: {history['count']}")
# 6. Get memory stats
print("\n6. Getting memory statistics...")
memory = requests.get(f"{BASE_URL}/api/v1/memory/stats").json()
print(f" Events in Memory: {memory['total_events']}")
def main():
"""Run all examples."""
print("=" * 70)
print("AstraGuard AI REST API Usage Examples")
print("=" * 70)
print(f"\nBase URL: {BASE_URL}")
print("\nMake sure the API server is running:")
print(" python run_api.py")
print(" OR")
print(" python cli.py api")
print("\n" + "=" * 70)
try:
# Basic examples
example_health_check()
example_submit_telemetry()
example_submit_anomalous_telemetry()
example_batch_telemetry()
# Status and phase examples
example_get_system_status()
example_get_phase()
# example_update_phase() # Commented to avoid changing phase
# Memory and history examples
example_get_memory_stats()
example_get_anomaly_history()
example_filter_anomaly_history()
# Integration workflow
example_integration_workflow()
print("\n" + "=" * 70)
print("All examples completed successfully!")
print("=" * 70)
except requests.exceptions.ConnectionError:
print("\n❌ ERROR: Could not connect to API server.")
print("Make sure the server is running on", BASE_URL)
print("\nStart the server with:")
print(" python run_api.py")
except Exception as e:
print(f"\n❌ ERROR: {str(e)}")
if __name__ == "__main__":
main()