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.DecimalFormat;
   20   import java.util.ArrayList;
   21   import java.util.List;
   22   import java.util.TreeMap;
   23   
   24   import org.apache.geronimo.monitoring.console.data.Graph;
   25   import org.apache.geronimo.monitoring.console.data.Node;
   26   
   27   public class GraphsBuilder {
   28       private static final long ONE_MINUTE_MS = 60000;
   29   
   30       // constructor
   31       public GraphsBuilder() {
   32       }
   33   
   34       public StatsGraph getStatsGraph(Graph graph) throws Exception {
   35           StatsGraph statsGraph = null;
   36           Node node = graph.getNode();
   37           if (node != null) {
   38               String ip = node.getHost();
   39               String username = node.getUserName();
   40               String password = node.getPassword();
   41               int port = node.getPort();
   42               String protocol = node.getProtocol();
   43               MRCConnector mrc = new MRCConnector(ip, username, password, port, protocol);
   44               Long snapshotDurationMS = mrc.getSnapshotDuration();
   45   
   46               List<Long> dataList1 = new ArrayList<Long>();
   47               List<Long> dataList2 = new ArrayList<Long>();
   48   
   49               boolean hasSecondSeries = (graph.getDataName2() != null) && !graph.getDataName2().equals("time")
   50                       && !graph.getDataName2().equals("null") && !graph.getDataName2().equals("");
   51   
   52               int snapCount;
   53               int timeFrame = graph.getTimeFrame();
   54               if ((timeFrame / 1440 >= 30)) {
   55                   snapCount = 17;
   56               } else {
   57                   if ((timeFrame / 1440) == 7) {
   58                       snapCount = 16;
   59                   } else if ((int) ((timeFrame / (snapshotDurationMS / ONE_MINUTE_MS))) <= 12)
   60                       snapCount = (int) ((timeFrame / (snapshotDurationMS / ONE_MINUTE_MS)));
   61                   else
   62                       snapCount = 12;
   63               }
   64   
   65               List<Long> snapshot_time = new ArrayList<Long>();
   66   
   67               String prettyTimeFrame;
   68               DecimalFormat fmt = new DecimalFormat("0.##");
   69               if (timeFrame / 60 > 24) {
   70                   prettyTimeFrame = fmt.format((float) (timeFrame / 1440)) + " day";
   71               } else {
   72                   if (timeFrame > 60) {
   73                       prettyTimeFrame = fmt.format((float) timeFrame / 60) + " hour";
   74                   } else {
   75                       prettyTimeFrame = fmt.format(timeFrame) + " minute";
   76                   }
   77               }
   78   
   79               int skipCount = (int) ((timeFrame / (snapshotDurationMS / ONE_MINUTE_MS))) / (snapCount);
   80   
   81               snapCount = snapCount + 2;
   82               TreeMap<Long, Long> snapshotList1 = mrc.getSpecificStatistics(graph.getMBeanName(), graph.getDataName1(), snapCount, skipCount, graph.isShowArchive());
   83               TreeMap<Long, Long> snapshotList2 = new TreeMap<Long, Long>();
   84               if (hasSecondSeries) {
   85                   snapshotList2 = mrc.getSpecificStatistics(graph.getMBeanName(), graph.getDataName2(), snapCount, skipCount, graph.isShowArchive());
   86               }
   87               mrc.dispose();
   88               // Check if snapshotList is empty
   89               if (snapshotList1.size() == 0) {
   90                   snapshotList1.put(System.currentTimeMillis(), (long) 0);
   91                   /*
   92                    * If there are not enough snapshots available to fill the
   93                    * requested number, insert some with values of 0 and the
   94                    * proper times.
   95                    */
   96                   while (snapshotList1.size() < snapCount) {
   97                       // Temporary, always is first element (oldest)
   98                       Long timeFix = snapshotList1.firstKey();
   99                       // Calculate appropriate time, add it to the submap,
  100                       // then
  101                       // add
  102                       // that to the tempMap
  103                       snapshotList1.put((timeFix - (snapshotDurationMS * skipCount)), (long) 0);
  104                   }
  105               }
  106               if (snapshotList2.size() == 0) {
  107                   snapshotList2.put(System.currentTimeMillis(), (long) 0);
  108                   while (snapshotList2.size() < snapCount) {
  109                       // Temporary, always is first element (oldest)
  110                       Long timeFix = snapshotList2.firstKey();
  111                       snapshotList2.put((timeFix - (snapshotDurationMS * skipCount)), (long) 0);
  112                   }
  113               }
  114   
  115               for (Long current : snapshotList1.keySet()) {
  116                   snapshot_time.add(current);
  117                   dataList1.add(snapshotList1.get(current));
  118                   if (hasSecondSeries) {
  119                       dataList2.add(snapshotList2.get(current));
  120                   }
  121               }
  122   
  123               String prettyGraphName = ip + " - " + graph.getXlabel() + " - " + prettyTimeFrame;
  124               int snapshotDurationSeconds = (int) (snapshotDurationMS / 1000);
  125               if (hasSecondSeries) {
  126                   statsGraph = new StatsGraph(graph,
  127                           prettyGraphName,
  128                           dataList1,
  129                           dataList2,
  130                           snapshot_time,
  131                           snapshotDurationSeconds
  132                   );
  133               } else {
  134                   statsGraph = new StatsGraph(graph,
  135                           prettyGraphName,
  136                           dataList1,
  137                           snapshot_time,
  138                           snapshotDurationSeconds
  139                   );
  140               }
  141           }
  142           return statsGraph;
  143       }
  144   }

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