Skip to content

Commit

Permalink
devtracker.rb - Add a route for the 5 digit DAC sector page and updat…
Browse files Browse the repository at this point in the history
…e the input parameters for the category page route. sector_helpers - update the sector_parent_data_list method to produce the correct data structures for the categories and sectors html pages. Categories & Sectors html pages - update these pages to consume the data structures provided by the sector_parent_data_list method
  • Loading branch information
Ross Clements committed Oct 7, 2015
1 parent 27af13a commit d4e7a40
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 33 deletions.
10 changes: 5 additions & 5 deletions devtracker.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# devtracker.rb
#devtracker.rb
#require 'rubygems'
#require 'bundler'
#Bundler.setup
Expand Down Expand Up @@ -220,22 +220,22 @@
end

# Category Page (e.g. Three Digit DAC Sector)
get '/sector/:CategoryCode/?' do
get '/sector/:high_level_sector_code/?' do
# Get the three digit DAC sector data from the API
erb :'sector/categories',
:layout => :'layouts/layout',
:locals => {
category_list: sector_parent_data_list( settings.oipa_api_url, "all_sectors", "Category (L2)", "Category Name", "High Level Code (L1)", "High Level Sector Description")
category_list: sector_parent_data_list( settings.oipa_api_url, "category", "Category (L2)", "Category Name", "High Level Code (L1)", "High Level Sector Description", params[:high_level_sector_code], "category")
}
end

# Sector Page (e.g. Five Digit DAC Sector)
get '/sector/:CategoryCode/categories/:SectorCode/?' do
get '/sector/:high_level_sector_code/categories/:category_code/?' do
# Get the three digit DAC sector data from the API
erb :'sector/sectors',
:layout => :'layouts/layout',
:locals => {
sector_list: sector_parent_data_list(settings.oipa_api_url, "all_sectors", "Code (L3)", "Name", "Category (L2)", "Category Name")
sector_list: sector_parent_data_list(settings.oipa_api_url, "sector", "Code (L3)", "Name", "Category (L2)", "Category Name", params[:high_level_sector_code], params[:category_code])
}
end

Expand Down
48 changes: 34 additions & 14 deletions helpers/sector_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def group_hashes arr, group_fields

def high_level_sector_list(apiUrl, listType, codeType, sectorDescription )

sectorValuesJSON = RestClient.get apiUrl + "activities/aggregations?reporting_organisation=GB-1&group_by=sector&aggregations=budget&order_by=-budget&format=json"
sectorValuesJSON = RestClient.get apiUrl + "activities/aggregations?reporting_organisation=GB-1&group_by=sector&aggregations=budget&format=json"
sectorValues = JSON.parse(sectorValuesJSON)
highLevelSector = JSON.parse(File.read('data/sector_hierarchies.json'))

Expand Down Expand Up @@ -73,9 +73,9 @@ def high_level_sector_list(apiUrl, listType, codeType, sectorDescription )
end

# Return all of the DAC Sector codes associated with the parent sector code - can be used for 3 digit (category) or 5 digit sector codes
def sector_parent_data_list(apiUrl, listType, code, description, parentCodeType, parentDescriptionType)
def sector_parent_data_list(apiUrl, pageType, code, description, parentCodeType, parentDescriptionType, urlHighLevelSectorCode, urlCategoryCode)

sectorValuesJSON = RestClient.get apiUrl + "activities/aggregations?reporting_organisation=GB-1&group_by=sector&aggregations=budget&order_by=-budget&format=json"
sectorValuesJSON = RestClient.get apiUrl + "activities/aggregations?reporting_organisation=GB-1&group_by=sector&aggregations=budget&format=json"
sectorValues = JSON.parse(sectorValuesJSON)
sectorHierarchy = JSON.parse(File.read('data/sector_hierarchies.json'))

