add min and max temp, refactore nvidia api

This commit is contained in:
2024-04-21 17:33:21 +00:00
parent 74432b1929
commit d75f4a8431
7 changed files with 192 additions and 179 deletions

View File

@@ -2,6 +2,7 @@ package api
import (
"encoding/json"
"errors"
"fmt"
"html/template"
"log"
@@ -20,22 +21,14 @@ import (
)
type (
NVIDIARepository interface {
GetGPU(ID int) (nvidia.GPUDetail, error)
GetGPUs() ([]nvidia.GPU, error)
DriverVersion() string
CUDAVersion() string
}
Server struct {
repo NVIDIARepository
mux *chi.Mux
errLog *log.Logger
}
WebPack struct {
GPUs []nvidia.GPU
GPU nvidia.GPUDetail
GPUs []nvidia.GPUSummary
GPU nvidia.GPU
Username string
DriverVersion string
CUDAVersion string
@@ -63,10 +56,9 @@ const internalServerErrorPage string = `<!DOCTYPE html>
</body>
</html>`
func New(repo NVIDIARepository) *Server {
func New() *Server {
s := &Server{
mux: chi.NewRouter(),
repo: repo,
errLog: log.New(os.Stderr, log.Prefix(), log.Flags()),
}
s.mux.Use(middleware.RequestID)
@@ -97,11 +89,7 @@ func (s *Server) Serve(port uint) error {
}
func (s *Server) handleRoot(w http.ResponseWriter, _ *http.Request) {
gpus, err := s.repo.GetGPUs()
if err != nil {
internalServerErrorHTML(w, err)
return
}
gpus := nvidia.Summary()
if len(gpus) > 0 {
w.Header().Add("Location", "/"+gpus[0].UUID)
w.WriteHeader(http.StatusTemporaryRedirect)
@@ -125,33 +113,32 @@ func (s *Server) handleRoot(w http.ResponseWriter, _ *http.Request) {
func (s *Server) handleGPU(w http.ResponseWriter, r *http.Request) {
uuid := chi.URLParam(r, "uuid")
gpus, err := s.repo.GetGPUs()
gpu, err := nvidia.GPUByUUID(uuid)
if err != nil {
if errors.Is(err, nvidia.ErrNotFound) {
w.Header().Add("Location", "/")
w.WriteHeader(http.StatusTemporaryRedirect)
return
}
internalServerErrorHTML(w, err)
return
}
driverVersion, err := nvidia.DriverVersion()
if err != nil {
internalServerErrorHTML(w, err)
return
}
i := -1
for _, gpu := range gpus {
if gpu.UUID == uuid {
i = gpu.Index
}
}
if i == -1 {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 page not found"))
}
gpu, err := s.repo.GetGPU(i)
cudaVersion, err := nvidia.CUDAVersion()
if err != nil {
internalServerErrorHTML(w, err)
return
}
wp := WebPack{
Username: "anonymous",
GPUs: gpus,
GPUs: nvidia.Summary(),
GPU: gpu,
DriverVersion: s.repo.DriverVersion(),
CUDAVersion: s.repo.CUDAVersion(),
DriverVersion: driverVersion,
CUDAVersion: cudaVersion,
Version: constant.Version,
}
@@ -168,24 +155,12 @@ func (s *Server) handleGPU(w http.ResponseWriter, r *http.Request) {
func (s *Server) handleGPUJSON(w http.ResponseWriter, r *http.Request) {
uuid := chi.URLParam(r, "uuid")
gpus, err := s.repo.GetGPUs()
gpu, err := nvidia.GPUByUUID(uuid)
if err != nil {
internalServerErrorJSON(w, err)
return
}
i := -1
for _, gpu := range gpus {
if gpu.UUID == uuid {
i = gpu.Index
if errors.Is(err, nvidia.ErrNotFound) {
notFoundJSON(w)
return
}
}
if i == -1 {
notFoundJSON(w)
return
}
gpu, err := s.repo.GetGPU(i)
if err != nil {
internalServerErrorJSON(w, err)
return
}