<commit_before>module Stretcher
  class Server
    attr_reader :uri, :http, :logger

    # Instantiate a new instance in a manner convenient for using the block syntax.
    # Can be used interchangably with +Stretcher::Server.new+ but will return the value
    # of the block if present. See the regular constructor for full options.
    def self.with_server(*args)
      s = self.new(*args)
      yield s
    end
    
    # Represents a Server context in elastic search.
    # Use +with_server+ when you want to use the block syntax.
    # The options hash takes an optional instance of Logger under :logger.
    #
    #    server = Stretcher::Server.new('http://localhost:9200')
    def initialize(uri='http://localhost:9200', options={})
      @uri = uri

      @http = Faraday.new(:url => @uri) do |builder|        
        builder.response :mashify
        builder.response :json, :content_type => /\bjson$/
        
        builder.request :json
        
        builder.adapter :net_http_persistent
        
        builder.options[:read_timeout] = 4
        builder.options[:open_timeout] = 2
      end
      
      if options[:logger]
        @logger = options[:logger]
      else
        @logger = Logger.new(STDOUT)
        @logger.level = Logger::WARN
      end
      
      @logger.formatter = proc do |severity, datetime, progname, msg|
        "[Stretcher][#{severity}]: #{msg}\n"
      end
    end

    # Returns a Stretcher::Index object for the index +name+.
    # Optionally takes a block, which will be passed a single arg with the Index obj
    # The block syntax returns the evaluated value of the block
    # 
    #    my_server.index(:foo) # => #<Stretcher::Index ...>
    #    my_server.index(:foo) {|idx| 1+1} # => 2
    def index(name, &block)
      idx = Index.new(self, name, logger: logger)
      block ? block.call(idx) : idx
    end

    def bulk(data)
      request(:post, path_uri("/_bulk")) do |req|
        req.body = data
      end
    end
    
    # Returns true if the server is currently reachable, raises an error otherwise
    def up?
      request(:get, path_uri)
      true
    end
    
    # Takes an array of msearch data as per
    # http://www.elasticsearch.org/guide/reference/api/multi-search.html
    # Should look something like:
    #    data = [
    #    {"index" : "test"}
    #    {"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
    #    {"index" : "test", "search_type" : "count"}
    #    {"query" : {"match_all" : {}}}
    #    ]
    # server.msearch(data)
    def msearch(body=[])
      raise ArgumentError, "msearch takes an array!" unless body.is_a?(Array)
      fmt_body = body.map(&:to_json).join("\n") + "\n"<commit_msg>Rdoc indentation fix<commit_after>module Stretcher
  class Server
    attr_reader :uri, :http, :logger

    # Instantiate a new instance in a manner convenient for using the block syntax.
    # Can be used interchangably with +Stretcher::Server.new+ but will return the value
    # of the block if present. See the regular constructor for full options.
    def self.with_server(*args)
      s = self.new(*args)
      yield s
    end
    
    # Represents a Server context in elastic search.
    # Use +with_server+ when you want to use the block syntax.
    # The options hash takes an optional instance of Logger under :logger.
    #
    #    server = Stretcher::Server.new('http://localhost:9200')
    def initialize(uri='http://localhost:9200', options={})
      @uri = uri

      @http = Faraday.new(:url => @uri) do |builder|        
        builder.response :mashify
        builder.response :json, :content_type => /\bjson$/
        
        builder.request :json
        
        builder.adapter :net_http_persistent
        
        builder.options[:read_timeout] = 4
        builder.options[:open_timeout] = 2
      end
      
      if options[:logger]
        @logger = options[:logger]
      else
        @logger = Logger.new(STDOUT)
        @logger.level = Logger::WARN
      end
      
      @logger.formatter = proc do |severity, datetime, progname, msg|
        "[Stretcher][#{severity}]: #{msg}\n"
      end
    end

    # Returns a Stretcher::Index object for the index +name+.
    # Optionally takes a block, which will be passed a single arg with the Index obj
    # The block syntax returns the evaluated value of the block
    # 
    #    my_server.index(:foo) # => #<Stretcher::Index ...>
    #    my_server.index(:foo) {|idx| 1+1} # => 2
    def index(name, &block)
      idx = Index.new(self, name, logger: logger)
      block ? block.call(idx) : idx
    end

    def bulk(data)
      request(:post, path_uri("/_bulk")) do |req|
        req.body = data
      end
    end
    
    # Returns true if the server is currently reachable, raises an error otherwise
    def up?
      request(:get, path_uri)
      true
    end
    
    # Takes an array of msearch data as per
    # http://www.elasticsearch.org/guide/reference/api/multi-search.html
    # Should look something like:
    #    data = [
    #      {"index" : "test"}
    #      {"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
    #      {"index" : "test", "search_type" : "count"}
    #      {"query" : {"match_all" : {}}}
    #    ]
    #    server.msearch(data)
    def msearch(body=[])
      raise ArgumentError, "msearch takes an array!" unless body.is_a?(Array)
      fmt_body = body.map(&:to_json).join("\n") + "\n"
{'fraction_non_alphanumeric': 0.12176165803108809, 'fraction_numerical': 0.005921539600296077, 'mean_word_length': 2.3282019704433496, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '320399', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1641, 'n_words': 497}