Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functional inputs serialisation to csv #1506

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions app/controllers/api/v3/inputs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class InputsController < ::Api::V3::BaseController
before_action do
@scenario = Scenario.find(params[:scenario_id])
authorize!(:read, @scenario)
set_default_format
end

# GET /api/v3/inputs
Expand All @@ -17,11 +18,33 @@ class InputsController < ::Api::V3::BaseController
def index
extras = ActiveModel::Type::Boolean.new.cast(params[:include_extras])

render json: InputSerializer.collection(
Input.all,
@scenario,
**serializer_args(extra_attributes: extras)
)
respond_to do |format|
format.json do
render json: InputSerializer.collection(
Input.all,
@scenario,
**serializer_args(extra_attributes: extras)
)
end

format.csv do
csv_data = CSV.generate(headers: true) do |csv|
csv << ["Key", "Min", "Max", "Default", "User value", "Unit", "Share Group"]
Input.all.each do |input|
serializer = InputSerializer.serializer_for(
input,
@scenario,
**serializer_args(extra_attributes: extras)
)
csv << serializer.to_csv_row
end
end

send_data csv_data,
filename: "scenario_#{@scenario.id}_inputs.csv",
type: "text/csv"
end
end
end

# GET /api/v3/inputs/:id
Expand Down Expand Up @@ -77,6 +100,10 @@ def serializer_args(extra_attributes:)
extra_attributes:
}
end

def set_default_format
request.format = :json unless params[:format]
end
end
end
end
13 changes: 13 additions & 0 deletions app/serializers/input_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ def as_json(*)
json
end

def to_csv_row
json = as_json.with_indifferent_access
[
@input.key, # Key
json[:min] || "", # Min Value
json[:max] || "", # Max Value
json[:default] || "", # Default Value
json[:user] || "", # User Value
json[:unit] || "", # Unit
json[:share_group] || "" # Share Group
]
end

# A simple wrapper around Scenario which converts user and balanced values
# to an indifferent-access hash. Prevents creating new copies of these
# hashes for each and every input being presented.
Expand Down
66 changes: 0 additions & 66 deletions db/migrate/20250113141155_remove_external_wtp.rb

This file was deleted.