All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.timeline.widget.advanced_timeline.html.erb Maven / Gradle / Ivy

There is a newer version: 1.5
Show newest version
<%
  # metric1 is mandatory, so "metrics" will contain at least this metric
  metrics = [widget_properties['metric1']]
  metrics << widget_properties['metric2'] if widget_properties['metric2']
  metrics << widget_properties['metric3'] if widget_properties['metric3']
  
  # Retrieve metric trend information and sort it by snapshot
  trend_data_by_sid = {}
  available_metric_ids = []
  TrendsChart.time_machine_measures(@resource, metrics.map {|m| m.id}, {}).each() do |trend_item|
    s_id = trend_item["sid"].to_i
    m_id = trend_item["metric_id"].to_i
    available_metric_ids << m_id unless available_metric_ids.include?(m_id)
    
    unless trend_data_by_sid[s_id]
      s_date = trend_item["created_at"]
      # Only Oracle returns a Time object, so let's parse this string if it's not a Time instance
      s_date = Time.parse(trend_item["created_at"]) unless s_date.is_a? Time
      
      trend_data_by_sid[s_id] = {:date => s_date}
    end
    trend_data_by_sid[s_id][m_id] = trend_item["value"]
  end
  
  if trend_data_by_sid.size() < 2
%>

  <%= message('widget.advanced_timeline.no_history') -%>

<%  
  else
    # Filter metrics and remove those which do not have any associated measure
    metrics.reject! {|m| !available_metric_ids.include?(m.id)}
    
    unless metrics.empty?
      # Retrieve events information
      Event.find(:all, :conditions => ["snapshot_id IN (?)", trend_data_by_sid.keys]).each() do |event|
        trend_data_by_sid[event.snapshot_id][:event] = event
      end
      
      # Generate JS data for the graph
      js_data = "new google.visualization.DataTable();data.addColumn('date', 'Date');\n"
      metrics.each_with_index() do |metric, index|
        js_data += "data.addColumn('number', '"
        js_data += metric.short_name
        js_data += "');\n"
        # add the 2 next columns for events only after the 1rst metric
        js_data += "data.addColumn('string', 'title1');\ndata.addColumn('string', 'text1');\n" if index==0
      end
      js_data += "data.addRows(["
      count = 0
      trend_data_by_sid.each() do |s_id, trend_data|
        # Format of a row is as following: [new Date(2008, 1 ,5), 41476, 'Bought Pens','Bought 200k pens', 66467, undefined, undefined],
        count += 1    
        
        # Need to decrease by 1 the month as the JS Date object start months at 0 (= January)
        s_date = trend_data[:date]
        js_data += "[new Date(" + s_date.year.to_s + "," + (s_date.month - 1).to_s + "," + s_date.day.to_s + "), "
        
        # Add first metric
        js_data += (trend_data[metrics[0].id] ? trend_data[metrics[0].id].to_s : 'undefined') + ", "
    
        # Add event if it exists    
        s_event = trend_data[:event]
        if s_event
          js_data += "\"" + s_event.name + "\", " + (s_event.description ? "\""+s_event.description+"\"" : "undefined")
        else
          js_data += "undefined, undefined"
        end
        
        # Add 2nd and 3rd optional metric if they exist
        (1..2).each do |index|
          metric = metrics[index]
          if metric
            js_data += ", " + (trend_data[metric.id] ? trend_data[metric.id].to_s : 'undefined')
          end
        end
        
        js_data += "]\n"
        js_data += "," if count < trend_data_by_sid.size
      end  
      js_data += "])"
      
      # Get the from_date if a period has been selected
      from_date = dashboard_configuration.from_datetime

%>

<% end end %>




© 2015 - 2024 Weber Informatics LLC | Privacy Policy