Spaces:
Running
Running
Commit
·
43e41ae
1
Parent(s):
f14bad9
Renamed a few schema columns and removed verified timestamp/flag due to extra redundancy
Browse files- api_monitor.py +34 -65
- schema.sql +8 -10
api_monitor.py
CHANGED
@@ -45,7 +45,7 @@ def validate_api_configuration(
|
|
45 |
additional_params,
|
46 |
schedule_interval_minutes,
|
47 |
stop_after_hours,
|
48 |
-
|
49 |
):
|
50 |
"""
|
51 |
TOOL: Validate and store API configuration for monitoring.
|
@@ -76,7 +76,7 @@ def validate_api_configuration(
|
|
76 |
- additional_params: Optional JSON string for complex parameters
|
77 |
- schedule_interval_minutes: Minutes between calls
|
78 |
- stop_after_hours: Hours after which to stop (max 168 = 1 week)
|
79 |
-
-
|
80 |
|
81 |
Input Examples:
|
82 |
|
@@ -92,7 +92,7 @@ def validate_api_configuration(
|
|
92 |
additional_params: "{}"
|
93 |
schedule_interval_minutes: 30
|
94 |
stop_after_hours: 24
|
95 |
-
|
96 |
|
97 |
2. API with complex parameters:
|
98 |
mcp_api_key: "your_mcp_key_here"
|
@@ -106,7 +106,7 @@ def validate_api_configuration(
|
|
106 |
additional_params: '{"severity": ["severe", "extreme"], "types": ["tornado", "hurricane"]}'
|
107 |
schedule_interval_minutes: 15
|
108 |
stop_after_hours: 48
|
109 |
-
|
110 |
|
111 |
Returns:
|
112 |
- Dictionary with success status, config_id (needed for setup_scheduler), message, and sample_response
|
@@ -175,11 +175,11 @@ def validate_api_configuration(
|
|
175 |
"config_id": None,
|
176 |
}
|
177 |
|
178 |
-
# Validate
|
179 |
-
if
|
180 |
try:
|
181 |
parsed_start_time = datetime.fromisoformat(
|
182 |
-
|
183 |
)
|
184 |
if parsed_start_time < datetime.now():
|
185 |
return {
|
@@ -212,24 +212,9 @@ def validate_api_configuration(
|
|
212 |
"config_id": None,
|
213 |
}
|
214 |
|
215 |
-
# Generate config ID and calculate timestamps
|
216 |
-
|
217 |
-
"mcp_api_key"
|
218 |
-
"name": name,
|
219 |
-
"description": description,
|
220 |
-
"method": method,
|
221 |
-
"base_url": base_url,
|
222 |
-
"endpoint": endpoint,
|
223 |
-
"param_keys_values": param_keys_values,
|
224 |
-
"header_keys_values": header_keys_values,
|
225 |
-
"additional_params": additional_params,
|
226 |
-
"schedule_interval_minutes": schedule_interval_minutes,
|
227 |
-
"stop_after_hours": stop_after_hours,
|
228 |
-
}
|
229 |
-
|
230 |
-
# Generate unique config ID
|
231 |
-
config_str = json.dumps(config_data, sort_keys=True) + str(
|
232 |
-
datetime.now().timestamp()
|
233 |
)
|
234 |
config_id = int(hashlib.md5(config_str.encode()).hexdigest()[:7], 16)
|
235 |
|
@@ -237,19 +222,6 @@ def validate_api_configuration(
|
|
237 |
created_at = datetime.now()
|
238 |
stop_at = parsed_start_time + timedelta(hours=stop_after_hours)
|
239 |
|
240 |
-
# Add metadata to config
|
241 |
-
config_data.update(
|
242 |
-
{
|
243 |
-
"config_id": config_id,
|
244 |
-
"created_at": created_at.isoformat(),
|
245 |
-
"start_at": parsed_start_time.isoformat(),
|
246 |
-
"stop_at": stop_at.isoformat(),
|
247 |
-
# @JamezyKim This will be used to track the status of whether the api is confirmed or not
|
248 |
-
"is_validated": False,
|
249 |
-
"api_response": result,
|
250 |
-
}
|
251 |
-
)
|
252 |
-
|
253 |
# Store configuration
|
254 |
try:
|
255 |
conn = connect_to_db()
|
@@ -260,11 +232,10 @@ def validate_api_configuration(
|
|
260 |
INSERT INTO api_configurations (
|
261 |
config_id, mcp_api_key, name, description, method,
|
262 |
base_url, endpoint, params, headers, additional_params,
|
263 |
-
|
264 |
-
time_to_start, created_at, validated_at
|
265 |
) VALUES (
|
266 |
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
|
267 |
-
%s, %s, %s, %s, %s
|
268 |
)
|
269 |
""",
|
270 |
(
|
@@ -278,13 +249,11 @@ def validate_api_configuration(
|
|
278 |
json.dumps(api_client.parse_key_value_string(param_keys_values)),
|
279 |
json.dumps(api_client.parse_key_value_string(header_keys_values)),
|
280 |
additional_params,
|
281 |
-
False,
|
282 |
-
False,
|
283 |
-
stop_at.isoformat(),
|
284 |
schedule_interval_minutes,
|
285 |
parsed_start_time,
|
|
|
286 |
created_at,
|
287 |
-
None,
|
288 |
),
|
289 |
)
|
290 |
|
@@ -308,7 +277,7 @@ def validate_api_configuration(
|
|
308 |
return {
|
309 |
"success": True,
|
310 |
"config_id": config_id,
|
311 |
-
"message": f"API call tested and stored successfully for '{name}'. Use this config_id in activate_monitoring() to activate monitoring.",
|
312 |
"sample_response": (
|
313 |
json.loads(result)
|
314 |
if result.startswith("{") or result.startswith("[")
|
@@ -526,26 +495,22 @@ def retrieve_monitored_data(config_id, mcp_api_key, mode="summary"):
|
|
526 |
|
527 |
# Check if monitoring is finished
|
528 |
now = datetime.now()
|
529 |
-
|
530 |
-
if
|
531 |
-
|
532 |
-
|
533 |
-
config.get("schedule_interval_minutes", 24) / 60 * 24
|
534 |
-
) # Default fallback
|
535 |
-
if hasattr(stop_time, "replace"):
|
536 |
-
stop_at = stop_time + timedelta(hours=stop_after_hours)
|
537 |
else:
|
538 |
-
stop_at = datetime.fromisoformat(
|
539 |
-
|
540 |
)
|
541 |
-
is_finished = now > stop_at
|
542 |
else:
|
543 |
-
is_finished =
|
544 |
|
545 |
# Calculate progress statistics
|
546 |
total_expected_calls = 0
|
547 |
-
if config.get("
|
548 |
-
start_time = config["
|
549 |
if hasattr(start_time, "replace"):
|
550 |
start_dt = start_time
|
551 |
else:
|
@@ -611,12 +576,16 @@ def retrieve_monitored_data(config_id, mcp_api_key, mode="summary"):
|
|
611 |
},
|
612 |
"schedule_info": {
|
613 |
"interval_minutes": config.get("schedule_interval_minutes"),
|
614 |
-
"
|
615 |
-
config.get("
|
616 |
-
if config.get("
|
|
|
|
|
|
|
|
|
|
|
617 |
else None
|
618 |
),
|
619 |
-
"is_stopped": config.get("stop", False),
|
620 |
},
|
621 |
"data": monitored_data,
|
622 |
}
|
@@ -703,7 +672,7 @@ if __name__ == "__main__":
|
|
703 |
additional_params="{}",
|
704 |
schedule_interval_minutes=20,
|
705 |
stop_after_hours=24,
|
706 |
-
|
707 |
)
|
708 |
print(validation_response)
|
709 |
print()
|
|
|
45 |
additional_params,
|
46 |
schedule_interval_minutes,
|
47 |
stop_after_hours,
|
48 |
+
start_at,
|
49 |
):
|
50 |
"""
|
51 |
TOOL: Validate and store API configuration for monitoring.
|
|
|
76 |
- additional_params: Optional JSON string for complex parameters
|
77 |
- schedule_interval_minutes: Minutes between calls
|
78 |
- stop_after_hours: Hours after which to stop (max 168 = 1 week)
|
79 |
+
- start_at: When to start the monitoring (datetime string or None for immediate)
|
80 |
|
81 |
Input Examples:
|
82 |
|
|
|
92 |
additional_params: "{}"
|
93 |
schedule_interval_minutes: 30
|
94 |
stop_after_hours: 24
|
95 |
+
start_at: ""
|
96 |
|
97 |
2. API with complex parameters:
|
98 |
mcp_api_key: "your_mcp_key_here"
|
|
|
106 |
additional_params: '{"severity": ["severe", "extreme"], "types": ["tornado", "hurricane"]}'
|
107 |
schedule_interval_minutes: 15
|
108 |
stop_after_hours: 48
|
109 |
+
start_at: "2024-06-15 09:00:00"
|
110 |
|
111 |
Returns:
|
112 |
- Dictionary with success status, config_id (needed for setup_scheduler), message, and sample_response
|
|
|
175 |
"config_id": None,
|
176 |
}
|
177 |
|
178 |
+
# Validate start_at if provided
|
179 |
+
if start_at:
|
180 |
try:
|
181 |
parsed_start_time = datetime.fromisoformat(
|
182 |
+
start_at.replace("Z", "+00:00")
|
183 |
)
|
184 |
if parsed_start_time < datetime.now():
|
185 |
return {
|
|
|
212 |
"config_id": None,
|
213 |
}
|
214 |
|
215 |
+
# Generate unique config ID and calculate timestamps
|
216 |
+
config_str = (
|
217 |
+
f"{mcp_api_key}_{name}_{base_url}_{endpoint}_{datetime.now().timestamp()}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
)
|
219 |
config_id = int(hashlib.md5(config_str.encode()).hexdigest()[:7], 16)
|
220 |
|
|
|
222 |
created_at = datetime.now()
|
223 |
stop_at = parsed_start_time + timedelta(hours=stop_after_hours)
|
224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
# Store configuration
|
226 |
try:
|
227 |
conn = connect_to_db()
|
|
|
232 |
INSERT INTO api_configurations (
|
233 |
config_id, mcp_api_key, name, description, method,
|
234 |
base_url, endpoint, params, headers, additional_params,
|
235 |
+
is_active, schedule_interval_minutes, start_at, stop_at, created_at
|
|
|
236 |
) VALUES (
|
237 |
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
|
238 |
+
%s, %s, %s, %s, %s
|
239 |
)
|
240 |
""",
|
241 |
(
|
|
|
249 |
json.dumps(api_client.parse_key_value_string(param_keys_values)),
|
250 |
json.dumps(api_client.parse_key_value_string(header_keys_values)),
|
251 |
additional_params,
|
252 |
+
False, # is_active starts as False until user activates
|
|
|
|
|
253 |
schedule_interval_minutes,
|
254 |
parsed_start_time,
|
255 |
+
stop_at.isoformat(),
|
256 |
created_at,
|
|
|
257 |
),
|
258 |
)
|
259 |
|
|
|
277 |
return {
|
278 |
"success": True,
|
279 |
"config_id": config_id,
|
280 |
+
"message": f"API call tested, validated, and stored successfully for '{name}'. Make sure to review the message manually before activating monitoring. Use this config_id in activate_monitoring() to activate monitoring.",
|
281 |
"sample_response": (
|
282 |
json.loads(result)
|
283 |
if result.startswith("{") or result.startswith("[")
|
|
|
495 |
|
496 |
# Check if monitoring is finished
|
497 |
now = datetime.now()
|
498 |
+
stop_at_time = config.get("stop_at")
|
499 |
+
if stop_at_time:
|
500 |
+
if hasattr(stop_at_time, "replace"):
|
501 |
+
stop_at = stop_at_time
|
|
|
|
|
|
|
|
|
502 |
else:
|
503 |
+
stop_at = datetime.fromisoformat(
|
504 |
+
str(stop_at_time).replace("Z", "+00:00")
|
505 |
)
|
506 |
+
is_finished = now > stop_at
|
507 |
else:
|
508 |
+
is_finished = False
|
509 |
|
510 |
# Calculate progress statistics
|
511 |
total_expected_calls = 0
|
512 |
+
if config.get("start_at") and config.get("schedule_interval_minutes"):
|
513 |
+
start_time = config["start_at"]
|
514 |
if hasattr(start_time, "replace"):
|
515 |
start_dt = start_time
|
516 |
else:
|
|
|
576 |
},
|
577 |
"schedule_info": {
|
578 |
"interval_minutes": config.get("schedule_interval_minutes"),
|
579 |
+
"start_at": (
|
580 |
+
config.get("start_at").isoformat()
|
581 |
+
if config.get("start_at")
|
582 |
+
else None
|
583 |
+
),
|
584 |
+
"stop_at": (
|
585 |
+
config.get("stop_at").isoformat()
|
586 |
+
if config.get("stop_at")
|
587 |
else None
|
588 |
),
|
|
|
589 |
},
|
590 |
"data": monitored_data,
|
591 |
}
|
|
|
672 |
additional_params="{}",
|
673 |
schedule_interval_minutes=20,
|
674 |
stop_after_hours=24,
|
675 |
+
start_at="",
|
676 |
)
|
677 |
print(validation_response)
|
678 |
print()
|
schema.sql
CHANGED
@@ -3,7 +3,7 @@ DROP TABLE IF EXISTS api_configurations;
|
|
3 |
|
4 |
CREATE TABLE api_configurations (
|
5 |
id SERIAL PRIMARY KEY,
|
6 |
-
|
7 |
mcp_api_key VARCHAR(255) NOT NULL,
|
8 |
name VARCHAR(255) NOT NULL,
|
9 |
description TEXT,
|
@@ -13,13 +13,11 @@ CREATE TABLE api_configurations (
|
|
13 |
params JSONB,
|
14 |
headers JSONB,
|
15 |
additional_params JSONB,
|
16 |
-
is_validated BOOLEAN DEFAULT FALSE,
|
17 |
is_active BOOLEAN DEFAULT FALSE,
|
18 |
-
stop_at TIMESTAMP,
|
19 |
schedule_interval_minutes INTEGER,
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
);
|
24 |
|
25 |
CREATE TABLE api_call_results (
|
@@ -34,7 +32,7 @@ CREATE TABLE api_call_results (
|
|
34 |
INSERT INTO api_configurations (
|
35 |
config_id, mcp_api_key, name, description, method, base_url, endpoint,
|
36 |
params, headers, additional_params,
|
37 |
-
|
38 |
) VALUES (
|
39 |
10101,
|
40 |
'abc123xyz',
|
@@ -46,10 +44,10 @@ INSERT INTO api_configurations (
|
|
46 |
'{"interval":"1d","range":"5d"}',
|
47 |
'{"Authorization":"Bearer token"}',
|
48 |
'{}',
|
49 |
-
TRUE,
|
50 |
TRUE,
|
51 |
-
|
52 |
-
|
|
|
53 |
);
|
54 |
|
55 |
INSERT INTO api_call_results (
|
|
|
3 |
|
4 |
CREATE TABLE api_configurations (
|
5 |
id SERIAL PRIMARY KEY,
|
6 |
+
config_id INTEGER NOT NULL UNIQUE,
|
7 |
mcp_api_key VARCHAR(255) NOT NULL,
|
8 |
name VARCHAR(255) NOT NULL,
|
9 |
description TEXT,
|
|
|
13 |
params JSONB,
|
14 |
headers JSONB,
|
15 |
additional_params JSONB,
|
|
|
16 |
is_active BOOLEAN DEFAULT FALSE,
|
|
|
17 |
schedule_interval_minutes INTEGER,
|
18 |
+
start_at TIMESTAMP,
|
19 |
+
stop_at TIMESTAMP,
|
20 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
21 |
);
|
22 |
|
23 |
CREATE TABLE api_call_results (
|
|
|
32 |
INSERT INTO api_configurations (
|
33 |
config_id, mcp_api_key, name, description, method, base_url, endpoint,
|
34 |
params, headers, additional_params,
|
35 |
+
is_active, schedule_interval_minutes, start_at, stop_at
|
36 |
) VALUES (
|
37 |
10101,
|
38 |
'abc123xyz',
|
|
|
44 |
'{"interval":"1d","range":"5d"}',
|
45 |
'{"Authorization":"Bearer token"}',
|
46 |
'{}',
|
|
|
47 |
TRUE,
|
48 |
+
20,
|
49 |
+
'2025-06-04T12:00:00',
|
50 |
+
'2025-06-11T12:00:00'
|
51 |
);
|
52 |
|
53 |
INSERT INTO api_call_results (
|