-
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 #59 from Vizzuality/feature/import-sector-benchmarks
Feature/import sector benchmarks
- Loading branch information
Showing
22 changed files
with
403 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
ActiveAdmin.register CP::Benchmark do | ||
config.sort_order = 'release_date_desc' | ||
|
||
menu parent: 'TPI', priority: 3, label: 'Carbon Performance Benchmarks' | ||
|
||
permit_params :scenario, :sector_id, :release_date, :emissions | ||
|
||
filter :release_date | ||
filter :sector | ||
|
||
data_export_sidebar 'CPBenchmarks' | ||
|
||
show do | ||
attributes_table do | ||
row :id | ||
row :release_date | ||
row :sector | ||
row :scenario | ||
row :created_at | ||
row :updated_at | ||
end | ||
|
||
panel 'Benchmark emission values' do | ||
render 'admin/cp/emissions_table', emissions: resource.emissions | ||
end | ||
end | ||
|
||
csv do | ||
year_columns = collection.flat_map(&:emissions_all_years).uniq.sort | ||
|
||
column :id | ||
column(:sector) { |b| b.sector.name } | ||
column(:release_date) { |b| b.release_date.strftime('%Y-%m') } | ||
column :scenario | ||
|
||
year_columns.map do |year| | ||
column year do |benchmark| | ||
benchmark.emissions[year] | ||
end | ||
end | ||
end | ||
|
||
index do | ||
id_column | ||
column :scenario | ||
column :sector | ||
column :release_date | ||
actions | ||
end | ||
|
||
form partial: 'form' | ||
|
||
controller do | ||
def scoped_collection | ||
super.includes(:sector) | ||
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
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,9 @@ | ||
form.cp_benchmark { | ||
table { | ||
tr.emission { | ||
input { | ||
padding: 8px; | ||
} | ||
} | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
app/javascript/admin/controllers/cp_benchmark_form_controller.js
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,43 @@ | ||
import { Controller } from 'stimulus'; | ||
|
||
export default class extends Controller { | ||
static targets = ['emissionTable', 'emissionRowTemplate', 'emissionRowAdd'] | ||
|
||
connect() { | ||
$(this.element).on('submit', () => { | ||
// serialize emissions | ||
this.element.querySelector('#input_emissions').value = JSON.stringify(this.serializeTableData()); | ||
}); | ||
|
||
$(this.emissionTableTarget).find('tbody').sortable({ | ||
handle: '.sortable-handle', | ||
items: 'tr.emission' | ||
}); | ||
} | ||
|
||
serializeTableData() { | ||
const result = {}; | ||
|
||
$(this.emissionTableTarget).find('tr.emission').each((idx, el) => { | ||
const year = $(el).find('input[name=emission_year]').val(); | ||
const value = parseFloat($(el).find('input[name=emission_value]').val()); | ||
|
||
if (!year || !value) return; | ||
|
||
result[year] = value; | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
//actions | ||
addEmission() { | ||
const content = this.emissionRowTemplateTarget.innerHTML; | ||
this.emissionRowAddTarget.insertAdjacentHTML('beforebegin', content); | ||
} | ||
|
||
removeEmission() { | ||
const element = event.target.closest('tr'); | ||
element.remove(); | ||
} | ||
} |
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,63 @@ | ||
module CSVImport | ||
class CPBenchmarks < BaseImporter | ||
include UploaderHelpers | ||
|
||
EMISSION_YEAR_PATTERN = /\d{4}/.freeze | ||
|
||
def import | ||
import_each_csv_row(csv) do |row| | ||
benchmark = prepare_benchmark(row) | ||
benchmark.assign_attributes(benchmark_attributes(row)) | ||
|
||
was_new_record = benchmark.new_record? | ||
any_changes = benchmark.changed? | ||
|
||
benchmark.save! | ||
|
||
update_import_results(was_new_record, any_changes) | ||
end | ||
end | ||
|
||
private | ||
|
||
def resource_klass | ||
CP::Benchmark | ||
end | ||
|
||
def prepare_benchmark(row) | ||
find_record_by(:id, row) || | ||
CP::Benchmark.find_or_initialize_by( | ||
sector: find_or_create_sector(row), | ||
release_date: parse_date(row[:release_date]), | ||
scenario: row[:scenario] | ||
) | ||
end | ||
|
||
def find_or_create_sector(row) | ||
return unless row[:sector].present? | ||
|
||
Sector.where('lower(name) = ?', row[:sector].downcase).first || | ||
Sector.new(name: row[:sector]) | ||
end | ||
|
||
def benchmark_attributes(row) | ||
{ | ||
scenario: row[:scenario], | ||
release_date: parse_date(row[:release_date]), | ||
emissions: emissions(row) | ||
} | ||
end | ||
|
||
def parse_date(date) | ||
Import::DateUtils.safe_parse(date, ['%Y-%m', '%Y-%m-%d']) | ||
end | ||
|
||
def emissions(row) | ||
row.headers.grep(EMISSION_YEAR_PATTERN).reduce({}) do |acc, year| | ||
next acc unless row[year].present? | ||
|
||
acc.merge(year.to_s.to_i => row[year].to_f) | ||
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
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,22 @@ | ||
<template data-target="cp-benchmark-form.emissionRowTemplate"> | ||
<%= render "emissions_table_row", year: '', value: '' %> | ||
</template> | ||
|
||
<table data-target="cp-benchmark-form.emissionTable"> | ||
<thead> | ||
<th></th> | ||
<th>Year</th> | ||
<th>Value</th> | ||
<th></th> | ||
</thead> | ||
<tbody> | ||
<% f.object&.emissions&.each do |year, value| %> | ||
<%= render "emissions_table_row", year: year, value: value %> | ||
<% end %> | ||
<tr data-target="cp-benchmark-form.emissionRowAdd"> | ||
<td colspan="4"> | ||
<%= button_tag "Add new value", type: 'button', class: 'button button--raised', data: { action: "click->cp-benchmark-form#addEmission" } %> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> |
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,6 @@ | ||
<tr class="emission"> | ||
<td><span class="sortable-handle"></span></td> | ||
<td><input name="emission_year" type="number" value="<%= year %>" /></td> | ||
<td><input name="emission_value" type="number" value="<%= value %>" /></td> | ||
<td><%= button_tag "Remove", type: 'button', class: 'button button--raised button--red', data: { action: "click->cp-benchmark-form#removeEmission" } %></td> | ||
</tr> |
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,22 @@ | ||
<%= semantic_form_for [:admin, resource], builder: ActiveAdmin::FormBuilder, html: {'data-controller' => 'check-modified cp-benchmark-form'} do |f| %> | ||
<%= f.semantic_errors(*f.object.errors.keys) %> | ||
<%= f.inputs do %> | ||
<%= f.input :sector %> | ||
<%= f.input :release_date %> | ||
<%= f.input :scenario %> | ||
<%= f.input :emissions, as: :hidden, input_html: { value: f.object.emissions.to_json, id: 'input_emissions' } %> | ||
<% end %> | ||
|
||
<div class="panel"> | ||
<h3>Benchmark emission values</h3> | ||
<div class="panel-contents padding-20"> | ||
<%= render "emissions_table", f: f %> | ||
</div> | ||
</div> | ||
|
||
<%= f.actions do %> | ||
<%= f.action :submit %> | ||
<%= f.action :cancel, :wrapper_html => { :class => 'cancel' } %> | ||
<% 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,8 @@ | ||
class CpBenchmarksFlatten < ActiveRecord::Migration[5.2] | ||
def change | ||
remove_column :cp_benchmarks, :benchmarks, :jsonb | ||
add_column :cp_benchmarks, :emissions, :jsonb | ||
add_column :cp_benchmarks, :scenario, :string | ||
rename_column :cp_benchmarks, :date, :release_date | ||
end | ||
end |
Oops, something went wrong.