Home » geronimo-2.2-source-release » org.apache.geronimo.monitoring.console » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.geronimo.monitoring.console;
   18   
   19   import java.text.Format;
   20   import java.text.SimpleDateFormat;
   21   import java.util.Date;
   22   import java.util.List;
   23   
   24   import org.apache.geronimo.monitoring.console.data.Graph;
   25   
   26   public class StatsGraph {
   27   
   28       private Graph graph;
   29   
   30       private String GraphName;
   31       private int SnapshotDuration;
   32       private int PointCount;
   33       private String GraphJS;
   34   
   35       public StatsGraph(Graph graph,
   36                         String graphName,
   37                         List<Long> dataSet1,
   38                         List<Long> dataSet2,
   39                         List<Long> snapshotTimes,
   40                         int snapshotDuration
   41       ) {
   42           this.graph = graph;
   43   
   44           GraphName = graphName;
   45           SnapshotDuration = snapshotDuration;
   46           PointCount = dataSet1.size();
   47   
   48           String dataDisplay = displayData(graph.getData1operation(), dataSet1, graph.getOperation(), graph.getData2operation(), dataSet2);
   49   
   50           this.GraphJS = buildJavaScript(dataSet1, snapshotTimes, dataDisplay);
   51       }
   52   
   53       private String buildJavaScript(List<Long> dataSet1, List<Long> snapshotTimes, String dataDisplay) {
   54           String GraphJS = "var " + "graph" + graph.getId()
   55                   + " = new dojox.charting.Chart2D(\"" + getDivName() + "\");\n"
   56                   + "graph" + graph.getId()
   57                   + ".addPlot(\"default\", {type: \"Areas\", tension:3});\n" + "graph"
   58                   + graph.getId() + ".setTheme(dojox.charting.themes.PlotKit.blue);\n";
   59   
   60           // Setup the x tick marks on the chart
   61           Format formatter;
   62           if ((graph.getTimeFrame() / 1440) > 7)
   63               formatter = new SimpleDateFormat("M/d");
   64           else {
   65               if ((graph.getTimeFrame() / 60) > 24)
   66                   formatter = new SimpleDateFormat("E a");
   67               else {
   68                   formatter = new SimpleDateFormat("HH:mm");
   69               }
   70           }
   71           GraphJS += "graph" + graph.getId() + ".addAxis(\"x\", {labels: [";
   72           for (int i = 1; i < dataSet1.size(); i++) {
   73               Date date = new Date(snapshotTimes.get(i));
   74               GraphJS += "{value: " + (i) + ", text: '" + formatter.format(date);
   75               if ((i+1) != dataSet1.size())
   76                   GraphJS += "' }, \n";
   77               else
   78               	GraphJS += "' } \n";
   79           }
   80           GraphJS += "]});\n";
   81           GraphJS += "graph" + graph.getId() + ".addAxis(\"y\", {vertical: true});\n";
   82   
   83           GraphJS += "graph" + graph.getId() + ".addSeries(\"Series" + graph.getId()
   84                   + "\", [";
   85           GraphJS = GraphJS + dataDisplay;
   86   
   87           GraphJS = GraphJS + "]);\n";
   88   
   89           GraphJS = GraphJS + "graph" + graph.getId() + ".render();\n";
   90           return GraphJS;
   91       }
   92   
   93       private String displayData(char data1operation, List<Long> dataSet1, String operation, char data2operation, List<Long> dataSet2) {
   94           String graphJS = "";
   95           if (data1operation == 'D' && data2operation == 'D') {
   96               for (int i = 1; i < dataSet1.size(); i++) {
   97                   if ((dataSet1.get(i) - dataSet1.get(i - 1)) < 0)
   98                       dataSet1.set(i - 1, dataSet1.get(i));
   99                   graphJS = graphJS
  100                           + (dataSet1.get(i) - dataSet1.get(i - 1));
  101                   // ensure there is not a division by 0
  102                   graphJS += appendOperation(operation, dataSet2.get(i)
  103                           - dataSet2.get(i - 1));
  104                   if ((i+1) != dataSet1.size())
  105                       graphJS += ",";
  106               }
  107           }
  108           if (data1operation == 'D' && data2operation != 'D') {
  109               for (int i = 1; i < dataSet1.size(); i++) {
  110                   graphJS = graphJS
  111                           + (dataSet1.get(i) - dataSet1.get(i - 1));
  112                   // ensure there is not a division by 0
  113                   graphJS += appendOperation(operation, dataSet2.get(i));
  114                   if ((i+1) != dataSet1.size())
  115                       graphJS += ",\n";
  116               }
  117           }
  118           if (data1operation != 'D' && data2operation == 'D') {
  119               for (int i = 1; i < dataSet1.size(); i++) {
  120                   graphJS = graphJS + dataSet1.get(i);
  121                   // ensure there is not a division by 0
  122                   graphJS += appendOperation(operation, dataSet2.get(i)
  123                           - dataSet2.get(i - 1));
  124                   if ((i+1) != dataSet1.size())
  125                       graphJS += ",\n";
  126               }
  127           }
  128           if (data1operation != 'D' && data2operation != 'D') {
  129               for (int i = 1; i < dataSet1.size(); i++) {
  130                   graphJS = graphJS + dataSet1.get(i);
  131                   // ensure there is not a division by 0
  132                   graphJS += appendOperation(operation, dataSet2.get(i));
  133                   if ((i+1) != dataSet1.size())
  134                       graphJS += ",";
  135               }
  136           }
  137           return graphJS;
  138       }
  139   
  140       public StatsGraph(Graph graph,
  141                         String graphName,
  142                         List<Long> dataSet1,
  143                         List<Long> snapshotTimes, int snapshotDuration
  144       ) {
  145   
  146           this.graph = graph;
  147   
  148           GraphName = graphName;
  149           SnapshotDuration = snapshotDuration;
  150           PointCount = dataSet1.size();
  151           String dataDisplay = displayData(graph.getData1operation(), dataSet1, graph.getOperation());
  152   
  153           this.GraphJS = buildJavaScript(dataSet1, snapshotTimes, dataDisplay);
  154       }
  155   
  156       private String displayData(char data1operation, List<Long> dataSet1, String operation) {
  157           String graphJS = "";
  158           if (data1operation == 'D')
  159               for (int i = 1; i < dataSet1.size(); i++) {
  160                   graphJS = graphJS
  161                           + (dataSet1.get(i) - dataSet1.get(i - 1)) + operation;
  162                   if ((i+1) != dataSet1.size())
  163                       graphJS += ",\n";
  164               }
  165           if (data1operation != 'D')
  166               for (int i = 1; i < dataSet1.size(); i++) {
  167                   graphJS = graphJS + dataSet1.get(i) + operation;
  168                   if ((i+1) != dataSet1.size())
  169                       graphJS += ",\n";
  170               }
  171           return graphJS;
  172       }
  173   
  174       private String appendOperation(String operation, Long number) {
  175           String retval = "";
  176           // ensure there is not a division by 0
  177           if (operation.endsWith("/") && number == 0) {
  178               retval += "*0";
  179           } else {
  180               retval += operation + number;
  181           }
  182           return retval;
  183       }
  184   
  185       public StatsGraph() {
  186   
  187       }
  188   
  189       public void redraw() {
  190   
  191       }
  192   
  193       public String getJS() {
  194           return GraphJS;
  195       }
  196   
  197       public String getDivImplement() {
  198           return "<div id=\"" + getDivName()
  199                   + "\" style=\"height: 220px;\"></div><br><div id='" + getDivName()
  200                   + "Sub' style='text-align: center;'>" + graph.getYlabel() + " vs. "
  201                   + graph.getXlabel() + "</div>" + "\n";
  202       }
  203   
  204       public String getDivName() {
  205           return "graph" + graph.getId() + "Container";
  206       }
  207   
  208       public String getXAxis() {
  209           return graph.getXlabel();
  210       }
  211   
  212       public String getYAxis() {
  213           return graph.getYlabel();
  214       }
  215   
  216       public String getName() {
  217           return GraphName;
  218       }
  219   
  220       public String getDescription() {
  221           return graph.getDescription();
  222       }
  223   
  224       public int getSnapshotDuration() {
  225           return SnapshotDuration;
  226       }
  227   
  228       public int getTimeFrame() {
  229           return graph.getTimeFrame();
  230       }
  231   
  232       public int getPointCount() {
  233           return PointCount;
  234       }
  235   
  236       public String getColor() {
  237           return graph.getColor();
  238       }
  239   }

Home » geronimo-2.2-source-release » org.apache.geronimo.monitoring.console » [javadoc | source]