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
import java.util.Properties;
20
 
21
import net.sourceforge.stat4j.util.Util;
22
 
23
 
24
/**
25
 * Name:		Statistic.java
26
 * Date:		Aug 29, 2004
27
 * Description:
28
 * 
29
 * This class represtents a statistic to be evaluated/derived from a log
30
 * 
31
 * A statistic consists of a calculator and one or two fiters.
32
 * 
33
 * Filters detect matches or conditions in the log. 
34
 * A match on a filter will trigger a reading to be taken
35
 * for the statistic.
36
 *
37
 * A statistic may be expressed over the units: TIME, MEMORY or VALUE. 
38
 * Value is the default and is typically just a count or
39
 * optionally a value scraped from the log message itself using
40
 * the filter scrap expression.
41
 * 
42
 * Typically statistics that define 2 filters yield dual readings.
43
 * The first reading will be cached and then the statistic calculated
44
 * when the second readng has been captured. This allows us to
45
 * support the concept of deltas (durations, memory usage etc).
46
 * 
47
 * For a simple statstic that defines just one filter then the reading 
48
 * will be taken whenever a log matches the first filter. Once the reading 
49
 * is taken it will be wrapped inside a metric and forwarded on to the 
50
 * statistic calculator to derive the actual statstic value.
51
 * 
52
 * @see Filter
53
 * @see Metric
54
 * @see Calculator
55
 * @see Unit
56
 * 
57
 * @author Lara D'Abreo
58
 */
59
public class Statistic {
60
 
61
	protected String name;
62
	protected String description;
63
	protected String calculator;
64
	protected Unit unit;
65
	protected Filter first;
66
	protected Filter second;
67
	protected String tag;
68
	protected boolean captureInThread;
69
 
70
	public Statistic() {
71
 
72
	}
73
	public void init(String name, Properties properties) {
74
		this.name = name;
75
		this.unit = Unit.parse(properties.getProperty("unit", "value"));
76
		this.description = properties.getProperty("description");
77
		if (this.description == null) {
78
			this.description  = this.name;
79
		}
80
		this.calculator = properties.getProperty("calculator", "simple");
81
 
82
		this.captureInThread =
83
			Util.getAsBoolean(properties.getProperty("threadlocal"), true);
84
 
85
		//first filter (mandatory)
86
 
87
		this.first = new Filter();
88
		this.first.setMatch(properties.getProperty("first.match"));
89
		this.first.setScape(properties.getProperty("first.scrape"));
90
		//		second filter (optional)
91
		String match = properties.getProperty("second.match");
92
		if (match != null) {
93
			this.second = new Filter();
94
			this.second.setMatch(match);
95
			this.second.setScape(properties.getProperty("second.scrape"));
96
		} //fi
97
 
98
		this.tag = properties.getProperty("tag.match");
99
 
100
	}
101
	/**
102
	 * @return
103
	 */
104
	public String getDescription() {
105
		return description;
106
	}
107
 
108
	/**
109
	 * @return
110
	 */
111
	public String getName() {
112
		return name;
113
	}
114
 
115
	/**
116
	 * @return
117
	 */
118
	public Unit getUnit() {
119
		return unit;
120
	}
121
 
122
	/**
123
	 * @param string
124
	 */
125
	public void setDescription(String string) {
126
		description = string;
127
	}
128
 
129
	/**
130
	 * @param string
131
	 */
132
	public void setName(String string) {
133
		name = string;
134
	}
135
 
136
	/**
137
	 * @param filter
138
	 */
139
	public void setStart(Filter filter) {
140
		first = filter;
141
	}
142
 
143
	/**
144
	 * @param unit
145
	 */
146
	public void setUnit(Unit unit) {
147
		this.unit = unit;
148
	}
149
 
150
	/**
151
	 * @return
152
	 */
153
	public String getCalculator() {
154
		return calculator;
155
	}
156
 
157
	/**
158
	 * @return
159
	 */
160
	public Filter getFirst() {
161
		return first;
162
	}
163
 
164
	/**
165
	 * @return
166
	 */
167
	public Filter getSecond() {
168
		return second;
169
	}
170
 
171
	/**
172
	 * @param string
173
	 */
174
	public void setCalculator(String string) {
175
		calculator = string;
176
	}
177
 
178
	/**
179
	 * @param filter
180
	 */
181
	public void setFirst(Filter filter) {
182
		first = filter;
183
	}
184
 
185
	/**
186
	 * @param filter
187
	 */
188
	public void setSecond(Filter filter) {
189
		second = filter;
190
	}
191
 
192
	/**
193
	 * @return
194
	 */
195
	public boolean isCaptureInThread() {
196
		return captureInThread;
197
	}
198
 
199
	/**
200
	 * @return
201
	 */
202
	public String getTag() {
203
		return tag;
204
	}
205
 
206
	public boolean isDual() {
207
		return (first != null) & (second != null);
208
	}
209
 
210
}