feat:
--Added sproket status (experimental)
This commit is contained in:
1 parent
da354286d3
commit
df12fb5188
2 files changed
+142
-19
No files matched your search
@@ -1,7 +1,8 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi import FastAPI, HTTPException,WebSocket, WebSocketDisconnect
|
||||
from pydantic import BaseModel
|
||||
import psycopg2
|
||||
|
||||
from psycopg2.extras import RealDictCursor
|
||||
import asyncio
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@@ -195,7 +196,7 @@ def link_nodes(data: LinkNodeRequest):
|
||||
'default_insert_only',
|
||||
'ddl_sql'
|
||||
],
|
||||
synchronize_structure := false, # CHANGE THIS TO TRUE IF YOU WANT TO SYNCHRONIZE DATA
|
||||
synchronize_structure := false,
|
||||
synchronize_data := false
|
||||
);
|
||||
""", (
|
||||
@@ -231,4 +232,105 @@ def link_nodes(data: LinkNodeRequest):
|
||||
cur.close()
|
||||
|
||||
if conn:
|
||||
conn.close()
|
||||
conn.close()
|
||||
|
||||
|
||||
DB_CONFIG = {
|
||||
"host": "172.16.10.123",
|
||||
"port": 5444,
|
||||
"dbname": "postgres",
|
||||
"user": "postgres",
|
||||
"password": "postgres"
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
def get_spock_status():
|
||||
conn = psycopg2.connect(**DB_CONFIG)
|
||||
|
||||
cursor = conn.cursor(cursor_factory=RealDictCursor)
|
||||
|
||||
# cursor.execute("""
|
||||
# SELECT *
|
||||
# FROM spock.sub_show_status();
|
||||
# """)
|
||||
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
subscription_name,
|
||||
status,
|
||||
provider_node
|
||||
FROM spock.sub_show_status();
|
||||
""")
|
||||
|
||||
rows = cursor.fetchall()
|
||||
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
return rows
|
||||
|
||||
|
||||
@app.websocket("/ws/spock-status")
|
||||
async def websocket_spock_status(websocket: WebSocket):
|
||||
await websocket.accept()
|
||||
|
||||
try:
|
||||
while True:
|
||||
|
||||
try:
|
||||
status = await asyncio.to_thread(get_spock_status)
|
||||
|
||||
await websocket.send_json({
|
||||
"database_reachable": True,
|
||||
"subscriptions": status
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
|
||||
await websocket.send_json({
|
||||
"database_reachable": False,
|
||||
"subscriptions": [],
|
||||
"error": str(e)
|
||||
})
|
||||
return False
|
||||
|
||||
await asyncio.sleep(2)
|
||||
|
||||
except WebSocketDisconnect:
|
||||
print("WebSocket client disconnected")
|
||||
return True
|
||||
|
||||
|
||||
|
||||
@app.get("/spock/subscriptions")
|
||||
def get_spock_subscription_status():
|
||||
try:
|
||||
conn = psycopg2.connect(**DB_CONFIG)
|
||||
|
||||
cursor = conn.cursor(cursor_factory=RealDictCursor)
|
||||
|
||||
cursor.execute("""
|
||||
SELECT *
|
||||
FROM spock.sub_show_status();
|
||||
""")
|
||||
|
||||
rows = cursor.fetchall()
|
||||
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"count": len(rows),
|
||||
"subscriptions": rows
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail=str(e)
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user