[PATCH/foreman 1/1] fixes #1321 - Have pie chart graphs link to the hosts the slices represent part 2

Signed-off-by: Jacob McCann <gaferion@gmail.com>

··· --- app/helpers/application_helper.rb | 3 ++- app/helpers/statistics_helper.rb | 12 ++++++------ app/models/fact_value.rb | 2 +- public/javascripts/application.js | 13 +++++++++++-- public/javascripts/charts.js | 22 +++++++++++++++++++++- 5 files changed, 41 insertions(+), 11 deletions(-)

diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index e8b7d40…b7975f2 100644
— a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -167,7 +167,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'  => ''
               }.merge(options))
    
    end

diff --git a/app/helpers/statistics_helper.rb b/app/helpers/statistics_helper.rb
index f687ef9…b6062be 100644
— a/app/helpers/statistics_helper.rb
+++ b/app/helpers/statistics_helper.rb
@@ -2,12 +2,12 @@ module StatisticsHelper

def charts
[

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

diff --git a/app/models/fact_value.rb b/app/models/fact_value.rb
index 98d0871…201f2c9 100644
— a/app/models/fact_value.rb
+++ b/app/models/fact_value.rb
@@ -42,7 +42,7 @@ class FactValue < Puppet::Rails::FactValue
def self.count_each(fact)
hash = {}
all(:select => “value”, :joins => :fact_name, :conditions => {:fact_names => {:name => fact}}).each do |fv|

  •  value = fv.value.strip.humanize
    
  •  value = fv.value.strip
     if hash[value].nil?
       hash[value] = 1
     else
    

diff --git a/public/javascripts/application.js b/public/javascripts/application.js
index 3e45e0c…0d9cf4e 100644
— a/public/javascripts/application.js
+++ b/public/javascripts/application.js
@@ -109,14 +109,23 @@ function pie_chart(div, title, data) {
},
plotOptions: {
pie: {

  •      allowPointSelect: true,
         cursor: 'pointer',
         dataLabels: {
            enabled: true,
            formatter: function() {
               return '<b>'+ this.point.name +'</b>: '+ this.y;
            }
    
  •      }
    
  •      },
    
  •      point: {
    
  •        events: {
    
  •            click: function() {
    
  •              var link = "hosts?search=facts."+title+"+%3D+"+this.name;
    
  •              if (link != undefined) {
    
  •                window.location.href = link;
    
  •              }
    
  •            }
    
  •         }
    
  • }
      }
    
    },
    series: [{
    diff --git a/public/javascripts/charts.js b/public/javascripts/charts.js
    index 40186fe…d8b0f17 100644
    — a/public/javascripts/charts.js
    +++ b/public/javascripts/charts.js
    @@ -73,13 +73,33 @@ function stat_pie(name, title, data) {
    },
    plotOptions: {
    pie: {
  •        allowPointSelect: true,
           cursor: 'pointer',
           dataLabels: {
              enabled: true,
              formatter: function() {
                 return  this.point.name + ': '+ Math.round(this.y*100)/100;
              }
    
  •        },
    
  •        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;
    
  •                }
    
  •                link = link.replace("~VAL1~", val1);
    
  •              }
    
  •              if (link != undefined) {
    
  •                window.location.href = link;
    
  •              }
    
  •            }
           }
        }
     },
    


1.7.5.4

Some notes I wanted to add to this:

  • This is based off of the latest nightly I got yesterday morning
    (11/14). I applied the latest nightly to my env, applied the patch
    from feature 1278 and then made the above changes.
  • I applied the changes I made in my environment to the git master
    branch. I started by looking at the develop branch but too many
    things moved around and some things changed for me to make a 1 for 1.
    But I do think what I have here can be cleaned up and easily applied
    to it. Not sure I have the time to do that.
    ** One thing the worries me on this front was that fact value charts
    went from 'pie_chart' to 'stat_chart'. The reason this worries me is
    to get the fact charts to be clickable I had to use:
  •    point: {
    
  •        events: {
    
  •            click: function() {
    
  •              var link = &quot;hosts?search=facts.&quot;+title+&quot;+%3D
    

+"+this.name;

  •              if (link != undefined) {
    
  •                window.location.href = link;
    
  •              }
    
  •            }
    
  •         }
    
  •     }
    

Instead of using the click event in 'pie' like stat_pie does. I tried
it inside of 'pie' at first but could not get it to work in the fact
charts. Maybe though since stat_pie works fine on dashboard and by
having the fact charts use that it will just work? Also, I use
different 'link's for fact charts.

  • I had to remove humanize from the app/models/fact_value.rb so that
    the value I use to search on was the correct case, otherwise search
    results from hosts?search=facts.blahblah would return nothing since
    something like UNIXTeam1 would become Unixteam1.
  • The thing I'm least proud of is the ~VAL1~ and ~VAL2~ placeholders I
    have in the link for mainly the statistics graphs. Hope someone can
    come up with something better. But it works!

So I hope this can be of use. This shows basically what I was aiming
for in feature 1278 - Have pie chart graphs link to the hosts the
slices represent … for all graphs that represent a set of hosts :wink:

