timeseries.go 764 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. //Todo: This should be made safer :)
  15. ts := &TimeSeries{
  16. Name: name,
  17. Points: points,
  18. }
  19. ts.Min = points[0][0]
  20. ts.Max = points[0][0]
  21. for _, v := range points {
  22. value := v[0]
  23. if value > ts.Max {
  24. ts.Max = value
  25. }
  26. if value < ts.Min {
  27. ts.Min = value
  28. }
  29. ts.Sum += value
  30. }
  31. ts.Avg = ts.Sum / float64(len(points))
  32. midPosition := int64(math.Floor(float64(len(points)) / float64(2)))
  33. ts.Mean = points[midPosition][0]
  34. return ts
  35. }