001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.access.filter;
015
016import ch.qos.logback.core.spi.LifeCycle;
017
018public class StatisticalViewImpl implements StatisticalView, LifeCycle {
019
020    final CountingFilter countingFilter;
021    boolean started;
022
023    StatsByMinute statsByMinute = new StatsByMinute();
024    StatsByHour statsByHour = new StatsByHour();
025    StatsByDay statsByDay = new StatsByDay();
026    StatsByWeek statsByWeek = new StatsByWeek();
027    StatsByMonth statsByMonth = new StatsByMonth();
028
029    StatisticalViewImpl(CountingFilter countingFilter) {
030        this.countingFilter = countingFilter;
031    }
032
033    @Override
034    public double getDailyAverage() {
035        return statsByDay.getAverage();
036    }
037
038    @Override
039    public long getLastDaysCount() {
040        return statsByDay.getLastCount();
041    }
042
043    @Override
044    public double getMonthlyAverage() {
045        return statsByMonth.getAverage();
046    }
047
048    @Override
049    public long getLastMonthsCount() {
050        return statsByMonth.getLastCount();
051    }
052
053    @Override
054    public long getTotal() {
055        return countingFilter.getTotal();
056    }
057
058    @Override
059    public double getWeeklyAverage() {
060        return statsByWeek.getAverage();
061    }
062
063    @Override
064    public long getLastWeeksCount() {
065        return statsByWeek.getLastCount();
066    }
067
068    void update(long now) {
069        long total = getTotal();
070        statsByMinute.update(now, total);
071        statsByHour.update(now, total);
072        statsByDay.update(now, total);
073        statsByWeek.update(now, total);
074        statsByMonth.update(now, total);
075
076    }
077
078    void update() {
079        long now = System.currentTimeMillis();
080        update(now);
081    }
082
083    @Override
084    public void start() {
085        System.out.println("StatisticalViewImpl start called");
086        started = true;
087        long now = System.currentTimeMillis();
088        statsByMinute = new StatsByMinute(now);
089        statsByHour = new StatsByHour(now);
090        statsByDay = new StatsByDay(now);
091        statsByWeek = new StatsByWeek(now);
092        statsByMonth = new StatsByMonth(now);
093    }
094
095    @Override
096    public boolean isStarted() {
097        return started;
098    }
099
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}