<commit_before>module Validators
  class CalculatingSmokingGunValidator < SmokingGunValidator
    include Validators::Validator
    def initialize(measures, records, test_id, options = {})
      if measures.length > 1
        # throw some kind of an error here because we only want to do this for
        # single measure testing
      end
      super
      @measure = measures.first
    end

    def compare_results(original, calculated, options)
      comp = true
      %w(IPP DENOM NUMER DENEX DENEXCEP MSRPOPL MSRPOPLEXCEP values).each do |pop|
        next unless original[pop] != calculated[pop]
        add_error("Calculated value for #{pop} does not match expected value  #{original[pop]} / #{calculated[pop]}",
                  file_name: options[:file_name])
        comp = false
      end
      comp
    end

    def validate_calculated_results(doc, options)
      te = options['test_execution']
      ex_opts = { 'test_id' => te.id, 'bundle_id' => @bundle.id,  'effective_date' => te.task.effective_date,
                  'enable_logging' => true, 'enable_rationale' => true,
                  'oid_dictionary' => generate_oid_dictionary(@measure, @bundle.id) }
      @mre = QME::MapReduce::Executor.new(@measure.hqmf_id, @measure.sub_id, ex_opts)
      record = HealthDataStandards::Import::Cat1::PatientImporter.instance.parse_cat1(doc)

      record.test_id = te.id
      record.medical_record_number = rand(1_000_000_000_000_000)
      record.save

      results = @mre.get_patient_result(record.medical_record_number)

      doc_name = build_doc_name(doc)
      mrn = @names[doc_name]
      return false unless mrn
      original_results = QME::PatientCache.where('value.medical_record_id' => mrn,
                                                 'value.test_id' => @test_id,
                                                 'value.measure_id' => @measure.hqmf_id,
                                                 'value.sub_id' => @measure.sub_id).first
      return true if original_results.nil? && results.nil?
      compare_results(original_results.value, results, options)
    end

    def generate_oid_dictionary(measure, bundle_id)
      valuesets = HealthDataStandards::CQM::Bundle.find(bundle_id).value_sets.in(oid: measure.oids)
      js = {}
      valuesets.each do |vs|
        js[vs.oid] = cached_value(vs)
      end
      js.to_json
    end

    def cached_value(vs)
      @loaded_valuesets ||= {}
      return @loaded_valuesets[vs.oid] if @loaded_valuesets[vs.oid]
      js = {}
      vs.concepts.each do |con|
        name = con.code_system_name
        js[name] ||= []
        js[name] << con.code.downcase unless js[name].index(con.code.downcase)
      end
      @loaded_valuesets[vs.oid] = js
      js
    end

    def validate(doc, options)
      @can_continue = true
      valid = validate_calculated_results(doc, options)
      if !valid
        doc_name = build_doc_name(doc)
        mrn = @names[doc_name]
        @found_names << doc_name if mrn
        validate_name(doc_name, options)
      else
        super
      end
    end
  end
end
<commit_msg>fixing bug where original smoking gun validatio nwas called for documents passing calculating smoking gun validations<commit_after>module Validators
  class CalculatingSmokingGunValidator < SmokingGunValidator
    include Validators::Validator
    def initialize(measures, records, test_id, options = {})
      if measures.length > 1
        # throw some kind of an error here because we only want to do this for
        # single measure testing
      end
      super
      @measure = measures.first
    end

    def compare_results(original, calculated, options)
      comp = true
      %w(IPP DENOM NUMER DENEX DENEXCEP MSRPOPL MSRPOPLEXCEP values).each do |pop|
        next unless original[pop] != calculated[pop]
        add_error("Calculated value for #{pop} does not match expected value  #{original[pop]} / #{calculated[pop]}",
                  file_name: options[:file_name])
        comp = false
      end
      comp
    end

    def validate_calculated_results(doc, options)
      te = options['test_execution']
      ex_opts = { 'test_id' => te.id, 'bundle_id' => @bundle.id,  'effective_date' => te.task.effective_date,
                  'enable_logging' => true, 'enable_rationale' => true,
                  'oid_dictionary' => generate_oid_dictionary(@measure, @bundle.id) }
      @mre = QME::MapReduce::Executor.new(@measure.hqmf_id, @measure.sub_id, ex_opts)
      record = HealthDataStandards::Import::Cat1::PatientImporter.instance.parse_cat1(doc)

      record.test_id = te.id
      record.medical_record_number = rand(1_000_000_000_000_000)
      record.save

      results = @mre.get_patient_result(record.medical_record_number)

      doc_name = build_doc_name(doc)
      mrn = @names[doc_name]
      return false unless mrn
      @found_names << doc_name
      original_results = QME::PatientCache.where('value.medical_record_id' => mrn,
                                                 'value.test_id' => @test_id,
                                                 'value.measure_id' => @measure.hqmf_id,
                                                 'value.sub_id' => @measure.sub_id).first
      return true if original_results.nil? && results.nil?
      compare_results(original_results.value, results, options)
    end

    def generate_oid_dictionary(measure, bundle_id)
      valuesets = HealthDataStandards::CQM::Bundle.find(bundle_id).value_sets.in(oid: measure.oids)
      js = {}
      valuesets.each do |vs|
        js[vs.oid] = cached_value(vs)
      end
      js.to_json
    end

    def cached_value(vs)
      @loaded_valuesets ||= {}
      return @loaded_valuesets[vs.oid] if @loaded_valuesets[vs.oid]
      js = {}
      vs.concepts.each do |con|
        name = con.code_system_name
        js[name] ||= []
        js[name] << con.code.downcase unless js[name].index(con.code.downcase)
      end
      @loaded_valuesets[vs.oid] = js
      js
    end

    def validate(doc, options)
      @can_continue = true
      super unless validate_calculated_results(doc, options)
    end
  end
end

{'fraction_non_alphanumeric': 0.09889033942558746, 'fraction_numerical': 0.006201044386422976, 'mean_word_length': 2.4568527918781724, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4147812', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1820, 'n_words': 399}