[82899] branches/gsoc11-statistics/stats-server/app
derek at macports.org
derek at macports.org
Sun Aug 21 15:55:07 PDT 2011
Revision: 82899
http://trac.macports.org/changeset/82899
Author: derek at macports.org
Date: 2011-08-21 15:55:04 -0700 (Sun, 21 Aug 2011)
Log Message:
-----------
Installed Ports - Summary page
- Display total number of participating users
- Number of ports in the MacPorts repository
- Average number of ports installed per user
- Most popular port (most installed) this month
- Most popular port this year
- Bar chart of install counts for the 25 most installed ports this month
Modified Paths:
--------------
branches/gsoc11-statistics/stats-server/app/controllers/installed_ports_controller.rb
branches/gsoc11-statistics/stats-server/app/views/installed_ports/index.html.erb
Modified: branches/gsoc11-statistics/stats-server/app/controllers/installed_ports_controller.rb
===================================================================
--- branches/gsoc11-statistics/stats-server/app/controllers/installed_ports_controller.rb 2011-08-21 22:29:11 UTC (rev 82898)
+++ branches/gsoc11-statistics/stats-server/app/controllers/installed_ports_controller.rb 2011-08-21 22:55:04 UTC (rev 82899)
@@ -1,14 +1,113 @@
-class InstalledPortsController < ApplicationController
+class InstalledPortsController < ChartController
+
+ # Populate the users chart
+ def populate_top25(chart_name, chart)
+ chart.string "Port"
+ chart.number "Number of installations"
+
+ dataset = chart_dataset chart_name
- def index
- @ports = InstalledPort.all
+ dataset.each do |item, count|
+ chart.add_row([item, count])
+ end
+ end
+
+ # Return the average number of ports each user has installed
+ def average_ports_per_user
+ users = User.all
+ sum = 0
+
+ users.each do |user|
+ sum = sum + user.installed_ports.count
+ end
+
+ average = sum / users.count
+ end
+
+ # Find the port that has been installed most this year
+ def most_installed_port_this_year
+ now = Time.now.to_date
+
+ # Find InstalledPort entries for this month
+ current = InstalledPort.where(:created_at => (now.at_beginning_of_year)..(now.at_end_of_year))
+
+
+ top = current.count(:port_id,
+ :group => :port_id,
+ :order => 'count_port_id DESC',
+ :limit => 1)
+
+ # most populator port this year
+ popular_port_year = top.first
+ @popular_port_year = Port.find(popular_port_year[0])
+ @popular_port_year_count = popular_port_year[1]
+ end
+
+ # Most popular port this month
+ def popular_port_this_month(port_id, count)
+ @popular_port_month = Port.find(port_id)
+ @popular_port_month_count = count
+ end
+
+ def gather_top25
+
+ top25 = Hash.new(0)
+ now = Time.now.to_date
+
+ # Full month name
+ @month = Time.now.strftime("%B")
+ # This year
+ @year = Time.now.strftime("%Y")
+
+ # Find InstalledPort entries for this month
+ current = InstalledPort.where(:created_at => (now.at_beginning_of_month)..(now.at_end_of_month))
+
+ @top = current.count(:port_id,
+ :group => :port_id,
+ :order => 'count_port_id DESC',
+ :limit => 25)
+
+ @top.each do |port_id, count|
+ port = Port.find(port_id)
+ if not port.nil?
+ top25[port.name] = count
+ end
+ end
+
+ # Sort the table by count
+ sorted = top25.sort_by { |k, v| v }
+ top25 = sorted.reverse # Descending order
+ add_chart :top25, top25, method(:populate_top25)
+ end
+
+ def gather_data
+
+ # Get the top 25 most installed ports for this month
+ gather_top25
+
+ # Average number of ports per user
+ @average_ports = average_ports_per_user
+
+ # most populator port this month
+ pop = @top.first
+ if not pop.nil?
+ popular_port_this_month(pop[0],pop[1])
+ end
+
+ # most popular port this year
+ most_installed_port_this_year
+ end
+
+ def index
+
+ @charts = Hash.new
+
+ gather_data
+
respond_to do |format|
format.html # index.html.erb
end
end
- def show
- @port = InstalledPort.find(params[:id])
- end
end
\ No newline at end of file
Modified: branches/gsoc11-statistics/stats-server/app/views/installed_ports/index.html.erb
===================================================================
--- branches/gsoc11-statistics/stats-server/app/views/installed_ports/index.html.erb 2011-08-21 22:29:11 UTC (rev 82898)
+++ branches/gsoc11-statistics/stats-server/app/views/installed_ports/index.html.erb 2011-08-21 22:55:04 UTC (rev 82899)
@@ -1,27 +1,33 @@
-<h1>All Installed Ports</h1>
+<% controller.set_chart_title :top25, 'Most popular ports this month' %>
+<% controller.set_chart_type :top25, "BarChart" %>
-<table border=1>
- <tr>
- <th> ID </th>
- <th> user_id </th>
- <th> Port ID </th>
- <th> Version </th>
- <th> Variants </th>
- <th> Month </th>
- <th> Year </th>
- <th> Details </th>
- </tr>
+<h1>Port Statistics</h1>
-<% @ports.each do |row| %>
- <tr>
- <td> <%= row.id %> </td>
- <td> <%= row.user_id %> </td>
- <td> <%= row.port_id %> </td>
- <td> <%= row.version %></td>
- <td> <%= row.variants %> </td>
- <td> <%= row.month %></td>
- <td> <%= row.year %></td>
- <td> <%= link_to 'Show', row %></td>
- </tr>
+Participating users: <%= User.count %> <br />
+Number of ports: <%= Port.count %> <br />
+Average number of ports installed per user: <%= @average_ports %><br />
+
+<br />
+<%# Most popular port this month %>
+<% unless @popular_port_month.nil? %>
+ <% port = @popular_port_month %>
+ Most popular port this month (<%= @month %>) is <%= link_to port.name, category_port_path(port.category, port) %>
+ with <%= @popular_port_month_count %> installs. <br />
<% end %>
-</table>
\ No newline at end of file
+
+<%# Most popular port this year %>
+<% unless @popular_port_year.nil? %>
+ <% port = @popular_port_year %>
+ Most popular port this year (<%= @year %>) is <%= link_to port.name, category_port_path(port.category, port) %>
+ with <%= @popular_port_year_count %> installs. <br />
+<% end %>
+
+<br />
+
+<%# Draw chart %>
+<%= render :partial => '/partials/chart_draw',
+ :locals => {:charts => [:top25],
+ :chart_width => 1000,
+ :chart_height => 1000}
+%>
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macports-changes/attachments/20110821/e7ef8ac6/attachment.html>
More information about the macports-changes
mailing list