clustvirt/lib/host/charts.go

39 lines
1.1 KiB
Go
Raw Normal View History

package host
import (
"fmt"
"log"
"strings"
"github.com/wcharczuk/go-chart/v2"
)
// This file contains utilities to create charts based on the different data
// When a chart is rendered, it can return either an SVG or PNG. SVG is preferrable.
func (h *Host) ChartMemory() string {
h.getNodeInfo()
log.Println("Generating Chart")
memFree := float64(h.NodeMemory.Free)
memCached := float64(h.NodeMemory.Cached)
memBuffer := float64(h.NodeMemory.Buffers)
memTotal := float64(h.NodeMemory.Total)
c := chart.PieChart{
Title: fmt.Sprintf("Memory Info %s", h.SystemHostName),
Width: 256,
Height: 256,
Values: []chart.Value{
{Value: memTotal - memFree, Label: fmt.Sprintf("%.2f%% Free", memFree/memTotal*100)},
{Value: memTotal - memCached, Label: fmt.Sprintf("%.2f%% Cached", memCached/memTotal*100)},
{Value: memTotal - memBuffer, Label: fmt.Sprintf("%.2f%% Buffers", memBuffer/memTotal*100)},
},
}
sb := new(strings.Builder)
log.Println("Rendering chart")
if err := c.Render(chart.SVG, sb); err != nil {
return err.Error()
}
return sb.String()
}