Class: EtnaApp::Administrate::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



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/commands.rb', line 308

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 = 'all'
  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