View Javadoc

1   /*
2    * The contents of this file are subject to the terms 
3    * of the Common Development and Distribution License 
4    * (the "License").  You may not use this file except 
5    * in compliance with the License.
6    * 
7    * You can obtain a copy of the license at 
8    * http://www.sun.com/cddl/cddl.html. 
9    * See the License for the specific language governing 
10   * permissions and limitations under the License.
11   * 
12   * When distributing Covered Code, include this CDDL 
13   * HEADER in each file and include the License file at 
14   * license.txt.  If applicable, add the following below 
15   * this CDDL HEADER, with the fields enclosed by brackets 
16   * "[]" replaced with your own identifying information: 
17   * Portions Copyright [yyyy] [name of copyright owner]
18   * 
19   * Portions Copyright 2004 eBay, Inc.
20   */
21  package com.ebay.carad.os.vitalsigns.dataretrievers;
22  
23  import org.apache.log4j.Logger;
24  
25  import com.ebay.carad.os.vitalsigns.util.ITimeConstants;
26  
27  /***
28   * <p>Takes a difference query, and logs its value as a rate per time period. The
29   * time period is specified in minutes, via the constructor.</p>
30   * 
31   * <p>Data that is logged = <tt>CurrentCount / (System.currentTimeMillis() - lastTimeStamp) * millisPerInterval</tt>
32   * 
33   * @author Jeremy Kraybill
34   * @author Jeremy Thomerson
35   * @version $Id$
36   */
37  public class DifferenceQueryRateLogger extends DifferenceQueryLogger {
38  
39  	/*** Class logger. */
40  	private static final Logger LOGGER = Logger.getLogger(DifferenceQueryRateLogger.class);
41  	
42  	private long millisPer = -1; // milliseconds per interval.
43  	private long lastTick = -1;
44  	
45  	/***
46  	 * Basic constructor.
47  	 */
48  	public DifferenceQueryRateLogger() {
49  	    super();
50  	}
51  	
52  	/***
53  	 * Sets the number of minutes to calculate rate against. For instance, if you want to log
54  	 * this data as a rate per minute, minsPerTick should be 1; per hour, 60; etc.
55  	 * 
56  	 * @param minsPerTick the number of minutes per interval
57  	 */
58  	public void setMinsPerTick(int minsPerTick) {
59  		millisPer = minsPerTick * ITimeConstants.MINUTE;
60  	}
61  	
62  	/***
63  	 * @see DifferenceQueryRateLogger class comment for details
64  	 * @see com.ebay.carad.os.vitalsigns.dataretrievers.QueryLogger#getData()
65  	 */
66  	public Float getData() {
67  		Float curVal = null;
68  		try {
69  			curVal = getValue();
70  			if (curVal != null) {
71  			    curVal = new Float(curVal.floatValue() / (System.currentTimeMillis() - lastTick) * millisPer);
72  			}
73  			lastTick = System.currentTimeMillis();
74  		} catch (Exception e) {
75  			LOGGER.error(e);
76  		}
77  		return curVal;
78  	}
79  }