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

org.sonar.server.dashboard.widget.timeline.html.erb Maven / Gradle / Ivy

There is a newer version: 7.0
Show newest version
<%
   # Retrieve widget settings
   metric_data_map = {}
   metric_map = {}
   metrics_specified = false
   (1..3).each do |index|
     metric=widget_properties["metric#{index}"]
     # we check that the current resource has the selected metric on the last snapshot
     # => if not, we do not display this metric at all
     if metric
       metrics_specified = true
       if measure(metric)
         metric_data_map[metric.id] = []
         metric_map[metric.id] = metric
       end
     end
   end
   unless metrics_specified
     # No metric has been selected, it's the first time the widget is displayed: 'ncloc' is the default metric
     ncloc = Metric.by_name('ncloc')
     metric_data_map[ncloc.id] = []
     metric_map[ncloc.id] = ncloc
   end
   chartHeight = widget_properties["chartHeight"]
   undefinedToZero = widget_properties["undefinedToZero"]

   unless metric_data_map.values.empty?
     # Retrieve metric trend information
     options = {}
     from_date = dashboard_configuration.from_datetime
     if from_date
       options[:from] = from_date
     end
     # Variables used if undefinedToZero is set to true
     previous_snapshot_id = nil
     previous_created_at = nil
     first_trend_item = true
     metric_previous_snapshot_id = {}
     metric_count_per_snapshot_id = {}
     total_number_of_metrics = metric_map.keys.size()
     TrendsChart.time_machine_measures(@resource, metric_data_map.keys, options).each() do |trend_item|
       sid = trend_item["sid"]
       if undefinedToZero
         if first_trend_item
           first_trend_item = false
           previous_snapshot_id = sid
           previous_created_at = Time.at(trend_item["created_at"].to_i/1000)
         end
         if previous_snapshot_id != sid
           if metric_count_per_snapshot_id[previous_snapshot_id] != total_number_of_metrics
             metric_map.keys.each do |metric_id|
               unless metric_previous_snapshot_id.include?(metric_id)
                 metric_data_map[metric_id] << {:date => previous_created_at, :value => 0.00, :sid => previous_snapshot_id}
                 if metric_count_per_snapshot_id[previous_snapshot_id]
                   metric_count_per_snapshot_id[previous_snapshot_id] += 1
                 else
                   metric_count_per_snapshot_id[previous_snapshot_id] = 1
                 end
               end
             end
           end
           previous_snapshot_id = sid
           previous_created_at = Time.at(trend_item["created_at"].to_i/1000)
           metric_previous_snapshot_id = {}
         end
         metric_previous_snapshot_id[trend_item["metric_id"].to_i] = 1
       end
       if metric_count_per_snapshot_id[sid]
         metric_count_per_snapshot_id[sid] += 1
       else
         metric_count_per_snapshot_id[sid] = 1
       end
       metric_data_map[trend_item["metric_id"].to_i] << {:date => Time.at(trend_item["created_at"].to_i/1000), :value => trend_item["value"], :sid => trend_item["sid"]}
     end

     # Create JS structures to print out in the HTML page
     js_data = "["
     js_snapshots = "["
     js_metrics = "["
     metric_data_map.keys.each_with_index() do |metric_id, index|
       unless metric_data_map[metric_id].empty?
         js_metrics += "\"" + metric_map[metric_id].short_name + "\","
         js_data += "["
         metric_data_map[metric_id].each() do |metric_data|
           # for every metric value, we need to check that the corresponding snapshot has values for each metric
           if metric_count_per_snapshot_id[metric_data[:sid]]==total_number_of_metrics
             m_date = metric_data[:date]
             m_value = sprintf("%0.02f", metric_data[:value])
             m_value_localized = ProjectMeasure.new(:metric => metric_map[metric_id]).format_numeric_value(metric_data[:value], {})
             js_data += "{x:d("
             js_data += m_date.year.to_s
             js_data += ","
             # Need to decrease by 1 the month as the JS Date object start months at 0 (= January)
             js_data += (m_date.month - 1).to_s
             js_data += ","
             js_data += m_date.day.to_s
             js_data += ","
             js_data += m_date.hour.to_s
             js_data += ","
             js_data += m_date.min.to_s
             js_data += ","
             js_data += m_date.sec.to_s
             js_data += "),y:"
             js_data += m_value
             js_data += ",yl:\""
             js_data += m_value_localized
             js_data += "\"},"
             if index == 0
               # we fill the js_snapshots array (no need to do this more than once)
               js_snapshots += "{sid:"
               js_snapshots += metric_data[:sid].to_s
               js_snapshots += ",d:\""
               js_snapshots += human_short_date m_date
               js_snapshots += "\"},"
             end
           end
         end
         js_data = js_data.chomp(',') + "],"
       end
     end
     js_data = js_data.chomp(',') + "]"
     js_snapshots = js_snapshots.chomp(',') + "]"
     js_metrics = js_metrics.chomp(',') + "]"

     # Prepare also event structure if required
     unless widget_properties["hideEvents"]
       events = {}
       unless from_date
         # find the oldest date
         metric_data_map.values.each() do |metric_data_array|
           first_date = metric_data_array[0][:date]
           from_date = first_date if !from_date || from_date > first_date
         end
       end
       Event.find(:all, :conditions => ["component_uuid=? AND event_date>=?", @resource.uuid, from_date.to_i*1000], :order => 'event_date').each() do |event|
         if events[event.event_date]
           events[event.event_date] << event
         else
           date_entry = [event]
           events[event.event_date] = date_entry
         end
       end
       js_events = "["
       events.keys().sort.each() do |e_date|
         e_details = events[e_date]
         js_events += "{sid:"
         js_events += e_details[0].snapshot_id.to_s
         js_events += ",d:d("
         js_events += e_date.year.to_s
         js_events += ","
         # Need to decrease by 1 the month as the JS Date object start months at 0 (= January)
         js_events += (e_date.month - 1).to_s
         js_events += ","
         js_events += e_date.day.to_s
         js_events += ","
         js_events += e_date.hour.to_s
         js_events += ","
         js_events += e_date.min.to_s
         js_events += ","
         js_events += e_date.sec.to_s
         js_events += "),l:["
         e_details.each() do |e|
           js_events += "{n:\""
           js_events += escape_javascript(e.name)
           js_events += "\"},"
         end
         js_events = js_events.chomp(',') + "]},"
       end
       js_events = js_events.chomp(',') + "]"
     end
  end


%>




<% if widget_properties["chartTitle"] %>
  

<%= h(widget_properties["chartTitle"]) -%>

<% end %> <% unless metric_data_map.values.empty? if metric_data_map.values[0].size == 1 %> <%= message('widget.timeline.timeline_not_displayed') -%> <% else %> <% timeline = 'timeline' + widget.id.to_s %>
<% end end %>




© 2015 - 2025 Weber Informatics LLC | Privacy Policy