Class: Inferno::CLI::Evaluate

Inherits:
Object
  • Object
show all
Defined in:
lib/inferno/apps/cli/evaluate.rb

Instance Method Summary collapse

Instance Method Details

#check_ig_version(ig) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/inferno/apps/cli/evaluate.rb', line 96

def check_ig_version(ig)
  versions = ig.ig_resource.fhirVersion

  return unless versions.any? { |v| v > '4.0.1' }

  puts '**WARNING** The selected IG targets a FHIR version higher than 4.0.1, which is not supported by Inferno.'
end

#evaluate(ig_path, data_path, options) ⇒ Object

See Also:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/inferno/apps/cli/evaluate.rb', line 60

def evaluate(ig_path, data_path, options)
  # NOTE: repositories is required here rather than at the top of the file because
  # the tree of requires means that this file and its requires get required by every CLI app.
  # Sequel::Model, used in some repositories, fetches the table schema at instantiation.
  # This breaks the `migrate` task by fetching a table before the task runs/creates it.
  require_relative '../../repositories'

  validate_args(ig_path, data_path)
  ig = Inferno::Repositories::IGs.new.find_or_load(ig_path)

  check_ig_version(ig)

  data =
    if data_path
      Inferno::DSL::FHIREvaluation::DatasetLoader.from_path(File.join(__dir__, data_path))
    else
      ig.examples
    end

  validator = setup_validator(ig_path)

  evaluator = Inferno::DSL::FHIREvaluation::Evaluator.new(ig, validator)

  config = Inferno::DSL::FHIREvaluation::Config.new
  results = evaluator.evaluate(data, config)
  output_results(results, options[:output])
end

#output_results(results, output) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/inferno/apps/cli/evaluate.rb', line 121

def output_results(results, output)
  if output&.end_with?('json')
    oo = FhirEvaluator::EvaluationResult.to_operation_outcome(results)
    File.write(output, oo.to_json)
    puts "Results written to #{output}"
  else
    counts = results.group_by(&:severity).transform_values(&:count)
    print(counts, 'Result Count')
    puts "\n"
    puts results
  end
end

#pad(string, length) ⇒ Object



147
148
149
# File 'lib/inferno/apps/cli/evaluate.rb', line 147

def pad(string, length)
  format("%#{length}.#{length}s", string)
end


134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/inferno/apps/cli/evaluate.rb', line 134

def print(output_fields, title)
  puts("╔══════════════ #{title} ═══════════════╗")
  puts('║ ╭────────────────┬──────────────────────╮ ║')
  output_fields.each_with_index do |(key, value), i|
    field_name = pad(key, 14)
    field_value = pad(value.to_s, 20)
    puts("║ │ #{field_name}#{field_value} │ ║")
    puts('║ ├────────────────┼──────────────────────┤ ║') unless i == output_fields.length - 1
  end
  puts('║ ╰────────────────┴──────────────────────╯ ║')
  puts('╚═══════════════════════════════════════════╝')
end

#run(ig_path, data_path, options) ⇒ Object

See Also:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/inferno/apps/cli/evaluate.rb', line 14

def run(ig_path, data_path, options)
  tmpdir = Dir.mktmpdir
  Dir.mkdir("#{tmpdir}/data")
  Dir.mkdir("#{tmpdir}/data/igs")
  Dir.mkdir("#{tmpdir}/config")
  FileUtils.cp(File.expand_path('evaluate/database.yml', __dir__), "#{tmpdir}/config/database.yml")

  ENV['TMPDIR'] = tmpdir
  ENV['FHIRPATH_URL'] = 'http://localhost:6790'
  ENV['FHIR_RESOURCE_VALIDATOR_URL'] = 'http://localhost:3501'

  puts 'Starting Inferno Evaluator Services...'
  system("#{services_base_command} up -d #{services_names}")

  ig_path = absolute_path_with_home_expansion(ig_path)
  data_path = absolute_path_with_home_expansion(data_path) if data_path

  Dir.chdir(tmpdir) do
    Migration.new.run(Logger::FATAL) # Hide migration output for evaluator
    evaluate(ig_path, data_path, options)
  end
ensure
  system("#{services_base_command} down #{services_names}")
  puts 'Stopped Inferno Evaluator Services'

  FileUtils.remove_entry_secure tmpdir
end

#services_base_commandObject



42
43
44
# File 'lib/inferno/apps/cli/evaluate.rb', line 42

def services_base_command
  "docker compose -f #{File.join(__dir__, 'evaluate', 'docker-compose.evaluate.yml')}"
end

#services_namesObject



46
47
48
# File 'lib/inferno/apps/cli/evaluate.rb', line 46

def services_names
  'hl7_validator_service fhirpath'
end

#setup_validator(ig_path) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/inferno/apps/cli/evaluate.rb', line 104

def setup_validator(ig_path)
  igs_directory = File.join(Dir.pwd, 'data', 'igs')
  if File.exist?(ig_path) && !File.realpath(ig_path).start_with?(igs_directory)
    destination_file_path = File.join(igs_directory, File.basename(ig_path))
    FileUtils.copy_file(ig_path, destination_file_path, true)
    ig_path = "igs/#{File.basename(ig_path)}"
  end
  Inferno::DSL::FHIRResourceValidation::Validator.new(:default, 'evaluator_cli') do
    igs(ig_path)

    cli_context do
      # For our purposes, code display mismatches should be warnings and not affect profile conformance
      displayWarnings(true)
    end
  end
end

#validate_args(ig_path, data_path) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/inferno/apps/cli/evaluate.rb', line 88

def validate_args(ig_path, data_path)
  raise 'A path to an IG is required!' unless ig_path

  return unless data_path && (!File.directory? data_path)

  raise "Provided path '#{data_path}' is not a directory"
end