class GroongaQueryLog::Command::Analyzer::HTMLReporter

Private Instance Methods

analyze_parameters() click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 239
        def analyze_parameters
          erb(<<-EOH, __LINE__ + 1)
      <div class="parameters">
        <h3>Analyze Parameters</h3>
        <table>
          <tr><th>Name</th><th>Value</th></tr>
          <tr>
            <th>Slow response threshold</th>
            <td><%= h(@slow_response_threshold) %>sec</td>
          </tr>
          <tr>
            <th>Slow operation threshold</th>
            <td><%= h(@slow_operation_threshold) %>sec</td>
          </tr>
        </table>
      </div>
          EOH
        end
directory_output?() click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 48
def directory_output?
  return false unless @output.is_a?(String)

  return true if File.directory?(@output)
  return true if File.extname(@output).empty?
  return true if @output.end_with?("/")

  false
end
div(attributes, content) click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 431
def div(attributes, content)
  tag("div", attributes, content)
end
erb(content, line, _binding=nil) click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 146
def erb(content, line, _binding=nil)
  _erb = ERB.new(content, nil, "<>")
  eval(_erb.src, _binding || binding, __FILE__, line)
end
finish() click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 62
def finish
  write(footer)
end
format_context(context) click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 411
def format_context(context)
  h(context).gsub(/,/, ",<wbr />")
end
format_elapsed(elapsed, options={}) click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 399
def format_elapsed(elapsed, options={})
  formatted_elapsed = span({:class => "elapsed"}, h("%8.8f" % elapsed))
  if options[:slow?]
    formatted_elapsed = span({:class => "slow"}, formatted_elapsed)
  end
  formatted_elapsed
end
format_ratio(ratio) click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 407
def format_ratio(ratio)
  h("%5.2f%%" % ratio)
end
format_time(time) click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 395
def format_time(time)
  span({:class => "time"}, h(super))
end
header() click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 151
        def header
          erb(<<-EOH, __LINE__ + 1)
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>groonga query analyzer</title>
    <style>
table,
table tr,
table tr th,
table tr td
{
  border: 1px solid black;
}

span.slow
{
  color: red;
}

div.parameters,
div.metrics,
div.workers
{
  float: left;
  padding: 2em;
}

div.parameters h3,
div.metrics h3,
div.workers h3
{
  text-align: center;
}

div.parameters table,
div.metrics table,
div.workers table
{
  margin-right: auto;
  margin-left: auto;
}

div.statistics
{
  clear: both;
}

td.elapsed,
td.ratio,
td.n,
td.n-records
{
  text-align: right;
}

td.name
{
  text-align: center;
}
    </style>
  </head>
  <body>
    <h1>groonga query analyzer</h1>
      EOH
        end
metrics() click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 258
        def metrics
          erb(<<-EOH, __LINE__ + 1)
      <div class="metrics">
        <h3>Metrics</h3>
        <table>
          <tr><th>Name</th><th>Value</th></tr>
          <tr>
            <th># of responses</th>
            <td><%= h(@statistics.n_responses) %></td>
          </tr>
          <tr>
            <th># of slow responses</th>
            <td><%= h(@statistics.n_slow_responses) %></td>
          </tr>
          <tr>
            <th>responses/sec</th>
            <td><%= h(@statistics.responses_per_second) %></td>
          </tr>
          <tr>
            <th>start time</th>
            <td><%= format_time(@statistics.start_time) %></td>
          </tr>
          <tr>
            <th>end time</th>
            <td><%= format_time(@statistics.end_time) %></td>
          </tr>
          <tr>
            <th>period</th>
            <td><%= h(@statistics.period) %>sec</td>
          </tr>
          <tr>
            <th>slow response ratio</th>
            <td><%= h(@statistics.slow_response_ratio) %>%</td>
          </tr>
          <tr>
            <th>total response time</th>
            <td><%= h(@statistics.total_elapsed) %>sec</td>
          </tr>
        </table>
      </div>
          EOH
        end
report_statistic(statistic) click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 85
        def report_statistic(statistic)
          command = statistic.command
          _ = command # XXX: suppress warning
          statistic_html = erb(<<-EOH, __LINE__ + 1, binding)
      <div class="statistic-heading">
        <h3>Command</h3>
        <div class="statistic-metrics">
          [<%= format_time(statistic.start_time) %>
           -
           <%= format_time(statistic.end_time) %>
           (<%= format_elapsed(statistic.elapsed_in_seconds,
                               :slow? => statistic.slow?) %>)]
          (<%= span({:class => "return-code"}, h(statistic.return_code)) %>)
        </div>
<% if @report_command_line %>
        <%= div({:class => "raw-command"}, h(statistic.raw_command)) %>
<% end %>
      </div>
      <div class="statistic-parameters">
        <h3>Parameters</h3>
        <dl>
          <dt>name</dt>
          <dd><%= h(command.name) %></dd>
<% command.arguments.each do |key, value| %>
          <dt><%= h(key) %></dt>
          <dd><%= h(value) %></dd>
