aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/profiler/chart/HtmlChartVisitor.java
blob: 4649326b312772889312a112933a813add797d55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.profiler.chart;

import java.io.PrintStream;
import java.util.List;

/**
 * {@link ChartVisitor} that builds HTML from the visited chart and prints it
 * out to the given {@link PrintStream}.
 */
public class HtmlChartVisitor implements ChartVisitor {

  /** The default width of a second in the chart. */
  private static final int DEFAULT_PIXEL_PER_SECOND = 50;

  /** The horizontal offset of second zero. */
  private static final int H_OFFSET = 40;

  /** The font size of the row labels. */
  private static final int ROW_LABEL_FONT_SIZE = 7;

  /** The height of a bar in pixels. */
  private static final int BAR_HEIGHT = 8;

  /** The space between twp bars in pixels. */
  private static final int BAR_SPACE = 2;

  /** The height of a row. */
  private static final int ROW_HEIGHT = BAR_HEIGHT + BAR_SPACE;

  /** The {@link PrintStream} to output the HTML to. */
  private final PrintStream out;

  /** The maxmimum stop time of any bar in the chart. */
  private long maxStop;

  /** The width of a second in the chart. */
  private final int pixelsPerSecond;

  /**
   * Creates the visitor, with a default width of a second of 50 pixels.
   *
   * @param out the {@link PrintStream} to output the HTML to
   */
  public HtmlChartVisitor(PrintStream out) {
    this(out, DEFAULT_PIXEL_PER_SECOND);
  }

  /**
   * Creates the visitor.
   *
   * @param out the {@link PrintStream} to output the HTML to
   * @param pixelsPerSecond The width of a second in the chart. (In pixels)
   */
  public HtmlChartVisitor(PrintStream out, int pixelsPerSecond) {
    this.out = out;
    this.pixelsPerSecond = pixelsPerSecond;
  }

  @Override
  public void visit(Chart chart) {
    maxStop = chart.getMaxStop();

    printContentBox();

    heading("Tasks", 2);
    out.println("<p>To get more information about a task point the mouse at one of the bars.</p>");

    out.printf("<div style='position:relative; height: %dpx; margin: %dpx'>\n",
        chart.getRowCount() * ROW_HEIGHT, H_OFFSET + 10);
  }

  @Override
  public void endVisit(Chart chart) {
    printTimeAxis(chart);
    out.println("</div>");

    heading("Legend", 2);
    printLegend(chart.getSortedTypes());
}

  @Override
  public void visit(ChartColumn column) {
    int width = scale(column.getWidth());
    if (width == 0) {
      return;
    }
    int left = scale(column.getStart());
    int height = column.getRowCount() * ROW_HEIGHT;
    String style = chartTypeNameAsCSSClass(column.getType().getName());
    box(left, 0, width, height, style, column.getLabel(), 10);
  }


  @Override
  public void visit(ChartRow slot) {
    String style = slot.getIndex() % 2 == 0 ? "shade-even" : "shade-odd";
    int top = slot.getIndex() * ROW_HEIGHT;
    int width = scale(maxStop) + 1;

    label(-H_OFFSET, top, width + H_OFFSET, ROW_HEIGHT, ROW_LABEL_FONT_SIZE, slot.getId());
    box(0, top, width, ROW_HEIGHT, style, "", 0);
  }

  @Override
  public void visit(ChartBar bar) {
    int width = scale(bar.getWidth());
    if (width == 0) {
      return;
    }
    int left = scale(bar.getStart());
    int top = bar.getRow().getIndex() * ROW_HEIGHT;
    String style = chartTypeNameAsCSSClass(bar.getType().getName());
    if (bar.getHighlight()) {
      style += "-highlight";
    }
    box(left, top + 2, width, BAR_HEIGHT, style, bar.getLabel(), 20);
  }

  @Override
  public void visit(ChartLine chartLine) {
    int start = chartLine.getStartRow().getIndex() * ROW_HEIGHT;
    int stop = chartLine.getStopRow().getIndex() * ROW_HEIGHT;
    int time = scale(chartLine.getStartTime());

    if (start < stop) {
      verticalLine(time, start + 1, 1, (stop - start) + ROW_HEIGHT, Color.RED);
    } else {
      verticalLine(time, stop + 1, 1, (start - stop) + ROW_HEIGHT, Color.RED);
    }
  }

  /**
   * Converts the given value from the bar of the chart to pixels.
   */
  private int scale(long value) {
    return (int) (value / (1000000000L / pixelsPerSecond));
  }

  /**
   * Prints a box with links to the sections of the generated HTML document.
   */
  private void printContentBox() {
    out.println("<div style='position:fixed; top:1em; right:1em; z-index:50; padding: 1ex;"
        + "border:1px solid #888; background-color:#eee; width:100px'><h3>Content</h3>");
    out.println("<p style='text-align:left;font-size:small;margin:2px'>"
        + "<a href='#Tasks'>Tasks</a></p>");
    out.println("<p style='text-align:left;font-size:small;margin:2px'>"
        + "<a href='#Legend'>Legend</a></p>");
    out.println("<p style='text-align:left;font-size:small;margin:2px'>"
        + "<a href='#Statistics'>Statistics</a></p></div>");
  }

