Spaces:
Running
Running
Commit
·
7fc0254
1
Parent(s):
d72e516
Took a while but got Gradio to send the full docstring over
Browse files- api_monitor.py +43 -36
api_monitor.py
CHANGED
@@ -108,7 +108,7 @@ def validate_api_configuration(
|
|
108 |
4. If sample_response contains error messages: Fix API parameters and retry validation
|
109 |
5. If sample_response looks valid: Use config_id in activate_monitoring() to activate monitoring
|
110 |
|
111 |
-
|
112 |
- mcp_api_key: MCP API key serves as user identifier
|
113 |
- name: User-friendly name for the monitoring task
|
114 |
- description: Description of what is being monitored
|
@@ -117,12 +117,14 @@ def validate_api_configuration(
|
|
117 |
- endpoint: The specific API endpoint
|
118 |
- param_keys_values: Parameter key-value pairs, one per line
|
119 |
- header_keys_values: Header key-value pairs, one per line
|
120 |
-
- additional_params: Optional JSON string for complex parameters
|
121 |
-
-
|
122 |
-
|
123 |
-
|
|
|
|
|
124 |
|
125 |
-
|
126 |
mcp_api_key: "your_mcp_key_here"
|
127 |
name: "NVDA Stock Price"
|
128 |
description: "Monitor NVIDIA stock price every 30 minutes"
|
@@ -134,7 +136,9 @@ def validate_api_configuration(
|
|
134 |
additional_params: "{}"
|
135 |
schedule_interval_minutes: 30
|
136 |
stop_after_hours: 1.5
|
137 |
-
start_at: ""
|
|
|
|
|
138 |
mcp_api_key: "your_mcp_key_here"
|
139 |
name: "Weather Alert Monitor"
|
140 |
description: "Monitor severe weather alerts"
|
@@ -356,7 +360,7 @@ async def activate_monitoring(config_id, mcp_api_key):
|
|
356 |
2. If validation successful, call this function with the config_id
|
357 |
3. Monitoring will run automatically according to the validated schedule
|
358 |
|
359 |
-
|
360 |
- config_id: The ID from successful validate_api_configuration() execution (required)
|
361 |
- mcp_api_key: User's MCP API key for verification (must match validation step)
|
362 |
|
@@ -464,26 +468,6 @@ async def activate_monitoring(config_id, mcp_api_key):
|
|
464 |
print(
|
465 |
f"Executing API monitoring job for {name} at {now.isoformat()}. Next call at {next_call.isoformat()}"
|
466 |
)
|
467 |
-
# If the current time is past the stop time, do not execute the job but set is_active to False
|
468 |
-
if now > stop_at:
|
469 |
-
print(
|
470 |
-
f"Stopping API monitoring job for {name} as the stop time has been reached."
|
471 |
-
)
|
472 |
-
try:
|
473 |
-
job_conn = connect_to_db()
|
474 |
-
job_cur = job_conn.cursor()
|
475 |
-
job_cur.execute(
|
476 |
-
"""
|
477 |
-
UPDATE api_configurations SET is_active = %s WHERE config_id = %s
|
478 |
-
""",
|
479 |
-
(False, config_id),
|
480 |
-
)
|
481 |
-
job_conn.commit()
|
482 |
-
job_cur.close()
|
483 |
-
job_conn.close()
|
484 |
-
except Exception as db_exc:
|
485 |
-
print(f"Failed to update configuration status: {db_exc}")
|
486 |
-
return # Stop the job if the time has passed
|
487 |
|
488 |
try:
|
489 |
# Extract API configuration parameters
|
@@ -551,13 +535,32 @@ async def activate_monitoring(config_id, mcp_api_key):
|
|
551 |
job_cur = job_conn.cursor()
|
552 |
|
553 |
# Mark config as active (only once, on first run)
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
559 |
)
|
560 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
561 |
|
562 |
# Insert the actual API call result
|
563 |
job_cur.execute(
|
@@ -654,7 +657,9 @@ def retrieve_monitored_data(config_id, mcp_api_key, mode="summary"):
|
|
654 |
|
655 |
PREREQUISITE: Must call validate_api_configuration() first and obtain a config_id from successful validation, then activate_monitoring() to start monitoring.
|
656 |
|
657 |
-
This function can be called at any time after monitoring activation to retrieve the latest data collected by the monitoring system.
|
|
|
|
|
658 |
- config_id: The ID of the API configuration to retrieve data for (required)
|
659 |
- mcp_api_key: User's MCP API key for verification (must match validation step)
|
660 |
- mode: Data return mode - "summary" (LLM-optimized), "details" (full responses, minimal metadata), "full" (everything)
|
@@ -666,7 +671,9 @@ def retrieve_monitored_data(config_id, mcp_api_key, mode="summary"):
|
|
666 |
|
667 |
2. Retrieve data for weather alerts:
|
668 |
config_id: 987654321
|
669 |
-
mcp_api_key: "your_mcp_key_here"
|
|
|
|
|
670 |
- Dictionary with monitoring status in one of three formats based on mode parameter
|
671 |
|
672 |
SUMMARY mode (LLM-optimized, default):
|
|
|
108 |
4. If sample_response contains error messages: Fix API parameters and retry validation
|
109 |
5. If sample_response looks valid: Use config_id in activate_monitoring() to activate monitoring
|
110 |
|
111 |
+
ARGUMENTS:
|
112 |
- mcp_api_key: MCP API key serves as user identifier
|
113 |
- name: User-friendly name for the monitoring task
|
114 |
- description: Description of what is being monitored
|
|
|
117 |
- endpoint: The specific API endpoint
|
118 |
- param_keys_values: Parameter key-value pairs, one per line
|
119 |
- header_keys_values: Header key-value pairs, one per line
|
120 |
+
- additional_params: Optional JSON string for complex parameters
|
121 |
+
- schedule_interval_minutes: Minutes between calls
|
122 |
+
- stop_after_hours: Hours after which to stop (supports decimals, max 168 = 1 week)
|
123 |
+
- start_at: Optional datetime string for when to start the monitoring. IMPORTANT: Leave as empty string "" for immediate start (most common use case, always default to this if no start time provided). Only provide a datetime string (e.g., "2024-06-15 09:00:00") if you need to schedule monitoring for a specific future time.
|
124 |
+
|
125 |
+
Input Examples:
|
126 |
|
127 |
+
1. Simple GET request to monitor stock price:
|
128 |
mcp_api_key: "your_mcp_key_here"
|
129 |
name: "NVDA Stock Price"
|
130 |
description: "Monitor NVIDIA stock price every 30 minutes"
|
|
|
136 |
additional_params: "{}"
|
137 |
schedule_interval_minutes: 30
|
138 |
stop_after_hours: 1.5
|
139 |
+
start_at: ""
|
140 |
+
|
141 |
+
2. API with complex parameters:
|
142 |
mcp_api_key: "your_mcp_key_here"
|
143 |
name: "Weather Alert Monitor"
|
144 |
description: "Monitor severe weather alerts"
|
|
|
360 |
2. If validation successful, call this function with the config_id
|
361 |
3. Monitoring will run automatically according to the validated schedule
|
362 |
|
363 |
+
ARGUMENTS:
|
364 |
- config_id: The ID from successful validate_api_configuration() execution (required)
|
365 |
- mcp_api_key: User's MCP API key for verification (must match validation step)
|
366 |
|
|
|
468 |
print(
|
469 |
f"Executing API monitoring job for {name} at {now.isoformat()}. Next call at {next_call.isoformat()}"
|
470 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
471 |
|
472 |
try:
|
473 |
# Extract API configuration parameters
|
|
|
535 |
job_cur = job_conn.cursor()
|
536 |
|
537 |
# Mark config as active (only once, on first run)
|
538 |
+
if not config["is_active"]:
|
539 |
+
job_cur.execute(
|
540 |
+
"""
|
541 |
+
UPDATE api_configurations SET is_active = %s WHERE config_id = %s
|
542 |
+
""",
|
543 |
+
(True, config_id),
|
544 |
+
)
|
545 |
+
print(f"Marked configuration {config_id} as active.")
|
546 |
+
|
547 |
+
# Check if this is the last call by comparing current time to stop_at
|
548 |
+
current_time = datetime.now()
|
549 |
+
next_call_time = current_time + timedelta(
|
550 |
+
minutes=schedule_interval_minutes
|
551 |
)
|
552 |
+
|
553 |
+
if next_call_time >= stop_at:
|
554 |
+
# This is the last call, mark as inactive
|
555 |
+
job_cur.execute(
|
556 |
+
"""
|
557 |
+
UPDATE api_configurations SET is_active = %s WHERE config_id = %s
|
558 |
+
""",
|
559 |
+
(False, config_id),
|
560 |
+
)
|
561 |
+
print(
|
562 |
+
f"Last call for configuration {config_id}. Marked as inactive."
|
563 |
+
)
|
564 |
|
565 |
# Insert the actual API call result
|
566 |
job_cur.execute(
|
|
|
657 |
|
658 |
PREREQUISITE: Must call validate_api_configuration() first and obtain a config_id from successful validation, then activate_monitoring() to start monitoring.
|
659 |
|
660 |
+
This function can be called at any time after monitoring activation to retrieve the latest data collected by the monitoring system.
|
661 |
+
|
662 |
+
ARGUMENTS:
|
663 |
- config_id: The ID of the API configuration to retrieve data for (required)
|
664 |
- mcp_api_key: User's MCP API key for verification (must match validation step)
|
665 |
- mode: Data return mode - "summary" (LLM-optimized), "details" (full responses, minimal metadata), "full" (everything)
|
|
|
671 |
|
672 |
2. Retrieve data for weather alerts:
|
673 |
config_id: 987654321
|
674 |
+
mcp_api_key: "your_mcp_key_here"
|
675 |
+
|
676 |
+
Returns:
|
677 |
- Dictionary with monitoring status in one of three formats based on mode parameter
|
678 |
|
679 |
SUMMARY mode (LLM-optimized, default):
|