require 'date' require 'nera_db_folders' require 'nera_simulator_records' require 'nera_parameter_records' require 'nera_run_records' require 'nera_job_records' module NERA class ParameterLayerController # instance of NERA::DbFolders @db_folders # simulator class @sim_class # instance of NERA::ParameterRecords @param_records def initialize( path_db_folder, sim_name) raise ArgumentError unless path_db_folder.is_a?(String) @db_folders = NERA::DbFolders.new( path_db_folder) sim_records = NERA::SimulatorRecords.new( @db_folders.path_to_simulators_table) sim_records.set_yaml_file( @db_folders.path_to_simulators_yaml ) found = sim_records.list.find do |rec| rec[:name] == sim_name.to_s end raise "No such simulator." unless found sim_id = found[:id] @sim_class = NERA::Simulator.inherited_simulators.find do |sim| sim.to_s == sim_name.to_s end raise "No such simulator." unless @sim_class p_tab_path = @db_folders.path_to_parameters_table( @sim_class) @param_records = NERA::ParameterRecords.new( p_tab_path, sim_records, sim_id) @param_records.set_yaml_file( @db_folders.path_to_parameters_yaml( @sim_class) ) end def parameters_list_in_csv l_active = @param_records.list_active header = @param_records.keys.dup header.delete( :in_trashbox?) csv_list = [] l_active.each do |p_hash| strings = [] header.each do |key| if p_hash[key].is_a?(DateTime) strings << p_hash[key].to_s.split('T')[0] elsif p_hash[key] strings << p_hash[key].to_s elsif p_hash[key] == nil found = @sim_class::Parameters.find do |p| p[0] == key end strings << found[2].to_s end end csv_list << strings.join(", ") end return header.join(", "), csv_list end def get_id_from_csv_string( pstr) return pstr.split(',').first.to_i end def list_of_parameters return @sim_class::Parameters.dup end def path_to_param_layer return @db_folders.path_to_parameter_layer( @sim_class) end def create_a_new_parameter_set( p_hash) pid = nil @param_records.transaction { pid = @param_records.add( p_hash) return nil unless pid b = @db_folders.create_new_parameter_folder( @sim_class, pid) raise "Creation of a new folder for #{@sim_calss}, #{pid} failed." unless b path = @db_folders.path_to_runs_table( @sim_class, pid) c = NERA::RunRecords.create_table( path) raise "File #{path} already exists." unless c @db_folders.logger.info(self.class.to_s) { "created a new parameter set (#{@sim_class.to_s}/#{pid})" } } return pid end def move_a_parameter_set_into_trashbox( id) return nil unless @param_records.find_by_id(id) run_records = NERA::RunRecords.new( @db_folders.path_to_runs_table( @sim_class, id), @param_records, id ) return nil if run_records.list_all_not_finished.size > 0 job_records = NERA::JobRecords.new( @db_folders.path_to_jobs_table) job_records.transaction { # to lock the runs tables @param_records.transaction { flag = @param_records.update_to_state_trashbox(id) return nil unless flag r_path = @db_folders.path_to_run_layer( @sim_class, id).sub(/\/$/,'') cmd = "tar cvjf #{r_path}.tar.bz2 #{r_path}" system(cmd) raise "Command #{cmd} failed" unless $? == 0 FileUtils.rm_r(r_path) @db_folders.logger.info(self.class.to_s) { "moved a parameter set into the trashbox (#{@sim_class.to_s}/#{id})" } } } return true end def trashbox_parameter_list_in_csv lt = @param_records.list_trashbox header = @param_records.keys.dup header.delete( :in_trashbox?) csv_list = [] lt.each do |p_hash| strings = [] header.each do |key| if p_hash[key].is_a?(DateTime) strings << p_hash[key].to_s.split('T')[0] elsif p_hash[key] strings << p_hash[key].to_s elsif p_hash[key] == nil found = @sim_class::Parameters.find do |p| p[0] == key end strings << found[2].to_s end end csv_list << strings.join(", ") end return header.join(", "), csv_list end def revert_a_parameter_set_in_trashbox( id) @param_records.transaction { flag = @param_records.update_to_state_active(id) return nil unless flag r_path = @db_folders.path_to_run_layer( @sim_class, id).sub(/\/$/,'') cmd = "tar xvjf #{r_path}.tar.bz2" system(cmd) raise "Command #{cmd} failed" unless $? == 0 FileUtils.rm(r_path+".tar.bz2") @db_folders.logger.info(self.class.to_s) { "reverted a parameter set in the trashbox (#{@sim_class.to_s}/#{id})" } } return true end def delete_a_parameter_set( id) @param_records.transaction { flag = @param_records.destroy(id) return nil unless flag r_path = @db_folders.path_to_run_layer( @sim_class, id).sub(/\/$/,'') FileUtils.rm("#{r_path}.tar.bz2") @db_folders.logger.info(self.class.to_s) { "deleted a parameter set in the trashbox (#{@sim_class.to_s}/#{id})" } } return true end end end