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