[81854] branches/gsoc11-statistics/stats-server/app/controllers/ os_statistics_controller.rb

derek at macports.org derek at macports.org
Fri Aug 5 22:24:56 PDT 2011


Revision: 81854
          http://trac.macports.org/changeset/81854
Author:   derek at macports.org
Date:     2011-08-05 22:24:53 -0700 (Fri, 05 Aug 2011)
Log Message:
-----------
Convert Ruby hashes to JSON for use with Google Charts API

- Added method hash_to_google_json which takes a hash and returns a JSON string
  in the format exected by Google Charts

- Added to_json method which takes a hash of frequency hashes and returns a hash of json strings 

- Turned many instance variables into local variants. All data to draw charts in the view is now stored in @chartjson

Modified Paths:
--------------
    branches/gsoc11-statistics/stats-server/app/controllers/os_statistics_controller.rb

Modified: branches/gsoc11-statistics/stats-server/app/controllers/os_statistics_controller.rb
===================================================================
--- branches/gsoc11-statistics/stats-server/app/controllers/os_statistics_controller.rb	2011-08-06 03:34:50 UTC (rev 81853)
+++ branches/gsoc11-statistics/stats-server/app/controllers/os_statistics_controller.rb	2011-08-06 05:24:53 UTC (rev 81854)
@@ -1,43 +1,86 @@
 class OsStatisticsController < ApplicationController
   
+  def hash_to_google_json(hash, columns)
+    json = Hash.new
+    
+    json['cols'] = columns
+    
+    # Create rows array
+    rows = []
+    hash.each do |key, frequency|
+      row = Hash.new
+      row['c'] = [{'v' => key}, {'v' => frequency}]
+      rows << row
+    end
+  
+    json['rows'] = rows
+    
+    return json.to_json.to_s
+  end
+  
+  def to_json(frequencies)
+    first_col = {'id' => 'name', 'label' => 'name', 'type' => 'string'}
+    second_col = {'id' => 'frequency', 'label' => 'Frequency', 'type' => 'number'}
+    
+    cols = [first_col, second_col]
+    
+    json = Hash.new
+    frequencies.each do |name, hash|
+      json[name] = hash_to_google_json(frequencies[name], cols)
+    end
+    
+    return json
+  end
+  
   def gather_frequencies(stats)
-    @macports_version = Hash.new(0)
-    @osx_version = Hash.new(0)
-    @os_arch = Hash.new(0)
-    @os_platform = Hash.new(0)
-    @build_arch = Hash.new(0)
-    @gcc_version = Hash.new(0)
-    @xcode_version = Hash.new(0)
+    macports_version = Hash.new(0)
+    osx_version = Hash.new(0)
+    os_arch = Hash.new(0)
+    os_platform = Hash.new(0)
+    build_arch = Hash.new(0)
+    gcc_version = Hash.new(0)
+    xcode_version = Hash.new(0)
     
     stats.each do |row|
       key = row.macports_version.to_sym  
-      @macports_version[key] = @macports_version[key] + 1
+      macports_version[key] = macports_version[key] + 1
       
       key = row.osx_version.to_sym  
-      @osx_version[key] = @osx_version[key] + 1
+      osx_version[key] = osx_version[key] + 1
     
       key = row.os_arch.to_sym  
-      @os_arch[key] = @os_arch[key] + 1
+      os_arch[key] = os_arch[key] + 1
     
       key = row.os_platform.to_sym  
-      @os_platform[key] = @os_platform[key] + 1
+      os_platform[key] = os_platform[key] + 1
       
       key = row.build_arch.to_sym  
-      @build_arch[key] = @build_arch[key] + 1
+      build_arch[key] = build_arch[key] + 1
       
       key = row.gcc_version.to_sym  
-      @gcc_version[key] = @gcc_version[key] + 1
+      gcc_version[key] = gcc_version[key] + 1
       
   		key = row.xcode_version.to_sym  
-      @xcode_version[key] = @xcode_version[key] + 1    
+      xcode_version[key] = xcode_version[key] + 1    
     end
     
+    frequencies = Hash.new
+    frequencies['macports_version'] = macports_version
+    frequencies['osx_version'] = osx_version
+    frequencies['os_arch'] = os_arch
+    frequencies['os_platform'] = os_platform
+    frequencies['build_arch'] = build_arch
+    frequencies['gcc_version'] = gcc_version
+    frequencies['xcode_version'] = xcode_version
+    
+    return frequencies
   end
   
   def index
     @stats = OsStatistic.all
     
-    gather_frequencies(@stats)
+    frequencies = gather_frequencies @stats
+    @chartjson = to_json frequencies
     
     respond_to do |format|
       format.html # index.html.erb
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macports-changes/attachments/20110805/2d03f98c/attachment.html>


More information about the macports-changes mailing list