Class: CdnManagerPcacheProbe::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/cdn_manager_pcache_probe/connector.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Connector

Returns a new instance of Connector.



6
7
8
9
# File 'lib/cdn_manager_pcache_probe/connector.rb', line 6

def initialize(url, options = {})
  @url = url
  @options = options
end

Instance Method Details

#get_live_probeObject



11
12
13
# File 'lib/cdn_manager_pcache_probe/connector.rb', line 11

def get_live_probe
  get_probe(Time.now)
end

#get_probe(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/cdn_manager_pcache_probe/connector.rb', line 15

def get_probe(options = {})
  probe = JSON.parse(
    RestClient.get(
      @url, params: { source: query(options) }
    )
  )
  puts "Response: \n" + JSON.pretty_generate(probe) if options[:debug]
  probe["aggregations"]["bytes_sent"]["value"]
end

#query(options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cdn_manager_pcache_probe/connector.rb', line 25

def query(options = {})
  interval = (options[:interval] || 5).to_i
  delay = (options[:delay] || 5).to_i
  volume_key_filter = "\"volume_key\": \"#{options[:key]}\"" if options[:key]
  query = %Q({
    "size": 0,
    "query": {
      "bool": {
          "filter": [
            {
              "range": {
                "@timestamp": {
                  "gt": "now-#{interval + delay}m",
                  "lt": "now-#{delay}m"
                }
              }
            },
            {
              "term": {
                "type": "pcache"
                #{", " + volume_key_filter if options[:key]}
              }
            }
          ]
      }
    },
    "aggs": {
      "bytes_sent": {
        "sum": {
          "field": "body_bytes_sent"
        }
      }
    }
  }).gsub(/\A[[:space:]]+/, '').gsub(/[[:space:]]+\z/, '').gsub(/[[:space:]]+/, ' ')
  puts "Query: \n" + JSON.pretty_generate(JSON.parse(query)) if options[:debug]
  query
end