Simulation Details page layout Update
This commit is contained in:
@@ -18,6 +18,17 @@ func NewRouter(db *sql.DB) *Router {
|
||||
func (rt *Router) Register(mux *http.ServeMux) {
|
||||
mux.HandleFunc("/api/simulations", rt.handleSimulationsBase)
|
||||
mux.HandleFunc("/api/simulations/", rt.handleSimulationsPath)
|
||||
mux.HandleFunc("/api/stats", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "OPTIONS" {
|
||||
rt.OptionsHandler(w, r)
|
||||
return
|
||||
}
|
||||
if r.Method == "GET" {
|
||||
rt.GetStats(w, r)
|
||||
} else {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (rt *Router) handleSimulationsBase(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -49,6 +60,11 @@ func (rt *Router) handleSimulationsPath(w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
if pathParts[0] == "clear-fails" && r.Method == "DELETE" {
|
||||
rt.ClearFailedSimulations(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
idStr := pathParts[0]
|
||||
|
||||
if len(pathParts) == 1 {
|
||||
|
||||
@@ -247,3 +247,44 @@ func (rt *Router) DeleteSimulation(w http.ResponseWriter, r *http.Request, idStr
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
|
||||
}
|
||||
|
||||
func (rt *Router) ClearFailedSimulations(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
|
||||
rows, err := rt.DB.Query(`
|
||||
SELECT id, name FROM simulations
|
||||
WHERE search_time IS NULL
|
||||
AND total_time IS NULL
|
||||
AND NOT EXISTS (SELECT 1 FROM resources WHERE simulation_id = simulations.id)
|
||||
`)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var deleteIDs []string
|
||||
var deleteDirs []string
|
||||
|
||||
for rows.Next() {
|
||||
var id, name string
|
||||
if err := rows.Scan(&id, &name); err == nil {
|
||||
deleteIDs = append(deleteIDs, id)
|
||||
deleteDirs = append(deleteDirs, name)
|
||||
}
|
||||
}
|
||||
rows.Close()
|
||||
|
||||
for i, idStr := range deleteIDs {
|
||||
rt.DB.Exec("DELETE FROM resources WHERE simulation_id = ?", idStr)
|
||||
rt.DB.Exec("DELETE FROM simulations WHERE id = ?", idStr)
|
||||
|
||||
dirPath := filepath.Join("../results", deleteDirs[i])
|
||||
if _, statErr := os.Stat(dirPath); statErr == nil {
|
||||
os.RemoveAll(dirPath)
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{"status": "success", "deleted_count": len(deleteIDs)})
|
||||
}
|
||||
|
||||
43
server/routes/stats.go
Normal file
43
server/routes/stats.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func (rt *Router) GetStats(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
|
||||
var totalSims int
|
||||
err := rt.DB.QueryRow("SELECT COUNT(*) FROM simulations").Scan(&totalSims)
|
||||
if err != nil {
|
||||
totalSims = 0
|
||||
}
|
||||
|
||||
var fastestSimID sql.NullInt64
|
||||
err = rt.DB.QueryRow("SELECT id FROM simulations WHERE total_time IS NOT NULL ORDER BY total_time ASC LIMIT 1").Scan(&fastestSimID)
|
||||
|
||||
var totalSize int64 = 0
|
||||
filepath.Walk("../results", func(path string, info os.FileInfo, err error) error {
|
||||
if err == nil && !info.IsDir() {
|
||||
totalSize += info.Size()
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
var fastest *int
|
||||
if fastestSimID.Valid {
|
||||
f := int(fastestSimID.Int64)
|
||||
fastest = &f
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"total_simulations": totalSims,
|
||||
"fastest_sim_id": fastest,
|
||||
"total_storage_bytes": totalSize,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user