Spaces:
Running
Running
Commit
·
1891daa
1
Parent(s):
ee1c777
Refactor database connection
Browse files- api_monitor.py +153 -94
api_monitor.py
CHANGED
@@ -3,6 +3,7 @@ import json
|
|
3 |
from datetime import datetime, timedelta
|
4 |
import hashlib
|
5 |
import psycopg2
|
|
|
6 |
import os
|
7 |
from dotenv import load_dotenv
|
8 |
|
@@ -10,6 +11,27 @@ from dotenv import load_dotenv
|
|
10 |
load_dotenv()
|
11 |
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def validate_api_configuration(
|
14 |
mcp_api_key,
|
15 |
name,
|
@@ -228,68 +250,59 @@ def validate_api_configuration(
|
|
228 |
)
|
229 |
|
230 |
# Store configuration
|
231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
|
233 |
-
|
234 |
-
|
|
|
|
|
235 |
return {
|
236 |
"success": False,
|
237 |
-
"message": "Database
|
238 |
"config_id": None,
|
239 |
}
|
240 |
|
241 |
-
conn = psycopg2.connect(
|
242 |
-
database="testdb",
|
243 |
-
user="postgres",
|
244 |
-
host="localhost",
|
245 |
-
password=db_password,
|
246 |
-
port=5432,
|
247 |
-
)
|
248 |
-
|
249 |
-
cur = conn.cursor()
|
250 |
-
|
251 |
-
cur.execute(
|
252 |
-
"""
|
253 |
-
INSERT INTO api_configurations (
|
254 |
-
config_id, mcp_api_key, name, description, method,
|
255 |
-
base_url, endpoint, params, headers, additional_params,
|
256 |
-
is_validated, is_active, stop, schedule_interval_minutes,
|
257 |
-
time_to_start, created_at, validated_at
|
258 |
-
) VALUES (
|
259 |
-
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
|
260 |
-
%s, %s, %s, %s, %s, %s, %s
|
261 |
-
)
|
262 |
-
""",
|
263 |
-
(
|
264 |
-
config_id,
|
265 |
-
mcp_api_key,
|
266 |
-
name,
|
267 |
-
description,
|
268 |
-
method,
|
269 |
-
base_url,
|
270 |
-
endpoint,
|
271 |
-
json.dumps(api_client.parse_key_value_string(param_keys_values)),
|
272 |
-
json.dumps(api_client.parse_key_value_string(header_keys_values)),
|
273 |
-
additional_params,
|
274 |
-
False,
|
275 |
-
False,
|
276 |
-
False,
|
277 |
-
schedule_interval_minutes,
|
278 |
-
parsed_start_time,
|
279 |
-
created_at,
|
280 |
-
None,
|
281 |
-
),
|
282 |
-
)
|
283 |
-
|
284 |
-
conn.commit()
|
285 |
-
cur.execute("SELECT * FROM api_configurations WHERE id = %s", (config_id,))
|
286 |
-
rows = cur.fetchall()
|
287 |
-
for row in rows:
|
288 |
-
print(row)
|
289 |
-
|
290 |
-
conn.close()
|
291 |
-
cur.close()
|
292 |
-
|
293 |
# Return success response
|
294 |
return {
|
295 |
"success": True,
|
@@ -318,8 +331,8 @@ def activate_monitoring(config_id, mcp_api_key):
|
|
318 |
TOOL: Activate periodic monitoring for a validated API configuration.
|
319 |
|
320 |
PURPOSE: Start automated recurring API calls based on a previously validated configuration.
|
321 |
-
This is STEP 2 of the monitoring setup process.
|
322 |
-
|
323 |
PREREQUISITE: Must call validate_api_configuration() first and obtain a config_id from successful validation. Make sure that the sample_response is what you expect
|
324 |
to see before proceeding with this function.
|
325 |
|
@@ -360,32 +373,22 @@ def activate_monitoring(config_id, mcp_api_key):
|
|
360 |
|
361 |
ERROR HANDLING: If config_id not found or invalid, returns success=False with error message
|
362 |
"""
|
|
|
|
|
|
|
|
|
363 |
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
mcp_api_key="your_api_key",
|
376 |
-
name="Dog Facts API",
|
377 |
-
description="Monitor random dog facts from a free API",
|
378 |
-
method="GET",
|
379 |
-
base_url="https://dogapi.dog",
|
380 |
-
endpoint="api/v2/facts",
|
381 |
-
param_keys_values="",
|
382 |
-
header_keys_values="",
|
383 |
-
additional_params="{}",
|
384 |
-
schedule_interval_minutes=20,
|
385 |
-
stop_after_hours=24,
|
386 |
-
time_to_start="",
|
387 |
-
)
|
388 |
-
print(response)
|
389 |
|
390 |
|
391 |
def retrieve_monitored_data(config_id, mcp_api_key):
|
@@ -396,7 +399,7 @@ def retrieve_monitored_data(config_id, mcp_api_key):
|
|
396 |
This is STEP 3 of the monitoring setup process.
|
397 |
|
398 |
PREREQUISITE: Must call validate_api_configuration() first and obtain a config_id from successful validation, then activate_monitoring() to start monitoring.
|
399 |
-
|
400 |
This function can be called at any time after monitoring activation to retrieve the latest data collected by the monitoring system.
|
401 |
|
402 |
Parameters:
|
@@ -435,13 +438,69 @@ def retrieve_monitored_data(config_id, mcp_api_key):
|
|
435 |
}
|
436 |
ERROR HANDLING: If config_id not found or invalid, returns success=False with error message
|
437 |
"""
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from datetime import datetime, timedelta
|
4 |
import hashlib
|
5 |
import psycopg2
|
6 |
+
import psycopg2.extras
|
7 |
import os
|
8 |
from dotenv import load_dotenv
|
9 |
|
|
|
11 |
load_dotenv()
|
12 |
|
13 |
|
14 |
+
def connect_to_db():
|
15 |
+
"""
|
16 |
+
Connect to the PostgreSQL database using environment variables.
|
17 |
+
Returns a connection object.
|
18 |
+
"""
|
19 |
+
db_password = os.getenv("DB_PASSWORD")
|
20 |
+
if not db_password:
|
21 |
+
raise ValueError(
|
22 |
+
"Database password not found in environment variables. Please set DB_PASSWORD."
|
23 |
+
)
|
24 |
+
|
25 |
+
return psycopg2.connect(
|
26 |
+
database="testdb",
|
27 |
+
user="postgres",
|
28 |
+
host="localhost",
|
29 |
+
password=db_password,
|
30 |
+
port=5432,
|
31 |
+
cursor_factory=psycopg2.extras.DictCursor,
|
32 |
+
)
|
33 |
+
|
34 |
+
|
35 |
def validate_api_configuration(
|
36 |
mcp_api_key,
|
37 |
name,
|
|
|
250 |
)
|
251 |
|
252 |
# Store configuration
|
253 |
+
try:
|
254 |
+
conn = connect_to_db()
|
255 |
+
cur = conn.cursor()
|
256 |
+
|
257 |
+
cur.execute(
|
258 |
+
"""
|
259 |
+
INSERT INTO api_configurations (
|
260 |
+
config_id, mcp_api_key, name, description, method,
|
261 |
+
base_url, endpoint, params, headers, additional_params,
|
262 |
+
is_validated, is_active, stop, schedule_interval_minutes,
|
263 |
+
time_to_start, created_at, validated_at
|
264 |
+
) VALUES (
|
265 |
+
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
|
266 |
+
%s, %s, %s, %s, %s, %s, %s
|
267 |
+
)
|
268 |
+
""",
|
269 |
+
(
|
270 |
+
config_id,
|
271 |
+
mcp_api_key,
|
272 |
+
name,
|
273 |
+
description,
|
274 |
+
method,
|
275 |
+
base_url,
|
276 |
+
endpoint,
|
277 |
+
json.dumps(api_client.parse_key_value_string(param_keys_values)),
|
278 |
+
json.dumps(api_client.parse_key_value_string(header_keys_values)),
|
279 |
+
additional_params,
|
280 |
+
False,
|
281 |
+
False,
|
282 |
+
False,
|
283 |
+
schedule_interval_minutes,
|
284 |
+
parsed_start_time,
|
285 |
+
created_at,
|
286 |
+
None,
|
287 |
+
),
|
288 |
+
)
|
289 |
+
|
290 |
+
conn.commit()
|
291 |
+
cur.execute("SELECT * FROM api_configurations WHERE id = %s", (config_id,))
|
292 |
+
rows = cur.fetchall()
|
293 |
+
for row in rows:
|
294 |
+
print(row)
|
295 |
|
296 |
+
conn.close()
|
297 |
+
cur.close()
|
298 |
+
|
299 |
+
except Exception as db_error:
|
300 |
return {
|
301 |
"success": False,
|
302 |
+
"message": f"Database error: {str(db_error)}",
|
303 |
"config_id": None,
|
304 |
}
|
305 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
306 |
# Return success response
|
307 |
return {
|
308 |
"success": True,
|
|
|
331 |
TOOL: Activate periodic monitoring for a validated API configuration.
|
332 |
|
333 |
PURPOSE: Start automated recurring API calls based on a previously validated configuration.
|
334 |
+
This is STEP 2 of the monitoring setup process.
|
335 |
+
|
336 |
PREREQUISITE: Must call validate_api_configuration() first and obtain a config_id from successful validation. Make sure that the sample_response is what you expect
|
337 |
to see before proceeding with this function.
|
338 |
|
|
|
373 |
|
374 |
ERROR HANDLING: If config_id not found or invalid, returns success=False with error message
|
375 |
"""
|
376 |
+
try:
|
377 |
+
conn = connect_to_db()
|
378 |
+
# TODO: Implement activation logic here
|
379 |
+
conn.close()
|
380 |
|
381 |
+
return {
|
382 |
+
"success": False,
|
383 |
+
"message": "Function not implemented yet; this is a placeholder.",
|
384 |
+
"config_id": config_id,
|
385 |
+
}
|
386 |
+
except Exception as e:
|
387 |
+
return {
|
388 |
+
"success": False,
|
389 |
+
"message": f"Database connection failed: {str(e)}",
|
390 |
+
"config_id": config_id,
|
391 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
392 |
|
393 |
|
394 |
def retrieve_monitored_data(config_id, mcp_api_key):
|
|
|
399 |
This is STEP 3 of the monitoring setup process.
|
400 |
|
401 |
PREREQUISITE: Must call validate_api_configuration() first and obtain a config_id from successful validation, then activate_monitoring() to start monitoring.
|
402 |
+
|
403 |
This function can be called at any time after monitoring activation to retrieve the latest data collected by the monitoring system.
|
404 |
|
405 |
Parameters:
|
|
|
438 |
}
|
439 |
ERROR HANDLING: If config_id not found or invalid, returns success=False with error message
|
440 |
"""
|
441 |
+
try:
|
442 |
+
conn = connect_to_db()
|
443 |
+
cur = conn.cursor()
|
444 |
+
cur.execute(
|
445 |
+
"SELECT * FROM api_configurations WHERE config_id = %s", (config_id,)
|
446 |
+
)
|
447 |
+
config = cur.fetchone()
|
448 |
+
|
449 |
+
if not config:
|
450 |
+
conn.close()
|
451 |
+
return {
|
452 |
+
"success": False,
|
453 |
+
"message": "Invalid config_id",
|
454 |
+
"data": [],
|
455 |
+
}
|
456 |
+
print(config)
|
457 |
+
# 2. Query the api_configurations table for the given config_id
|
458 |
+
# 3. If found, retrieve the associated monitored data
|
459 |
+
# 4. Return the data in the specified format
|
460 |
+
conn.close()
|
461 |
+
return {
|
462 |
+
"success": False,
|
463 |
+
"message": "Function not implemented yet; this is a placeholder.",
|
464 |
+
"data": [],
|
465 |
+
}
|
466 |
+
except Exception as e:
|
467 |
+
return {
|
468 |
+
"success": False,
|
469 |
+
"message": f"Database connection failed: {str(e)}",
|
470 |
+
"data": [],
|
471 |
+
}
|
472 |
+
|
473 |
+
|
474 |
+
## testing
|
475 |
+
if __name__ == "__main__":
|
476 |
+
validation_response = validate_api_configuration(
|
477 |
+
mcp_api_key="your_api_key",
|
478 |
+
name="Dog Facts API",
|
479 |
+
description="Monitor random dog facts from a free API",
|
480 |
+
method="GET",
|
481 |
+
base_url="https://dogapi.dog",
|
482 |
+
endpoint="api/v2/facts",
|
483 |
+
param_keys_values="",
|
484 |
+
header_keys_values="",
|
485 |
+
additional_params="{}",
|
486 |
+
schedule_interval_minutes=20,
|
487 |
+
stop_after_hours=24,
|
488 |
+
time_to_start="",
|
489 |
+
)
|
490 |
+
print(validation_response)
|
491 |
+
print()
|
492 |
+
print()
|
493 |
+
|
494 |
+
activate_monitoring_response = activate_monitoring(
|
495 |
+
config_id=validation_response.get("config_id"),
|
496 |
+
mcp_api_key="your_api_key",
|
497 |
+
)
|
498 |
+
print(activate_monitoring_response)
|
499 |
+
print()
|
500 |
+
print()
|
501 |
+
|
502 |
+
response = retrieve_monitored_data(
|
503 |
+
config_id=activate_monitoring_response.get("config_id"),
|
504 |
+
mcp_api_key="your_api_key",
|
505 |
+
)
|
506 |
+
print(response)
|