class Drill

Constants

HEADERS
VERSION

Public Class Methods

new(url: nil, open_timeout: 3, read_timeout: nil) click to toggle source
# File lib/drill-sergeant.rb, line 17
def initialize(url: nil, open_timeout: 3, read_timeout: nil)
  url ||= ENV["DRILL_URL"] || "http://localhost:8047"
  # remove trailing slash
  @uri = URI.parse(url.sub(/\/\z/, ""))
  @http = Net::HTTP.new(@uri.host, @uri.port)
  @http.use_ssl = true if @uri.scheme == "https"
  @http.open_timeout = open_timeout if open_timeout
  @http.read_timeout = read_timeout if read_timeout
end

Public Instance Methods

cluster() click to toggle source
# File lib/drill-sergeant.rb, line 54
def cluster
  get("cluster.json")
end
metrics() click to toggle source
# File lib/drill-sergeant.rb, line 58
def metrics
  # no .json suffix
  get("status/metrics")
end
options() click to toggle source
# File lib/drill-sergeant.rb, line 63
def options
  get("options.json")
end
profiles(query_id = nil) click to toggle source
# File lib/drill-sergeant.rb, line 44
def profiles(query_id = nil)
  path = query_id ? "profiles/#{escape_path(query_id)}.json" : "profiles.json"
  get(path)
end
query(statement) click to toggle source
# File lib/drill-sergeant.rb, line 27
def query(statement)
  data = {
    queryType: "sql",
    query: statement
  }

  body = post("query.json", data)

  # return columns in order
  result = []
  columns = body["columns"]
  body["rows"].each do |row|
    result << columns.each_with_object({}) { |c, memo| memo[c] = row[c] }
  end
  result
end
storage(name = nil) click to toggle source
# File lib/drill-sergeant.rb, line 49
def storage(name = nil)
  path = name ? "storage/#{escape_path(name)}.json" : "storage.json"
  get(path)
end

Private Instance Methods

escape_path(path) click to toggle source
# File lib/drill-sergeant.rb, line 97
def escape_path(path)
  CGI.escape(path).gsub("+", "%20")
end
get(path) click to toggle source
# File lib/drill-sergeant.rb, line 69
def get(path)
  handle_response do
    @http.get("#{@uri.request_uri}#{path}", HEADERS)
  end
end
handle_response() { || ... } click to toggle source
# File lib/drill-sergeant.rb, line 81
def handle_response
  begin
    response = yield
  rescue Errno::ECONNREFUSED => e
    raise Drill::Error, e.message
  end

  unless response.kind_of?(Net::HTTPSuccess)
    body = JSON.parse(response.body) rescue nil
    message = body["errorMessage"] || "Bad response: #{response.code}"
    raise Drill::Error, message
  end

  JSON.parse(response.body)
end
post(path, data) click to toggle source
# File lib/drill-sergeant.rb, line 75
def post(path, data)
  handle_response do
    @http.post("#{@uri.request_uri}#{path}", data.to_json, HEADERS)
  end
end