Files
sim-link/server/routes/stats.go

44 lines
1018 B
Go

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,
})
}