Code reorganization Update
This commit is contained in:
@@ -34,6 +34,9 @@ func initDBConnection() {
|
||||
db.Exec("ALTER TABLE simulations ADD COLUMN config TEXT")
|
||||
db.Exec("ALTER TABLE simulations ADD COLUMN search_time REAL")
|
||||
db.Exec("ALTER TABLE simulations ADD COLUMN total_time REAL")
|
||||
db.Exec("ALTER TABLE simulations ADD COLUMN cpu_info TEXT")
|
||||
db.Exec("ALTER TABLE simulations ADD COLUMN gpu_info TEXT")
|
||||
db.Exec("ALTER TABLE simulations ADD COLUMN ram_info TEXT")
|
||||
|
||||
createResourcesTableSQL := `CREATE TABLE IF NOT EXISTS resources (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -65,7 +68,6 @@ func syncResults() {
|
||||
simName := entry.Name()
|
||||
simID := insertSimulation(simName)
|
||||
|
||||
// Read subfiles
|
||||
subFiles, err := os.ReadDir(filepath.Join(resultsDir, simName))
|
||||
if err == nil {
|
||||
db.Exec("DELETE FROM resources WHERE simulation_id = ?", simID)
|
||||
|
||||
34
server/routes/hardware.go
Normal file
34
server/routes/hardware.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (rt *Router) UpdateSimulationHardware(w http.ResponseWriter, r *http.Request, idStr string) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
|
||||
var reqBody struct {
|
||||
CPUInfo *string `json:"cpu_info"`
|
||||
GPUInfo *string `json:"gpu_info"`
|
||||
RAMInfo *string `json:"ram_info"`
|
||||
}
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&reqBody)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
_, err = rt.DB.Exec("UPDATE simulations SET cpu_info = ?, gpu_info = ?, ram_info = ? WHERE id = ?", reqBody.CPUInfo, reqBody.GPUInfo, reqBody.RAMInfo, idStr)
|
||||
if err != nil {
|
||||
log.Println("Update simulation hardware error:", err)
|
||||
http.Error(w, "Failed to update", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
|
||||
}
|
||||
@@ -9,4 +9,7 @@ type Simulation struct {
|
||||
SearchTime *float64 `json:"search_time"`
|
||||
TotalTime *float64 `json:"total_time"`
|
||||
TotalSizeBytes int64 `json:"total_size_bytes"`
|
||||
CPUInfo *string `json:"cpu_info"`
|
||||
GPUInfo *string `json:"gpu_info"`
|
||||
RAMInfo *string `json:"ram_info"`
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func (rt *Router) UploadSimulationResource(w http.ResponseWriter, r *http.Reques
|
||||
return
|
||||
}
|
||||
|
||||
err = r.ParseMultipartForm(32 << 20) // 32 MB max memory bounds, rest spills to disk
|
||||
err = r.ParseMultipartForm(32 << 20)
|
||||
if err != nil {
|
||||
http.Error(w, "Error parsing form: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
|
||||
@@ -72,6 +72,10 @@ func (rt *Router) handleSimulationsPath(w http.ResponseWriter, r *http.Request)
|
||||
rt.RenameSimulation(w, r, idStr)
|
||||
return
|
||||
}
|
||||
if r.Method == "PUT" && action == "hardware" {
|
||||
rt.UpdateSimulationHardware(w, r, idStr)
|
||||
return
|
||||
}
|
||||
if r.Method == "POST" && action == "upload" {
|
||||
rt.UploadSimulationResource(w, r, idStr)
|
||||
return
|
||||
|
||||
@@ -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