Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5827 amar.kumar 1
/*
2
 *	Copyright 2005 stat4j.org
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *	You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
 
17
package net.sourceforge.stat4j;
18
 
19
/**
20
 * Name:		Metric.java
21
 * Date:		Aug 29, 2004
22
 * Description:
23
 * 
24
 * A metric is collected from a log either at a point in time (single reading) or
25
 * over 2 points in time (dual reading). A dual reading enables deltas such as durations and
26
 * memory usage to be calculated.
27
 * 
28
 * A metric will only be generated for a statsictic if the log message matches the 
29
 * statistic filter. The filter also defines an optional expression for
30
 * scaping user defined values from the log.
31
 * 
32
 * A reading holds the time,free memory and a user defined
33
 * value for a point in time. 
34
 * 
35
 * Typically metrics that require dual readings will be cached
36
 * for the first reading and then sent to the statstic calculator once
37
 * the second reading has been captured. The statistic defines the filters
38
 * for each reading.
39
 * 
40
 * Calculators may override caching by setting  setApplyImmediate to true. RunningTotals are
41
 * examples of calculators that must be sent  metrics
42
 * when a match occurs on either statisitic filter.
43
 * 
44
 * Metrics may be collected across all threads or 
45
 * locally within a given thread (thread local). This
46
 * is so that we can support metrics for a given call stack such
47
 * as method duration and global metrics such as average user session
48
 * duration.
49
 * 
50
 * 
51
 * 
52
 * @see Filter
53
 * @see Reading 
54
 * @see Calculator
55
 * @see Statistic
56
 * 
57
 * value scraped from the log taken at one or 2 points in time
58
 * @author Lara D'Abreo
59
 */
60
public class Metric {
61
 
62
	protected String	statisticName;
63
	protected Reading	firstReading;
64
	protected Reading	secondReading;
65
 
66
	public Metric(String statisticName,Reading reading) {
67
		this.statisticName = statisticName;
68
		this.firstReading = reading;
69
	}
70
 
71
	public Metric(String statisticName,Reading first,Reading second) {
72
			this.statisticName = statisticName;
73
			this.firstReading = first;
74
			this.secondReading = second;
75
		}
76
 
77
 
78
	public boolean isSingle() {
79
		return ! isDual();
80
	}
81
 
82
	public boolean isDual() {
83
			return hasFirstReading() && hasSecondReading();
84
	}
85
 
86
	public boolean hasFirstReading() {
87
		return (firstReading != null);
88
	}
89
 
90
	public boolean hasSecondReading() {
91
		return (secondReading != null);
92
	}
93
	/**
94
	 * @return
95
	 */
96
	public Reading getSecondReading() {
97
		return secondReading;
98
	}
99
 
100
	/**
101
	 * @return
102
	 */
103
	public Reading getFirstReading() {
104
		return firstReading;
105
	}
106
 
107
	/**
108
	 * @param reading
109
	 */
110
	public void setSecondReading(Reading reading) {
111
		secondReading = reading;
112
	}
113
 
114
 
115
 
116
	public double getReadingDelta(Unit unit) {
117
		return secondReading.getReading(unit) - firstReading.getReading(unit);
118
	}
119
 
120
	public double getReading(Unit unit) {
121
		return firstReading.getReading(unit);
122
	}
123
 
124
	public long getTimestamp() {
125
		if (hasSecondReading())
126
			return secondReading.getTimestamp();
127
		else
128
			return firstReading.getTimestamp();
129
	}
130
 
131
	/**
132
	 * @return
133
	 */
134
	public String getStatisticName() {
135
		return statisticName;
136
	}
137
 
138
}