[PATCH 2/2] RM3509 generic-ize chart code to enable easier re-use

Tzu-Mainn Chen tzumainn at redhat.com
Thu Jul 19 17:30:32 UTC 2012


---
 src/app/controllers/logs_controller.rb      |   41 ++++--------------
 src/app/controllers/providers_controller.rb |   26 +++---------
 src/app/util/chart_datasets.rb              |   59 +++++++++++++++++++++++++++
 src/app/views/charts/_time_chart.html.haml  |   25 +++++++++++
 src/app/views/logs/_pretty_view.html.haml   |   25 +-----------
 src/app/views/providers/index.html.haml     |   25 +-----------
 6 files changed, 101 insertions(+), 100 deletions(-)
 create mode 100644 src/app/util/chart_datasets.rb
 create mode 100644 src/app/views/charts/_time_chart.html.haml

diff --git a/src/app/controllers/logs_controller.rb b/src/app/controllers/logs_controller.rb
index 316b375..06ff76a 100644
--- a/src/app/controllers/logs_controller.rb
+++ b/src/app/controllers/logs_controller.rb
@@ -99,7 +99,7 @@ class LogsController < ApplicationController
       Date.civil(params[:from_date][:year].to_i,
                  params[:from_date][:month].to_i,
                  params[:from_date][:day].to_i)
-    @to_date = params[:to_date].nil? ? Date.today + 1.days :
+    @to_date = params[:to_date].nil? ? Date.today :
       Date.civil(params[:to_date][:year].to_i,
                  params[:to_date][:month].to_i,
                  params[:to_date][:day].to_i)
@@ -283,27 +283,17 @@ class LogsController < ApplicationController
         find(:all, :conditions => initial_conditions)
     end
 
-    @datasets = Hash.new
-    counts = Hash.new
-
-    counts["All"] = @initial_sources.count
+    @datasets = ChartDatasets.new(@from_date, @to_date)
+    @datasets.increment_count("All", at initial_sources.count)
 
     if @group_options.include?(@group) 
       @initial_sources.each do |source|
         label = get_source_label(source, @group)
-
-        if counts.has_key?(label)
-          counts[label] = counts[label] + 1
-        else
-          counts[label] = 1
-        end
+        @datasets.increment_count(label,1)
       end
     end
 
-    counts.each.map{ |label,count|
-      @datasets[label] = [[@from_date.to_datetime.beginning_of_day.to_i * 1000,
-                           count]]
-    }
+    @datasets.initialize_datasets
 
     @events.each do |event|
       event_timestamp = event.event_time.to_i * 1000
@@ -311,23 +301,21 @@ class LogsController < ApplicationController
       if event.status_code == start_code || event.status_code == end_code
         increment = (event.status_code == end_code) ? -1 : 1
 
-        add_dataset_point("All", event_timestamp, increment, counts)
+        @datasets.add_dataset_point("All", event_timestamp, increment)
 
         if @group_options.include?(@group) 
           label = get_source_label(event.source, @group)
-          add_dataset_point(label, event_timestamp, increment, counts)
+          @datasets.add_dataset_point(label, event_timestamp, increment)
         end
       end
     end
 
-    counts.each.map{ |label,count|
-      @datasets[label] << [@to_date.to_datetime.end_of_day.to_i * 1000, count]
-    }
+    @datasets.finalize_datasets
   end
 
   def get_source_label(source, label_type)
     label = "Unknown"
-    case @group
+    case label_type
     when t('logs.index.pool')
       label = source.pool.name unless source.pool.nil?
     when t('logs.index.provider')
@@ -338,15 +326,4 @@ class LogsController < ApplicationController
 
     label
   end
-
-  def add_dataset_point(label, timestamp, increment, counts)
-    if !@datasets.has_key?(label)
-      counts[label] = 0
-      @datasets[label] = [ [timestamp - 1, 0] ]
-    end
-
-    @datasets[label] << [timestamp - 1, counts[label]]
-    counts[label] = counts[label] + increment
-    @datasets[label] << [timestamp, counts[label]]
-  end
 end
