View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.access.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  }