You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timeline.html.erb 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <%
  2. # Retrieve widget settings
  3. metric_data_map = {}
  4. metric_name_map = {}
  5. (1..3).each do |index|
  6. metric=widget_properties["metric#{index}"]
  7. if metric
  8. metric_data_map[metric.id] = []
  9. metric_name_map[metric.id] = metric.short_name
  10. end
  11. end
  12. if metric_data_map.empty?
  13. # No metric has been selected, it's the first time the widget is displayed: 'ncloc' is the default metric
  14. ncloc = Metric.find(:first, :conditions => "name = 'ncloc'")
  15. metric_data_map[ncloc.id] = []
  16. metric_name_map[ncloc.id] = message('metric.ncloc.name')
  17. end
  18. chartHeight = widget_properties["chartHeight"].to_i == 0 ? "null" : widget_properties["chartHeight"]
  19. # Retrieve metric trend information
  20. options = {}
  21. from_date = dashboard_configuration.from_datetime
  22. if from_date
  23. options[:from] = from_date
  24. end
  25. metric_count_per_snapshot_id = {}
  26. TrendsChart.time_machine_measures(@resource, metric_data_map.keys, options).each() do |trend_item|
  27. sid = trend_item["sid"]
  28. if metric_count_per_snapshot_id[sid]
  29. metric_count_per_snapshot_id[sid] += 1
  30. else
  31. metric_count_per_snapshot_id[sid] = 1
  32. end
  33. metric_data_map[trend_item["metric_id"].to_i] << {:date => trend_item["created_at"], :value => trend_item["value"], :sid => trend_item["sid"]}
  34. end
  35. # Create JS structures to print out in the HTML page
  36. js_data = "["
  37. js_snapshots = "["
  38. js_metrics = "["
  39. total_number_of_metrics = metric_name_map.keys.size()
  40. metric_data_map.keys.each_with_index() do |metric_id, index|
  41. unless metric_data_map[metric_id].empty?
  42. js_metrics += "\"" + metric_name_map[metric_id] + "\","
  43. js_data += "["
  44. metric_data_map[metric_id].each() do |metric_data|
  45. # for every metric value, we need to check that the corresponding snapshot has values for each metric (if not, Protovis won't be able to display)
  46. if metric_count_per_snapshot_id[metric_data[:sid]]==total_number_of_metrics
  47. m_date = Time.parse(metric_data[:date])
  48. js_data += "{x:d("
  49. js_data += m_date.year.to_s
  50. js_data += ","
  51. # Need to decrease by 1 the month as the JS Date object start months at 0 (= January)
  52. js_data += (m_date.month - 1).to_s
  53. js_data += ","
  54. js_data += m_date.day.to_s
  55. js_data += ","
  56. js_data += m_date.hour.to_s
  57. js_data += ","
  58. js_data += m_date.min.to_s
  59. js_data += ","
  60. js_data += m_date.sec.to_s
  61. js_data += "),y:"
  62. js_data += sprintf( "%0.02f", metric_data[:value])
  63. js_data += "},"
  64. if index == 0
  65. # we fill the js_snapshots array (no need to do this more than once)
  66. js_snapshots += "{sid:"
  67. js_snapshots += metric_data[:sid].to_s
  68. js_snapshots += ",d:\""
  69. js_snapshots += human_short_date m_date
  70. js_snapshots += "\"},"
  71. end
  72. end
  73. end
  74. js_data = js_data.chomp(',') + "],"
  75. end
  76. end
  77. js_data = js_data.chomp(',') + "]"
  78. js_snapshots = js_snapshots.chomp(',') + "]"
  79. js_metrics = js_metrics.chomp(',') + "]"
  80. # Prepare also event structure if required
  81. unless widget_properties["hideEvents"]
  82. events = {}
  83. unless from_date
  84. # find the oldest date
  85. metric_data_map.values.each() do |metric_data_array|
  86. first_date = Time.parse(metric_data_array[0][:date])
  87. from_date = first_date if !from_date || from_date > first_date
  88. end
  89. end
  90. Event.find(:all, :conditions => ["resource_id=? AND event_date>=?", @resource.id, from_date], :order => 'event_date').each() do |event|
  91. if events[event.event_date]
  92. events[event.event_date] << event
  93. else
  94. date_entry = [event]
  95. events[event.event_date] = date_entry
  96. end
  97. end
  98. js_events = "["
  99. events.keys().sort.each() do |e_date|
  100. e_details = events[e_date]
  101. js_events += "{sid:"
  102. js_events += e_details[0].snapshot_id.to_s
  103. js_events += ",d:d("
  104. js_events += e_date.year.to_s
  105. js_events += ","
  106. # Need to decrease by 1 the month as the JS Date object start months at 0 (= January)
  107. js_events += (e_date.month - 1).to_s
  108. js_events += ","
  109. js_events += e_date.day.to_s
  110. js_events += ","
  111. js_events += e_date.hour.to_s
  112. js_events += ","
  113. js_events += e_date.min.to_s
  114. js_events += ","
  115. js_events += e_date.sec.to_s
  116. js_events += "),l:["
  117. e_details.each() do |e|
  118. js_events += "{n:\""
  119. js_events += e.name
  120. js_events += "\"},"
  121. end
  122. js_events = js_events.chomp(',') + "]},"
  123. end
  124. js_events = js_events.chomp(',') + "]"
  125. end
  126. %>
  127. <% if widget_properties["chartTitle"] %>
  128. <h3 style="text-align: center; margin-bottom: 10px"><%= h(widget_properties["chartTitle"]) -%></h3>
  129. <% end %>
  130. <% if metric_data_map.values[0].size == 1 %>
  131. <span style="color: #777777; font-size: 93%; font-style:italic"><%= message('widget.timeline.timeline_not_displayed') -%></span>
  132. <% else %>
  133. <div id="timeline-chart-<%= widget.id -%>"></div>
  134. <script type="text/javascript+protovis">
  135. function d(y,m,d,h,min,s) {
  136. return new Date(y,m,d,h,min,s);
  137. }
  138. var data = <%= js_data -%>;
  139. var snapshots = <%= js_snapshots -%>;
  140. var metrics = <%= js_metrics -%>;
  141. var events = <%= js_events ? js_events : "null" -%>;
  142. var timeline = new SonarWidgets.Timeline('timeline-chart-<%= widget.id -%>')
  143. .height(<%= chartHeight -%>)
  144. .data(data)
  145. .snapshots(snapshots)
  146. .metrics(metrics)
  147. .events(events);
  148. timeline.render();
  149. </script>
  150. <% end %>