1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package com.ebay.carad.os.vitalsigns.util;
22
23 import java.text.DecimalFormat;
24 import java.text.NumberFormat;
25
26 import com.ebay.carad.os.vitalsigns.DataPoint;
27
28 /***
29 * Home for miscellaneous reporting and DB utilities and constants.
30 *
31 * @author Jeremy Kraybill
32 * @author Jeremy Thomerson
33 * @version $Id$
34 */
35 public abstract class ReportUtil {
36
37 /*** Number formatter. */
38 private static final NumberFormat NUMBER_FORMATTER = new DecimalFormat("0.0");
39
40 /***
41 * Attempts to convert an object into a float. If it is not
42 * convertible, it returns 0.0.
43 *
44 * @param data the object to convert
45 * @return the float it converts to, or 0 if not convertible
46 */
47 public static float getFloatFrom(Object data) {
48 float curValue = 0.0f;
49 if (data != null) {
50 try {
51 curValue = Float.parseFloat(data.toString());
52 } catch (NumberFormatException e) {
53
54
55 }
56 }
57 return curValue;
58 }
59
60 /***
61 * Taks a logtime / data row and returns a new map, with logtime shifted by the given amount of milliseconds.
62 * Used for moving dataseries to the right.
63 *
64 * @param row the row to transpose
65 * @param ms the number of milliseconds to move to the right.
66 * @return a new Map
67 */
68 public static DataPoint shiftTime(DataPoint row, long ms) {
69 DataPoint newRow = new DataPoint();
70 long cur = row.getLogTime();
71 newRow.setData(row.getData());
72 newRow.setLogTime(cur + ms);
73 return newRow;
74 }
75
76 /***
77 * Takes a list of float deltas and writes an HTML string of the form "NN/NN/NN/..NN" where NN is expressed as a +/- percentage value.
78 *
79 * @param delta a list of float % deltas (where 1.0 is +100%, -1.0 is -100% etc)
80 * @param moreIsBetter if true, negative values are red and positive are green; otherwise vice versa.
81 * @return a formatted HTML string
82 */
83 public static String getDeltasAsHtml(float[] delta, boolean moreIsBetter) {
84 StringBuffer ret = new StringBuffer();
85 for (int delt = 0; delt < delta.length; delt++) {
86 String deltString = ((delta[delt] == -1.0f) || (delta[delt] == 0.0f)) ? "--" : ((delta[delt] > 0 ? "+" : "") + NUMBER_FORMATTER.format(delta[delt] * 100) + "%");
87 String deltaClass = (delta[delt] >= 0) == (moreIsBetter) ? "goodDelta" : "badDelta";
88 deltString = "<span class=\"" + deltaClass + "\">" + deltString + "</span>";
89 ret.append(deltString);
90 ret.append((delt < 2) ? "/" : "");
91 }
92 return ret.toString();
93 }
94 }