1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.access.common.filter;
15
16 abstract public class PeriodicStats {
17
18 private long nextPeriodBegins = 0;
19 private long lastTotal = 0;
20 private long lastCount = 0;
21
22 private double average;
23 private int n;
24
25 PeriodicStats() {
26 this(System.currentTimeMillis());
27 }
28
29 PeriodicStats(long now) {
30 nextPeriodBegins = computeStartOfNextPeriod(now);
31 }
32
33 void update(long now, long total) {
34 if (now > nextPeriodBegins) {
35 lastCount = total - lastTotal;
36 lastTotal = total;
37 average = (average * n + lastCount) / (++n);
38 nextPeriodBegins = computeStartOfNextPeriod(now);
39 }
40 }
41
42 public double getAverage() {
43 return average;
44 }
45
46 public long getLastCount() {
47 return lastCount;
48 }
49
50 void reset(long now) {
51 nextPeriodBegins = computeStartOfNextPeriod(now);
52 lastTotal = 0;
53 lastCount = 0;
54 average = 0.0;
55 n = 0;
56 }
57
58 void reset() {
59 reset(System.currentTimeMillis());
60 }
61
62 abstract long computeStartOfNextPeriod(long now);
63
64 }