[PATCH/foreman 1/1] fixes #1321 add click event to the facts and stats pie chart. The click event redirect to the relevant hosts search that match the clicked slice

Signed-off-by: Amos Benari <abenari@redhat.com>

··· --- app/helpers/application_helper.rb | 3 ++- app/helpers/statistics_helper.rb | 12 ++++++------ public/javascripts/charts.js | 18 +++++++++++++++++- 3 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 4cadd8d…60d3f94 100644
— a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -168,7 +168,8 @@ module ApplicationHelper
:class => ‘statistics_pie’,
:‘chart-name’ => name,
:‘chart-title’ => title,

  •              :'chart-data'   => data.to_a.to_json
    
  •              :'chart-data'   => data.to_a.to_json,
    
  •              :'chart-href'   => options[:search] ? "/hosts?search=#{URI.encode(options.delete(:search))}" : ''
               }.merge(options))
    
    end

diff --git a/app/helpers/statistics_helper.rb b/app/helpers/statistics_helper.rb
index d654e1f…9b2ae97 100644
— a/app/helpers/statistics_helper.rb
+++ b/app/helpers/statistics_helper.rb
@@ -2,12 +2,12 @@ module StatisticsHelper
def charts
options = {:class => “statistics_pie small”, :expandable => true, :‘border’ => 0, :show_title => false}
[

  •  pie_chart("os_dist" ,"OS Distribution", @os_count, options),
    
  •  pie_chart("arch_dist" ,"Architecture Distribution", @arch_count, options),
    
  •  pie_chart("env_dist" ,"Environments Distribution", @env_count, options),
    
  •  pie_chart("cpu_num" ,"Number of CPUs", @cpu_count,options),
    
  •  pie_chart("hardware" ,"Hardware", @model_count, options),
    
  •  pie_chart("class_dist" ,"Class Distribution", @klass_count, options),
    
  •  pie_chart("os_dist" ,"OS Distribution", @os_count, options.merge(:search => "facts.operatingsystem=~VAL1~ and facts.operatingsystemrelease=~VAL2~")),
    
  •  pie_chart("arch_dist" ,"Architecture Distribution", @arch_count, options.merge( :search => "facts.architecture=~VAL1~")),
    
  •  pie_chart("env_dist" ,"Environments Distribution", @env_count, options.merge( :search => "environment=~VAL1~" )),
    
  •  pie_chart("cpu_num" ,"Number of CPUs", @cpu_count,options.merge( :search => "facts.processorcount=~VAL1~")),
    
  •  pie_chart("hardware" ,"Hardware", @model_count, options.merge( :search => "facts.manufacturer=~VAL1~")),
    
  •  pie_chart("class_dist" ,"Class Distribution", @klass_count, options.merge( :search => "class=~VAL1~")),
     pie_chart("mem_usage" ,"Average memory usage", [["free memory (GB)",@mem_free],["used memory (GB)",@mem_size-@mem_free]], options),
     pie_chart("swap_usage" ,"Average swap usage", [["free swap (GB)",@swap_free],["used swap (GB)",@swap_size-@swap_free]], options),
     pie_chart("mem_totals" ,"Total memory usage", [["free memory (GB)", @mem_totfree],["used memory (GB)",@mem_totsize-@mem_totfree]],options),
    

diff --git a/public/javascripts/charts.js b/public/javascripts/charts.js
index b785d13…12594b9 100644
— a/public/javascripts/charts.js
+++ b/public/javascripts/charts.js
@@ -61,7 +61,7 @@ function expand_chart(ref){
{
$(‘body’).append(’

’);
$("#"+modal_id).append('
×

’ +title+ ’

')
  •          .append('<div id="' + modal_id + '-body" class="fact_chart modal-body">Loading ...</div>');
    
  •          .append('<div chart-href='+chart.attr("chart-href")+' id="' + modal_id + '-body" class="fact_chart modal-body">Loading ...</div>');
    
    $("#"+modal_id).modal(‘show’);
    stat_pie(modal_id+’-body’, title, data, 0, false, false)
    } else {$("#"+modal_id).modal(‘show’);}
    @@ -75,6 +75,8 @@ function get_pie_chart(div, url) {
    .append(‘
    Loading …
    ’);
    $("#"+div).modal(‘show’);
    $.getJSON(url, function(data) {
  •  var ref = "/hosts?search=facts."+data.name+"~~VAL1~"
    
  •  $("#"+div+"-body").attr('chart-href', ref);
     stat_pie(div+'-body', data.name, data.values,0);
    
    });
    } else {$("#"+div).modal(‘show’);}
    @@ -131,6 +133,20 @@ function stat_pie(name, title, data, border, expandable, show_title) {
    events: {
    click: function(event) {
    var link = $($(’#links-tbl tr td a’)[event.point.x]).attr(‘href’);
  •              if (link == undefined) {
    
  •                link = $($('#'+name)[0]).attr('chart-href');
    
  •                if (link.indexOf("~VAL2~") != -1) {
    
  •                  var strSplit = event.point.name.split(" ");
    
  •                  var val1 = strSplit[0];
    
  •                  var val2 = strSplit[1];
    
  •                  link = link.replace("~VAL2~", val2);
    
  •                } else {
    
  •                  var val1 = event.point.name;
    
  •                  if (val1.indexOf(" ") != -1) val1 = '"' + val1 +'"';
    
  •                }
    
  •                link = link.replace("~VAL1~", val1);
    
  •              }
    
  •              if (link != undefined) {
                   window.location.href = link;
                 }
    


1.7.4.2