Googolplexic commited on
Commit
c08d4bb
·
2 Parent(s): feab7d6 693029a

Merge branch 'sql-testing' into tweaks

Browse files
Files changed (2) hide show
  1. api_monitor.py +72 -30
  2. requirements.txt +2 -0
api_monitor.py CHANGED
@@ -10,6 +10,7 @@ from dotenv import load_dotenv
10
  from apscheduler.schedulers.asyncio import AsyncIOScheduler
11
  import requests
12
 
 
13
 
14
  # Load environment variables from .env file
15
  load_dotenv(override=True)
@@ -84,6 +85,44 @@ def verify_mcp_api_key(api_key):
84
  return {"success": False, "message": f"Key verification error: {str(e)}"}
85
 
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  def validate_api_configuration(
88
  mcp_api_key,
89
  name,
@@ -977,38 +1016,41 @@ import asyncio
977
 
978
 
979
  async def main():
980
- validation_response = validate_api_configuration(
981
- mcp_api_key=os.getenv("MCP_API_KEY"),
982
- name="Dog Facts API",
983
- description="Monitor random dog facts from a free API",
984
- method="GET",
985
- base_url="https://dogapi.dog",
986
- endpoint="api/v2/facts",
987
- param_keys_values="",
988
- header_keys_values="",
989
- additional_params="{}",
990
- schedule_interval_minutes=0.05,
991
- stop_after_hours=1,
992
- start_at="",
993
- )
994
- print(validation_response)
995
- print()
996
- print()
997
-
998
- activate_monitoring_response = await activate_monitoring(
999
- config_id=validation_response.get("config_id"),
1000
- mcp_api_key="os.getenv('MCP_API_KEY')",
1001
- )
1002
- print(activate_monitoring_response)
1003
- print()
1004
- print()
 
 
 
1005
 
1006
  await asyncio.sleep(10)
1007
- response = retrieve_monitored_data(
1008
- config_id=activate_monitoring_response.get("config_id"),
1009
- mcp_api_key=os.getenv("MCP_API_KEY"),
1010
- )
1011
- print(json.dumps(response, indent=2, default=str))
1012
 
1013
 
1014
  if __name__ == "__main__":
 
10
  from apscheduler.schedulers.asyncio import AsyncIOScheduler
11
  import requests
12
 
13
+ from apscheduler.schedulers.blocking import BlockingScheduler
14
 
15
  # Load environment variables from .env file
16
  load_dotenv(override=True)
 
85
  return {"success": False, "message": f"Key verification error: {str(e)}"}
86
 
87
 
88
+ def cleanup_old_configurations():
89
+ cleanup_situations = [
90
+ """
91
+ DELETE FROM api_configurations
92
+ WHERE stop_at IS NOT NULL
93
+ AND stop_at < NOW() - INTERVAL '14 days';
94
+ """,
95
+ ]
96
+
97
+ conn = None
98
+ try:
99
+ conn = connect_to_db()
100
+ with conn.cursor() as cur:
101
+ for raw_sql in cleanup_situations:
102
+ sql = raw_sql.strip()
103
+ if not sql:
104
+ continue
105
+
106
+ cur.execute(sql)
107
+ deleted = cur.rowcount
108
+ print(f"[CLEANUP] {deleted} rows deleted.")
109
+ conn.commit()
110
+
111
+ except Exception as e:
112
+ print(f"[ERROR] cleanup failed: {e}")
113
+
114
+ finally:
115
+ if conn:
116
+ conn.close()
117
+
118
+
119
+ def job_schedule():
120
+ sched = BlockingScheduler()
121
+ sched.add_job(cleanup_old_configurations, "cron", hour=0, minute=0)
122
+ print("cleanup job scheduled at 00:00 UTC")
123
+ sched.start()
124
+
125
+
126
  def validate_api_configuration(
127
  mcp_api_key,
128
  name,
 
1016
 
1017
 
1018
  async def main():
1019
+ cleanup_old_configurations()
1020
+ job_schedule()
1021
+
1022
+ # validation_response = validate_api_configuration(
1023
+ # mcp_api_key=os.getenv("MCP_API_KEY"),
1024
+ # name="Dog Facts API",
1025
+ # description="Monitor random dog facts from a free API",
1026
+ # method="GET",
1027
+ # base_url="https://dogapi.dog",
1028
+ # endpoint="api/v2/facts",
1029
+ # param_keys_values="",
1030
+ # header_keys_values="",
1031
+ # additional_params="{}",
1032
+ # schedule_interval_minutes=0.05,
1033
+ # stop_after_hours=1,
1034
+ # start_at="",
1035
+ # )
1036
+ # print(validation_response)
1037
+ # print()
1038
+ # print()
1039
+
1040
+ # activate_monitoring_response = await activate_monitoring(
1041
+ # config_id=validation_response.get("config_id"),
1042
+ # mcp_api_key="os.getenv('MCP_API_KEY')",
1043
+ # )
1044
+ # print(activate_monitoring_response)
1045
+ # print()
1046
+ # print()
1047
 
1048
  await asyncio.sleep(10)
1049
+ # response = retrieve_monitored_data(
1050
+ # config_id=activate_monitoring_response.get("config_id"),
1051
+ # mcp_api_key=os.getenv("MCP_API_KEY"),
1052
+ # )
1053
+ # print(json.dumps(response, indent=2, default=str))
1054
 
1055
 
1056
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -62,3 +62,5 @@ tzlocal==5.3.1
62
  urllib3==2.4.0
63
  uvicorn==0.34.3
64
  websockets==15.0.1
 
 
 
62
  urllib3==2.4.0
63
  uvicorn==0.34.3
64
  websockets==15.0.1
65
+
66
+ apscheduler==3.11.0