Produces Bar Chart as Expected with Title and Key, code is fine.

looks good.

Ack :-)

----- Original Message -----
From: "Steve Linabery" <[email protected]>
To: [email protected]
Sent: Tuesday, June 8, 2010 8:00:15 AM GMT +00:00 GMT Britain, Ireland, Portugal
Subject: [deltacloud-devel] [PATCH aggregator] Generate 'Instances By Provider' 
Pie Chart

This requires 'gem install scruffy', as well as the fedora ruby-RMagick package.
Also fixed a bug introduced by a previous commit where a nil option passed to 
graph methods
was resulting in setting a default parameter to zero (since nil.to_i -> 0).
---
 deltacloud-aggregator.spec.in     |    1 +
 src/app/models/graph.rb           |    1 +
 src/app/services/graph_service.rb |   51 ++++++++++++++++++++++++++++++++++---
 src/config/environment.rb         |    1 +
 4 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/deltacloud-aggregator.spec.in b/deltacloud-aggregator.spec.in
index 88ed70e..6baf65e 100644
--- a/deltacloud-aggregator.spec.in
+++ b/deltacloud-aggregator.spec.in
@@ -29,6 +29,7 @@ Requires: postgresql
 Requires: postgresql-server
 Requires: ruby-postgres
 Requires: gnuplot >= 4.2.6
+Requires: ruby-RMagick
 
 %package daemons
 Summary:   Deltacloud Aggregator daemons config
diff --git a/src/app/models/graph.rb b/src/app/models/graph.rb
index f520a82..1c20dc3 100644
--- a/src/app/models/graph.rb
+++ b/src/app/models/graph.rb
@@ -3,6 +3,7 @@ class Graph
 
   QOS_AVG_TIME_TO_SUBMIT = "qos_avg_time_to_submit"
   QUOTA_INSTANCES_IN_USE = "quota_instances_in_use"
+  INSTANCES_BY_PROVIDER_PIE = "instances_by_provider_pie"
   def initialize
     @svg = ""
   end
diff --git a/src/app/services/graph_service.rb 
b/src/app/services/graph_service.rb
index c2cf4c5..f56bbfc 100644
--- a/src/app/services/graph_service.rb
+++ b/src/app/services/graph_service.rb
@@ -1,6 +1,7 @@
 class GraphService
   require 'gnuplot'
   require 'nokogiri'
+  require 'scruffy'
 
   def self.dashboard_quota (user,opts = {})
     #FIXME add permission checks to filter what graphs user can get
@@ -44,6 +45,12 @@ class GraphService
     graphs
   end
 
+  def self.dashboard_instances_by_provider (user,opts = {})
+    #FIXME add permission checks to see if user can view this graph
+    graphs = Hash.new
+    graphs[Graph::INSTANCES_BY_PROVIDER_PIE] = instances_by_provider_pie(opts)
+  end
+
   private
   def self.gnuplot_open( persist=false )
     cmd = Gnuplot.gnuplot( persist ) or raise 'gnuplot not found'
@@ -56,8 +63,8 @@ class GraphService
     unless max_value = opts[:max_value]
       max_value = 100 unless max_value = 
Quota.maximum('maximum_running_instances')
     end
-    height = 80 unless height = opts[:height].to_i
-    width = 150 unless width  = opts[:width].to_i
+    height = 80 unless opts[:height].nil? ? nil : height = opts[:height].to_i
+    width = 150 unless  opts[:width].nil? ? nil : width  = opts[:width].to_i
 
 
     raw_svg = ""
@@ -153,8 +160,8 @@ class GraphService
   def self.qos_avg_time_to_submit_graph (provider, opts = {})
     #things we're checking for in opts: :height, :width
 
-    height = 60 unless height = opts[:height].to_i
-    width = 100 unless width  = opts[:width].to_i
+    height = 60 unless opts[:height].nil? ? nil : height = opts[:height].to_i
+    width = 100 unless  opts[:width].nil? ? nil : width  = opts[:width].to_i
 
     graph = Graph.new
     gp = gnuplot_open
@@ -193,4 +200,40 @@ class GraphService
     graph
   end
 
+  def self.instances_by_provider_pie (opts = {})
+    #things we're checking for in opts: :height, :width
+
+    height = 200 unless opts[:height].nil? ? nil : height = opts[:height].to_i
+    width =  300 unless  opts[:width].nil? ? nil : width  = opts[:width].to_i
+
+    graph = Graph.new
+
+    mytheme = Scruffy::Themes::Keynote.new
+    mytheme.background = :white
+    mytheme.marker = :black #sets the label text color
+
+    scruffy_graph = Scruffy::Graph.new({:theme => mytheme})
+    scruffy_graph.title = "Instances by Provider"
+    scruffy_graph.renderer = Scruffy::Renderers::Pie.new
+
+    pie_opts = {}
+    #FIXME query Instances table to get this information!
+    pie_opts[:"EC2"] = 55
+    pie_opts[:"RHEV-M"] = 78
+    pie_opts[:other] = 21
+
+    scruffy_graph.add :pie, '', pie_opts
+
+    raw_svg = scruffy_graph.render :width => width, :height => height
+
+    xml = Nokogiri::XML(raw_svg)
+    svg = xml.css 'svg'
+    svg.each do |node|
+      node.set_attribute 'viewBox',"0 0 #{width} #{height}"
+    end
+
+    graph.svg = xml.to_s
+    graph
+  end
+
 end
diff --git a/src/config/environment.rb b/src/config/environment.rb
index 53ed761..919a710 100644
--- a/src/config/environment.rb
+++ b/src/config/environment.rb
@@ -48,6 +48,7 @@ Rails::Initializer.run do |config|
   config.gem "will_paginate"
   config.gem "nokogiri", :version => ">= 1.4.0"
   config.gem "gnuplot"
+  config.gem "scruffy"
 
   config.active_record.observers = :instance_observer, :task_observer
   # Only load the plugins named here, in the order given. By default, all 
plugins
-- 
1.6.6.1

_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel
_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel

Reply via email to