001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.monitoring.console;
017:
018: import java.text.Format;
019: import java.text.SimpleDateFormat;
020: import java.util.ArrayList;
021: import java.util.Date;
022:
023: public class StatsGraph {
024: private String GraphName;
025: private String DivName;
026: private String Description;
027: private String DivDefine;
028: private String DivImplement;
029: private String XAxisLabel;
030: private String YAxisLabel;
031: private int SnapshotDuration;
032: private int TimeFrame;
033: private int PointCount;
034: private String HexColor;
035: private String GraphJS;
036:
037: public StatsGraph(Integer graph_id, String graphName,
038: String description, String xAxisLabel, String yAxisLabel,
039: char data1operation, ArrayList<Object> dataSet1,
040: String operation, char data2operation,
041: ArrayList<Object> dataSet2,
042: ArrayList<Object> snapshotTimes, int snapshotDuration,
043: int timeFrame, String hexColor, float warninglevel1,
044: float warninglevel2) {
045:
046: DivName = "graph" + graph_id + "Container";
047: GraphName = graphName;
048: Description = description;
049: XAxisLabel = xAxisLabel;
050: YAxisLabel = yAxisLabel;
051: SnapshotDuration = snapshotDuration;
052: TimeFrame = timeFrame;
053: PointCount = dataSet1.size();
054: HexColor = hexColor;
055:
056: DivImplement = "<div id=\"" + DivName
057: + "\" style=\"height: 220px;\"></div><br><div id='"
058: + DivName + "Sub' style='text-align: center;'>"
059: + yAxisLabel + " vs. " + xAxisLabel + "</div>" + "\n";
060:
061: GraphJS = "var " + "graph" + graph_id
062: + " = new dojox.charting.Chart2D(\"" + DivName
063: + "\");\n" + "graph" + graph_id
064: + ".addPlot(\"default\", {type: \"Areas\"});\n"
065: + "graph" + graph_id
066: + ".setTheme(dojox.charting.themes.PlotKit.blue);\n";
067:
068: // Setup the x tick marks on the chart
069: Format formatter = new SimpleDateFormat("HH:mm");
070: if ((timeFrame / 1440) > 7)
071: formatter = new SimpleDateFormat("M/d");
072: else {
073: if ((timeFrame / 60) > 24)
074: formatter = new SimpleDateFormat("E a");
075: else {
076: formatter = new SimpleDateFormat("HH:mm");
077: }
078: }
079: GraphJS += "graph" + graph_id + ".addAxis(\"x\", {labels: [";
080: for (int i = 1; i < dataSet1.size(); i++) {
081: Date date = new Date((Long) snapshotTimes.get(i));
082: GraphJS += "{value: " + (i) + ", text: '"
083: + formatter.format(date) + "' }, \n";
084: }
085: GraphJS += "]});\n";
086: GraphJS += "graph" + graph_id
087: + ".addAxis(\"y\", {vertical: true});\n";
088:
089: GraphJS += "graph" + graph_id + ".addSeries(\"Series"
090: + graph_id + "\", [";
091: if (data1operation == 'D' && data2operation == 'D') {
092: for (int i = 1; i < dataSet1.size(); i++) {
093: if (((Long) dataSet1.get(i) - (Long) dataSet1
094: .get(i - 1)) < 0)
095: dataSet1.set(i - 1, dataSet1.get(i));
096: GraphJS = GraphJS
097: + ((Long) dataSet1.get(i) - (Long) dataSet1
098: .get(i - 1));
099: // ensure there is not a division by 0
100: GraphJS += appendOperation(operation, (Long) dataSet2
101: .get(i)
102: - (Long) dataSet2.get(i - 1));
103: GraphJS += ",";
104: }
105: }
106: if (data1operation == 'D' && data2operation != 'D') {
107: for (int i = 1; i < dataSet1.size(); i++) {
108: GraphJS = GraphJS
109: + ((Long) dataSet1.get(i) - (Long) dataSet1
110: .get(i - 1));
111: // ensure there is not a division by 0
112: GraphJS += appendOperation(operation, (Long) dataSet2
113: .get(i));
114: GraphJS += ",";
115: }
116: }
117: if (data1operation != 'D' && data2operation == 'D') {
118: for (int i = 1; i < dataSet1.size(); i++) {
119: GraphJS = GraphJS + dataSet1.get(i);
120: // ensure there is not a division by 0
121: GraphJS += appendOperation(operation, (Long) dataSet2
122: .get(i)
123: - (Long) dataSet2.get(i - 1));
124: GraphJS += ",";
125: }
126: }
127: if (data1operation != 'D' && data2operation != 'D') {
128: for (int i = 1; i < dataSet1.size(); i++) {
129: GraphJS = GraphJS + dataSet1.get(i);
130: // ensure there is not a division by 0
131: GraphJS += appendOperation(operation, (Long) dataSet2
132: .get(i));
133: GraphJS += ",";
134: }
135: }
136:
137: GraphJS = GraphJS + "]);\n";
138:
139: GraphJS = GraphJS + "graph" + graph_id + ".render();\n";
140:
141: }
142:
143: public StatsGraph(Integer graph_id, String graphName,
144: String description, String xAxisLabel, String yAxisLabel,
145: char data1operation, ArrayList<Object> dataSet1,
146: String operation, ArrayList<Object> snapshotTimes,
147: int snapshotDuration, int timeFrame, String hexColor,
148: float warninglevel1, float warninglevel2) {
149:
150: DivName = "graph" + graph_id + "Container";
151: GraphName = graphName;
152: Description = description;
153: XAxisLabel = xAxisLabel;
154: YAxisLabel = yAxisLabel;
155: SnapshotDuration = snapshotDuration;
156: TimeFrame = timeFrame;
157: PointCount = dataSet1.size();
158: HexColor = hexColor;
159:
160: DivImplement = "<div id=\"" + DivName
161: + "\" style=\"height: 220px;\"></div><br><div id='"
162: + DivName + "Sub' style='text-align: center;'>"
163: + yAxisLabel + " vs. " + xAxisLabel + "</div>" + "\n";
164:
165: GraphJS = "var " + "graph" + graph_id
166: + " = new dojox.charting.Chart2D(\"" + DivName
167: + "\");\n" + "graph" + graph_id
168: + ".addPlot(\"default\", {type: \"Areas\"});\n"
169: + "graph" + graph_id
170: + ".setTheme(dojox.charting.themes.PlotKit.blue);\n";
171:
172: // Setup the x tick marks on the chart
173: Format formatter = new SimpleDateFormat("HH:mm");
174: if ((timeFrame / 1440) > 7)
175: formatter = new SimpleDateFormat("M/d");
176: else {
177: if ((timeFrame / 60) > 24)
178: formatter = new SimpleDateFormat("E a");
179: else {
180: formatter = new SimpleDateFormat("HH:mm");
181: }
182: }
183: GraphJS += "graph" + graph_id + ".addAxis(\"x\", {labels: [";
184: for (int i = 1; i < dataSet1.size(); i++) {
185: Date date = new Date((Long) snapshotTimes.get(i));
186: GraphJS += "{value: " + (i) + ", text: '"
187: + formatter.format(date) + "' }, \n";
188: }
189: GraphJS += "]});\n";
190: GraphJS += "graph" + graph_id
191: + ".addAxis(\"y\", {vertical: true});\n";
192:
193: GraphJS += "graph" + graph_id + ".addSeries(\"Series"
194: + graph_id + "\", [";
195: if (data1operation == 'D')
196: for (int i = 1; i < dataSet1.size(); i++) {
197: GraphJS = GraphJS
198: + ((Long) dataSet1.get(i) - (Long) dataSet1
199: .get(i - 1)) + operation + ",\n";
200: }
201: if (data1operation != 'D')
202: for (int i = 1; i < dataSet1.size(); i++) {
203: GraphJS = GraphJS + dataSet1.get(i) + operation + ",\n";
204: }
205:
206: GraphJS = GraphJS + "]);\n";
207:
208: GraphJS = GraphJS + "graph" + graph_id + ".render();\n";
209: }
210:
211: private String appendOperation(String operation, Long number) {
212: String retval = "";
213: // ensure there is not a division by 0
214: if (operation.endsWith("/") && number == 0) {
215: retval += "*0";
216: } else {
217: retval += operation + number;
218: }
219: return retval;
220: }
221:
222: public StatsGraph() {
223:
224: }
225:
226: public void redraw() {
227:
228: }
229:
230: public String getJS() {
231: return GraphJS;
232: }
233:
234: public String getDiv() {
235: return DivDefine;
236: }
237:
238: public String getDivImplement() {
239: return DivImplement;
240: }
241:
242: public String getDivName() {
243: return DivName;
244: }
245:
246: public String getXAxis() {
247: return XAxisLabel;
248: }
249:
250: public String getYAxis() {
251: return YAxisLabel;
252: }
253:
254: public String getName() {
255: return GraphName;
256: }
257:
258: public String getDescription() {
259: return Description;
260: }
261:
262: public int getSnapshotDuration() {
263: return SnapshotDuration;
264: }
265:
266: public int getTimeFrame() {
267: return TimeFrame;
268: }
269:
270: public int getPointCount() {
271: return PointCount;
272: }
273:
274: public String getColor() {
275: return HexColor;
276: }
277: }
|