Spaces:
Building
Building
Update spark_controller.py
Browse files- spark_controller.py +35 -78
spark_controller.py
CHANGED
@@ -1,82 +1,10 @@
|
|
1 |
-
from fastapi import APIRouter,
|
2 |
-
from config_provider import get_config,
|
3 |
-
from
|
4 |
-
import
|
5 |
-
import json
|
6 |
|
7 |
router = APIRouter()
|
8 |
|
9 |
-
@router.get("/project_list")
|
10 |
-
def get_spark_project_list(config: ServiceConfig = Depends(get_config)):
|
11 |
-
if not config.projects:
|
12 |
-
raise HTTPException(status_code=404, detail="No projects found")
|
13 |
-
|
14 |
-
project_list = []
|
15 |
-
for project_name, project_data in config.projects.items():
|
16 |
-
latest_version = max(project_data["versions"], key=lambda v: v["version_number"])
|
17 |
-
project_list.append({
|
18 |
-
"project_name": project_name,
|
19 |
-
"version": latest_version["version_number"],
|
20 |
-
"enabled": project_data.get("enabled", False),
|
21 |
-
"status": random.choice(["loading", "ready", "error"]), # mock status
|
22 |
-
"last_accessed": project_data.get("last_updated")
|
23 |
-
})
|
24 |
-
|
25 |
-
return {"projects": project_list}
|
26 |
-
|
27 |
-
@router.post("/enable")
|
28 |
-
async def enable_project(request: Request, config: ServiceConfig = Depends(get_config)):
|
29 |
-
data = await request.json()
|
30 |
-
project_name = data.get("project_name")
|
31 |
-
if not project_name:
|
32 |
-
raise HTTPException(status_code=400, detail="project_name is required")
|
33 |
-
|
34 |
-
project = config.projects.get(project_name)
|
35 |
-
if not project:
|
36 |
-
raise HTTPException(status_code=404, detail="Project not found")
|
37 |
-
|
38 |
-
project["enabled"] = True
|
39 |
-
|
40 |
-
with open(config.config_path, "w", encoding="utf-8") as f:
|
41 |
-
json.dump(config, f, indent=2)
|
42 |
-
|
43 |
-
return {"message": f"Project {project_name} enabled"}
|
44 |
-
|
45 |
-
@router.post("/disable")
|
46 |
-
async def disable_project(request: Request, config: ServiceConfig = Depends(get_config)):
|
47 |
-
data = await request.json()
|
48 |
-
project_name = data.get("project_name")
|
49 |
-
if not project_name:
|
50 |
-
raise HTTPException(status_code=400, detail="project_name is required")
|
51 |
-
|
52 |
-
project = config.projects.get(project_name)
|
53 |
-
if not project:
|
54 |
-
raise HTTPException(status_code=404, detail="Project not found")
|
55 |
-
|
56 |
-
project["enabled"] = False
|
57 |
-
|
58 |
-
with open(config.config_path, "w", encoding="utf-8") as f:
|
59 |
-
json.dump(config, f, indent=2)
|
60 |
-
|
61 |
-
return {"message": f"Project {project_name} disabled"}
|
62 |
-
|
63 |
-
@router.post("/delete")
|
64 |
-
async def delete_project(request: Request, config: ServiceConfig = Depends(get_config)):
|
65 |
-
data = await request.json()
|
66 |
-
project_name = data.get("project_name")
|
67 |
-
if not project_name:
|
68 |
-
raise HTTPException(status_code=400, detail="project_name is required")
|
69 |
-
|
70 |
-
if project_name not in config.projects:
|
71 |
-
raise HTTPException(status_code=404, detail="Project not found")
|
72 |
-
|
73 |
-
del config.projects[project_name]
|
74 |
-
|
75 |
-
with open(config.config_path, "w", encoding="utf-8") as f:
|
76 |
-
json.dump(config, f, indent=2)
|
77 |
-
|
78 |
-
return {"message": f"Project {project_name} deleted"}
|
79 |
-
|
80 |
@router.get("/spark/projects")
|
81 |
def get_spark_projects(config: ServiceConfig = Depends(get_config)):
|
82 |
try:
|
@@ -85,7 +13,36 @@ def get_spark_projects(config: ServiceConfig = Depends(get_config)):
|
|
85 |
response.raise_for_status()
|
86 |
projects = response.json()
|
87 |
return { "projects": projects }
|
88 |
-
|
89 |
except Exception as e:
|
90 |
log(f"⚠️ Spark service unreachable: {str(e)}")
|
91 |
-
return { "error": True }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter, Depends, HTTPException
|
2 |
+
from config_provider import get_config, ServiceConfig
|
3 |
+
from utils import save_service_config, log
|
4 |
+
import requests
|
|
|
5 |
|
6 |
router = APIRouter()
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
@router.get("/spark/projects")
|
9 |
def get_spark_projects(config: ServiceConfig = Depends(get_config)):
|
10 |
try:
|
|
|
13 |
response.raise_for_status()
|
14 |
projects = response.json()
|
15 |
return { "projects": projects }
|
|
|
16 |
except Exception as e:
|
17 |
log(f"⚠️ Spark service unreachable: {str(e)}")
|
18 |
+
return { "error": True, "projects": [] }
|
19 |
+
|
20 |
+
@router.post("/spark/enable")
|
21 |
+
def enable_project(project_name: str, config: ServiceConfig = Depends(get_config)):
|
22 |
+
project = next((p for p in config.data['projects'] if p['name'] == project_name), None)
|
23 |
+
if not project:
|
24 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
25 |
+
project['enabled'] = True
|
26 |
+
save_service_config(config)
|
27 |
+
log(f"✅ Project '{project_name}' enabled")
|
28 |
+
return { "status": "enabled" }
|
29 |
+
|
30 |
+
@router.post("/spark/disable")
|
31 |
+
def disable_project(project_name: str, config: ServiceConfig = Depends(get_config)):
|
32 |
+
project = next((p for p in config.data['projects'] if p['name'] == project_name), None)
|
33 |
+
if not project:
|
34 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
35 |
+
project['enabled'] = False
|
36 |
+
save_service_config(config)
|
37 |
+
log(f"✅ Project '{project_name}' disabled")
|
38 |
+
return { "status": "disabled" }
|
39 |
+
|
40 |
+
@router.post("/spark/delete")
|
41 |
+
def delete_project_spark(project_name: str, config: ServiceConfig = Depends(get_config)):
|
42 |
+
project = next((p for p in config.data['projects'] if p['name'] == project_name), None)
|
43 |
+
if not project:
|
44 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
45 |
+
config.data['projects'].remove(project)
|
46 |
+
save_service_config(config)
|
47 |
+
log(f"🗑️ Project '{project_name}' deleted from Spark")
|
48 |
+
return { "status": "deleted" }
|