<% end %>
         </dl>
      </div>
      <div class="statistic-operations">
        <h3>Operations</h3>
        <table>
          <thead>
            <tr>
              <th>Nth</th>
              <th>Elapsed(sec)</th>
              <th>Name</th>
              <th>N records</th>
              <th>Context</th>
            </tr>
          </thead>
          <tbody>
<% statistic.each_operation.with_index do |operation, i| %>
            <tr>
              <td class="n"><%= h(i + 1) %></td>
              <td class="elapsed">
                <%= format_elapsed(operation[:relative_elapsed_in_seconds],
                                   :slow? => operation[:slow?]) %>
              </td>
              <td class="name"><%= h(operation[:name]) %></td>
              <td class="n-records"><%= h(operation[:n_records]) %></td>
              <td class="context"><%= h(operation[:context]) %></td>
            </tr>
<% end %>
          </tbody>
        </table>
      </div>
          EOH
          write(statistic_html)
        end
report_statistics() click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 79
def report_statistics
  write(statistics_header)
  super
  write(statistics_footer)
end
report_summary() click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 66
        def report_summary
          summary_html = erb(<<-EOH, __LINE__ + 1, binding)
    <h2>Summary</h2>
    <div class="summary">
<%= analyze_parameters %>
<%= metrics %>
<%= workers %>
<%= slow_operations %>
    </div>
          EOH
          write(summary_html)
        end
setup_output() { |output| ... } click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 31
def setup_output
  @directory_output_mode = false
  return super unless directory_output?

  @directory_output_mode = true
  original_output = @output
  begin
    FileUtils.mkdir_p(@output)
    File.open(File.join(@output, "index.html"), "w") do |output|
      @output = output
      yield(@output)
    end
  ensure
    @output = original_output
  end
end
slow_operations() click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 357
        def slow_operations
          erb(<<-EOH, __LINE__ + 1)
      <div class="statistics">
        <h3>Slow Operations</h3>
        <table class="slow-operations">
          <tr>
            <th>total elapsed(sec)</th>
            <th>total elapsed(%)</th>
            <th># of operations</th>
            <th># of operations(%)</th>
            <th>operation name</th>
            <th>context</th>
          </tr>
<% @statistics.each_slow_operation do |grouped_operation| %>
          <tr>
            <td class="elapsed">
              <%= format_elapsed(grouped_operation[:total_elapsed]) %>
            </td>
            <td class="ratio">
              <%= format_ratio(grouped_operation[:total_elapsed_ratio]) %>
            </td>
            <td class="n">
              <%= h(grouped_operation[:n_operations]) %>
            </td>
            <td class="ratio">
              <%= format_ratio(grouped_operation[:n_operations_ratio]) %>
            </td>
            <td class="name"><%= h(grouped_operation[:name]) %></td>
            <td class="context">
              <%= format_context(grouped_operation[:context]) %>
            </td>
          </tr>
<% end %>
        </table>
      </div>
          EOH
        end
span(attributes, content) click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 427
def span(attributes, content)
  tag("span", attributes, content)
end
start() click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 58
def start
  write(header)
end
statistics_header() click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 226
        def statistics_header
          erb(<<-EOH, __LINE__ + 1)
    <h2>Slow Queries</h2>
    <div>
          EOH
        end
tag(name, attributes, content) click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 415
def tag(name, attributes, content)
  html = "<#{name}"
  html_attributes = attributes.collect do |key, value|
    "#{h(key)}=\"#{h(value)}\""
  end
  html << " #{html_attributes.join(' ')}" unless attributes.empty?
  html << ">"
  html << content
  html << "</#{name}>"
  html
end
workers() click to toggle source
# File lib/groonga-query-log/command/analyzer/reporter/html.rb, line 301
        def workers
          html = ""
          html << erb(<<-WORKERS, __LINE__ + 1)
      <div class="workers">
        <h3>Workers</h3>
        <table>
          <tr>
            <th>ID</th>
            <th># of processed requests</th>
            <th>idle time total</th>
            <th>idle time mean</th>
            <th>idle time min</th>
            <th>idle time max</th>
          </tr>
<% @statistics.each_worker do |worker| %>
          <tr>
            <td><%= h(worker.id) %></td>
            <td><%= h(worker.n_statistics) %></td>
            <td><%= h("%.3f" % worker.idle_time_total) %></td>
            <td><%= h("%.3f" % worker.idle_time_mean) %></td>
            <td><%= h("%.3f" % worker.idle_time_min) %></td>
            <td><%= h("%.3f" % worker.idle_time_max) %></td>
          </tr>
<% end %>
        </table>
          WORKERS
          use_charty = false # TODO
          if use_charty
            plotter = Charty::Plotter.new(:google_charts)
            @statistics.each_worker do |worker|
              figure = plotter.curve do
                metrics = worker.metrics
                series(metrics[:timestamp],
                       metrics[:idle_time],
                       label: worker.id)
                xlabel("timestamp")
                ylabel("idle time")
              end
              html << figure.render
              figure = plotter.curve do
                metrics = worker.metrics
                series(metrics[:timestamp],
                       metrics[:elapsed],
                       label: worker.id)
                xlabel("timestamp")
                ylabel("elapsed")
              end
              html << figure.render
            end
          end
          html << erb(<<-WORKERS, __LINE__ + 1)
      </div>
          WORKERS
          html
        end