··· On Nov 15, 7:49 am, Jacob McCann wrote: > Signed-off-by: Jacob McCann > --- > app/helpers/application_helper.rb | 3 ++- > app/helpers/statistics_helper.rb | 12 ++++++------ > app/models/fact_value.rb | 2 +- > public/javascripts/application.js | 13 +++++++++++-- > public/javascripts/charts.js | 22 +++++++++++++++++++++- > 5 files changed, 41 insertions(+), 11 deletions(-) > > diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb > index e8b7d40..b7975f2 100644 > --- a/app/helpers/application_helper.rb > +++ b/app/helpers/application_helper.rb > @@ -167,7 +167,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' => '' > }.merge(options)) > end > > diff --git a/app/helpers/statistics_helper.rb b/app/helpers/statistics_helper.rb > index f687ef9..b6062be 100644 > --- a/app/helpers/statistics_helper.rb > +++ b/app/helpers/statistics_helper.rb > @@ -2,12 +2,12 @@ module StatisticsHelper > > def charts > [ > - pie_chart("os_dist" ,"OS Distribution", @os_count, :class => "statistics_pie small"), > - pie_chart("arch_dist" ,"Architecture Distribution", @arch_count, :class => "statistics_pie small"), > - pie_chart("env_dist" ,"Environments Distribution", @env_count, :class => "statistics_pie small"), > - pie_chart("cpu_num" ,"Number of CPUs", @cpu_count, :class => "statistics_pie small"), > - pie_chart("hardware" ,"Hardware", @model_count, :class => "statistics_pie small"), > - pie_chart("class_dist" ,"Class Distribution", @klass_count, :class => "statistics_pie small"), > + pie_chart("os_dist" ,"OS Distribution", @os_count, { :class => "statistics_pie small", :'chart-href' => "hosts?search=facts.operatingsystem%3D~VAL1~+and+facts.operatingsystemrelea se%3D~VAL2~" }), > + pie_chart("arch_dist" ,"Architecture Distribution", @arch_count, { :class => "statistics_pie small", :'chart-href' => "hosts?search=facts.architecture%3D~VAL1~" }), > + pie_chart("env_dist" ,"Environments Distribution", @env_count, { :class => "statistics_pie small", :'chart-href' => "hosts?search=environment%3D~VAL1~" }), > + pie_chart("cpu_num" ,"Number of CPUs", @cpu_count, { :class => "statistics_pie small", :'chart-href' => "hosts?search=facts.processorcount%3D~VAL1~" }), > + pie_chart("hardware" ,"Hardware", @model_count, { :class => "statistics_pie small", :'chart-href' => "hosts?search=facts.manufacturer%3D~VAL1~" }), > + pie_chart("class_dist" ,"Class Distribution", @klass_count, { :class => "statistics_pie small", :'chart-href' => "hosts?search=class%3D~VAL1~" }), > pie_chart("mem_usage" ,"Average memory usage", [["free memory (GB)",@mem_free],["used memory (GB)",@mem_size-@mem_free]], :class => "statistics_pie small"), > pie_chart("swap_usage" ,"Average swap usage", [["free swap (GB)",@swap_free],["used swap (GB)",@swap_size-@swap_free]], :class => "statistics_pie small"), > pie_chart("mem_totals" ,"Total memory usage", [["free memory (GB)", @mem_totfree],["used memory (GB)",@mem_totsize-@mem_totfree]], :class => "statistics_pie small"), > diff --git a/app/models/fact_value.rb b/app/models/fact_value.rb > index 98d0871..201f2c9 100644 > --- a/app/models/fact_value.rb > +++ b/app/models/fact_value.rb > @@ -42,7 +42,7 @@ class FactValue < Puppet::Rails::FactValue > def self.count_each(fact) > hash = {} > all(:select => "value", :joins => :fact_name, :conditions => {:fact_names => {:name => fact}}).each do |fv| > - value = fv.value.strip.humanize > + value = fv.value.strip > if hash[value].nil? > hash[value] = 1 > else > diff --git a/public/javascripts/application.js b/public/javascripts/application.js > index 3e45e0c..0d9cf4e 100644 > --- a/public/javascripts/application.js > +++ b/public/javascripts/application.js > @@ -109,14 +109,23 @@ function pie_chart(div, title, data) { > }, > plotOptions: { > pie: { > - allowPointSelect: true, > cursor: 'pointer', > dataLabels: { > enabled: true, > formatter: function() { > return ''+ this.point.name +': '+ this.y; > } > - } > + }, > + point: { > + events: { > + click: function() { > + var link = "hosts?search=facts."+title+"+%3D+"+this.name; > + if (link != undefined) { > + window.location.href = link; > + } > + } > + } > + } > } > }, > series: [{ > diff --git a/public/javascripts/charts.js b/public/javascripts/charts.js > index 40186fe..d8b0f17 100644 > --- a/public/javascripts/charts.js > +++ b/public/javascripts/charts.js > @@ -73,13 +73,33 @@ function stat_pie(name, title, data) { > }, > plotOptions: { > pie: { > - allowPointSelect: true, > cursor: 'pointer', > dataLabels: { > enabled: true, > formatter: function() { > return this.point.name + ': '+ Math.round(this.y*100)/100; > } > + }, > + 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; > + } > + link = link.replace("~VAL1~", val1); > + } > + if (link != undefined) { > + window.location.href = link; > + } > + } > } > } > }, > -- > 1.7.5.4