Skip to content

Commit

Permalink
feat: ASCOR Pathways model
Browse files Browse the repository at this point in the history
  • Loading branch information
martintomas committed Sep 14, 2023
1 parent 9109e68 commit 5faf849
Show file tree
Hide file tree
Showing 17 changed files with 814 additions and 3 deletions.
103 changes: 103 additions & 0 deletions app/admin/ascor/pathways.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
ActiveAdmin.register ASCOR::Pathway do
config.sort_order = 'country_id_asc'
includes :country

menu label: 'Pathways', parent: 'ASCOR', priority: 3

permit_params :country_id, :publication_date, :assessment_date, :emissions_metric, :emissions_boundary, :land_use, :units,
:emissions, :last_reported_year, :trend_1_year, :trend_3_year, :trend_5_year

filter :country, as: :select, collection: -> { ASCOR::Country.all.order(:name) }
filter :assessment_date, as: :select, collection: -> { ASCOR::Pathway.pluck(:assessment_date).uniq }
filter :emissions_metric, as: :select, collection: -> { ASCOR::EmissionsMetric::VALUES }
filter :emissions_boundary, as: :select, collection: -> { ASCOR::EmissionsBoundary::VALUES }
filter :land_use, as: :select, collection: -> { ASCOR::LandUse::VALUES }

data_export_sidebar 'ASCORPathways'

index do
column :country
column :assessment_date
column :emissions_metric
column :emissions_boundary
column :land_use
column :units

actions
end

show do
attributes_table do
row :id
row :country
row :assessment_date
row :publication_date
row :emissions_metric
row :emissions_boundary
row :land_use
row :units
row :last_reported_year
row :trend_1_year
row :trend_3_year
row :trend_5_year
row :created_at
row :updated_at
end

panel 'Benchmark emission values' do
render 'admin/cp/emissions_table', emissions: resource.emissions
end

active_admin_comments
end

form html: {'data-controller' => 'check-modified with-emission-table-form'} do |f|
f.semantic_errors(*f.object.errors.keys)

f.inputs do
f.input :country, as: :select, collection: ASCOR::Country.all.order(:name)
f.input :assessment_date, as: :datepicker
f.input :publication_date, as: :datepicker
f.input :emissions_metric, as: :select, collection: ASCOR::EmissionsMetric::VALUES
f.input :emissions_boundary, as: :select, collection: ASCOR::EmissionsBoundary::VALUES
f.input :land_use, as: :select, collection: ASCOR::LandUse::VALUES
f.input :units
f.input :last_reported_year
f.input :trend_1_year
f.input :trend_3_year
f.input :trend_5_year
f.input :emissions, as: :hidden, input_html: {value: f.object.emissions.to_json, id: 'input_emissions'}
end

div class: 'panel' do
h3 'Benchmark emission values'
div class: 'panel-contents padding-20' do
render 'admin/cp/emissions_table_edit', f: f
end
end

f.actions
end

csv do
year_columns = ASCOR::Pathway.select(:emissions).flat_map(&:emissions_all_years).uniq.sort

column :id
column(:country) { |b| b.country.name }
column(:assessment_date) { |b| b.assessment_date.strftime '%m/%d/%y' }
column(:publication_date) { |b| b.publication_date.to_s(:year_month) }
column :emissions_metric
column :emissions_boundary
column :land_use
column :units
column :last_reported_year
year_columns.map do |year|
column year do |benchmark|
benchmark.emissions[year]
end
end
column '1-year trend', &:trend_1_year
column '3-year trend', &:trend_3_year
column '5-year trend', &:trend_5_year
end
end
1 change: 1 addition & 0 deletions app/models/ascor/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ASCOR::Country < ApplicationRecord
].freeze

has_many :benchmarks, class_name: 'ASCOR::Benchmark', foreign_key: :country_id, dependent: :destroy
has_many :pathways, class_name: 'ASCOR::Pathway', foreign_key: :country_id, dependent: :destroy

validates_presence_of :name, :iso, :region, :wb_lending_group, :fiscal_monitor_category
validates_uniqueness_of :name, :iso
Expand Down
2 changes: 1 addition & 1 deletion app/models/ascor/emissions_metric.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class ASCOR::EmissionsMetric
VALUES = [
'Absolute',
'Per capital emissions',
'Per capita emissions',
'PPP-adjusted GDP emissions'
].freeze
end
10 changes: 10 additions & 0 deletions app/models/ascor/pathway.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class ASCOR::Pathway < ApplicationRecord
include HasEmissions

belongs_to :country, class_name: 'ASCOR::Country', foreign_key: :country_id

