| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package metric |
| 16 |
| 17 | import ( |
| 18 | "sync" |
| 19 | "time" |
| 20 |
| 21 | "k8s.io/utils/clock" |
| 22 | ) |
| 23 |
| 24 | type DateCounter interface { |
| 25 | TodayCount() int64 |
| 26 | GetLastDaysCount(lastdays int64) []int64 |
| 27 | Inc(int64) |
| 28 | Dec(int64) |
| 29 | Snapshot() DateCounter |
| 30 | Clear() |
| 31 | } |
| 32 |
| 33 | func NewDateCounter(reserveDays int64) DateCounter { |
| 34 | if reserveDays <= 0 { |
| 35 | reserveDays = 1 |
| 36 | } |
| 37 | return newStandardDateCounter(reserveDays) |
| 38 | } |
| 39 |
| 40 | type StandardDateCounter struct { |
| 41 | reserveDays int64 |
| 42 | counts []int64 |
| 43 | clock clock.PassiveClock |
| 44 |
| 45 | lastUpdateDate time.Time |
| 46 | mu sync.Mutex |
| 47 | } |
| 48 |
| 49 | func newStandardDateCounter(reserveDays int64) *StandardDateCounter { |
| 50 | return newStandardDateCounterWithClock(reserveDays, clock.RealClock{}) |
| 51 | } |
| 52 |
| 53 | func newStandardDateCounterWithClock(reserveDays int64, clk clock.PassiveClock) *StandardDateCounter { |
| 54 | if clk == nil { |
| 55 | clk = clock.RealClock{} |
| 56 | } |
| 57 | return &StandardDateCounter{ |
| 58 | reserveDays: reserveDays, |
| 59 | counts: make([]int64, reserveDays), |
| 60 | clock: clk, |
| 61 | lastUpdateDate: startOfDay(clk.Now()), |
| 62 | } |
| 63 | } |
| 64 |
| 65 | func (c *StandardDateCounter) TodayCount() int64 { |
| 66 | c.mu.Lock() |
| 67 | defer c.mu.Unlock() |
| 68 |
| 69 | c.rotate(c.clock.Now()) |
| 70 | return c.counts[0] |
| 71 | } |
| 72 |
| 73 | func (c *StandardDateCounter) GetLastDaysCount(lastdays int64) []int64 { |
| 74 | if lastdays > c.reserveDays { |
| 75 | lastdays = c.reserveDays |
| 76 | } |
| 77 | counts := make([]int64, lastdays) |
| 78 |
| 79 | c.mu.Lock() |
| 80 | defer c.mu.Unlock() |
| 81 | c.rotate(c.clock.Now()) |
| 82 | copy(counts, c.counts) |
| 83 | return counts |
| 84 | } |
| 85 |
| 86 | func (c *StandardDateCounter) Inc(count int64) { |
| 87 | c.mu.Lock() |
| 88 | defer c.mu.Unlock() |
| 89 | c.rotate(c.clock.Now()) |
| 90 | c.counts[0] += count |
| 91 | } |
| 92 |
| 93 | func (c *StandardDateCounter) Dec(count int64) { |
| 94 | c.mu.Lock() |
| 95 | defer c.mu.Unlock() |
| 96 | c.rotate(c.clock.Now()) |
| 97 | c.counts[0] -= count |
| 98 | } |
| 99 |
| 100 | func (c *StandardDateCounter) Snapshot() DateCounter { |
| 101 | c.mu.Lock() |
| 102 | defer c.mu.Unlock() |
| 103 | tmp := newStandardDateCounterWithClock(c.reserveDays, c.clock) |
| 104 | copy(tmp.counts, c.counts) |
| 105 | return tmp |
| 106 | } |
| 107 |
| 108 | func (c *StandardDateCounter) Clear() { |
| 109 | c.mu.Lock() |
| 110 | defer c.mu.Unlock() |
| 111 | clear(c.counts) |
| 112 | } |
| 113 |
| 114 | // rotate |
| 115 | // Must hold the lock before calling this function. |
| 116 | func (c *StandardDateCounter) rotate(now time.Time) { |
| 117 | now = startOfDay(now) |
| 118 | days := int(now.Sub(c.lastUpdateDate).Hours() / 24) |
| 119 | reserveDays := int(c.reserveDays) |
| 120 |
| 121 | if days <= 0 { |
| 122 | return |
| 123 | } else if days >= reserveDays { |
| 124 | c.counts = make([]int64, c.reserveDays) |
| 125 | c.lastUpdateDate = now |
| 126 | return |
| 127 | } |
| 128 | newCounts := make([]int64, c.reserveDays) |
| 129 |
| 130 | copy(newCounts[days:], c.counts[:reserveDays-days]) |
| 131 | c.counts = newCounts |
| 132 | c.lastUpdateDate = now |
| 133 | } |
| 134 |
| 135 | // startOfDay returns midnight in t's location. |
| 136 | func startOfDay(t time.Time) time.Time { |
| 137 | return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) |
| 138 | } |