diff --git a/src/app/controllers/providers_controller.rb b/src/app/controllers/providers_controller.rb
index 5142997..a77269c 100644
--- a/src/app/controllers/providers_controller.rb
+++ b/src/app/controllers/providers_controller.rb
@@ -425,12 +425,9 @@ class ProvidersController < ApplicationController
            :include => {:provider_account => [:provider]}
            )
 
-    @datasets = Hash.new
-    counts = Hash.new
+    @datasets = ChartDatasets.new(@from_date, @to_date)
     events = Array.new
 
-    counts["All"] = 0
-
     historical_instances.each do |instance|
       provider_account = instance.provider_account
 
@@ -439,15 +436,11 @@ class ProvidersController < ApplicationController
                   'Unknown' :
                   provider_account.provider.name +
                   " (" + provider_account.name + ")"
-
-        if !counts.has_key?(label)
-          counts[label] = 0
-        end
         
         # see if instance started before from_date
         if instance.time_last_running <= @from_date.to_datetime.beginning_of_day
-          counts[label] = counts[label] + 1
-          counts["All"] = counts["All"] + 1
+          @datasets.increment_count(label,1)
+          @datasets.increment_count("All",1)
         else
           events << {
             "time" => instance.time_last_running,
@@ -467,24 +460,17 @@ class ProvidersController < ApplicationController
       end
     end
 
-    counts.each.map{ |label,count|
-      @datasets[label] = [[@from_date.to_datetime.beginning_of_day.to_i * 1000,
-                           count]]
-    }
+    @datasets.initialize_datasets
 
     events.sort_by {|event| event["time"]}.each do |event|
       timestamp = event["time"].to_i * 1000
       increment = event["increment"]
 
       [ event["label"], "All" ].each { |label|
-        @datasets[label] << [timestamp - 1, counts[label]]
-        counts[label] = counts[label] + increment
-        @datasets[label] << [timestamp, counts[label]]
+        @datasets.add_dataset_point(label,timestamp,increment)
       }
     end
 
-    counts.each.map{ |label,count|
-      @datasets[label] << [@to_date.to_datetime.end_of_day.to_i * 1000, count]
-    }
+    @datasets.finalize_datasets
   end
 end
diff --git a/src/app/util/chart_datasets.rb b/src/app/util/chart_datasets.rb
new file mode 100644
index 0000000..3e9d3e6
--- /dev/null
+++ b/src/app/util/chart_datasets.rb
@@ -0,0 +1,59 @@
+#
+#   Copyright 2011 Red Hat, Inc.
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#
+
+class ChartDatasets
+  attr_accessor :data
+
+  def initialize(from_date, to_date)
+    @data = Hash.new
+    @counts = Hash.new
+    @from_date = from_date
+    @to_date = to_date
+  end
+
+  def initialize_datasets()
+    @counts.each.map{ |label,count|
+      @data[label] = [[@from_date.to_datetime.beginning_of_day.to_i * 1000,
+                           count]]
+    }
+  end
+
+  def finalize_datasets()
+    @counts.each.map{ |label,count|
+      @data[label] << [@to_date.to_datetime.end_of_day.to_i * 1000, count]
+    }
+  end
+
+  def increment_count(label, increment)
+    if !@counts.has_key?(label)
+      @counts[label] = 0
+    end
+
+    @counts[label] = @counts[label] + increment
+  end
+
+  def add_dataset_point(label, timestamp, increment)
+    if !@data.has_key?(label)
+      @data[label] = [[timestamp - 1, 0]]
+    else
+      @data[label] << [timestamp - 1, @counts[label]]
+    end
+
+    increment_count(label,increment)
+    @data[label] << [timestamp, @counts[label]]
+  end
+
+end
diff --git a/src/app/views/charts/_time_chart.html.haml b/src/app/views/charts/_time_chart.html.haml
new file mode 100644
index 0000000..78c7f71
--- /dev/null
+++ b/src/app/views/charts/_time_chart.html.haml
@@ -0,0 +1,25 @@
+%div{:id => "#{name}", :style => "width:#{width};height:#{height};"}
+
+:javascript
+  $(function () {
+    var datasets = [
+      #{ datasets.reject{|label,data| label != "All"}.each.map{ |label,data| "{
+        label: \"" + label + "\",
+        data: [#{ data.each.map{ |p| "[#{p[0]}, #{p[1]}]"}.join(", ") }]
+      },
+      "} }
+      #{ datasets.reject{|label,data| label == "All"}.each.map{ |label,data| "{
+        label: \"" + label + "\",
+        data: [#{ data.each.map{ |p| "[#{p[0]}, #{p[1]}]"}.join(", ") }]
+      }" }.join(",
+      ") }
+    ];
+
+    $.plot($("##{name}"), datasets, {
+      xaxis: {
+        mode: "time",
+        min: #{from_date.to_datetime.beginning_of_day.to_i * 1000},
+        max: #{to_date.to_datetime.end_of_day.to_i * 1000}
+      }
+    });
+  });
diff --git a/src/app/views/logs/_pretty_view.html.haml b/src/app/views/logs/_pretty_view.html.haml
index d559a4f..6167e66 100644
--- a/src/app/views/logs/_pretty_view.html.haml
+++ b/src/app/views/logs/_pretty_view.html.haml
@@ -14,27 +14,4 @@
         = restful_submit_tag t("filter_table.apply_filters"), "index", filter_logs_path, 'POST', :class => 'button', :id => 'apply_logs_filter'
 %br
 
-%div{:id => 'placeholder', :style => 'width:925px;height:600px;'}
-
-:javascript
-  $(function () {
-    var datasets = [
-      {
-        label: "All",
-        data: [#{ @datasets["All"].each.map{ |p| "[#{p[0]}, #{p[1]}]"}.join(", ") }]
-      },
-      #{ @datasets.reject{|label,data| label == "All"}.each.map{ |label,data| "{
-        label: \"" + label + "\",
-        data: [#{ data.each.map{ |p| "[#{p[0]}, #{p[1]}]"}.join(", ") }]
-      }" }.join(",
-      ") }
-    ];
-
-    $.plot($("#placeholder"), datasets, {
-      xaxis: {
-        mode: "time",
-        min: #{@from_date.to_datetime.beginning_of_day.to_i * 1000},
-        max: #{@to_date.to_datetime.end_of_day.to_i * 1000}
-      }
-    });
-  });
+= render :partial => 'charts/time_chart', :locals => { :datasets => @datasets.data, :from_date => @from_date, :to_date => @to_date, :name => 'log-history-graph', :height => '600px', :width => '925px' }
diff --git a/src/app/views/providers/index.html.haml b/src/app/views/providers/index.html.haml
index 9939656..8700da9 100644
--- a/src/app/views/providers/index.html.haml
+++ b/src/app/views/providers/index.html.haml
@@ -45,27 +45,4 @@
           = statistics["historical_error_instances"]
 
   %br
-  %div{:id => 'provider-history-graph', :style => 'width:925px;height:250px;'}
-
-:javascript
-  $(function () {
-    var datasets = [
-      {
-        label: "All",
-        data: [#{ @datasets["All"].each.map{ |p| "[#{p[0]}, #{p[1]}]"}.join(", ") }]
-      },
-      #{ @datasets.reject{|label,data| label == "All"}.each.map{ |label,data| "{
-        label: \"" + label + "\",
-        data: [#{ data.each.map{ |p| "[#{p[0]}, #{p[1]}]"}.join(", ") }]
-      }" }.join(",
-      ") }
-    ];
-
-    $.plot($("#provider-history-graph"), datasets, {
-      xaxis: {
-        mode: "time",
-        min: #{@from_date.to_datetime.beginning_of_day.to_i * 1000},
-        max: #{@to_date.to_datetime.end_of_day.to_i * 1000}
-      }
-    });
-  });
+  = render :partial => 'charts/time_chart', :locals => { :datasets => @datasets.data, :from_date => @from_date, :to_date => @to_date, :name => 'provider-history-graph', :height => '250px', :width => '925px' }
-- 
1.7.6.5




More information about the aeolus-devel mailing list