Spaces:
Running
Running
Commit
·
5389598
1
Parent(s):
d0775cb
Allowed decimal for stopping time, and removed some imports
Browse files- api_monitor.py +13 -14
- main.py +7 -7
api_monitor.py
CHANGED
@@ -117,14 +117,11 @@ 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 |
-
- stop_after_hours: Hours after which to stop (max 168 = 1 week)
|
123 |
- start_at: When to start the monitoring (datetime string or None for immediate)
|
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"
|
@@ -135,7 +132,7 @@ def validate_api_configuration(
|
|
135 |
header_keys_values: "Authorization: Bearer your_token"
|
136 |
additional_params: "{}"
|
137 |
schedule_interval_minutes: 30
|
138 |
-
stop_after_hours:
|
139 |
start_at: ""
|
140 |
|
141 |
2. API with complex parameters:
|
@@ -149,7 +146,7 @@ def validate_api_configuration(
|
|
149 |
header_keys_values: "X-API-Key: weather_key\nContent-Type: application/json"
|
150 |
additional_params: '{"severity": ["severe", "extreme"], "types": ["tornado", "hurricane"]}'
|
151 |
schedule_interval_minutes: 15
|
152 |
-
stop_after_hours:
|
153 |
start_at: "2024-06-15 09:00:00"
|
154 |
|
155 |
Returns:
|
@@ -172,9 +169,7 @@ def validate_api_configuration(
|
|
172 |
"success": False,
|
173 |
"message": "MCP API key is required",
|
174 |
"config_id": None,
|
175 |
-
}
|
176 |
-
|
177 |
-
# Verify the MCP API key with the key generation server
|
178 |
key_verification = verify_mcp_api_key(mcp_api_key)
|
179 |
if not key_verification["success"]:
|
180 |
return {
|
@@ -217,12 +212,12 @@ def validate_api_configuration(
|
|
217 |
|
218 |
if (
|
219 |
not isinstance(stop_after_hours, (int, float))
|
220 |
-
or stop_after_hours < 1
|
221 |
or stop_after_hours > 168
|
222 |
):
|
223 |
return {
|
224 |
"success": False,
|
225 |
-
"message": "Stop after hours must be between 1 and 168 hours (1 week max)",
|
226 |
"config_id": None,
|
227 |
}
|
228 |
|
@@ -472,7 +467,9 @@ async def activate_monitoring(config_id, mcp_api_key):
|
|
472 |
)
|
473 |
# If the current time is past the stop time, do not execute the job but set is_active to False
|
474 |
if now > stop_at:
|
475 |
-
print(
|
|
|
|
|
476 |
try:
|
477 |
job_conn = connect_to_db()
|
478 |
job_cur = job_conn.cursor()
|
@@ -958,6 +955,7 @@ def retrieve_monitored_data(config_id, mcp_api_key, mode="summary"):
|
|
958 |
## testing
|
959 |
import asyncio
|
960 |
|
|
|
961 |
async def main():
|
962 |
validation_response = validate_api_configuration(
|
963 |
mcp_api_key=os.getenv("MCP_API_KEY"),
|
@@ -992,5 +990,6 @@ async def main():
|
|
992 |
)
|
993 |
print(json.dumps(response, indent=2, default=str))
|
994 |
|
|
|
995 |
if __name__ == "__main__":
|
996 |
asyncio.run(main())
|
|
|
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 - schedule_interval_minutes: Minutes between calls
|
121 |
+
- stop_after_hours: Hours after which to stop (supports decimals, max 168 = 1 week)
|
|
|
122 |
- start_at: When to start the monitoring (datetime string or None for immediate)
|
123 |
|
124 |
+
Input Examples: 1. Simple GET request to monitor stock price:
|
|
|
|
|
125 |
mcp_api_key: "your_mcp_key_here"
|
126 |
name: "NVDA Stock Price"
|
127 |
description: "Monitor NVIDIA stock price every 30 minutes"
|
|
|
132 |
header_keys_values: "Authorization: Bearer your_token"
|
133 |
additional_params: "{}"
|
134 |
schedule_interval_minutes: 30
|
135 |
+
stop_after_hours: 1.5
|
136 |
start_at: ""
|
137 |
|
138 |
2. API with complex parameters:
|
|
|
146 |
header_keys_values: "X-API-Key: weather_key\nContent-Type: application/json"
|
147 |
additional_params: '{"severity": ["severe", "extreme"], "types": ["tornado", "hurricane"]}'
|
148 |
schedule_interval_minutes: 15
|
149 |
+
stop_after_hours: 0.75
|
150 |
start_at: "2024-06-15 09:00:00"
|
151 |
|
152 |
Returns:
|
|
|
169 |
"success": False,
|
170 |
"message": "MCP API key is required",
|
171 |
"config_id": None,
|
172 |
+
} # Verify the MCP API key with the key generation server
|
|
|
|
|
173 |
key_verification = verify_mcp_api_key(mcp_api_key)
|
174 |
if not key_verification["success"]:
|
175 |
return {
|
|
|
212 |
|
213 |
if (
|
214 |
not isinstance(stop_after_hours, (int, float))
|
215 |
+
or stop_after_hours < 0.1
|
216 |
or stop_after_hours > 168
|
217 |
):
|
218 |
return {
|
219 |
"success": False,
|
220 |
+
"message": "Stop after hours must be between 0.1 and 168 hours (1 week max)",
|
221 |
"config_id": None,
|
222 |
}
|
223 |
|
|
|
467 |
)
|
468 |
# If the current time is past the stop time, do not execute the job but set is_active to False
|
469 |
if now > stop_at:
|
470 |
+
print(
|
471 |
+
f"Stopping API monitoring job for {name} as the stop time has been reached."
|
472 |
+
)
|
473 |
try:
|
474 |
job_conn = connect_to_db()
|
475 |
job_cur = job_conn.cursor()
|
|
|
955 |
## testing
|
956 |
import asyncio
|
957 |
|
958 |
+
|
959 |
async def main():
|
960 |
validation_response = validate_api_configuration(
|
961 |
mcp_api_key=os.getenv("MCP_API_KEY"),
|
|
|
990 |
)
|
991 |
print(json.dumps(response, indent=2, default=str))
|
992 |
|
993 |
+
|
994 |
if __name__ == "__main__":
|
995 |
asyncio.run(main())
|
main.py
CHANGED
@@ -1,11 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
-
from api_client import call_api
|
3 |
from api_monitor import (
|
4 |
validate_api_configuration,
|
5 |
activate_monitoring,
|
6 |
retrieve_monitored_data,
|
7 |
)
|
8 |
-
import json
|
9 |
|
10 |
|
11 |
# API Validation Tab
|
@@ -45,7 +43,9 @@ validation_tab = gr.Interface(
|
|
45 |
gr.Number(
|
46 |
label="Schedule Interval (minutes)", value=20, minimum=1, maximum=1440
|
47 |
),
|
48 |
-
gr.Number(
|
|
|
|
|
49 |
gr.Textbox(
|
50 |
label="Start Time (optional)",
|
51 |
placeholder="YYYY-MM-DD HH:MM:SS or leave empty for immediate start",
|
@@ -54,7 +54,7 @@ validation_tab = gr.Interface(
|
|
54 |
],
|
55 |
outputs=gr.Textbox(label="Validation Result", lines=10),
|
56 |
title="API Validation & Storage",
|
57 |
-
description="STEP 1: Validate and test your API configuration. This tool tests the API call and stores the configuration if successful. If validation fails, retry with corrected parameters. If validation succeeds, proceed directly to 'Activate Scheduler' tab with the returned Config ID. Required for LLM tools that need to monitor external APIs periodically. Max monitoring period is 1 week (168 hours).",
|
58 |
flagging_mode="manual",
|
59 |
flagging_options=["Invalid Request", "API Error", "Config Issue", "Other"],
|
60 |
examples=[
|
@@ -69,7 +69,7 @@ validation_tab = gr.Interface(
|
|
69 |
"",
|
70 |
"{}",
|
71 |
30,
|
72 |
-
|
73 |
"",
|
74 |
],
|
75 |
[
|
@@ -83,7 +83,7 @@ validation_tab = gr.Interface(
|
|
83 |
"",
|
84 |
"{}",
|
85 |
60,
|
86 |
-
|
87 |
"",
|
88 |
],
|
89 |
[
|
@@ -97,7 +97,7 @@ validation_tab = gr.Interface(
|
|
97 |
"Accept: application/vnd.github.v3+json\nUser-Agent: MCP-Monitor",
|
98 |
"{}",
|
99 |
120,
|
100 |
-
|
101 |
"",
|
102 |
],
|
103 |
],
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from api_monitor import (
|
3 |
validate_api_configuration,
|
4 |
activate_monitoring,
|
5 |
retrieve_monitored_data,
|
6 |
)
|
|
|
7 |
|
8 |
|
9 |
# API Validation Tab
|
|
|
43 |
gr.Number(
|
44 |
label="Schedule Interval (minutes)", value=20, minimum=1, maximum=1440
|
45 |
),
|
46 |
+
gr.Number(
|
47 |
+
label="Stop After (hours)", value=24, minimum=0.1, maximum=168, step=0.1
|
48 |
+
),
|
49 |
gr.Textbox(
|
50 |
label="Start Time (optional)",
|
51 |
placeholder="YYYY-MM-DD HH:MM:SS or leave empty for immediate start",
|
|
|
54 |
],
|
55 |
outputs=gr.Textbox(label="Validation Result", lines=10),
|
56 |
title="API Validation & Storage",
|
57 |
+
description="STEP 1: Validate and test your API configuration. This tool tests the API call and stores the configuration if successful. If validation fails, retry with corrected parameters. If validation succeeds, proceed directly to 'Activate Scheduler' tab with the returned Config ID. Required for LLM tools that need to monitor external APIs periodically. Max monitoring period is 1 week (168 hours). Supports decimal hours (e.g., 0.5 for 30 minutes).",
|
58 |
flagging_mode="manual",
|
59 |
flagging_options=["Invalid Request", "API Error", "Config Issue", "Other"],
|
60 |
examples=[
|
|
|
69 |
"",
|
70 |
"{}",
|
71 |
30,
|
72 |
+
1.5,
|
73 |
"",
|
74 |
],
|
75 |
[
|
|
|
83 |
"",
|
84 |
"{}",
|
85 |
60,
|
86 |
+
0.5,
|
87 |
"",
|
88 |
],
|
89 |
[
|
|
|
97 |
"Accept: application/vnd.github.v3+json\nUser-Agent: MCP-Monitor",
|
98 |
"{}",
|
99 |
120,
|
100 |
+
2.25,
|
101 |
"",
|
102 |
],
|
103 |
],
|