tracker.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Copyright (c) 2015 Arista Networks, Inc.
  19. // Use of this source code is governed by the Apache License 2.0
  20. // that can be found in the COPYING file.
  21. package stats
  22. import (
  23. "math"
  24. "sync"
  25. "time"
  26. )
  27. // Tracker is a min/max value tracker that keeps track of its min/max values
  28. // over a given period of time, and with a given resolution. The initial min
  29. // and max values are math.MaxInt64 and math.MinInt64 respectively.
  30. type Tracker struct {
  31. mu sync.RWMutex
  32. min, max int64 // All time min/max.
  33. minTS, maxTS [3]*timeseries
  34. lastUpdate time.Time
  35. }
  36. // newTracker returns a new Tracker.
  37. func newTracker() *Tracker {
  38. now := TimeNow()
  39. t := &Tracker{}
  40. t.minTS[hour] = newTimeSeries(now, time.Hour, time.Minute)
  41. t.minTS[tenminutes] = newTimeSeries(now, 10*time.Minute, 10*time.Second)
  42. t.minTS[minute] = newTimeSeries(now, time.Minute, time.Second)
  43. t.maxTS[hour] = newTimeSeries(now, time.Hour, time.Minute)
  44. t.maxTS[tenminutes] = newTimeSeries(now, 10*time.Minute, 10*time.Second)
  45. t.maxTS[minute] = newTimeSeries(now, time.Minute, time.Second)
  46. t.init()
  47. return t
  48. }
  49. func (t *Tracker) init() {
  50. t.min = math.MaxInt64
  51. t.max = math.MinInt64
  52. for _, ts := range t.minTS {
  53. ts.set(math.MaxInt64)
  54. }
  55. for _, ts := range t.maxTS {
  56. ts.set(math.MinInt64)
  57. }
  58. }
  59. func (t *Tracker) advance() time.Time {
  60. now := TimeNow()
  61. for _, ts := range t.minTS {
  62. ts.advanceTimeWithFill(now, math.MaxInt64)
  63. }
  64. for _, ts := range t.maxTS {
  65. ts.advanceTimeWithFill(now, math.MinInt64)
  66. }
  67. return now
  68. }
  69. // LastUpdate returns the last update time of the range.
  70. func (t *Tracker) LastUpdate() time.Time {
  71. t.mu.RLock()
  72. defer t.mu.RUnlock()
  73. return t.lastUpdate
  74. }
  75. // Push adds a new value if it is a new minimum or maximum.
  76. func (t *Tracker) Push(value int64) {
  77. t.mu.Lock()
  78. defer t.mu.Unlock()
  79. t.lastUpdate = t.advance()
  80. if t.min > value {
  81. t.min = value
  82. }
  83. if t.max < value {
  84. t.max = value
  85. }
  86. for _, ts := range t.minTS {
  87. if ts.headValue() > value {
  88. ts.set(value)
  89. }
  90. }
  91. for _, ts := range t.maxTS {
  92. if ts.headValue() < value {
  93. ts.set(value)
  94. }
  95. }
  96. }
  97. // Min returns the minimum value of the tracker
  98. func (t *Tracker) Min() int64 {
  99. t.mu.RLock()
  100. defer t.mu.RUnlock()
  101. return t.min
  102. }
  103. // Max returns the maximum value of the tracker.
  104. func (t *Tracker) Max() int64 {
  105. t.mu.RLock()
  106. defer t.mu.RUnlock()
  107. return t.max
  108. }
  109. // Min1h returns the minimum value for the last hour.
  110. func (t *Tracker) Min1h() int64 {
  111. t.mu.Lock()
  112. defer t.mu.Unlock()
  113. t.advance()
  114. return t.minTS[hour].min()
  115. }
  116. // Max1h returns the maximum value for the last hour.
  117. func (t *Tracker) Max1h() int64 {
  118. t.mu.Lock()
  119. defer t.mu.Unlock()
  120. t.advance()
  121. return t.maxTS[hour].max()
  122. }
  123. // Min10m returns the minimum value for the last 10 minutes.
  124. func (t *Tracker) Min10m() int64 {
  125. t.mu.Lock()
  126. defer t.mu.Unlock()
  127. t.advance()
  128. return t.minTS[tenminutes].min()
  129. }
  130. // Max10m returns the maximum value for the last 10 minutes.
  131. func (t *Tracker) Max10m() int64 {
  132. t.mu.Lock()
  133. defer t.mu.Unlock()
  134. t.advance()
  135. return t.maxTS[tenminutes].max()
  136. }
  137. // Min1m returns the minimum value for the last 1 minute.
  138. func (t *Tracker) Min1m() int64 {
  139. t.mu.Lock()
  140. defer t.mu.Unlock()
  141. t.advance()
  142. return t.minTS[minute].min()
  143. }
  144. // Max1m returns the maximum value for the last 1 minute.
  145. func (t *Tracker) Max1m() int64 {
  146. t.mu.Lock()
  147. defer t.mu.Unlock()
  148. t.advance()
  149. return t.maxTS[minute].max()
  150. }
  151. // Reset resets the range to an empty state.
  152. func (t *Tracker) Reset() {
  153. t.mu.Lock()
  154. defer t.mu.Unlock()
  155. now := TimeNow()
  156. for _, ts := range t.minTS {
  157. ts.reset(now)
  158. }
  159. for _, ts := range t.maxTS {
  160. ts.reset(now)
  161. }
  162. t.init()
  163. }