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  
26  /***
27   * <p>Takes the output of a simple one-cell query (column name "data") from the DB and logs
28   * the DIFFERENCE between the last time the query was run and the current run. The first 
29   * time it is invoked it does not log anything.</p>
30   * 
31   * <p>Data that is logged = <tt>CurrentCount - LastCount</tt></p>
32   * 
33   * @author Jeremy Kraybill
34   * @author Jeremy Thomerson
35   * @version $Id$
36   */
37  public class DifferenceQueryLogger extends QueryLogger {
38  
39  	private static final Logger LOGGER = Logger.getLogger(DifferenceQueryLogger.class);
40  	private Float lastCount = null;
41  	
42  	/***
43  	 * Basic constructor.
44  	 */
45  	public DifferenceQueryLogger() {
46  	    super();
47  	}
48  	
49  	/***
50  	 * @see DifferenceQueryLogger class comment for details
51  	 * @see com.ebay.carad.os.vitalsigns.dataretrievers.QueryLogger#getValue()
52  	 */
53  	public Float getValue() {
54  	    Float diff = null;
55  		if (lastCount == null) { // it's our first logging; don't log anything, just take a checkpoint.
56  			lastCount = super.getValue();
57  		} else { // we have a past logging.
58  			Float curCount = super.getValue();
59  			diff = new Float(curCount.floatValue() - lastCount.floatValue());
60  			lastCount = curCount;
61  		}
62  		return diff;
63  	}
64  	
65  	public Float getData() {
66  		Float value = ZERO;
67  		try {
68  			value = getValue();
69  		} catch (Exception e) {
70  			LOGGER.error(e);
71  		}
72  		return value;			
73  	}
74  }