<commit_before>this_dir = File.expand_path(File.dirname(__FILE__))
lib_dir = File.join(this_dir, 'lib')
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)

require 'grpc'
require_relative 'client/version'
require_relative 'proto/vtgateservice_services'
require_relative 'proto/vtrpc'

# Util
require 'socket'

module Vitess
  class Util
    NETWORK_INTERFACE_NAMES = ["eth0", "en0"]

    class << self
      def process_id
        Proc.id
      end

      def local_ip_address
        Socket.getifaddrs.select(&:broadaddr).find{ |ip_addr| NETWORK_INTERFACE_NAMES.include?(ip_addr.name) && ip_addr.addr.ipv4? }
      end
    end
  end

  class Client
    class << self
      attr_accessor :host

      def host
        'localhost:50051'
      end

      def connect
        # vtgatesservice.begin(Vtgateservice::BeginRequest.new(caller_id: caller_id(:connect)))
      end

      def end
      end

      def session
        connect.session
      end

      def query(sql, options = {})
        vtgateservice.execute(Vtgateservice::Request.new({caller_id: caller_id(:query), session: session, query: bound_query(sql)}))
      end

      private

      def bound_query(sql)
        Vtgateservice::BoundQuery.new(sql: sql,bind_variables: {})
      end

      def vtgateservice
        @vtgatesservice ||= Vtgateservice::Vitess::Service::Stub.new(host)
      end

      def caller_id(method_name: "", options: {})
        Vtrpc::CallerID.new(principal: Vitess::Util.local_ip_address, component: Vitess::Util.process_id, subcomponent: method_name)
      end
    end
  end
end

connection = Vitess::Client.connect
session  = connection.session
response = Vitess::Client.query('SELECT * FROM USERS LIMIT 5;')
p response.result<commit_msg>now a client is possible to throw a query to other services<commit_after>current_dir = File.expand_path(File.dirname(__FILE__))
proto_dir   = File.join(current_dir + '/lib/vitessproto')
$LOAD_PATH.unshift(current_dir)
$LOAD_PATH.unshift(proto_dir)

require 'google/protobuf'

require 'proto/query'
require 'proto/vtgate'
require 'proto/vtgateservice'

# Util
require 'socket'

# debug
require 'pry-byebug'

module Vitess
  class Util
    NETWORK_INTERFACE_NAMES = ["eth0", "en0"]

    class << self
      def process_id
        Process.pid
      end

      def local_ip_address
        Socket.getifaddrs.select(&:broadaddr).find{ |ip_addr| NETWORK_INTERFACE_NAMES.include?(ip_addr.name) && ip_addr.addr.ipv4? }
      end
    end
  end

  class Client
    class << self
      attr_accessor :host

      def host
      'localhost:15999'
      end

      def connect
        # vtgatesservice.begin(Vtgateservice::BeginRequest.new(caller_id: caller_id(:connect)))
      end

      def end
      end

      def query(sql, options = {})
        session = Vtgate::Session.new
        vtgateservice.execute(Vtgate::ExecuteRequest.new({caller_id: caller_id(:query), session: session, query: bound_query(sql)}))
      end

      private

      def bound_query(sql)
        Query::BoundQuery.new(sql: sql,bind_variables: {})
      end

      def vtgateservice
        @vtgatesservice ||= Vtgate::Stub.new(host)
      end

      def caller_id(method_name="", options: {})
        # FIXME : handle with possible exceptions
        principal = Vitess::Util.local_ip_address.addr.ip_address.encode("UTF-8")
        component = "process_id: #{Vitess::Util.process_id.to_s}"
        Vtrpc::CallerID.new({principal: principal, component: component, subcomponent: method_name.to_s})
      end
    end
  end
end

response = Vitess::Client.query('SELECT * FROM test_table LIMIT 5;')
p response.result
{'fraction_non_alphanumeric': 0.10795140806184428, 'fraction_numerical': 0.005245720596355604, 'mean_word_length': 3.2423887587822016, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, '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': '2089483', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1181, 'n_words': 234}