001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.metrics;
024:
025: import java.io.Serializable;
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Date;
029: import java.util.Iterator;
030: import java.util.List;
031:
032: import org.w3c.dom.Element;
033:
034: import biz.hammurapi.xml.dom.DomSerializable;
035:
036: /**
037: * @author Pavel Vlasov
038: *
039: * @version $Revision: 1.4 $
040: */
041: public class SimpleSlice implements Slice, Serializable,
042: DomSerializable {
043:
044: /**
045: * Comment for <code>serialVersionUID</code>
046: */
047: private static final long serialVersionUID = 6148347920575353798L;
048:
049: private String name;
050: private boolean keepMeasurements;
051: private int measurements;
052: private double total;
053: private double min;
054: private double max;
055: private double deviation;
056: private List measurementsList;
057: private long from;
058: private long to;
059:
060: public SimpleSlice(String name) {
061: this (name, false);
062: }
063:
064: public SimpleSlice(String name, boolean keepMeasurements) {
065: this .name = name;
066: this .keepMeasurements = keepMeasurements;
067: measurementsList = new ArrayList();
068: }
069:
070: /**
071: * Constructs a copy of master slice.
072: * @param master
073: */
074: public SimpleSlice(Slice master) {
075: name = master.getName();
076: measurements = master.getNumber();
077: total = master.getTotal();
078: min = master.getMin();
079: max = master.getMax();
080: deviation = master.getDeviation();
081: from = master.getFrom();
082: to = master.getTo();
083:
084: if (master.getMeasurements() != null
085: && !master.getMeasurements().isEmpty()) {
086: keepMeasurements = true;
087: measurementsList = new ArrayList();
088: Iterator it = master.getMeasurements().iterator();
089: while (it.hasNext()) {
090: measurementsList.add(new SimpleMeasurement(
091: (Measurement) it.next()));
092: }
093: }
094: }
095:
096: public int getNumber() {
097: return measurements;
098: }
099:
100: public double getMin() {
101: return min;
102: }
103:
104: public double getMax() {
105: return max;
106: }
107:
108: public double getAvg() {
109: return total / measurements;
110: }
111:
112: public double getTotal() {
113: return total;
114: }
115:
116: public void add(final double value, final long time) {
117: if (measurements == 0 || value < min) {
118: min = value;
119: }
120:
121: if (measurements == 0 || value > max) {
122: max = value;
123: }
124:
125: if (measurements == 0 || from > time) {
126: from = time;
127: }
128:
129: if (measurements == 0 || to < time) {
130: to = time;
131: }
132:
133: total += value;
134: measurements++;
135: deviation += Math.abs(value - total / measurements);
136:
137: if (keepMeasurements) {
138: class PrivateMeasurement implements Measurement,
139: Serializable {
140: public double getValue() {
141: return value;
142: }
143:
144: public long getTime() {
145: return time;
146: }
147: }
148:
149: measurementsList.add(new PrivateMeasurement());
150: }
151: }
152:
153: public void add(Metric metric) {
154: if (measurements == 0 || metric.getMin() < min) {
155: min = metric.getMin();
156: }
157:
158: if (measurements == 0 || metric.getMax() > max) {
159: max = metric.getMax();
160: }
161:
162: if (metric instanceof Slice) {
163: Slice slice = (Slice) metric;
164: if (measurements == 0 || from > slice.getFrom()) {
165: from = slice.getFrom();
166: }
167:
168: if (measurements == 0 || to < slice.getTo()) {
169: to = slice.getTo();
170: }
171: }
172:
173: measurements += metric.getNumber();
174: total += metric.getTotal();
175: deviation += metric.getDeviation() * metric.getNumber();
176:
177: if (keepMeasurements) {
178: measurementsList.addAll(metric.getMeasurements());
179: }
180: }
181:
182: public Collection getMeasurements() {
183: return measurementsList;
184: }
185:
186: public String getName() {
187: return name;
188: }
189:
190: public String toString() {
191: StringBuffer ret = new StringBuffer();
192: ret.append(measurements).append("/").append(total).append(
193: " <= ").append(name).append("\n");
194: if (measurementsList != null) {
195: Iterator it = measurementsList.iterator();
196: while (it.hasNext()) {
197: Measurement m = (Measurement) it.next();
198: ret.append("\t");
199: ret.append(m.getValue());
200: ret.append(" <- ");
201: ret.append(new Date(m.getTime()));
202: ret.append("\n");
203: }
204: }
205: return ret.toString();
206: }
207:
208: public int compareTo(Object o) {
209: if (o instanceof Slice) {
210: return Double.compare(((Slice) o).getTotal(), total);
211: }
212:
213: return 1;
214: }
215:
216: public long getFrom() {
217: return from;
218: }
219:
220: public long getTo() {
221: return to;
222: }
223:
224: public double getDeviation() {
225: return deviation / measurements;
226: }
227:
228: public void toDom(Element holder) {
229: holder.setAttribute("name", getName());
230: holder.setAttribute("avg", String.valueOf(getAvg()));
231: holder.setAttribute("min", String.valueOf(getMin()));
232: holder.setAttribute("max", String.valueOf(getMax()));
233:
234: holder.setAttribute("total", String.valueOf(getTotal()));
235: holder.setAttribute("number", String.valueOf(getNumber()));
236: holder
237: .setAttribute("deviation", String
238: .valueOf(getDeviation()));
239:
240: Collection measurements = getMeasurements();
241: if (measurements != null) {
242: Iterator it = measurements.iterator();
243: while (it.hasNext()) {
244: Measurement ms = (Measurement) it.next();
245: Element me = holder.getOwnerDocument().createElement(
246: "measurement");
247: holder.appendChild(me);
248: me.setAttribute("value", String.valueOf(ms.getValue()));
249: me.setAttribute("time", String.valueOf(ms.getTime()));
250: }
251: }
252: }
253: }
|