activeminutes.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package aggregator
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. // activateMinutesAggregator is a MetricAggregator which records the first and last timestamp of updates called on it
  7. type activeMinutesAggregator struct {
  8. lock sync.Mutex
  9. name string
  10. labelValues []string
  11. start *time.Time
  12. end *time.Time
  13. }
  14. func ActiveMinutes(name string, labelValues []string) MetricAggregator {
  15. return &activeMinutesAggregator{
  16. name: name,
  17. labelValues: labelValues,
  18. }
  19. }
  20. func (a *activeMinutesAggregator) Name() string {
  21. return a.name
  22. }
  23. func (a *activeMinutesAggregator) AdditionInfo() map[string]string {
  24. return nil
  25. }
  26. func (a *activeMinutesAggregator) LabelValues() []string {
  27. return a.labelValues
  28. }
  29. func (a *activeMinutesAggregator) Update(value float64, timestamp time.Time, additionalInfo map[string]string) {
  30. a.lock.Lock()
  31. defer a.lock.Unlock()
  32. if a.start == nil {
  33. a.start = &timestamp
  34. }
  35. if !timestamp.Equal(*a.start) {
  36. a.end = &timestamp
  37. }
  38. }
  39. func (a *activeMinutesAggregator) Value() []MetricValue {
  40. a.lock.Lock()
  41. defer a.lock.Unlock()
  42. metricValues := make([]MetricValue, 0)
  43. if a.start != nil {
  44. metricValues = append(metricValues, MetricValue{Value: 1, Timestamp: a.start})
  45. }
  46. if a.end != nil {
  47. metricValues = append(metricValues, MetricValue{Value: 1, Timestamp: a.end})
  48. }
  49. return metricValues
  50. }