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