New Ticket     Tickets     Wiki     Browse Source     Timeline     Roadmap     Ticket Reports     Search

Changeset 82155


Ignore:
Timestamp:
08/08/11 20:03:21 (4 years ago)
Author:
derek@…
Message:
  • Use gvis rather than manually generating JSON
  • Removed JSON helper functions
  • Inherit from ChartController
  • Use methods in ChartController to manage charts rather than @frequencies hash
File:
1 edited

Legend:

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

    r81854 r82155  
    1 class OsStatisticsController < ApplicationController 
     1class OsStatisticsController < ChartController 
    22   
    3   def hash_to_google_json(hash, columns) 
    4     json = Hash.new 
     3  # Populate charts with data 
     4  def populate(chart_name, chart) 
     5    # This is the column's label 
     6    chart.string "Item" 
    57     
    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 
     8    # This is the numerical value associated with the label 
     9    chart.number "Frequency" 
     10 
     11    # Add rows of data 
     12    dataset = chart_dataset chart_name 
     13    dataset.each do |item, count| 
     14      chart.add_row([item, count]) 
    1415    end 
    15    
    16     json['rows'] = rows 
    17      
    18     return json.to_json.to_s 
    1916  end 
    2017   
    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    
     18  # Gather all needed data from the model 
    3519  def gather_frequencies(stats) 
    3620    macports_version = Hash.new(0) 
     
    4226    xcode_version = Hash.new(0) 
    4327     
     28    # For each column in OsStatistics count the number of unique entries 
    4429    stats.each do |row| 
    4530      key = row.macports_version.to_sym   
     
    6550    end 
    6651     
    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 
     52    populate = method(:populate) 
    7553     
    76     return frequencies 
     54    add_chart :macports_version, macports_version, populate 
     55    add_chart :osx_version, osx_version, populate 
     56    add_chart :os_arch, os_arch, populate 
     57    add_chart :os_platform, os_platform, populate 
     58    add_chart :build_arch, build_arch, populate 
     59    add_chart :macports_version, macports_version, populate 
     60    add_chart :gcc_version, gcc_version, populate 
     61    add_chart :xcode_version, xcode_version, populate 
    7762  end 
    78    
     63     
    7964  def index 
    8065    @stats = OsStatistic.all 
    81      
    82     frequencies = gather_frequencies @stats 
    83     @chartjson = to_json frequencies 
     66    @charts = Hash.new 
     67    gather_frequencies @stats 
    8468     
    8569    respond_to do |format| 
Note: See TracChangeset for help on using the changeset viewer.