Spaces:
Runtime error
Runtime error
Update SuperAgent/main.py
Browse files- SuperAgent/main.py +10 -21
SuperAgent/main.py
CHANGED
|
@@ -155,19 +155,22 @@ async def chat_message(payload: ChatRequest) -> ChatResponse:
|
|
| 155 |
|
| 156 |
@app.get("/api/v1/schedules")
|
| 157 |
async def list_schedules():
|
| 158 |
-
|
|
|
|
| 159 |
analysis_agent_url = os.getenv("ANALYSIS_AGENT_URL", f"http://127.0.0.1:{port}/analysis")
|
| 160 |
if "localhost" in analysis_agent_url or "127.0.0.1" in analysis_agent_url:
|
|
|
|
| 161 |
analysis_agent_url = re.sub(r":\d+", f":{port}", analysis_agent_url)
|
| 162 |
async with httpx.AsyncClient(timeout=10.0) as client:
|
| 163 |
response = await client.get(f"{analysis_agent_url}/api/v1/schedules")
|
| 164 |
return response.json()
|
| 165 |
-
|
| 166 |
@app.post("/api/v1/schedules/run/{schedule_id}")
|
| 167 |
async def run_schedule(schedule_id: str, offset: int = 0, span_days: int = 1):
|
| 168 |
-
|
|
|
|
| 169 |
analysis_agent_url = os.getenv("ANALYSIS_AGENT_URL", f"http://127.0.0.1:{port}/analysis")
|
| 170 |
if "localhost" in analysis_agent_url or "127.0.0.1" in analysis_agent_url:
|
|
|
|
| 171 |
analysis_agent_url = re.sub(r":\d+", f":{port}", analysis_agent_url)
|
| 172 |
async with httpx.AsyncClient(timeout=60.0) as client:
|
| 173 |
response = await client.post(
|
|
@@ -175,34 +178,20 @@ async def run_schedule(schedule_id: str, offset: int = 0, span_days: int = 1):
|
|
| 175 |
params={"offset": offset, "span_days": span_days}
|
| 176 |
)
|
| 177 |
return response.json()
|
| 178 |
-
|
| 179 |
@app.get("/api/v1/anomalies")
|
| 180 |
async def get_active_anomalies(user_id: str = None, schema_name: str = None):
|
| 181 |
-
#
|
| 182 |
-
|
| 183 |
-
if schema_name and schema_name.lower() != "openremote":
|
| 184 |
-
try:
|
| 185 |
-
from app.services.asset_registry import asset_registry
|
| 186 |
-
from sqlalchemy import text
|
| 187 |
-
with asset_registry.SessionLocal() as session:
|
| 188 |
-
res = session.execute(text(
|
| 189 |
-
"SELECT 1 FROM information_schema.tables WHERE table_schema = :schema AND table_name = 'asset' LIMIT 1"
|
| 190 |
-
), {"schema": schema_name})
|
| 191 |
-
if res.scalar() is not None:
|
| 192 |
-
resolved_schema = schema_name
|
| 193 |
-
except Exception:
|
| 194 |
-
resolved_schema = "openremote"
|
| 195 |
-
|
| 196 |
-
port = os.getenv("PORT", "8000")
|
| 197 |
anomaly_agent_url = os.getenv("ANOMALY_AGENT_URL", f"http://127.0.0.1:{port}/anomaly")
|
| 198 |
if "localhost" in anomaly_agent_url or "127.0.0.1" in anomaly_agent_url:
|
|
|
|
| 199 |
anomaly_agent_url = re.sub(r":\d+", f":{port}", anomaly_agent_url)
|
| 200 |
async with httpx.AsyncClient(timeout=300.0) as client:
|
| 201 |
response = await client.get(
|
| 202 |
f"{anomaly_agent_url}/api/v1/anomalies",
|
| 203 |
params={
|
| 204 |
"user_id": user_id or DEFAULT_USER_ID,
|
| 205 |
-
"schema_name":
|
| 206 |
}
|
| 207 |
)
|
| 208 |
return response.json()
|
|
|
|
| 155 |
|
| 156 |
@app.get("/api/v1/schedules")
|
| 157 |
async def list_schedules():
|
| 158 |
+
# Hugging Face injects SPACE_ID. If present, force port to 7860. Otherwise default to 8000.
|
| 159 |
+
port = "7860" if os.getenv("SPACE_ID") else os.getenv("PORT", "8000")
|
| 160 |
analysis_agent_url = os.getenv("ANALYSIS_AGENT_URL", f"http://127.0.0.1:{port}/analysis")
|
| 161 |
if "localhost" in analysis_agent_url or "127.0.0.1" in analysis_agent_url:
|
| 162 |
+
analysis_agent_url = analysis_agent_url.replace("localhost", "127.0.0.1")
|
| 163 |
analysis_agent_url = re.sub(r":\d+", f":{port}", analysis_agent_url)
|
| 164 |
async with httpx.AsyncClient(timeout=10.0) as client:
|
| 165 |
response = await client.get(f"{analysis_agent_url}/api/v1/schedules")
|
| 166 |
return response.json()
|
|
|
|
| 167 |
@app.post("/api/v1/schedules/run/{schedule_id}")
|
| 168 |
async def run_schedule(schedule_id: str, offset: int = 0, span_days: int = 1):
|
| 169 |
+
# Hugging Face injects SPACE_ID. If present, force port to 7860. Otherwise default to 8000.
|
| 170 |
+
port = "7860" if os.getenv("SPACE_ID") else os.getenv("PORT", "8000")
|
| 171 |
analysis_agent_url = os.getenv("ANALYSIS_AGENT_URL", f"http://127.0.0.1:{port}/analysis")
|
| 172 |
if "localhost" in analysis_agent_url or "127.0.0.1" in analysis_agent_url:
|
| 173 |
+
analysis_agent_url = analysis_agent_url.replace("localhost", "127.0.0.1")
|
| 174 |
analysis_agent_url = re.sub(r":\d+", f":{port}", analysis_agent_url)
|
| 175 |
async with httpx.AsyncClient(timeout=60.0) as client:
|
| 176 |
response = await client.post(
|
|
|
|
| 178 |
params={"offset": offset, "span_days": span_days}
|
| 179 |
)
|
| 180 |
return response.json()
|
|
|
|
| 181 |
@app.get("/api/v1/anomalies")
|
| 182 |
async def get_active_anomalies(user_id: str = None, schema_name: str = None):
|
| 183 |
+
# Hugging Face injects SPACE_ID. If present, force port to 7860. Otherwise default to 8000.
|
| 184 |
+
port = "7860" if os.getenv("SPACE_ID") else os.getenv("PORT", "8000")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
anomaly_agent_url = os.getenv("ANOMALY_AGENT_URL", f"http://127.0.0.1:{port}/anomaly")
|
| 186 |
if "localhost" in anomaly_agent_url or "127.0.0.1" in anomaly_agent_url:
|
| 187 |
+
anomaly_agent_url = anomaly_agent_url.replace("localhost", "127.0.0.1")
|
| 188 |
anomaly_agent_url = re.sub(r":\d+", f":{port}", anomaly_agent_url)
|
| 189 |
async with httpx.AsyncClient(timeout=300.0) as client:
|
| 190 |
response = await client.get(
|
| 191 |
f"{anomaly_agent_url}/api/v1/anomalies",
|
| 192 |
params={
|
| 193 |
"user_id": user_id or DEFAULT_USER_ID,
|
| 194 |
+
"schema_name": schema_name
|
| 195 |
}
|
| 196 |
)
|
| 197 |
return response.json()
|