-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #445 from Vizzuality/feature/ascor-exports
feat: ASCOR export
- Loading branch information
Showing
37 changed files
with
588 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module CSVExport | ||
module ASCOR | ||
class AssessmentIndicators | ||
HEADERS = ['Id', 'Type', 'Code', 'Text', 'Units or response type'].freeze | ||
|
||
def call | ||
# BOM UTF-8 | ||
CSV.generate("\xEF\xBB\xBF") do |csv| | ||
csv << HEADERS | ||
|
||
assessment_indicators.each do |indicator| | ||
csv << [ | ||
indicator.id, | ||
indicator.indicator_type, | ||
indicator.code, | ||
indicator.text, | ||
indicator.units_or_response_type | ||
] | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def assessment_indicators | ||
@assessment_indicators ||= ::ASCOR::AssessmentIndicator.order(:id) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
module CSVExport | ||
module ASCOR | ||
class Assessments | ||
def call | ||
CSV.generate("\xEF\xBB\xBF") do |csv| | ||
csv << headers | ||
|
||
assessments.each do |assessment| | ||
csv << [ | ||
assessment.id, | ||
assessment.assessment_date, | ||
assessment.publication_date, | ||
assessment.country_id, | ||
assessment.country.name, | ||
*answer_values_for(assessment), | ||
*source_values_for(assessment), | ||
*year_values_for(assessment), | ||
assessment.notes | ||
] | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def headers | ||
result = ['Id', 'Assessment date', 'Publication date', 'Country Id', 'Country'] | ||
result += assessment_indicators.reject { |i| i.indicator_type == 'pillar' || i.code.in?(%w[EP.1.a.i EP.1.a.ii]) } | ||
.map { |i| "#{i.indicator_type} #{i.code}" } | ||
result += assessment_indicators.select { |i| i.indicator_type.in?(%w[indicator metric]) } | ||
.map { |i| "source #{i.indicator_type} #{i.code}" } | ||
result += assessment_indicators.select { |i| i.indicator_type == 'metric' } | ||
.map { |i| "year #{i.indicator_type} #{i.code}" } | ||
result += ['Notes'] | ||
result | ||
end | ||
|
||
def answer_values_for(assessment) | ||
assessment_indicators.reject { |i| i.indicator_type == 'pillar' || i.code.in?(%w[EP.1.a.i EP.1.a.ii]) } | ||
.map do |indicator| | ||
assessment_results[[assessment.id, indicator.id]]&.first&.answer | ||
end | ||
end | ||
|
||
def source_values_for(assessment) | ||
assessment_indicators.select { |i| i.indicator_type.in?(%w[indicator metric]) } | ||
.map do |indicator| | ||
assessment_results[[assessment.id, indicator.id]]&.first&.source | ||
end | ||
end | ||
|
||
def year_values_for(assessment) | ||
assessment_indicators.select { |i| i.indicator_type == 'metric' } | ||
.map do |indicator| | ||
assessment_results[[assessment.id, indicator.id]]&.first&.year | ||
end | ||
end | ||
|
||
def assessments | ||
@assessments ||= ::ASCOR::Assessment.joins(:country).includes(:country).order(:assessment_date, 'ascor_countries.name') | ||
end | ||
|
||
def assessment_results | ||
@assessment_results ||= ::ASCOR::AssessmentResult.all.group_by { |r| [r.assessment_id, r.indicator_id] } | ||
end | ||
|
||
def assessment_indicators | ||
@assessment_indicators ||= ::ASCOR::AssessmentIndicator.order(:id) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module CSVExport | ||
module ASCOR | ||
class Benchmarks | ||
HEADERS = [ | ||
'Id', | ||
'Country', | ||
'Publication date', | ||
'Emissions metric', | ||
'Emissions boundary', | ||
'Units', | ||
'Benchmark type' | ||
].freeze | ||
|
||
def call | ||
CSV.generate("\xEF\xBB\xBF") do |csv| | ||
csv << (HEADERS + year_columns) | ||
|
||
benchmarks.each do |benchmark| | ||
csv << [ | ||
benchmark.id, | ||
benchmark.country.name, | ||
benchmark.publication_date, | ||
benchmark.emissions_metric, | ||
benchmark.emissions_boundary, | ||
benchmark.units, | ||
benchmark.benchmark_type, | ||
year_columns.map do |year| | ||
benchmark.emissions[year] | ||
end | ||
].flatten | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def year_columns | ||
@year_columns ||= benchmarks.flat_map(&:emissions_all_years).uniq.sort | ||
end | ||
|
||
def benchmarks | ||
@benchmarks ||= ::ASCOR::Benchmark.joins(:country).includes(:country).order('ascor_countries.name') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module CSVExport | ||
module ASCOR | ||
class Countries | ||
HEADERS = [ | ||
'Id', | ||
'Name', | ||
'Country ISO code', | ||
'Region', | ||
'World Bank lending group', | ||
'International Monetary Fund fiscal monitor category', | ||
'Type of Party to the United Nations Framework Convention on Climate Change' | ||
].freeze | ||
|
||
def call | ||
CSV.generate("\xEF\xBB\xBF") do |csv| | ||
csv << HEADERS | ||
|
||
countries.each do |country| | ||
csv << [ | ||
country.id, | ||
country.name, | ||
country.iso, | ||
country.region, | ||
country.wb_lending_group, | ||
country.fiscal_monitor_category, | ||
country.type_of_party | ||
] | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def countries | ||
@countries ||= ::ASCOR::Country.order(:name) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.