Class: Yao::Resources::Tenant

Inherits:
Base
  • Object
show all
Defined in:
lib/kakin/yao_ext/tenant.rb

Instance Method Summary collapse

Instance Method Details

#network_usage(ip_regexp, type, start_time, end_time) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kakin/yao_ext/tenant.rb', line 5

def network_usage(ip_regexp, type, start_time, end_time)
  servers.inject(0) do |t, server|
    samples = server.old_samples(counter_name: "network.#{type}.bytes", query: {'q.field': 'timestamp', 'q.op': 'gt', 'q.value': start_time})
    if samples.empty?
      t
    else
      wan_samples = samples.select{|s| s.["mac"] == server.mac_address(ip_regexp) }.sort_by(&:timestamp)
      if wan_samples.empty? || (wan_samples.size == 1)
        t
      else
        last_sample_index = wan_samples.find_index{|s| s.timestamp > Time.parse(end_time) }
        last_sample = if last_sample_index
                        wan_samples[last_sample_index]
                      else
                        wan_samples[-1]
                      end
        transferred_bits = last_sample.counter_volume - wan_samples[0].counter_volume
        period = (last_sample.timestamp - wan_samples[0].timestamp).to_i
        # ignored negative number or zero period. Thease are unexpected data.
        # We need to investigate why generate these counter.
        if transferred_bits < 0 || period.zero?
          t
        else
          t + transferred_bits / period
        end
      end
    end
  end
end