Expand All @@ -96,24 +96,44 @@ def sector_parent_data_list(apiUrl, listType, code, description, parentCodeType,
end

#TODO - test the input to see that there is no bad data comming in

if pageType == "category"
inputCode = urlHighLevelSectorCode
sectorHierarchyPath = {
:highLevelSectorCode => inputCode,
:highLevelSectorDescription => sectorHierarchy.find { |i| i[parentCodeType].to_s == inputCode }[parentDescriptionType]
}
else
inputCode = urlCategoryCode
sectorHierarchyPath = {
:highLevelSectorCode => urlHighLevelSectorCode,
:highLevelSectorDescription => sectorHierarchy.find { |i| i[parentCodeType].to_s == inputCode}["High Level Sector Description"],
:categoryCode => inputCode,
:categoryDescription => sectorHierarchy.find { |i| i[parentCodeType].to_s == inputCode }[parentDescriptionType]
}
end

#Return the parent's description
parentDescription = sectorHierarchy.find { |i| i[parentCodeType].to_s == inputCode }[parentDescriptionType]

#Remove all items from the data structure aren't related to the selected parent code
selectedCodes = budgetData.delete_if { |h| h[:parentCode].to_s != "122" }
selectedCodes = budgetData.delete_if { |h| h[:parentCode].to_s != inputCode }

#Aggregate all of the budget values for each child code & sort the list by name
selectedCodesBudgetAggregated = group_hashes selectedCodes, [:code,:name,:parentCode]
selectedCodesBudgetAggregatedSorted = selectedCodesBudgetAggregated.sort_by{ |k| k[:name]}
if pageType == "category"
#Aggregate all of the budget values for each child code & sort the list by name
selectedCodesBudgetAggregated = group_hashes selectedCodes, [:code,:name,:parentCode]
selectedCodesBudgetAggregatedSorted = selectedCodesBudgetAggregated.sort_by{ |k| k[:name]}
else
#DAC 5 digit sector code budgets are pre-aggregated in the API
selectedCodesBudgetAggregatedSorted = selectedCodes.sort_by{ |k| k[:name]}
end

#Calculate the sum of all of the budgets in the data structure
totalBudget = Float(selectedCodesBudgetAggregated.map { |s| s[:budget].to_f }.inject(:+))

#Return the parent's description
parentDescription = sectorHierarchy.find { |i| i[parentCodeType].to_s == "122" }[parentDescriptionType]

totalBudget = Float(selectedCodesBudgetAggregatedSorted.map { |s| s[:budget].to_f }.inject(:+))

returnObject = {
:sectorData => selectedCodesBudgetAggregatedSorted,
:totalBudget => totalBudget,
:parentDesc => parentDescription
:sectorHierarchyPath => sectorHierarchyPath
}
end
end
10 changes: 5 additions & 5 deletions views/sector/categories.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ul>
<li><a href="/">Home</a></li>
<li><a href="/sector">Aid by Sector</a></li>
<li><%= category_list[:parentDesc] %></li>
<li><%= category_list[:sectorHierarchyPath][:highLevelSectorDescription] %></li>
</ul>
</div>
<h1>
Expand All @@ -21,11 +21,11 @@

<div class="row">
<div class="twelve columns content">
<h2 class="section-group-title"><%= category_list[:parentDesc] %> Sector Breakdown</h2>
<h2 class="section-group-title"><%= category_list[:sectorHierarchyPath][:highLevelSectorDescription] %> Sector Breakdown</h2>
<div class="row">
<div class="four columns summary">
<p><%= category_list[:parentDesc] %></p>
<%# <a class="button" href="/sector/<%=sector['_id']%> <%# /projects" style="width:100%;">View All <%= sector['sectorName'] %> <%# Projects</a> #%>
<p><%= category_list[:sectorHierarchyPath][:highLevelSectorDescription] %></p>
<a class="button" href="/sector/<%=category_list[:sectorHierarchyPath][:highLevelSectorCode]%>/projects" style="width:100%;">View All <%= category_list[:sectorHierarchyPath][:highLevelSectorDescription]%> Projects</a>
</div>
<div class="eight columns">
<ul class="sector-list">
Expand All @@ -35,7 +35,7 @@
<div class="progress">
<%budgetPercentage = 100*category[:budget].to_f/category_list[:totalBudget].to_f %>
<em><%=format_percentage budgetPercentage%> </em>
<span class="meter" style="width: <%=format_percentage budgetPercentage%>"></span>
<span class="meter" style="width: budgetPercentage<%# <%=format_percentage budgetPercentage%>"></span>
<span>£<%=format_round_m category[:budget].to_f || 0.0 %> </span>
</div>
</li>
Expand Down
16 changes: 7 additions & 9 deletions views/sector/sectors.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
<ul>
<li><a href="/">Home</a></li>
<li><a href="/sector">Aid by Sector</a></li>
<li><a href="/sector/<%= sector_list[:parentDesc] %>"><%= sector_list[:parentDesc] %></a></li>
<li><a href="/sector/<%=sector_list[:sectorHierarchyPath][:highLevelSectorCode]%>/"><%=sector_list[:sectorHierarchyPath][:highLevelSectorDescription]%></a></li>
<li><%=sector_list[:sectorHierarchyPath][:categoryDescription]%></li>
</ul>
</div>
<h1>
Expand All @@ -18,24 +19,21 @@

<div class="row">
<div class="twelve columns content">
<h2 class="section-group-title"><%= sector_list[:parentDesc] %> Sector Breakdown</h2>
<h2 class="section-group-title"><%= sector_list[:sectorHierarchyPath][:categoryDescription]%> Sector Breakdown</h2>
<div class="row">
<div class="four columns summary">
<p><%= sector_list[:parentDesc] %></p>
<%#
<a class="button" href="/sector/<%=sector['sectorCode']%> <%# /categories/<%=sector_list['_id']%> <%#/projects" style="width:100%;">View All <%= sector['categoryName'] %> <%# Projects</a>
#%>
<p><%= sector_list[:sectorHierarchyPath][:categoryDescription]%></p>
<a class="button" href="/sector/<%=sector_list[:sectorHierarchyPath][:highLevelSectorCode]%>/categories/<%=sector_list[:sectorHierarchyPath][:categoryCode]%>/projects" style="width:100%;">View All <%= sector_list[:sectorHierarchyPath][:categoryDescription]%> Projects</a>
</div>
<div class="eight columns">
<ul class="sector-list">
<% (sector_list[:sectorData]).each do |sector| %>
<li>
<%# <a href="/sector/<%=sector['sectorCode']%> <%#/categories/<%=sector_list['_id']%> <%# /projects/<%=s[:code]%> <%# "><%=s[:name]%> <%#</a> #%>
<a href="/"> <%=sector[:name]%></a>
<a href="/sector/<%=sector_list[:sectorHierarchyPath][:highLevelSectorCode]%>/categories/<%=sector_list[:sectorHierarchyPath][:categoryCode]%>/projects/<%=sector[:code]%>"><%=sector[:name]%></a>
<div class="progress">
<%budgetPercentage = 100*sector[:budget].to_f/sector_list[:totalBudget].to_f %>
<em><%=format_percentage budgetPercentage%> </em>
<span class="meter" style="width: <%=format_percentage budgetPercentage%>"></span>
<span class="meter" style="width:<%=format_percentage budgetPercentage%>"></span>
<span>£<%=format_round_m sector[:budget].to_f || 0.0 %> </span>
</div>
</li>
Expand Down

0 comments on commit d4e7a40

Please sign in to comment.