timeseries.go 725 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package models
  2. import "math"
  3. type TimeSeries struct {
  4. Name string `json:"name"`
  5. Points [][2]float64 `json:"points"`
  6. Avg float64
  7. Sum float64
  8. Min float64
  9. Max float64
  10. Mean float64
  11. }
  12. type TimeSeriesSlice []*TimeSeries
  13. func NewTimeSeries(name string, points [][2]float64) *TimeSeries {
  14. ts := &TimeSeries{
  15. Name: name,
  16. Points: points,
  17. }
  18. ts.Min = points[0][0]
  19. ts.Max = points[0][0]
  20. for _, v := range points {
  21. value := v[0]
  22. if value > ts.Max {
  23. ts.Max = value
  24. }
  25. if value < ts.Min {
  26. ts.Min = value
  27. }
  28. ts.Sum += value
  29. }
  30. ts.Avg = ts.Sum / float64(len(points))
  31. midPosition := int64(math.Floor(float64(len(points)) / float64(2)))
  32. ts.Mean = points[midPosition][0]
  33. return ts
  34. }