Changeset 82157
- Timestamp:
- 08/08/11 20:19:39 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/gsoc11-statistics/stats-server/app/controllers/ports_controller.rb
r80511 r82157 1 class PortsController < ApplicationController1 class PortsController < ChartController 2 2 caches_page :index, :show 3 3 cache_sweeper :port_sweeper, :only => [:create, :update, :destroy] 4 5 # Populate a simple two column chart 6 def populate_simple_chart(chart_name, chart) 7 chart.string "Item" 8 chart.number "Frequency" 4 9 10 dataset = chart_dataset chart_name 11 12 dataset.each do |item, count| 13 chart.add_row([item, count]) 14 end 15 end 16 17 # Populate the versions over time chart 18 def populate_monthly_versions(chart_name, chart) 19 chart.string "Month" 20 21 # Add version columns 22 column_order = [] 23 @top_versions.each do |version, count| 24 chart.number version; 25 column_order << version 26 end 27 28 # Add the data 29 dataset = chart_dataset chart_name 30 dataset.each do |month, version_counts| 31 row = [month] 32 column_order.each do |version| 33 row << version_counts[version] 34 end 35 chart.add_row(row) 36 end 37 end 38 39 # Gather all chart datasets 40 def gather_data 41 gather_frequencies 42 gather_data_over_months 43 end 44 45 # Frequency tallys 46 def gather_frequencies() 47 variant_count = Hash.new(0) 48 version_count = Hash.new(0) 49 50 @installed.each do |row| 51 if not row.variants.nil? 52 # row.variants is a space delimited string of varients 53 variants = row.variants.split 54 55 # If no variant is present increment a dummy variant 'None' 56 if variants.empty? 57 variant_count['None'] = variant_count['None'] + 1 58 end 59 60 # Count 61 variants.each do |variant| 62 key = variant.to_sym 63 variant_count[key] = variant_count[key] + 1 64 end 65 end 66 67 # Count versions 68 key = row.version.to_sym 69 version_count[key] = version_count[key] + 1 70 end 71 72 populate = method(:populate_simple_chart) 73 add_chart :variant_count, variant_count, populate 74 add_chart :version_count, version_count, populate 75 end 76 77 # Gather month by month tallys 78 def gather_data_over_months() 79 monthly_installs = Hash.new(0) 80 monthly_versions = Hash.new 81 82 now = Time.now 83 now_d = now.to_date 84 month_range = (0..11) 85 86 for i in month_range 87 month = now.months_ago(i).to_date 88 89 # Find InstalledPort entries for month 90 entries = @installed.where(:created_at => (month.at_beginning_of_month)..(month.at_end_of_month)) 91 92 count_monthly_installs monthly_installs, month, entries 93 count_monthly_versions monthly_versions, month, entries 94 end 95 96 add_chart :versions_over_time, monthly_versions, method(:populate_monthly_versions) 97 add_chart :installs_over_time, monthly_installs, method(:populate_simple_chart) 98 end 99 100 # Count the number of installs of this port for the given month 101 def count_monthly_installs(monthly_installs, month, entries) 102 if entries.size == 0 103 return 104 end 105 106 key = month.at_beginning_of_month.to_s 107 monthly_installs[key] = entries.size 108 end 109 110 # Count the number of times each version of this port was installed for 111 # the given month 112 def count_monthly_versions(monthly_versions, month, entries) 113 @top_versions.each do |version, count| 114 version_entries = entries.where("version = ?", version) 115 116 key = month.at_beginning_of_month.to_s 117 118 if monthly_versions[key].nil? 119 monthly_versions[key] = Hash.new 120 end 121 122 counts_for_month = monthly_versions[key] 123 counts_for_month[version] = version_entries.size 124 monthly_versions[key] = counts_for_month 125 end 126 end 127 5 128 def index 6 129 unless params[:category_id].nil? … … 15 138 end 16 139 end 17 140 18 141 def show 19 142 @port = Category.find(params[:category_id]).ports.find(params[:id]) 143 @installed = InstalledPort.where("port_id = ?", @port.id) 144 @top_versions = @installed.group(:version).order("count_all DESC").limit(5).size 145 @charts = Hash.new 20 146 147 gather_data 148 21 149 respond_to do |format| 22 150 format.html
Note: See TracChangeset
for help on using the changeset viewer.

