<commit_before>require 'sprockets'
require 'tilt'
require 'query_utilities'

class QueryExecutor
  
  include QueryUtilities
  
  PATIENTS_COLLECTION = "records"

  def initialize(map_js, reduce_js, query_id, filter={})
    @map_js = map_js
    @reduce_js = reduce_js
    @query_id = query_id
    @filter = filter
  end

  def execute
    db =  Mongoid.master
    # convert the filter hash to a mongo query style hash.  Currently we are passing in a mongo style query hash so this is a no-op
    exts = db['system.js'].find().to_a.collect do |ext|
      if (ext['value'].class == BSON::Code)
        "#{ext['_id']}();\n"
      else
        ""
      end
    end
    results = db[PATIENTS_COLLECTION].map_reduce(build_map_function(exts) , @reduce_js, :query => @filter, raw: true, out: {inline: 1})
    result = Result.new
    result.query = Query.find(@query_id)
    
    results['results'].each do |rv|
      key = stringify_key(rv['_id'])
      result.write_attribute(key.to_sym, rv['value'])
    end
    
    result.save!
  end
  
  def stringify_key(key)
    if (key.is_a? BSON::OrderedHash)
      key = key.values.join('_')
    end
    if (key.is_a? Array)
      key = (key.map {|val| stringify_key(val)}).join('_')
    end
    key.to_s
  end

  def self.patient_api_javascript
    Tilt::CoffeeScriptTemplate.default_bare=true
    Rails.application.assets.find_asset("patient")
  end

  private
  def build_map_function(exts)
    "function() {
      this.hQuery || (this.hQuery = {});
      var hQuery = this.hQuery;
      #{exts.join}
      #{QueryExecutor.patient_api_javascript}
      #{@map_js}
      var patient = new hQuery.Patient(this);
      map(patient);
    };"

  end
end
<commit_msg>added more recursion to stringify key<commit_after>require 'sprockets'
require 'tilt'
require 'query_utilities'

class QueryExecutor
  
  include QueryUtilities
  
  PATIENTS_COLLECTION = "records"

  def initialize(map_js, reduce_js, query_id, filter={})
    @map_js = map_js
    @reduce_js = reduce_js
    @query_id = query_id
    @filter = filter
  end

  def execute
    db =  Mongoid.master
    # convert the filter hash to a mongo query style hash.  Currently we are passing in a mongo style query hash so this is a no-op
    exts = db['system.js'].find().to_a.collect do |ext|
      if (ext['value'].class == BSON::Code)
        "#{ext['_id']}();\n"
      else
        ""
      end
    end
    results = db[PATIENTS_COLLECTION].map_reduce(build_map_function(exts) , @reduce_js, :query => @filter, raw: true, out: {inline: 1})
    result = Result.new
    result.query = Query.find(@query_id)
    
    results['results'].each do |rv|
      key = stringify_key(rv['_id'])
      result.write_attribute(key.to_sym, rv['value'])
    end
    
    result.save!
  end
  
  def stringify_key(key)
    if (key.is_a? BSON::OrderedHash)
      key = (key.map {|val| stringify_key(val)}).join('_')
    end
    if (key.is_a? Array)
      key = (key.map {|val| stringify_key(val)}).join('_')
    end
    key.to_s
  end
  
  def self.patient_api_javascript
    Tilt::CoffeeScriptTemplate.default_bare=true
    Rails.application.assets.find_asset("patient")
  end

  private
  def build_map_function(exts)
    "function() {
      this.hQuery || (this.hQuery = {});
      var hQuery = this.hQuery;
      #{exts.join}
      #{QueryExecutor.patient_api_javascript}
      #{@map_js}
      var patient = new hQuery.Patient(this);
      map(patient);
    };"

  end
end

{'fraction_non_alphanumeric': 0.12746234067207415, 'fraction_numerical': 0.0005793742757821553, 'mean_word_length': 2.9061085972850678, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1837678', 'n_tokens_mistral': 1233, 'n_tokens_neox': 1220, 'n_words': 271}