lib/urbanopt/scenario/default_reports/scenario_report.rb in urbanopt-scenario-0.1.0 vs lib/urbanopt/scenario/default_reports/scenario_report.rb in urbanopt-scenario-0.1.1

- old
+ new

@@ -1,7 +1,7 @@ # ********************************************************************************* -# URBANopt, Copyright (c) 2019, Alliance for Sustainable Energy, LLC, and other +# URBANopt, Copyright (c) 2019-2020, Alliance for Sustainable Energy, LLC, and other # contributors. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: # @@ -32,10 +32,11 @@ require 'urbanopt/scenario/default_reports/feature_report' require 'urbanopt/scenario/default_reports/logger' require 'urbanopt/scenario/default_reports/program' require 'urbanopt/scenario/default_reports/reporting_period' require 'urbanopt/scenario/default_reports/timeseries_csv' +require 'urbanopt/scenario/default_reports/distributed_generation' require 'urbanopt/scenario/default_reports/validator' require 'json-schema' require 'json' require 'pathname' @@ -49,11 +50,11 @@ # The second is a CSV format saved to 'default_scenario_report.csv'. ## class ScenarioReport attr_accessor :id, :name, :directory_name, :timesteps_per_hour, :number_of_not_started_simulations, :number_of_started_simulations, :number_of_complete_simulations, :number_of_failed_simulations, - :timeseries_csv, :location, :program, :construction_costs, :reporting_periods, :feature_reports # :nodoc: + :timeseries_csv, :location, :program, :construction_costs, :reporting_periods, :feature_reports, :distributed_generation # :nodoc: # ScenarioReport class intializes the scenario report attributes: # +:id+ , +:name+ , +:directory_name+, +:timesteps_per_hour+ , +:number_of_not_started_simulations+ , # +:number_of_started_simulations+ , +:number_of_complete_simulations+ , +:number_of_failed_simulations+ , # +:timeseries_csv+ , +:location+ , +:program+ , +:construction_costs+ , +:reporting_periods+ , +:feature_reports+ ## @@ -86,16 +87,18 @@ @reporting_periods = [] hash[:reporting_periods].each do |rp| @reporting_periods << ReportingPeriod.new(rp) end - # intialized here to be used in the add_feature_report method + # feature_report is intialized here to be used in the add_feature_report method @feature_reports = [] hash[:feature_reports].each do |fr| @feature_reports << FeatureReport.new(fr) end + @distributed_generation = DistributedGeneration.new(hash[:distributed_generation] || {}) + @file_name = 'default_scenario_report' # initialize class variables @@validator and @@schema @@validator ||= Validator.new @@schema ||= @@validator.schema # initialize @@logger @@ -126,44 +129,62 @@ ## # Gets the saved JSON file path. ## def json_path - File.join(@directory_name, 'default_scenario_report.json') + File.join(@directory_name, @file_name + '.json') end ## # Gets the saved CSV file path. ## def csv_path - File.join(@directory_name, 'default_scenario_report.csv') + File.join(@directory_name, @file_name + '.csv') end ## # Saves the 'default_feature_report.json' and 'default_scenario_report.csv' files ## - def save + # [parameters]: + # +file_name+ - _String_ - Assign a name to the saved scenario results file + def save(file_name = 'default_scenario_report') + # reassign the initialize local variable @file_name to the file name input. + @file_name = file_name + + # save the csv data + old_timeseries_path = nil + if !@timeseries_csv.path.nil? + old_timeseries_path = @timeseries_csv.path + end + + @timeseries_csv.path = File.join(@directory_name, file_name + '.csv') + @timeseries_csv.save_data + hash = {} hash[:scenario_report] = to_hash hash[:feature_reports] = [] @feature_reports.each do |feature_report| hash[:feature_reports] << feature_report.to_hash end - File.open(json_path, 'w') do |f| + json_name_path = File.join(@directory_name, file_name + '.json') + + File.open(json_name_path, 'w') do |f| f.puts JSON.pretty_generate(hash) - # make sure data is written to the disk one way or the other #:nodoc: + # make sure data is written to the disk one way or the other begin f.fsync rescue StandardError f.flush end end - # save the csv data #:nodoc: - timeseries_csv.save_data(csv_path) - + if !old_timeseries_path.nil? + @timeseries_csv.path = old_timeseries_path + else + @timeseries_csv.path = File.join(@directory_name, 'default_scenario_report.csv') + end return true end ## # Converts to a Hash equivalent for JSON serialization. @@ -182,10 +203,11 @@ result[:number_of_complete_simulations] = @number_of_complete_simulations if @number_of_complete_simulations result[:number_of_failed_simulations] = @number_of_failed_simulations if @number_of_failed_simulations result[:timeseries_csv] = @timeseries_csv.to_hash if @timeseries_csv result[:location] = @location.to_hash if @location result[:program] = @program.to_hash if @program + result[:distributed_generation] = @distributed_generation.to_hash if @distributed_generation result[:construction_costs] = [] @construction_costs.each { |cc| result[:construction_costs] << cc.to_hash } if @construction_costs result[:reporting_periods] = [] @@ -262,10 +284,12 @@ @construction_costs = ConstructionCost.merge_construction_costs(@construction_costs, feature_report.construction_costs) # merge reporting_periods information @reporting_periods = ReportingPeriod.merge_reporting_periods(@reporting_periods, feature_report.reporting_periods) - # add the array of feature_reports + @distributed_generation = DistributedGeneration.merge_distributed_generation(@distributed_generation, feature_report.distributed_generation) + + # add feature_report @feature_reports << feature_report # scenario report location takes the location of the first feature in the list @location = feature_reports[0].location end