gupta.go 952 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package gupta
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. )
  7. const version = "0.0.1"
  8. func Hello() string {
  9. return fmt.Sprintf("Welcome to gupta version %s", version)
  10. }
  11. type GuptaReport struct {
  12. Time time.Time
  13. LoadAvarage float64
  14. CPUUsage *CPUStat `json:"cpu usage, omitempty"`
  15. MemoryUsage *Memory `json:"memory,omitempty"`
  16. }
  17. // usually you'd want a g GuptaReport reciever here ...
  18. // but than you could now check for the object properties ...
  19. func (g *GuptaReport) MarshalJSON() ([]byte, error) {
  20. report := []byte(fmt.Sprintf("{\"timestamp\": \"%d\", \"metrics\": [",
  21. g.Time.Unix()))
  22. if g.CPUUsage != nil {
  23. usage, _ := json.Marshal(g.CPUUsage)
  24. report = append(report, usage...)
  25. }
  26. if g.MemoryUsage != nil {
  27. if g.CPUUsage != nil {
  28. report = append(report, []byte(",")...)
  29. }
  30. usage, _ := json.Marshal(g.MemoryUsage)
  31. report = append(report, usage...)
  32. }
  33. report = append(report, []byte("]}")...)
  34. return report, nil
  35. }