Class: Druid::DataSource

Inherits:
Object
  • Object
show all
Defined in:
lib/druid/data_source.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, uri) ⇒ DataSource

Returns a new instance of DataSource.



9
10
11
12
13
14
15
16
17
# File 'lib/druid/data_source.rb', line 9

def initialize(name, uri)
  @name = name.split('/').last
  uri = uri.sample if uri.is_a?(Array)
  if uri.is_a?(String)
    @uri = URI(uri)
  else
    @uri = uri
  end
end

Instance Attribute Details

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



7
8
9
# File 'lib/druid/data_source.rb', line 7

def dimensions
  @dimensions
end

#metricsObject (readonly)

Returns the value of attribute metrics.



7
8
9
# File 'lib/druid/data_source.rb', line 7

def metrics
  @metrics
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/druid/data_source.rb', line 7

def name
  @name
end

#uriObject (readonly)

Returns the value of attribute uri.



7
8
9
# File 'lib/druid/data_source.rb', line 7

def uri
  @uri
end

Instance Method Details

#metadataObject



19
20
21
# File 'lib/druid/data_source.rb', line 19

def 
   ||= metadata!
end

#metadata!(opts = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/druid/data_source.rb', line 23

def metadata!(opts = {})
  meta_path = "#{@uri.path}datasources/#{name}"

  if opts[:interval]
    from, to = opts[:interval]
    from = from.respond_to?(:iso8601) ? from.iso8601 : ISO8601::DateTime.new(from).to_s
    to = to.respond_to?(:iso8601) ? to.iso8601 : ISO8601::DateTime.new(to).to_s

    meta_path += "?interval=#{from}/#{to}"
  end

  req = Net::HTTP::Get.new(meta_path)
  response = Net::HTTP.new(uri.host, uri.port).start do |http|
    http.open_timeout = 10 # if druid is down fail fast
    http.read_timeout = nil # we wait until druid is finished
    http.request(req)
  end

  if response.code != '200'
    raise "Request failed: #{response.code}: #{response.body}"
  end

  MultiJson.load(response.body)
end

#post(query) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/druid/data_source.rb', line 56

def post(query)
  query = query.query if query.is_a?(Druid::Query::Builder)
  query = Query.new(MultiJson.load(query)) if query.is_a?(String)
  query.dataSource = name

  req = Net::HTTP::Post.new(uri.path, { 'Content-Type' => 'application/json' })
  query_as_json = query.as_json
  req.body = MultiJson.dump(query_as_json)


  response = ActiveSupport::Notifications.instrument('post.druid', data_source: name, query: query_as_json) do
    Net::HTTP.new(uri.host, uri.port).start do |http|
      http.open_timeout = 10 # if druid is down fail fast
      http.read_timeout = nil # we wait until druid is finished
      http.request(req)
    end
  end

  if response.code != '200'
    # ignore GroupBy cache issues and try again without cached results
    if query.context.useCache != false && response.code == "500" && response.body =~ /Cannot have a null result!/
      query.context.useCache = false
      return self.post(query)
    end

    raise Error.new(response)
  end

  MultiJson.load(response.body)
end