validates_presence_of :emissions_metric, :emissions_boundary, :land_use, :units, :assessment_date
validates :emissions_metric, inclusion: {in: ASCOR::EmissionsMetric::VALUES}, allow_nil: true
validates :emissions_boundary, inclusion: {in: ASCOR::EmissionsBoundary::VALUES}, allow_nil: true
validates :land_use, inclusion: {in: ASCOR::LandUse::VALUES}, allow_nil: true
end
1 change: 1 addition & 0 deletions app/models/data_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class DataUpload < ApplicationRecord
UPLOADERS = {
'ASCOR Countries' => 'ASCORCountries',
'ASCOR Benchmarks' => 'ASCORBenchmarks',
'ASCOR Pathways' => 'ASCORPathways',
'Banks' => 'Banks',
'Bank Assessment Indicators' => 'BankAssessmentIndicators',
'Bank Assessments' => 'BankAssessments',
Expand Down
64 changes: 64 additions & 0 deletions app/services/csv_import/ascor_pathways.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module CSVImport
class ASCORPathways < BaseImporter
include Helpers

def import
import_each_csv_row(csv) do |row|
pathway = prepare_pathway(row)

pathway.country = countries[row[:country]].first if row.header?(:country)
pathway.emissions_metric = row[:emissions_metric] if row.header?(:emissions_metric)
pathway.emissions_boundary = row[:emissions_boundary] if row.header?(:emissions_boundary)
pathway.land_use = row[:land_use] if row.header?(:land_use)
pathway.units = row[:units] if row.header?(:units)
pathway.assessment_date = assessment_date(row) if row.header?(:assessment_date)
pathway.publication_date = publication_date(row) if row.header?(:publication_date)
pathway.last_reported_year = row[:last_reported_year] if row.header?(:last_reported_year)
pathway.emissions = parse_emissions(row) if emission_headers?(row)
pathway.trend_1_year = row[:'1year_trend'] if row.header?(:'1year_trend')
pathway.trend_3_year = row[:'3year_trend'] if row.header?(:'3year_trend')
pathway.trend_5_year = row[:'5year_trend'] if row.header?(:'5year_trend')

was_new_record = pathway.new_record?
any_changes = pathway.changed?

pathway.save!

update_import_results(was_new_record, any_changes)
end
end

private

def resource_klass
ASCOR::Pathway
end

def required_headers
[:id]
end

def prepare_pathway(row)
find_record_by(:id, row) ||
ASCOR::Pathway.find_or_initialize_by(
country: countries[row[:country]].first,
emissions_metric: row[:emissions_metric],
emissions_boundary: row[:emissions_boundary],
land_use: row[:land_use],
assessment_date: assessment_date(row)
)
end

def countries
@countries ||= ASCOR::Country.all.group_by(&:name)
end

def assessment_date(row)
CSVImport::DateUtils.safe_parse!(row[:assessment_date], ['%Y-%m-%d', '%m/%d/%y']) if row[:assessment_date]
end

def publication_date(row)
CSVImport::DateUtils.safe_parse!(row[:publication_date], ['%Y-%m'])
end
end
end
4 changes: 4 additions & 0 deletions app/services/seed/tpi_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def call
TimedLogger.log('Import ASCOR Benchmarks') do
run_importer CSVImport::ASCORBenchmarks.new(seed_file('ascor_benchmarks.csv'))
end

TimedLogger.log('Import ASCOR Pathways') do
run_importer CSVImport::ASCORPathways.new(seed_file('ascor_pathways.csv'))
end
end

def import_sector_clusters
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20230912074824_create_ascor_countries.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateAscorCountries < ActiveRecord::Migration[6.1]
class CreateASCORCountries < ActiveRecord::Migration[6.1]
def change
create_table :ascor_countries do |t|
t.string :name, index: { unique: true }
Expand Down
20 changes: 20 additions & 0 deletions db/migrate/20230914065509_create_ascor_pathways.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class CreateASCORPathways < ActiveRecord::Migration[6.1]
def change
create_table :ascor_pathways do |t|
t.references :country, null: false, foreign_key: { to_table: :ascor_countries }
t.string :emissions_metric
t.string :emissions_boundary
t.string :land_use
t.string :units
t.date :assessment_date
t.date :publication_date, null: true
t.integer :last_reported_year, null: true
t.string :trend_1_year, null: true
t.string :trend_3_year, null: true
t.string :trend_5_year, null: true
t.jsonb :emissions, default: {}

t.timestamps
end
end
end
Loading

0 comments on commit 5faf849

Please sign in to comment.