Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 additions & 0 deletions app/controllers/api/v3/export_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,26 @@ def costs_parameters
send_csv(CostsParametersSerializer.new(@scenario), 'costs_parameters.%d.csv')
end

# GET /api/v3/scenarios/:id/bulk_output_curves
#
# Returns a single CSV file with specified output curves as columns.
# Query parameter 'curve_types' specifies which curves to include (comma-separated).
def bulk_output_curves
curve_types = params[:curve_types]&.split(',')&.map(&:strip)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do this with permit as []

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even with a require? That way we can tackle an empty csv automagically before we move into the serializer


if curve_types.blank?
render json: { error: 'curve_types parameter is required' }, status: :bad_request
return
end

serializer = OutputCurvesBulkSerializer.new(@scenario, curve_types)
send_data(
serializer.as_csv,
type: 'text/csv',
filename: "output_curves.#{@scenario.id}.csv"
)
end

private

def send_csv(serializer, filename_template)
Expand Down
40 changes: 40 additions & 0 deletions app/serializers/output_curves_bulk_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

# Serializes specified output curves for a scenario as a single CSV with each curve as a column.
class OutputCurvesBulkSerializer

def initialize(scenario, curve_types)
@scenario = scenario
@curve_types = curve_types || []
end
Comment on lines +6 to +9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def initialize(scenario, curve_types)
@scenario = scenario
@curve_types = curve_types || []
end
def initialize(scenario, curve_types = [])
@scenario = scenario
@curve_types = curve_types
end


def as_csv
return empty_csv if @curve_types.empty?

curves = {}
@curve_types.each do |curve_type|
csv = QueryCurveCsvSerializer.new(@scenario, curve_type).as_csv
lines = csv.lines.map(&:strip).reject(&:empty?)
lines.shift if lines.first && lines.first.match?(/[^\d.\-eE]/)
curves[curve_type] = lines
rescue
curves[curve_type] = []
end

max_len = curves.values.map(&:length).max || 0
require 'csv'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can move this one to the top!

CSV.generate do |csv|
csv << curves.keys
(0...max_len).each do |i|
csv << curves.keys.map { |k| curves[k][i] }
end
end
end

private

def empty_csv
require 'csv'
CSV.generate { |csv| csv << [] }
end
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
get :costs_parameters, to: 'export#costs_parameters'
get :sankey, to: 'export#sankey'
get :storage_parameters, to: 'export#storage_parameters'
get :bulk_output_curves, to: 'export#bulk_output_curves'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking the endpoint is already called (output)curves. Maybe we can rename this to just bulk or combined for clarity? What do you think?

get :merit
get :dump
put :dashboard
Expand Down Expand Up @@ -80,7 +81,6 @@
controller: :user_sortables, sortable_type: :space_heating

resources :custom_curves, only: %i[index]

get 'custom_curves/*id', to: 'custom_curves#show'
put 'custom_curves/*id', to: 'custom_curves#update'
delete 'custom_curves/*id', to: 'custom_curves#destroy'
Expand Down