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  import ch.qos.logback.core.spi.LifeCycle;
17  
18  public class StatisticalViewImpl implements StatisticalView, LifeCycle {
19  
20      final CountingFilter countingFilter;
21      boolean started;
22  
23      StatsByMinute statsByMinute = new StatsByMinute();
24      StatsByHour statsByHour = new StatsByHour();
25      StatsByDay statsByDay = new StatsByDay();
26      StatsByWeek statsByWeek = new StatsByWeek();
27      StatsByMonth statsByMonth = new StatsByMonth();
28  
29      StatisticalViewImpl(CountingFilter countingFilter) {
30          this.countingFilter = countingFilter;
31      }
32  
33      @Override
34      public double getDailyAverage() {
35          return statsByDay.getAverage();
36      }
37  
38      @Override
39      public long getLastDaysCount() {
40          return statsByDay.getLastCount();
41      }
42  
43      @Override
44      public double getMonthlyAverage() {
45          return statsByMonth.getAverage();
46      }
47  
48      @Override
49      public long getLastMonthsCount() {
50          return statsByMonth.getLastCount();
51      }
52  
53      @Override
54      public long getTotal() {
55          return countingFilter.getTotal();
56      }
57  
58      @Override
59      public double getWeeklyAverage() {
60          return statsByWeek.getAverage();
61      }
62  
63      @Override
64      public long getLastWeeksCount() {
65          return statsByWeek.getLastCount();
66      }
67  
68      void update(long now) {
69          long total = getTotal();
70          statsByMinute.update(now, total);
71          statsByHour.update(now, total);
72          statsByDay.update(now, total);
73          statsByWeek.update(now, total);
74          statsByMonth.update(now, total);
75  
76      }
77  
78      void update() {
79          long now = System.currentTimeMillis();
80          update(now);
81      }
82  
83      @Override
84      public void start() {
85          System.out.println("StatisticalViewImpl start called");
86          started = true;
87          long now = System.currentTimeMillis();
88          statsByMinute = new StatsByMinute(now);
89          statsByHour = new StatsByHour(now);
90          statsByDay = new StatsByDay(now);
91          statsByWeek = new StatsByWeek(now);
92          statsByMonth = new StatsByMonth(now);
93      }
94  
95      @Override
96      public boolean isStarted() {
97          return started;
98      }
99  
100     @Override
101     public void stop() {
102         started = false;
103         statsByMinute.reset();
104         statsByHour.reset();
105         statsByDay.reset();
106         statsByWeek.reset();
107         statsByMonth.reset();
108     }
109 
110     @Override
111     public long getLastMinuteCount() {
112         return statsByMinute.getLastCount();
113     }
114 
115     @Override
116     public double getMinuteAverage() {
117         return statsByMinute.getAverage();
118     }
119 
120     @Override
121     public double getHourlyAverage() {
122         return statsByHour.getAverage();
123     }
124 
125     @Override
126     public long getLastHoursCount() {
127         return statsByHour.getLastCount();
128     }
129 
130 }