Coverage Report - sk.baka.webvm.WebVMPage
 
Classes in this File Line Coverage Branch Coverage Complexity
WebVMPage
11%
4/35
0%
0/12
2.4
 
 1  
 /**
 2  
  * Copyright 2009 Martin Vysny.
 3  
  *
 4  
  * This file is part of WebVM.
 5  
  *
 6  
  * WebVM is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License, or
 9  
  * (at your option) any later version.
 10  
  *
 11  
  * WebVM is distributed in the hope that it will be useful,
 12  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  
  * GNU General Public License for more details.
 15  
  *
 16  
  * You should have received a copy of the GNU General Public License
 17  
  * along with WebVM.  If not, see <http://www.gnu.org/licenses/>.
 18  
  */
 19  
 package sk.baka.webvm;
 20  
 
 21  
 import java.io.File;
 22  
 import java.lang.management.MemoryUsage;
 23  
 import java.net.URL;
 24  
 import java.util.List;
 25  
 import org.apache.wicket.markup.html.WebPage;
 26  
 import org.apache.wicket.markup.html.basic.Label;
 27  
 import sk.baka.tools.UrlUtils;
 28  
 import sk.baka.webvm.analyzer.HistorySample;
 29  
 import sk.baka.webvm.analyzer.HistorySampler;
 30  
 import sk.baka.webvm.misc.BluffGraph;
 31  
 import sk.baka.webvm.misc.DivGraph;
 32  
 import sk.baka.webvm.misc.GraphStyle;
 33  
 
 34  
 /**
 35  
  * A superclass of all webvm pages.
 36  
  * @author Martin Vysny
 37  
  */
 38  
 public class WebVMPage extends WebPage {
 39  
 
 40  
     private static final long serialVersionUID = 1L;
 41  
     /**
 42  
      * Each page is wrapped in this border.
 43  
      */
 44  
     protected final AppBorder border;
 45  
 
 46  
     /**
 47  
      * Creates new webvm page.
 48  
      */
 49  
     public WebVMPage() {
 50  1
         super();
 51  1
         border = new AppBorder("appBorder");
 52  1
         add(border);
 53  1
     }
 54  
 
 55  
     /**
 56  
      * Draws details for given memory usage object line.
 57  
      * @param history the history to draw
 58  
      * @param wid chain result with this wicket id
 59  
      * @param index the memory usage index to the {@link HistorySample#memUsage} array.
 60  
      */
 61  
     public final void drawMemoryUsageGraph(final List<HistorySample> history, final String wid, final int index) {
 62  0
         if (history.size() == 0) {
 63  0
             border.add(new Label(wid, ""));
 64  0
             return;
 65  
         }
 66  0
         final GraphStyle gs = Graphs.newDefaultStyle();
 67  0
         gs.colors = new String[]{Graphs.COLOR_BLUE, Graphs.COLOR_BROWN};
 68  0
         long maxMem = history.get(0).memPoolUsage[index].getMax();
 69  0
         if (maxMem == -1) {
 70  0
             maxMem = 0;
 71  0
             for (final HistorySample hs : history) {
 72  0
                 final MemoryUsage usage = hs.memPoolUsage[index];
 73  0
                 if (maxMem < usage.getCommitted()) {
 74  0
                     maxMem = usage.getCommitted();
 75  
                 }
 76  0
             }
 77  0
             maxMem = maxMem * 5 / 4;
 78  
         }
 79  0
         final BluffGraph dg = new BluffGraph((int) maxMem, gs);
 80  0
         for (final HistorySample hs : history) {
 81  0
             final MemoryUsage usage = hs.memPoolUsage[index];
 82  0
             dg.add(new int[]{(int) usage.getUsed(), (int) usage.getCommitted()});
 83  0
         }
 84  0
         dg.fillWithZero(HistorySampler.HISTORY_VMSTAT.getHistoryLength(), false);
 85  0
         unescaped(wid, dg.draw());
 86  0
     }
 87  
 
 88  
     /**
 89  
      * Shows given string unescaped.
 90  
      * @param wid the wicket component
 91  
      * @param value the value to show
 92  
      */
 93  
     public final void unescaped(final String wid, final String value) {
 94  0
         final Label l = new Label(wid, value);
 95  0
         l.setEscapeModelStrings(false);
 96  0
         border.add(l);
 97  0
     }
 98  
 
 99  
     /**
 100  
      * Draws a memory usage status for given memory usage object
 101  
      * @param usage the memory usage object, must be in megabytes as int arithmetics is used.
 102  
      * @param wid the wicket component
 103  
      * @param width the width of the bar in pixels.
 104  
      */
 105  
     public final void drawMemoryStatus(final MemoryUsage usage, final String wid, final int width) {
 106  0
         final String bar = DivGraph.drawMemoryStatus(usage, width);
 107  0
         unescaped(wid, bar);
 108  0
     }
 109  
 
 110  
     public static File toFile(final URL url) {
 111  0
         final String file = UrlUtils.toLocalFile(url.toString());
 112  0
         return file == null ? null : new File(file);
 113  
     }
 114  
 }