Pulse/internal/utils/helpers.go
Pulse Monitor 7f414623fc fix: ensure consistent CPU core display for all guests
- Fix CPU core display to show for all guests with CPU data
- Previously only showed cores when CPU > 0 (truthy)
- Now shows "(0.0/X cores)" consistently for all running/stopped guests
- Improve code organization with new helper utilities
- Clean up import statements and remove debug logs
2025-08-09 15:03:28 +00:00

40 lines
1.1 KiB
Go

package utils
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
// Float64Ptr returns a pointer to the given float64 value
func Float64Ptr(v float64) *float64 {
return &v
}
// GenerateID generates a unique ID with the given prefix
func GenerateID(prefix string) string {
return fmt.Sprintf("%s-%d", prefix, time.Now().UnixNano())
}
// WriteJSONResponse writes a JSON response to the http.ResponseWriter
func WriteJSONResponse(w http.ResponseWriter, data interface{}) error {
w.Header().Set("Content-Type", "application/json")
return json.NewEncoder(w).Encode(data)
}
// WriteJSONError writes a JSON error response to the http.ResponseWriter
func WriteJSONError(w http.ResponseWriter, message string, code int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
json.NewEncoder(w).Encode(map[string]string{"error": message})
}
// DecodeJSONBody decodes a JSON request body into the given interface
func DecodeJSONBody(r *http.Request, v interface{}) error {
if err := json.NewDecoder(r.Body).Decode(v); err != nil {
return err
}
return nil
}