| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package sk.baka.webvm.misc; |
| 20 | |
|
| 21 | |
import java.lang.management.MemoryUsage; |
| 22 | |
import sk.baka.webvm.Graphs; |
| 23 | |
import static sk.baka.webvm.misc.Constants.*; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
public final class DivGraph { |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public static final int MEMSTAT_BAR_HEIGHT = 20; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
private static String drawBarValueAsDiv(final int[] pixels, int i, final GraphStyle style, final int[] values, final int max) { |
| 46 | 0 | final StringBuilder sb = new StringBuilder(); |
| 47 | 0 | final boolean isEmpty = pixels[i] == 0; |
| 48 | 0 | if (isEmpty) { |
| 49 | 0 | return ""; |
| 50 | |
} |
| 51 | 0 | final boolean isLastValue = i == pixels.length - 1; |
| 52 | 0 | drawDivOpening(sb, isLastValue, style, i, pixels[i]); |
| 53 | 0 | drawDivContents(isLastValue, style, sb, values, i, max); |
| 54 | 0 | sb.append("</div>"); |
| 55 | 0 | return sb.toString(); |
| 56 | |
} |
| 57 | |
|
| 58 | |
private static void drawDivContents(final boolean isLastValue, final GraphStyle style, final StringBuilder sb, final int[] values, final int i, final int max) { |
| 59 | |
|
| 60 | 0 | if (!isLastValue) { |
| 61 | 0 | if (style.showValues) { |
| 62 | 0 | sb.append(values[i]); |
| 63 | 0 | if (style.showPercentage) { |
| 64 | 0 | sb.append(" ("); |
| 65 | |
} |
| 66 | |
} |
| 67 | 0 | if (style.showPercentage) { |
| 68 | 0 | if (max <= 0) { |
| 69 | 0 | sb.append('?'); |
| 70 | |
} else { |
| 71 | 0 | sb.append(values[i] * HUNDRED_PERCENT / max); |
| 72 | |
} |
| 73 | 0 | sb.append('%'); |
| 74 | 0 | if (style.showValues) { |
| 75 | 0 | sb.append(')'); |
| 76 | |
} |
| 77 | |
} |
| 78 | |
} |
| 79 | 0 | if (isLastValue || (!style.showValues && !style.showPercentage)) { |
| 80 | |
|
| 81 | 0 | sb.append("<!-- -->"); |
| 82 | |
} |
| 83 | 0 | } |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
private static void drawDivOpening(final StringBuilder sb, final boolean isLastValue, final GraphStyle style, int i, final int pixel) { |
| 94 | 0 | sb.append("<div style=\""); |
| 95 | |
|
| 96 | 0 | if (!isLastValue) { |
| 97 | 0 | sb.append("background-color: "); |
| 98 | 0 | sb.append(style.colors[i]); |
| 99 | 0 | sb.append("; "); |
| 100 | 0 | if (style.fontColors != null) { |
| 101 | 0 | final String fontColor = style.fontColors[i]; |
| 102 | 0 | if (fontColor != null) { |
| 103 | 0 | sb.append("color: "); |
| 104 | 0 | sb.append(fontColor); |
| 105 | 0 | sb.append("; "); |
| 106 | |
} |
| 107 | |
} |
| 108 | |
} |
| 109 | 0 | if (!isLastValue && !style.vertical) { |
| 110 | 0 | sb.append("float: left; "); |
| 111 | |
} |
| 112 | 0 | sb.append(style.vertical ? "height: " : "width: "); |
| 113 | 0 | sb.append(pixel); |
| 114 | 0 | sb.append("px;\">"); |
| 115 | 0 | } |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
private static String getDivStyle(final GraphStyle style, final boolean floatLeft) { |
| 124 | 0 | final StringBuilder sb = new StringBuilder(); |
| 125 | 0 | sb.append("style=\""); |
| 126 | 0 | if (style.border != null) { |
| 127 | 0 | sb.append("border: 1px solid "); |
| 128 | 0 | sb.append(style.border); |
| 129 | 0 | sb.append("; "); |
| 130 | |
} |
| 131 | 0 | if (style.showPercentage || style.showValues) { |
| 132 | 0 | sb.append("text-align: "); |
| 133 | 0 | sb.append(style.vertical ? "center" : "right"); |
| 134 | 0 | sb.append("; "); |
| 135 | |
} |
| 136 | |
|
| 137 | 0 | if (style.vertical && floatLeft) { |
| 138 | 0 | sb.append("float:left; "); |
| 139 | |
} |
| 140 | 0 | sb.append("width: "); |
| 141 | 0 | sb.append(style.width); |
| 142 | 0 | sb.append("px; height: "); |
| 143 | 0 | sb.append(style.height); |
| 144 | 0 | sb.append("px;\""); |
| 145 | 0 | return sb.toString(); |
| 146 | |
} |
| 147 | |
|
| 148 | 0 | private DivGraph() { |
| 149 | 0 | throw new AssertionError(); |
| 150 | |
} |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
public static String drawStackedBar(final GraphStyle style, final int[] values, final int max, final boolean floatLeft) { |
| 161 | 0 | final StringBuilder sb = new StringBuilder(); |
| 162 | 0 | drawStackedBar(style, values, max, floatLeft, sb); |
| 163 | 0 | return sb.toString(); |
| 164 | |
} |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
public static void drawStackedBar(final GraphStyle style, final int[] values, final int max, final boolean floatLeft, final StringBuilder sb) { |
| 175 | 0 | style.validate(); |
| 176 | 0 | if (max < 0) { |
| 177 | 0 | throw new IllegalArgumentException("Invalid max value: " + max); |
| 178 | |
} |
| 179 | 0 | final int[] pixels = toPixels(values, max, style.vertical ? style.height : style.width); |
| 180 | 0 | sb.append("<div "); |
| 181 | 0 | sb.append(getDivStyle(style, floatLeft)); |
| 182 | 0 | sb.append(">"); |
| 183 | |
|
| 184 | |
|
| 185 | 0 | if (style.vertical) { |
| 186 | 0 | for (int i = pixels.length - 1; i >= 0; i--) { |
| 187 | 0 | sb.append(drawBarValueAsDiv(pixels, i, style, values, max)); |
| 188 | |
} |
| 189 | |
} else { |
| 190 | 0 | for (int i = 0; i < pixels.length; i++) { |
| 191 | 0 | sb.append(drawBarValueAsDiv(pixels, i, style, values, max)); |
| 192 | |
} |
| 193 | |
} |
| 194 | 0 | sb.append("</div>"); |
| 195 | 0 | } |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
static int[] toPixels(final int[] values, final int max, final int maxPixels) { |
| 207 | 4 | if (values.length == 0) { |
| 208 | 0 | throw new IllegalArgumentException("values array is empty"); |
| 209 | |
} |
| 210 | 4 | final int[] result = new int[values.length + 1]; |
| 211 | 4 | int i = 0; |
| 212 | |
|
| 213 | 4 | boolean isMaximumReached = false; |
| 214 | 4 | int pixelSize = 0; |
| 215 | 4 | int prevPixelSize = 0; |
| 216 | 10 | for (int value : values) { |
| 217 | 6 | prevPixelSize = pixelSize; |
| 218 | 6 | if (value >= max) { |
| 219 | 1 | isMaximumReached = true; |
| 220 | |
} |
| 221 | 6 | if (isMaximumReached) { |
| 222 | 1 | pixelSize = maxPixels; |
| 223 | |
} else { |
| 224 | 5 | pixelSize = value * maxPixels / max; |
| 225 | |
} |
| 226 | 6 | result[i++] = pixelSize - prevPixelSize; |
| 227 | |
} |
| 228 | 4 | result[values.length] = maxPixels - pixelSize; |
| 229 | 4 | return result; |
| 230 | |
} |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
public static String drawMemoryStatus(final MemoryUsage usage, final int width) { |
| 239 | 0 | final GraphStyle gs = new GraphStyle(); |
| 240 | 0 | gs.vertical = false; |
| 241 | 0 | gs.width = width; |
| 242 | 0 | gs.height = MEMSTAT_BAR_HEIGHT; |
| 243 | 0 | gs.showValues = true; |
| 244 | 0 | gs.colors = new String[]{Graphs.COLOR_BLUE, Graphs.COLOR_BROWN}; |
| 245 | 0 | gs.border = Graphs.COLOR_GREY; |
| 246 | 0 | gs.fontColors = new String[]{Graphs.COLOR_WHITE, null}; |
| 247 | |
int max; |
| 248 | 0 | if (usage.getMax() >= 0) { |
| 249 | 0 | max = (int) usage.getMax(); |
| 250 | |
} else { |
| 251 | 0 | max = (int) (usage.getCommitted() * 7 / 5); |
| 252 | |
} |
| 253 | 0 | if (max <= 0) { |
| 254 | |
|
| 255 | 0 | max = 1; |
| 256 | |
} |
| 257 | 0 | return DivGraph.drawStackedBar(gs, new int[]{(int) usage.getUsed(), (int) usage.getCommitted()}, max, false); |
| 258 | |
} |
| 259 | |
} |