Code reorganization Update
This commit is contained in:
@@ -57,17 +57,7 @@ func (rt *Router) CreateSimulation(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var existingID int
|
||||
var existingName string
|
||||
err = rt.DB.QueryRow("SELECT id, name FROM simulations WHERE config = ?", configStr).Scan(&existingID, &existingName)
|
||||
if err == nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"id": existingID,
|
||||
"name": existingName,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
var count int
|
||||
rt.DB.QueryRow("SELECT COUNT(*) FROM simulations").Scan(&count)
|
||||
@@ -104,7 +94,7 @@ func (rt *Router) CreateSimulation(w http.ResponseWriter, r *http.Request) {
|
||||
func (rt *Router) GetSimulations(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
|
||||
rows, err := rt.DB.Query("SELECT id, name, config, created_at, search_time, total_time FROM simulations")
|
||||
rows, err := rt.DB.Query("SELECT id, name, config, created_at, search_time, total_time, cpu_info, gpu_info, ram_info FROM simulations")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@@ -114,7 +104,7 @@ func (rt *Router) GetSimulations(w http.ResponseWriter, r *http.Request) {
|
||||
var sims []Simulation
|
||||
for rows.Next() {
|
||||
var s Simulation
|
||||
err = rows.Scan(&s.ID, &s.Name, &s.Config, &s.CreatedAt, &s.SearchTime, &s.TotalTime)
|
||||
err = rows.Scan(&s.ID, &s.Name, &s.Config, &s.CreatedAt, &s.SearchTime, &s.TotalTime, &s.CPUInfo, &s.GPUInfo, &s.RAMInfo)
|
||||
if err != nil {
|
||||
log.Println("Row scan error:", err)
|
||||
continue
|
||||
@@ -134,7 +124,7 @@ func (rt *Router) GetSimulationDetails(w http.ResponseWriter, r *http.Request, i
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
|
||||
var s Simulation
|
||||
err := rt.DB.QueryRow("SELECT id, name, config, created_at, search_time, total_time FROM simulations WHERE id = ?", idStr).Scan(&s.ID, &s.Name, &s.Config, &s.CreatedAt, &s.SearchTime, &s.TotalTime)
|
||||
err := rt.DB.QueryRow("SELECT id, name, config, created_at, search_time, total_time, cpu_info, gpu_info, ram_info FROM simulations WHERE id = ?", idStr).Scan(&s.ID, &s.Name, &s.Config, &s.CreatedAt, &s.SearchTime, &s.TotalTime, &s.CPUInfo, &s.GPUInfo, &s.RAMInfo)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
http.Error(w, "Simulation not found", http.StatusNotFound)
|
||||
@@ -188,7 +178,6 @@ func (rt *Router) RenameSimulation(w http.ResponseWriter, r *http.Request, idStr
|
||||
return
|
||||
}
|
||||
|
||||
// Make sure the new name doesn't already exist
|
||||
var tempID int
|
||||
err = rt.DB.QueryRow("SELECT id FROM simulations WHERE name = ?", newName).Scan(&tempID)
|
||||
if err == nil {
|
||||
@@ -196,7 +185,6 @@ func (rt *Router) RenameSimulation(w http.ResponseWriter, r *http.Request, idStr
|
||||
return
|
||||
}
|
||||
|
||||
// Rename the physical storage folder to maintain link parity
|
||||
oldDirPath := filepath.Join("../results", oldName)
|
||||
newDirPath := filepath.Join("../results", newName)
|
||||
if _, statErr := os.Stat(oldDirPath); statErr == nil {
|
||||
@@ -207,13 +195,11 @@ func (rt *Router) RenameSimulation(w http.ResponseWriter, r *http.Request, idStr
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// Just ensure target dir exists
|
||||
os.MkdirAll(newDirPath, 0755)
|
||||
}
|
||||
|
||||
_, err = rt.DB.Exec("UPDATE simulations SET name = ? WHERE id = ?", newName, idStr)
|
||||
if err != nil {
|
||||
// Attempt revert if DB dies halfway
|
||||
os.Rename(newDirPath, oldDirPath)
|
||||
log.Println("Update simulation name error:", err)
|
||||
http.Error(w, "Failed to update database", http.StatusInternalServerError)
|
||||
@@ -238,7 +224,6 @@ func (rt *Router) DeleteSimulation(w http.ResponseWriter, r *http.Request, idStr
|
||||
return
|
||||
}
|
||||
|
||||
// Attempt dropping resource map hooks first
|
||||
_, err = rt.DB.Exec("DELETE FROM resources WHERE simulation_id = ?", idStr)
|
||||
if err != nil {
|
||||
log.Println("Warning: Error deleting resources map from DB:", err)
|
||||
@@ -251,7 +236,6 @@ func (rt *Router) DeleteSimulation(w http.ResponseWriter, r *http.Request, idStr
|
||||
return
|
||||
}
|
||||
|
||||
// Physically destroy data payload mapped onto it
|
||||
dirPath := filepath.Join("../results", name)
|
||||
if _, statErr := os.Stat(dirPath); statErr == nil {
|
||||
err = os.RemoveAll(dirPath)
|
||||
|
||||
Reference in New Issue
Block a user