Skip to content

Commit

Permalink
Group allocations by platform and funding source
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew committed Aug 27, 2024
1 parent 405cbaf commit f1e0d98
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
20 changes: 18 additions & 2 deletions app/models/allocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def calculate_funded_projects
score: allocation[:score]
)
end

update(funded_projects_count: project_allocations.count)
end

def weights
Expand Down Expand Up @@ -114,8 +116,22 @@ def export_to_csv
end

def group_projects_by_funding_platform
project_allocations.where('amount_cents >= 1').order('amount_cents desc').includes(:project)
.group_by { |pa| pa.project.preferred_funding_platform }
project_allocations.order('amount_cents desc').includes(:funding_source)
.group_by { |pa| pa.funding_source.try(:platform) || 'Unknown' }
.transform_values { |pas| pas.sum(&:amount_cents) }
.sort_by { |platform, amount| -amount }
end

def group_projects_by_funding_source
project_allocations.order('amount_cents desc').includes(:funding_source)
.group_by { |pa| pa.funding_source.try(:url) || 'Unknown' }
.transform_values { |pas| pas.sum(&:amount_cents) }
.sort_by { |platform, amount| -amount }
end

def group_projects_by_funding_source_and_platform
project_allocations.order('amount_cents desc').includes(:funding_source).with_funding_source
.group_by { |pa| [pa.funding_source.platform, pa.funding_source.url] }
.transform_values { |pas| pas.sum(&:amount_cents) }
.sort_by { |platform, amount| -amount }
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/project_allocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ class ProjectAllocation < ApplicationRecord
belongs_to :allocation
belongs_to :fund
belongs_to :funding_source, optional: true

scope :with_funding_source, -> { where.not(funding_source_id: nil) }
end
25 changes: 23 additions & 2 deletions app/views/allocations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<th>Downloads</th>
<th>Dependent Repos</th>
<th>Dependent Pkgs</th>
<th>Stars</th>
<th>Platform</th>
</tr>
</tr>
Expand All @@ -46,12 +45,34 @@
<td><%= number_with_delimiter project_allocation.project.downloads %></td>
<td><%= number_with_delimiter project_allocation.project.dependent_repos_count %></td>
<td><%= number_with_delimiter project_allocation.project.dependent_packages_count %></td>
<td><%= number_with_delimiter project_allocation.project.stars %></td>
<td title='<%= project_allocation.project.funding_links.join(', ') %>'>
<%= link_to_unless project_allocation.project.preferred_funding_link.blank?, project_allocation.project.preferred_funding_platform, project_allocation.project.preferred_funding_link, target: :_blank %>
</td>
</tr>
<% end %>
</tbody>
</table>

<div class='row'>
<% @allocation.group_projects_by_funding_source_and_platform.group_by{|i,j| i[0] }.each do |platform, sources| %>
<div class='col-3'>
<table class="table w-auto">
<thead>
<tr>
<th><%= platform %> Funding Source</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<% sources.each do |source, total_cents|%>
<tr>
<td><%= link_to source[1].split('/').last, source[1] %></td>
<td><%= number_to_currency(total_cents/100.0) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% end %>
</div>
</div>

0 comments on commit f1e0d98

Please sign in to comment.