New Ticket     Tickets     Wiki     Browse Source     Timeline     Roadmap     Ticket Reports     Search

Changeset 82899


Ignore:
Timestamp:
08/21/11 15:55:04 (4 years ago)
Author:
derek@…
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
Location:
branches/gsoc11-statistics/stats-server/app
Files:
2 edited

Legend:

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

    r80511 r82899  
    1 class InstalledPortsController < ApplicationController 
     1class InstalledPortsController < ChartController 
     2   
     3  # Populate the users chart 
     4  def populate_top25(chart_name, chart) 
     5    chart.string "Port" 
     6    chart.number "Number of installations" 
     7 
     8    dataset = chart_dataset chart_name 
    29     
    3   def index 
    4     @ports = InstalledPort.all 
     10    dataset.each do |item, count| 
     11      chart.add_row([item, count]) 
     12    end 
     13  end 
     14   
     15  # Return the average number of ports each user has installed 
     16  def average_ports_per_user 
     17    users = User.all 
     18    sum = 0 
     19     
     20    users.each do |user| 
     21      sum = sum + user.installed_ports.count 
     22    end 
     23     
     24    average = sum / users.count 
     25  end 
     26   
     27  # Find the port that has been installed most this year 
     28  def most_installed_port_this_year 
     29    now = Time.now.to_date 
     30 
     31    # Find InstalledPort entries for this month 
     32    current = InstalledPort.where(:created_at => (now.at_beginning_of_year)..(now.at_end_of_year)) 
     33     
     34     
     35    top = current.count(:port_id, 
     36                               :group => :port_id, 
     37                               :order => 'count_port_id DESC', 
     38                               :limit => 1) 
     39                                
     40   # most populator port this year 
     41   popular_port_year = top.first 
     42   @popular_port_year = Port.find(popular_port_year[0]) 
     43   @popular_port_year_count = popular_port_year[1] 
     44  end 
     45   
     46  # Most popular port this month 
     47  def popular_port_this_month(port_id, count) 
     48    @popular_port_month = Port.find(port_id) 
     49    @popular_port_month_count = count 
     50  end 
     51   
     52  def gather_top25 
     53     
     54    top25 = Hash.new(0) 
     55    now = Time.now.to_date 
     56     
     57    # Full month name 
     58    @month = Time.now.strftime("%B") 
     59    # This year 
     60    @year = Time.now.strftime("%Y") 
     61     
     62    # Find InstalledPort entries for this month 
     63    current = InstalledPort.where(:created_at => (now.at_beginning_of_month)..(now.at_end_of_month)) 
     64     
     65    @top = current.count(:port_id, 
     66                               :group => :port_id, 
     67                               :order => 'count_port_id DESC', 
     68                               :limit => 25)     
     69     
     70    @top.each do |port_id, count| 
     71      port = Port.find(port_id) 
     72      if not port.nil? 
     73        top25[port.name] = count 
     74      end 
     75    end 
     76     
     77    # Sort the table by count 
     78    sorted = top25.sort_by { |k, v| v } 
     79    top25 = sorted.reverse # Descending order 
    580         
     81    add_chart :top25, top25, method(:populate_top25) 
     82  end 
     83   
     84  def gather_data 
     85     
     86    # Get the top 25 most installed ports for this month 
     87    gather_top25 
     88     
     89    # Average number of ports per user                         
     90    @average_ports = average_ports_per_user 
     91     
     92    # most populator port this month 
     93    pop = @top.first 
     94    if not pop.nil? 
     95      popular_port_this_month(pop[0],pop[1]) 
     96    end 
     97     
     98    # most popular port this year 
     99    most_installed_port_this_year 
     100  end 
     101   
     102  def index    
     103     
     104    @charts = Hash.new 
     105     
     106    gather_data 
     107     
    6108    respond_to do |format| 
    7109      format.html # index.html.erb 
     
    9111  end 
    10112   
    11   def show 
    12     @port = InstalledPort.find(params[:id]) 
    13   end 
    14113end 
  • branches/gsoc11-statistics/stats-server/app/views/installed_ports/index.html.erb

    r80731 r82899  
    1 <h1>All Installed Ports</h1> 
     1<% controller.set_chart_title :top25, 'Most popular ports this month' %> 
     2<% controller.set_chart_type  :top25, "BarChart" %> 
    23 
    3 <table border=1> 
    4         <tr> 
    5                 <th> ID </th> 
    6                 <th> user_id </th> 
    7                 <th> Port ID </th> 
    8                 <th> Version </th> 
    9                 <th> Variants </th> 
    10                 <th> Month </th> 
    11                 <th> Year </th> 
    12                 <th> Details </th> 
    13         </tr> 
     4<h1>Port Statistics</h1> 
    145 
    15 <% @ports.each do |row| %> 
    16   <tr> 
    17     <td> <%= row.id %> </td> 
    18                 <td> <%= row.user_id %> </td> 
    19                 <td> <%= row.port_id %> </td> 
    20                 <td> <%= row.version %></td> 
    21                 <td> <%= row.variants %> </td> 
    22                 <td> <%= row.month %></td> 
    23                 <td> <%= row.year %></td> 
    24     <td> <%= link_to 'Show', row %></td> 
    25   </tr> 
     6Participating users: <%= User.count %> <br /> 
     7Number of ports: <%= Port.count %> <br /> 
     8Average number of ports installed per user: <%= @average_ports %><br /> 
     9 
     10<br /> 
     11<%# Most popular port this month %> 
     12<% unless @popular_port_month.nil? %> 
     13  <% port = @popular_port_month %> 
     14  Most popular port this month (<%= @month %>) is <%= link_to port.name, category_port_path(port.category, port) %> 
     15  with <%= @popular_port_month_count %> installs. <br /> 
    2616<% end %> 
    27 </table> 
     17 
     18<%# Most popular port this year %> 
     19<% unless @popular_port_year.nil? %> 
     20  <% port = @popular_port_year %> 
     21  Most popular port this year (<%= @year %>) is <%= link_to port.name, category_port_path(port.category, port) %> 
     22  with <%= @popular_port_year_count %> installs. <br /> 
     23<% end %> 
     24 
     25<br /> 
     26 
     27<%# Draw chart  %> 
     28<%= render :partial => '/partials/chart_draw',  
     29    :locals => {:charts => [:top25], 
     30                :chart_width => 1000, 
     31                :chart_height => 1000}  
     32%> 
     33 
Note: See TracChangeset for help on using the changeset viewer.