[82157] branches/gsoc11-statistics/stats-server/app/controllers/ ports_controller.rb
derek at macports.org
derek at macports.org
Mon Aug 8 20:19:41 PDT 2011
Revision: 82157
http://trac.macports.org/changeset/82157
Author: derek at macports.org
Date: 2011-08-08 20:19:39 -0700 (Mon, 08 Aug 2011)
Log Message:
-----------
Gather data to display charts for a port
- Inherit from ChartController
- Count all different installed variants
- Count all different installed versions
- Get the top 5 most installed versions of this port
- For each of the past 12 months count the total number of installations of this port
- For each of the past 12 months count the total number of installations of the top 5 versions
- populate_* methods to populate a gvis visualization with the gathered data
Modified Paths:
--------------
branches/gsoc11-statistics/stats-server/app/controllers/ports_controller.rb
Modified: branches/gsoc11-statistics/stats-server/app/controllers/ports_controller.rb
===================================================================
--- branches/gsoc11-statistics/stats-server/app/controllers/ports_controller.rb 2011-08-09 03:08:54 UTC (rev 82156)
+++ branches/gsoc11-statistics/stats-server/app/controllers/ports_controller.rb 2011-08-09 03:19:39 UTC (rev 82157)
@@ -1,7 +1,130 @@
-class PortsController < ApplicationController
+class PortsController < ChartController
caches_page :index, :show
cache_sweeper :port_sweeper, :only => [:create, :update, :destroy]
+
+ # Populate a simple two column chart
+ def populate_simple_chart(chart_name, chart)
+ chart.string "Item"
+ chart.number "Frequency"
+ dataset = chart_dataset chart_name
+
+ dataset.each do |item, count|
+ chart.add_row([item, count])
+ end
+ end
+
+ # Populate the versions over time chart
+ def populate_monthly_versions(chart_name, chart)
+ chart.string "Month"
+
+ # Add version columns
+ column_order = []
+ @top_versions.each do |version, count|
+ chart.number version;
+ column_order << version
+ end
+
+ # Add the data
+ dataset = chart_dataset chart_name
+ dataset.each do |month, version_counts|
+ row = [month]
+ column_order.each do |version|
+ row << version_counts[version]
+ end
+ chart.add_row(row)
+ end
+ end
+
+ # Gather all chart datasets
+ def gather_data
+ gather_frequencies
+ gather_data_over_months
+ end
+
+ # Frequency tallys
+ def gather_frequencies()
+ variant_count = Hash.new(0)
+ version_count = Hash.new(0)
+
+ @installed.each do |row|
+ if not row.variants.nil?
+ # row.variants is a space delimited string of varients
+ variants = row.variants.split
+
+ # If no variant is present increment a dummy variant 'None'
+ if variants.empty?
+ variant_count['None'] = variant_count['None'] + 1
+ end
+
+ # Count
+ variants.each do |variant|
+ key = variant.to_sym
+ variant_count[key] = variant_count[key] + 1
+ end
+ end
+
+ # Count versions
+ key = row.version.to_sym
+ version_count[key] = version_count[key] + 1
+ end
+
+ populate = method(:populate_simple_chart)
+ add_chart :variant_count, variant_count, populate
+ add_chart :version_count, version_count, populate
+ end
+
+ # Gather month by month tallys
+ def gather_data_over_months()
+ monthly_installs = Hash.new(0)
+ monthly_versions = Hash.new
+
+ now = Time.now
+ now_d = now.to_date
+ month_range = (0..11)
+
+ for i in month_range
+ month = now.months_ago(i).to_date
+
+ # Find InstalledPort entries for month
+ entries = @installed.where(:created_at => (month.at_beginning_of_month)..(month.at_end_of_month))
+
+ count_monthly_installs monthly_installs, month, entries
+ count_monthly_versions monthly_versions, month, entries
+ end
+
+ add_chart :versions_over_time, monthly_versions, method(:populate_monthly_versions)
+ add_chart :installs_over_time, monthly_installs, method(:populate_simple_chart)
+ end
+
+ # Count the number of installs of this port for the given month
+ def count_monthly_installs(monthly_installs, month, entries)
+ if entries.size == 0
+ return
+ end
+
+ key = month.at_beginning_of_month.to_s
+ monthly_installs[key] = entries.size
+ end
+
+ # Count the number of times each version of this port was installed for
+ # the given month
+ def count_monthly_versions(monthly_versions, month, entries)
+ @top_versions.each do |version, count|
+ version_entries = entries.where("version = ?", version)
+
+ key = month.at_beginning_of_month.to_s
+
+ if monthly_versions[key].nil?
+ monthly_versions[key] = Hash.new
+ end
+
+ counts_for_month = monthly_versions[key]
+ counts_for_month[version] = version_entries.size
+ monthly_versions[key] = counts_for_month
+ end
+ end
+
def index
unless params[:category_id].nil?
@ports = Category.find(params[:category_id]).ports.paginate :page => params[:page], :order => 'name ASC', :per_page => 50
@@ -14,10 +137,15 @@
format.html
end
end
-
+
def show
@port = Category.find(params[:category_id]).ports.find(params[:id])
+ @installed = InstalledPort.where("port_id = ?", @port.id)
+ @top_versions = @installed.group(:version).order("count_all DESC").limit(5).size
+ @charts = Hash.new
+ gather_data
+
respond_to do |format|
format.html
end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macports-changes/attachments/20110808/58b86c1c/attachment.html>
More information about the macports-changes
mailing list