Class: Network

Inherits:
Object
  • Object
show all
Includes:
Graphs::AreaNotStackedChart, Mongoid::Attributes::Dynamic, Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/app/models/network.rb

Class Method Summary collapse

Class Method Details

.all_interfaces_charts(options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/app/models/network.rb', line 18

def self.all_interfaces_charts(options)
  charts = []
  Network.where(:timestamp.gte => options[:start]).
          where(:timestamp.lt => options[:end]).
   where(:host_id => options[:host_id]).
   where(:plugin_id => options[:plugin_id]).
   order_by(timestamp: :asc).group_by{|u| u.interface}.each_pair do |interface, values|
    charts << self.interface_chart(interface, values)
  end
  return charts
end

.chart_data(options = {}) ⇒ Object



12
13
14
15
16
# File 'lib/app/models/network.rb', line 12

def self.chart_data(options = {})
  charts = []
  charts = self.all_interfaces_charts(options)
  return charts
end

.graphsObject



52
53
54
55
56
57
# File 'lib/app/models/network.rb', line 52

def self.graphs  
  {
    :bytes_receive_per_sec => { :line_color => '#0033FF' },
    :bytes_transmit_per_sec => {:line_color => '#00FF00' }
  }
end

.interface_chart(interface, values) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/app/models/network.rb', line 30

def self.interface_chart(interface, values)
  chart_data = self.chart_structure({:title => "Network traffic for #{interface}", :value_axis => { :title => "Network traffic for #{interface}"}})
  
  prev = nil
  values.each do |value|
    if prev.nil? then
      prev = value
      next
    end
    time_diff = value[:timestamp].to_i - prev[:timestamp].to_i
    chart_data[:graph_data] << {
      "timestamp" => value[:timestamp],
      "bytes_receive_per_sec" => ((value[:bytes_receive].to_i - prev[:bytes_receive].to_i)/time_diff).to_i,
      "bytes_transmit_per_sec" => ((value[:bytes_transmit].to_i - prev[:bytes_transmit].to_i)/time_diff).to_i
    }
    prev = value 
    
  end

  chart_data
end