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