flare / spark_controller.py
ciyidogan's picture
Update spark_controller.py
db7a7ca verified
raw
history blame
2.81 kB
from fastapi import APIRouter, HTTPException, Depends, Request
from config_provider import get_config, service_config
import random
import json
router = APIRouter()
@router.get("/project_list")
def get_spark_project_list(config: ServiceConfig = Depends(get_config)):
if not config.projects:
raise HTTPException(status_code=404, detail="No projects found")
project_list = []
for project_name, project_data in config.projects.items():
latest_version = max(project_data["versions"], key=lambda v: v["version_number"])
project_list.append({
"project_name": project_name,
"version": latest_version["version_number"],
"enabled": project_data.get("enabled", False),
"status": random.choice(["loading", "ready", "error"]), # mock status
"last_accessed": project_data.get("last_updated")
})
return {"projects": project_list}
@router.post("/enable")
async def enable_project(request: Request, config: ServiceConfig = Depends(get_config)):
data = await request.json()
project_name = data.get("project_name")
if not project_name:
raise HTTPException(status_code=400, detail="project_name is required")
project = config.projects.get(project_name)
if not project:
raise HTTPException(status_code=404, detail="Project not found")
project["enabled"] = True
with open(config.config_path, "w", encoding="utf-8") as f:
json.dump(config, f, indent=2)
return {"message": f"Project {project_name} enabled"}
@router.post("/disable")
async def disable_project(request: Request, config: ServiceConfig = Depends(get_config)):
data = await request.json()
project_name = data.get("project_name")
if not project_name:
raise HTTPException(status_code=400, detail="project_name is required")
project = config.projects.get(project_name)
if not project:
raise HTTPException(status_code=404, detail="Project not found")
project["enabled"] = False
with open(config.config_path, "w", encoding="utf-8") as f:
json.dump(config, f, indent=2)
return {"message": f"Project {project_name} disabled"}
@router.post("/delete")
async def delete_project(request: Request, config: ServiceConfig = Depends(get_config)):
data = await request.json()
project_name = data.get("project_name")
if not project_name:
raise HTTPException(status_code=400, detail="project_name is required")
if project_name not in config.projects:
raise HTTPException(status_code=404, detail="Project not found")
del config.projects[project_name]
with open(config.config_path, "w", encoding="utf-8") as f:
json.dump(config, f, indent=2)
return {"message": f"Project {project_name} deleted"}