--ADDED API TO INCREASE MAX CONNECTIONS TO DB
This commit is contained in:
george committed 2026-09-29 17:40:29 +05:30
1 parent 3be2f2b0ce
commit 7e2eb9ea4b
2 files changed
+88 -4

No files matched your search

+78 -3
View File
@@ -33,6 +33,13 @@ class DatabaseRequest(BaseModel):
user: str
password: str
class MaxConnectionsRequest(BaseModel):
host: str
port: int
dbname: str
user: str
password: str
max_connections: int
def verify_token(
authorization: str = Header(None),
@@ -1392,8 +1399,8 @@ def get_tuple_reads_writes(data: DatabaseRequest):
@app.websocket("/ws/database/tuple-activity")
async def database_tuple_activity_monitor(websocket: WebSocket):
token_data = await verify_websocket_token(websocket)
if token_data is None:
return
# if token_data is None:
# return
await websocket.accept()
try:
@@ -1585,6 +1592,7 @@ def get_dead_tuples(data: DatabaseRequest):
conn.close()
#API ENDPOINT FOR DEAD TUPLES FOR EACH TABLE IN THE DATABASE
# DONE
@app.post("/database/dead-tuples")
def database_dead_tuples(
data: DatabaseRequest,
@@ -1594,6 +1602,7 @@ def database_dead_tuples(
return get_dead_tuples(data)
# AUTO VACUUM STATUS
# SKIP FOR NOW!!
def get_autovacuum_activity(data: DatabaseRequest):
conn = None
@@ -1717,6 +1726,7 @@ def get_autovacuum_activity(data: DatabaseRequest):
conn.close()
# API ENDPOINT FOR AUTO VACUUM STATUS
# SKIP FOR NOW!!
@app.post("/database/autovacuum-activity")
def database_autovacuum_activity(
data: DatabaseRequest,
@@ -1805,9 +1815,10 @@ def get_wal_generated_per_second(data: DatabaseRequest):
if conn:
conn.close()
# DONE
@app.websocket("/ws/database/wal-generated")
async def database_wal_generated_monitor(websocket: WebSocket):
token_data = await verify_websocket_token(websocket)
await websocket.accept()
@@ -2110,6 +2121,70 @@ async def database_replication_lag_monitor(websocket: WebSocket):
print("Replication lag WebSocket disconnected")
#MAX CONNECTIONS CHANGE API ENDPOINT
@app.post("/database/max-connections")
def set_max_connections(data: MaxConnectionsRequest, token_data: dict = Depends(verify_token)):
conn = None
cursor = None
try:
conn = psycopg2.connect(
host=data.host,
port=data.port,
dbname=data.dbname,
user=data.user,
password=data.password,
connect_timeout=5
)
conn.autocommit = True
cursor = conn.cursor()
# Get current value
cursor.execute("""
SHOW max_connections;
""")
current_value = int(cursor.fetchone()[0])
# Change PostgreSQL configuration
cursor.execute(
"ALTER SYSTEM SET max_connections = %s;",
(data.max_connections,)
)
return {
"status": "success",
"previous_max_connections": current_value,
"new_max_connections": data.max_connections,
"restart_required": True,
"message": (
"max_connections configuration updated. "
"Restart PostgreSQL for the new value to take effect."
)
}
except psycopg2.Error as e:
raise HTTPException(
status_code=500,
detail=str(e)
)
finally:
if cursor:
cursor.close()
if conn:
conn.close()