-
Notifications
You must be signed in to change notification settings - Fork 8
Added route and serializer for fetching output curves in bulk #1613
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
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' | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking the endpoint is already called (output) |
||
get :merit | ||
get :dump | ||
put :dashboard | ||
|
@@ -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' | ||
|
There was a problem hiding this comment.
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[]
There was a problem hiding this comment.
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