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 016abstract public class PeriodicStats { 017 018 private long nextPeriodBegins = 0; 019 private long lastTotal = 0; 020 private long lastCount = 0; 021 022 private double average; 023 private int n; 024 025 PeriodicStats() { 026 this(System.currentTimeMillis()); 027 } 028 029 PeriodicStats(long now) { 030 nextPeriodBegins = computeStartOfNextPeriod(now); 031 } 032 033 void update(long now, long total) { 034 if (now > nextPeriodBegins) { 035 lastCount = total - lastTotal; 036 lastTotal = total; 037 average = (average * n + lastCount) / (++n); 038 nextPeriodBegins = computeStartOfNextPeriod(now); 039 } 040 } 041 042 public double getAverage() { 043 return average; 044 } 045 046 public long getLastCount() { 047 return lastCount; 048 } 049 050 void reset(long now) { 051 nextPeriodBegins = computeStartOfNextPeriod(now); 052 lastTotal = 0; 053 lastCount = 0; 054 average = 0.0; 055 n = 0; 056 } 057 058 void reset() { 059 reset(System.currentTimeMillis()); 060 } 061 062 abstract long computeStartOfNextPeriod(long now); 063 064}