Class: EtnaApp::Magma::Models::Attributes::LoadTableFromCsv

Inherits:
Etna::Command
  • Object
show all
Includes:
WithEtnaClients
Defined in:
lib/commands.rb

Instance Attribute Summary

Attributes inherited from Etna::Command

#parent

Instance Method Summary collapse

Methods included from WithEtnaClients

#environment, #exit, exit, #janus_client, #magma_client, #metis_client, #polyphemus_client, #token

Methods inherited from Etna::Command

#completions, #fill_in_missing_params, #find_command, #initialize, parent_scope, #setup

Methods included from Etna::CommandOrExecutor

#command_name, #completions_for, #desc, #flag_argspec, #flag_as_parameter, included, #parse_flags, #program_name, #usage

Constructor Details

This class inherits a constructor from Etna::Command

Instance Method Details

#execute(project_name, model_name, file_path, execute: false) ⇒ Object



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/commands.rb', line 511

def execute(project_name, model_name, file_path, execute: false)
  request = Etna::Clients::Magma::RetrievalRequest.new(project_name: project_name)
  request.model_name = model_name
  request.attribute_names = 'all'
  request.record_names = []
  model = magma_client.retrieve(request).models.model(model_name)
  model_parent_name = model.template.attributes.all.select do |attribute|
    attribute.attribute_type == Etna::Clients::Magma::AttributeType::PARENT
  end.first.name

  other_attribute_names = model.template.attributes.all.reject do |attribute|
    attribute.attribute_type == Etna::Clients::Magma::AttributeType::PARENT
  end.map do |attribute|
    attribute.name
  end

  # NOTE: This does not call ensure_parent currently because of MVIR1 consent--
  #   if the timepoint doesn't exist, the patient may be no study? (one example, at least)
  update_request = Etna::Clients::Magma::UpdateRequest.new(project_name: project_name)

  data = CSV.parse(File.read(file_path), headers: true)

  data.by_row.each do |row|
    revision = {}
    other_attribute_names.each do |attribute_name|
      revision[attribute_name] = row[attribute_name] unless row[attribute_name].nil?
    end
    update_request.append_table(model_parent_name, row[model_parent_name], model_name, revision)
  end

  puts update_request

  if execute
    magma_client.update_json(update_request)
  end
end