001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.qos.metrics;
028:
029: import java.io.PrintWriter;
030: import java.text.DateFormat;
031: import java.text.DecimalFormat;
032: import java.text.FieldPosition;
033: import java.util.Date;
034:
035: /**
036: * Utility class with static fields and methods that are useful for
037: * html display of metrics data.
038: *
039: * Colors picks a color for displaying a Metric. The both metric value
040: * and metric credibility a taken into account when choosing the
041: * color. Nine color bins are defined, with each dimension having
042: * three places. The metric value can has thresholds for Ignore,
043: * Normal, Highlight and the credibility has thresholds for Default,
044: * Config, Measured
045: *
046: * The current color scheme uses credibility to gray out the metric value
047: * and uses the background color to indicate if the metric value has
048: * exceeded the threshold
049: */
050: public class ServletUtilities implements Constants {
051: private static final DecimalFormat f2_1 = new DecimalFormat("0.0");
052: private static final DateFormat dateFormatter = DateFormat
053: .getDateTimeInstance();
054:
055: private static final String PALE_YELLOW = "\"#ffffee\"";
056: private static final String PALE_GREEN = "\"#eeffee\"";
057: private static final String PALE_PINK = "\"#ffeeee\"";
058:
059: private static final String LIGHT_GRAY = "\"#cccccc\"";
060: private static final String MEDIUM_GRAY = "\"#888888\"";
061: private static final String BLACK = "\"#000000\"";
062:
063: private static String brightness(Metric metric) {
064: double credibility = metric.getCredibility();
065: if (credibility <= DEFAULT_CREDIBILITY) {
066: return LIGHT_GRAY;
067: } else if (credibility <= USER_DEFAULT_CREDIBILITY) {
068: return MEDIUM_GRAY;
069: } else {
070: return BLACK;
071: }
072: }
073:
074: private static String bgcolor(Metric metric,
075: double uninteresting_value, double threshold,
076: boolean increasing) {
077: double value = metric.doubleValue();
078: if (value == uninteresting_value) {
079: return PALE_YELLOW;
080: } else if (increasing && value >= threshold || !increasing
081: && value <= threshold) {
082: return PALE_PINK;
083: } else {
084: return PALE_GREEN;
085: }
086: }
087:
088: public static String credibilityDoc(double credibility) {
089: if (credibility >= ORACLE_CREDIBILITY)
090: return "Oracle";
091: else if (credibility >= CONFIRMED_MEAS_CREDIBILITY)
092: return "Confirmed Measurement";
093: else if (credibility >= SECOND_MEAS_CREDIBILITY)
094: return "Seconds Measurement";
095: else if (credibility >= MINUTE_MEAS_CREDIBILITY)
096: return "Minutes Measurement";
097: else if (credibility >= HOURLY_MEAS_CREDIBILITY)
098: return "Hourly Measurement";
099: else if (credibility >= USER_BASE_CREDIBILITY)
100: return "User Baseline";
101: else if (credibility >= SYS_BASE_CREDIBILITY)
102: return "System Baseline";
103: else if (credibility >= USER_DEFAULT_CREDIBILITY)
104: return "User Configuration File";
105: else if (credibility >= SYS_DEFAULT_CREDIBILITY)
106: return "System Configuration File";
107: else if (credibility >= DEFAULT_CREDIBILITY)
108: return "Compile Time Default";
109: else if (credibility >= NO_CREDIBILITY)
110: return "NO SOURCE";
111: else
112: return "Bogus value";
113: }
114:
115: private static final FieldPosition FP = new FieldPosition(
116: DecimalFormat.INTEGER_FIELD);
117:
118: public static String mouseDoc(Metric data, double threshold,
119: boolean increasing, DecimalFormat formatter) {
120: StringBuffer buf = new StringBuffer();
121: Date timestamp = new Date(data.getTimestamp());
122: String units = data.getUnits();
123: double credibility = data.getCredibility();
124: String provenance = data.getProvenance();
125: long halflife = data.getHalflife();
126: formatter.format(data.getRawValue(), buf, FP);
127: if (units != null) {
128: buf.append(' ');
129: buf.append(units);
130: }
131: buf.append(" with credibility ");
132: buf.append(credibility);
133:
134: buf.append(" threshold ");
135: if (increasing)
136: buf.append(">");
137: else
138: buf.append("<");
139: buf.append(threshold);
140:
141: if (provenance != null) {
142: buf.append(" from ");
143: buf.append(provenance);
144: }
145:
146: buf.append(" at ");
147: dateFormatter.format(timestamp, buf, FP);
148: if (halflife > 0) {
149: buf.append(" [halflife=");
150: buf.append(halflife);
151: buf.append(']');
152: }
153: return buf.toString();
154: }
155:
156: public static String XMLString(Metric data) {
157: Metric metric = data;
158: Object value = null;
159: if (metric != null)
160: value = metric.getRawValue();
161: StringBuffer buf = new StringBuffer();
162:
163: buf.append("<metric>");
164: if (value == null) {
165: buf.append("<type>Undefined</type>");
166: } else {
167: buf.append("<type>");
168: if (value instanceof Float || value instanceof Double)
169: buf.append("Double");
170: else if (value instanceof Number)
171: buf.append("Long");
172: else if (value instanceof Boolean)
173: buf.append("Boolean");
174: else if (value instanceof String)
175: buf.append("String");
176: else if (value instanceof Character)
177: buf.append("Character");
178: buf.append("</type>");
179: buf.append("<value>");
180: buf.append(value.toString());
181: buf.append("</value>");
182: buf.append("<credibility>");
183: buf.append(metric.getCredibility());
184: buf.append("</credibility>");
185: if (metric.getUnits() != null) {
186: buf.append("<units>");
187: buf.append(metric.getUnits());
188: buf.append("</units>");
189: }
190: if (metric.getProvenance() != null) {
191: buf.append("<provenance>");
192: buf.append(metric.getProvenance());
193: buf.append("</provenance>");
194: }
195: buf.append("<timestamp>");
196: buf.append(metric.getTimestamp());
197: buf.append("</timestamp>");
198: buf.append("<halflife>");
199: buf.append(metric.getHalflife());
200: buf.append("</halflife>");
201: }
202: buf.append("</metric>");
203: return buf.toString();
204: }
205:
206: public static void valueTable(Metric metric,
207: double uninteresting_value, // the SPECIAL! one
208: double threshold, boolean increasing, // polarity of comparison
209: DecimalFormat formatter, PrintWriter out) {
210: if (metric == null) {
211: out.print("<td><no value></td>");
212: return;
213: }
214: String brightness = brightness(metric);
215: String bgcolor = bgcolor(metric, uninteresting_value,
216: threshold, increasing);
217: String mouse_doc = mouseDoc(metric, threshold, increasing,
218: formatter);
219: String value_text = formatter.format(metric.doubleValue());
220:
221: out.print("<td");
222: out.print(" onmouseover=\"window.status='");
223: out.print(mouse_doc);
224: out.print("'; return true;\"");
225:
226: out.print(" onmouseout=\"window.status=''; return true;\"");
227:
228: out.print(" bgcolor=");
229: out.print(bgcolor);
230:
231: out.print(">");
232:
233: out.print("<font color=");
234: out.print(brightness);
235: out.print(">");
236:
237: out.print(value_text);
238:
239: // end <font>
240: out.print("</font>");
241:
242: out.print("</td>");
243: }
244:
245: private static final Metric m1_1 = new MetricImpl(new Double(0.00),
246: DEFAULT_CREDIBILITY, "units", "test");
247: private static final Metric m1_2 = new MetricImpl(new Double(0.00),
248: SYS_DEFAULT_CREDIBILITY, "units", "test");
249: private static final Metric m1_3 = new MetricImpl(new Double(0.00),
250: SECOND_MEAS_CREDIBILITY, "units", "test");
251: private static final Metric m2_1 = new MetricImpl(new Double(0.50),
252: DEFAULT_CREDIBILITY, "units", "test");
253: private static final Metric m2_2 = new MetricImpl(new Double(0.50),
254: SYS_DEFAULT_CREDIBILITY, "units", "test");
255: private static final Metric m2_3 = new MetricImpl(new Double(0.50),
256: SECOND_MEAS_CREDIBILITY, "units", "test");
257: private static final Metric m3_1 = new MetricImpl(new Double(1.00),
258: DEFAULT_CREDIBILITY, "units", "test");
259: private static final Metric m3_2 = new MetricImpl(new Double(1.00),
260: SYS_DEFAULT_CREDIBILITY, "units", "test");
261: private static final Metric m3_3 = new MetricImpl(new Double(1.00),
262: SECOND_MEAS_CREDIBILITY, "units", "test");
263:
264: public static void colorTest(PrintWriter out) {
265: out.print("<table border=1>\n <tr>");
266:
267: out.print("<th>VALUE \\ CRED</th>");
268: out.print("<th>Default</th>");
269: out.print("<th>Config</th>");
270: out.print("<th>Measured</th>");
271: out.print("</tr>");
272:
273: // row "ignore"
274: out.print("<tr><td><b>Ignore</b></td>");
275: valueTable(m1_1, 0.0, 1.0, true, f2_1, out);
276: valueTable(m1_2, 0.0, 1.0, true, f2_1, out);
277: valueTable(m1_3, 0.0, 1.0, true, f2_1, out);
278: out.print("</tr>");
279:
280: // row "Normal"
281: out.print("<tr><td><b>Normal</b></td>");
282: valueTable(m2_1, 0.0, 1.0, true, f2_1, out);
283: valueTable(m2_2, 0.0, 1.0, true, f2_1, out);
284: valueTable(m2_3, 0.0, 1.0, true, f2_1, out);
285: out.print("</tr>");
286:
287: // row "highlight"
288: out.print("<tr><td><b>Highlight</b></td>");
289: valueTable(m3_1, 0.0, 1.0, true, f2_1, out);
290: valueTable(m3_2, 0.0, 1.0, true, f2_1, out);
291: valueTable(m3_3, 0.0, 1.0, true, f2_1, out);
292: out.print("</tr>");
293:
294: out.print("</table>");
295: }
296:
297: }
|