New Ticket     Tickets     Wiki     Browse Source     Timeline     Roadmap     Ticket Reports     Search

Changeset 81854


Ignore:
Timestamp:
08/05/11 22:24:53 (4 years ago)
Author:
derek@…
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
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/gsoc11-statistics/stats-server/app/controllers/os_statistics_controller.rb

    r80511 r81854  
    11class OsStatisticsController < ApplicationController 
    22   
     3  def hash_to_google_json(hash, columns) 
     4    json = Hash.new 
     5     
     6    json['cols'] = columns 
     7     
     8    # Create rows array 
     9    rows = [] 
     10    hash.each do |key, frequency| 
     11      row = Hash.new 
     12      row['c'] = [{'v' => key}, {'v' => frequency}] 
     13      rows << row 
     14    end 
     15   
     16    json['rows'] = rows 
     17     
     18    return json.to_json.to_s 
     19  end 
     20   
     21  def to_json(frequencies) 
     22    first_col = {'id' => 'name', 'label' => 'name', 'type' => 'string'} 
     23    second_col = {'id' => 'frequency', 'label' => 'Frequency', 'type' => 'number'} 
     24     
     25    cols = [first_col, second_col] 
     26     
     27    json = Hash.new 
     28    frequencies.each do |name, hash| 
     29      json[name] = hash_to_google_json(frequencies[name], cols) 
     30    end 
     31     
     32    return json 
     33  end 
     34   
    335  def gather_frequencies(stats) 
    4     @macports_version = Hash.new(0) 
    5     @osx_version = Hash.new(0) 
    6     @os_arch = Hash.new(0) 
    7     @os_platform = Hash.new(0) 
    8     @build_arch = Hash.new(0) 
    9     @gcc_version = Hash.new(0) 
    10     @xcode_version = Hash.new(0) 
     36    macports_version = Hash.new(0) 
     37    osx_version = Hash.new(0) 
     38    os_arch = Hash.new(0) 
     39    os_platform = Hash.new(0) 
     40    build_arch = Hash.new(0) 
     41    gcc_version = Hash.new(0) 
     42    xcode_version = Hash.new(0) 
    1143     
    1244    stats.each do |row| 
    1345      key = row.macports_version.to_sym   
    14       @macports_version[key] = @macports_version[key] + 1 
     46      macports_version[key] = macports_version[key] + 1 
    1547       
    1648      key = row.osx_version.to_sym   
    17       @osx_version[key] = @osx_version[key] + 1 
     49      osx_version[key] = osx_version[key] + 1 
    1850     
    1951      key = row.os_arch.to_sym   
    20       @os_arch[key] = @os_arch[key] + 1 
     52      os_arch[key] = os_arch[key] + 1 
    2153     
    2254      key = row.os_platform.to_sym   
    23       @os_platform[key] = @os_platform[key] + 1 
     55      os_platform[key] = os_platform[key] + 1 
    2456       
    2557      key = row.build_arch.to_sym   
    26       @build_arch[key] = @build_arch[key] + 1 
     58      build_arch[key] = build_arch[key] + 1 
    2759       
    2860      key = row.gcc_version.to_sym   
    29       @gcc_version[key] = @gcc_version[key] + 1 
     61      gcc_version[key] = gcc_version[key] + 1 
    3062       
    3163                key = row.xcode_version.to_sym   
    32       @xcode_version[key] = @xcode_version[key] + 1     
     64      xcode_version[key] = xcode_version[key] + 1     
    3365    end 
    3466     
     67    frequencies = Hash.new 
     68    frequencies['macports_version'] = macports_version 
     69    frequencies['osx_version'] = osx_version 
     70    frequencies['os_arch'] = os_arch 
     71    frequencies['os_platform'] = os_platform 
     72    frequencies['build_arch'] = build_arch 
     73    frequencies['gcc_version'] = gcc_version 
     74    frequencies['xcode_version'] = xcode_version 
     75     
     76    return frequencies 
    3577  end 
    3678   
     
    3880    @stats = OsStatistic.all 
    3981     
    40     gather_frequencies(@stats) 
     82    frequencies = gather_frequencies @stats 
     83    @chartjson = to_json frequencies 
    4184     
    4285    respond_to do |format| 
Note: See TracChangeset for help on using the changeset viewer.