  /**
   * Prints the time axis of the chart and vertical lines for every second.
   */
  private void printTimeAxis(Chart chart) {
    int location = 0;
    int second = 0;
    int end = scale(chart.getMaxStop());
    while (location < end) {
      label(location + 4, -17, pixelsPerSecond, ROW_HEIGHT, 0, second + "s");
      verticalLine(location, -20, 1, chart.getRowCount() * ROW_HEIGHT + 20, Color.GRAY);
      location += pixelsPerSecond;
      second += 1;
    }
  }

  public void printCss(List<ChartBarType> types) {
    out.println("<style type=\"text/css\"><!--");
    out.println("body { font-family: Sans; }");
    out.printf("div.shade-even { position:absolute; border: 0px; background-color:#dddddd }\n");
    out.printf("div.shade-odd { position:absolute; border: 0px; background-color:#eeeeee }\n");
    for (ChartBarType type : types) {
      String name = chartTypeNameAsCSSClass(type.getName());
      String color = formatColor(type.getColor());

      out.printf(
          "div.%s-border { position:absolute; border:1px solid grey; background-color:%s }\n",
          name, color);
      out.printf(
          "div.%s-highlight { position:absolute; border:1px solid red; background-color:%s }\n",
          name, color);
      out.printf("div.%s { position:absolute; border:0px; margin:1px; background-color:%s }\n",
          name, color);
    }
    out.println("--></style>");
  }

  /**
   * Prints the legend for the chart at the current position in the document. The
   * legend is printed in columns of 10 rows each.
   *
   * @param types the list of {@link ChartBarType}s to print in the legend.
   */
  private void printLegend(List<ChartBarType> types) {
    final int boxHeight = 20;
    final int lineHeight = 25;
    final int entriesPerColumn = 10;
    final int legendWidth = 350;
    int legendHeight;
    if (types.size() / entriesPerColumn >= 1) {
      legendHeight = entriesPerColumn;
    } else {
      legendHeight = types.size() % entriesPerColumn;
    }

    out.printf("<div style='position:relative; height: %dpx;'>",
        (legendHeight + 1) * lineHeight);

    int left = -legendWidth;
    int top;
    int i = 0;
    for (ChartBarType type : types) {
      if (i % entriesPerColumn == 0) {
        left += legendWidth;
        i = 0;
      }
      top = lineHeight * i;
      String style = chartTypeNameAsCSSClass(type.getName()) + "-border";
      box(left, top, boxHeight, boxHeight, style, type.getName(), 0);
      label(left + lineHeight + 10, top, legendWidth - 10, boxHeight, 0, type.getName());
      i++;
    }
    out.println("</div>");
  }

  /**
   * Prints a head-line at the current position in the document.
   *
   * @param text the text to print
   * @param level the headline level
   */
  private void heading(String text, int level) {
    anchor(text);
    out.printf("<h%d >%s</h%d>\n", level, text, level);
  }

  /**
   * Prints a box with the given location, size, background color and border.
   *
   * @param x the x location of the top left corner of the box
   * @param y the y location of the top left corner of the box
   * @param width the width location of the box
   * @param height the height location of the box
   * @param style the CSS style class to use for the box
   * @param title the text displayed when the mouse hovers over the box
   */
  private void box(int x, int y, int width, int height, String style, String title, int zIndex) {
    out.printf("<div class=\"%s\" title=\"%s\" "
        + "style=\"left:%dpx; top:%dpx; width:%dpx; height:%dpx; z-index:%d\"></div>\n",
        style, title, x, y, width, height, zIndex);
  }

  /**
   * Prints a label with the given location, size, background color and border.
   *
   * @param x the x location of the top left corner of the box
   * @param y the y location of the top left corner of the box
   * @param width the width location of the box
   * @param height the height location of the box
   * @param fontSize the font size of text in the box, 0 for default
   * @param text the text displayed in the box
   */
  private void label(int x, int y, int width, int height, int fontSize, String text) {
    if (fontSize > 0) {
      out.printf("<div style=\"position:absolute; left:%dpx; top:%dpx; width:%dpx; "
          + "height:%dpx; font-size:%dpt\">%s</div>\n",
          x, y, width, height, fontSize, text);
    } else {
      out.printf("<div style=\"position:absolute; left:%dpx; top:%dpx; width:%dpx; "
          + "height:%dpx\">%s</div>\n",
          x, y, width, height, text);
    }
  }

  /**
   * Prints a vertical line of given width, height and color at the given
   * location.
   *
   * @param x the x location of the start point of the line
   * @param y the y location of the start point of the line
   * @param width the width of the line
   * @param length the length of the line
   * @param color the color of the line
   */
  private void verticalLine(int x, int y, int width, int length, Color color) {
    out.printf("<div style='position: absolute; left: %dpx; top: %dpx; width: %dpx; "
        + "height: %dpx; border-left: %dpx solid %s'" + "></div>\n",
        x, y, width, length, width, formatColor(color));
  }

  /**
   * Prints an HTML anchor with the given name,
   */
  private void anchor(String name) {
    out.println("<a name='" + name + "'/>");
  }

  /**
   * Formats the given {@link Color} to a css style color string.
   */
  private String formatColor(Color color) {
    int r = color.getRed();
    int g = color.getGreen();
    int b = color.getBlue();
    int a = color.getAlpha();

    return String.format("rgba(%d,%d,%d,%f)", r, g, b, (a / 255.0));
  }

  /**
   * Transform the name into a form suitable as a css class.
   */
  private String chartTypeNameAsCSSClass(String name) {
    return name.replace(' ', '_');
  }
}