Spaces:
Sleeping
Sleeping
Commit
·
d0775cb
1
Parent(s):
85220b1
Update test case, added stop checking
Browse files- api_monitor.py +33 -7
api_monitor.py
CHANGED
|
@@ -470,6 +470,25 @@ async def activate_monitoring(config_id, mcp_api_key):
|
|
| 470 |
print(
|
| 471 |
f"Executing API monitoring job for {name} at {now.isoformat()}. Next call at {next_call.isoformat()}"
|
| 472 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
try:
|
| 474 |
# Extract API configuration parameters
|
| 475 |
method = config.get("method", "GET")
|
|
@@ -542,6 +561,7 @@ async def activate_monitoring(config_id, mcp_api_key):
|
|
| 542 |
""",
|
| 543 |
(True, config_id),
|
| 544 |
)
|
|
|
|
| 545 |
|
| 546 |
# Insert the actual API call result
|
| 547 |
job_cur.execute(
|
|
@@ -936,9 +956,11 @@ def retrieve_monitored_data(config_id, mcp_api_key, mode="summary"):
|
|
| 936 |
|
| 937 |
|
| 938 |
## testing
|
| 939 |
-
|
|
|
|
|
|
|
| 940 |
validation_response = validate_api_configuration(
|
| 941 |
-
mcp_api_key="
|
| 942 |
name="Dog Facts API",
|
| 943 |
description="Monitor random dog facts from a free API",
|
| 944 |
method="GET",
|
|
@@ -947,24 +969,28 @@ if __name__ == "__main__":
|
|
| 947 |
param_keys_values="",
|
| 948 |
header_keys_values="",
|
| 949 |
additional_params="{}",
|
| 950 |
-
schedule_interval_minutes=
|
| 951 |
-
stop_after_hours=
|
| 952 |
start_at="",
|
| 953 |
)
|
| 954 |
print(validation_response)
|
| 955 |
print()
|
| 956 |
print()
|
| 957 |
|
| 958 |
-
activate_monitoring_response = activate_monitoring(
|
| 959 |
config_id=validation_response.get("config_id"),
|
| 960 |
-
mcp_api_key="
|
| 961 |
)
|
| 962 |
print(activate_monitoring_response)
|
| 963 |
print()
|
| 964 |
print()
|
| 965 |
|
|
|
|
| 966 |
response = retrieve_monitored_data(
|
| 967 |
config_id=activate_monitoring_response.get("config_id"),
|
| 968 |
-
mcp_api_key="
|
| 969 |
)
|
| 970 |
print(json.dumps(response, indent=2, default=str))
|
|
|
|
|
|
|
|
|
|
|
|
| 470 |
print(
|
| 471 |
f"Executing API monitoring job for {name} at {now.isoformat()}. Next call at {next_call.isoformat()}"
|
| 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(f"Stopping API monitoring job for {name} as the stop time has been reached.")
|
| 476 |
+
try:
|
| 477 |
+
job_conn = connect_to_db()
|
| 478 |
+
job_cur = job_conn.cursor()
|
| 479 |
+
job_cur.execute(
|
| 480 |
+
"""
|
| 481 |
+
UPDATE api_configurations SET is_active = %s WHERE config_id = %s
|
| 482 |
+
""",
|
| 483 |
+
(False, config_id),
|
| 484 |
+
)
|
| 485 |
+
job_conn.commit()
|
| 486 |
+
job_cur.close()
|
| 487 |
+
job_conn.close()
|
| 488 |
+
except Exception as db_exc:
|
| 489 |
+
print(f"Failed to update configuration status: {db_exc}")
|
| 490 |
+
return # Stop the job if the time has passed
|
| 491 |
+
|
| 492 |
try:
|
| 493 |
# Extract API configuration parameters
|
| 494 |
method = config.get("method", "GET")
|
|
|
|
| 561 |
""",
|
| 562 |
(True, config_id),
|
| 563 |
)
|
| 564 |
+
print(f"Marked configuration {config_id} as active.")
|
| 565 |
|
| 566 |
# Insert the actual API call result
|
| 567 |
job_cur.execute(
|
|
|
|
| 956 |
|
| 957 |
|
| 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"),
|
| 964 |
name="Dog Facts API",
|
| 965 |
description="Monitor random dog facts from a free API",
|
| 966 |
method="GET",
|
|
|
|
| 969 |
param_keys_values="",
|
| 970 |
header_keys_values="",
|
| 971 |
additional_params="{}",
|
| 972 |
+
schedule_interval_minutes=0.05,
|
| 973 |
+
stop_after_hours=1,
|
| 974 |
start_at="",
|
| 975 |
)
|
| 976 |
print(validation_response)
|
| 977 |
print()
|
| 978 |
print()
|
| 979 |
|
| 980 |
+
activate_monitoring_response = await activate_monitoring(
|
| 981 |
config_id=validation_response.get("config_id"),
|
| 982 |
+
mcp_api_key="os.getenv('MCP_API_KEY')",
|
| 983 |
)
|
| 984 |
print(activate_monitoring_response)
|
| 985 |
print()
|
| 986 |
print()
|
| 987 |
|
| 988 |
+
await asyncio.sleep(10)
|
| 989 |
response = retrieve_monitored_data(
|
| 990 |
config_id=activate_monitoring_response.get("config_id"),
|
| 991 |
+
mcp_api_key=os.getenv("MCP_API_KEY"),
|
| 992 |
)
|
| 993 |
print(json.dumps(response, indent=2, default=str))
|
| 994 |
+
|
| 995 |
+
if __name__ == "__main__":
|
| 996 |
+
asyncio.run(main())
|