lib/riemann/dash/helper/renderer.rb in riemann-dash-0.0.3 vs lib/riemann/dash/helper/renderer.rb in riemann-dash-0.0.4

- old
+ new

@@ -45,11 +45,11 @@ def state_grid(states = Dash.client.query) h2('States by Host') + table( *Event.partition(states, :host).map do |host, states| tr( - th(host, class: 'host'), + th(host, :class => 'host'), *Event.sort(states, :service).map do |state| state_short state end ) end @@ -57,11 +57,11 @@ end # Renders a state as the given HTML tag with a % width corresponding to # metric / max. def state_bar(s, opts = {}) - opts = {tag: 'div', max: 1}.merge opts + opts = {:tag => 'div', :max => 1}.merge opts return '' unless s x = s.metric # Text @@ -69,25 +69,34 @@ when Float '%.2f' % x when Integer x.to_s else - '?' + s.state || '?' end # Size - size = begin - (x || 0) * 100 / opts[:max] - rescue ZeroDivisionError - 0 - end + size = case x + when 0 + 0 + when nil + 100 + else + begin + x * 100 / opts[:max] + rescue ZeroDivisionError + 0 + end + end size = "%.2f" % size + time = Time.at(s.time).strftime(Dash.config[:strftime]) + tag opts[:tag], h(text), :class => "state #{s.state}", - style: "opacity: #{age_fraction s.time}; width: #{size}%", - title: s.description + :style => "opacity: #{age_fraction s.time}; width: #{size}%", + :title => "#{s.state}\n#{s.description}\n\n(at #{time})" end # Renders a set of states in a chart. Each row is a given host, each # service is a column. Each state is shown as a bar with an inferred # maximum for the entire service, so you can readily compare multiple @@ -177,16 +186,15 @@ tr( th(service_names[service]), *hosts.map do |host| s = by[[host, service]] td( - s ? state_bar(s, max: maxima[service]) : nil + s ? state_bar(s, :max => maxima[service]) : nil ) end ) - end, - :class => 'chart' + end << {:class => 'chart'} # ruby 1.8.7 this is your fault ) else h2(title) + table( tr( @@ -199,21 +207,21 @@ tr( th(host), *services.map do |service| s = by[[host, service]] td( - s ? state_bar(s, max: maxima[service]) : nil + s ? state_bar(s, :max => maxima[service]) : nil ) end ) - end, - :class => 'chart' + end << + {:class => 'chart'} ) end end # Renders a state as a short tag. - def state_short(s, opts={tag: 'li'}) + def state_short(s, opts={:tag => 'li'}) if s "<#{opts[:tag]} class=\"state #{s.state}\" style=\"opacity: #{age_fraction s.time}\" title=\"#{h s.description}\">#{h s.host} #{h s.service}</#{opts[:tag]}>" else "<#{opts[:tag]} class=\"service\"></#{opts[:tag]}>" end