Class: Ifconf::InterfaceStatistics

Inherits:
Object
  • Object
show all
Defined in:
lib/ifconf/interface_statistics.rb

Overview

Immutable value object holding RX/TX packet, byte, and error counters for an interface.

Constant Summary collapse

BYTE_UNITS =
["B", "KiB", "MiB", "GiB", "TiB"].freeze
COUNTER_KEYS =
[
  :rx_packets, :rx_bytes, :rx_errors, :rx_dropped, :rx_overruns, :rx_frame,
  :tx_packets, :tx_bytes, :tx_errors, :tx_dropped, :tx_overruns, :tx_carrier,
  :collisions
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**counters) ⇒ InterfaceStatistics

Returns a new instance of InterfaceStatistics.



38
39
40
41
42
# File 'lib/ifconf/interface_statistics.rb', line 38

def initialize(**counters)
  validate_counters(counters)
  assign_counters(counters)
  freeze
end

Instance Attribute Details

#collisionsObject (readonly)

Returns the value of attribute collisions.



6
7
8
# File 'lib/ifconf/interface_statistics.rb', line 6

def collisions
  @collisions
end

#rx_bytesObject (readonly)

Returns the value of attribute rx_bytes.



6
7
8
# File 'lib/ifconf/interface_statistics.rb', line 6

def rx_bytes
  @rx_bytes
end

#rx_droppedObject (readonly)

Returns the value of attribute rx_dropped.



6
7
8
# File 'lib/ifconf/interface_statistics.rb', line 6

def rx_dropped
  @rx_dropped
end

#rx_errorsObject (readonly)

Returns the value of attribute rx_errors.



6
7
8
# File 'lib/ifconf/interface_statistics.rb', line 6

def rx_errors
  @rx_errors
end

#rx_frameObject (readonly)

Returns the value of attribute rx_frame.



6
7
8
# File 'lib/ifconf/interface_statistics.rb', line 6

def rx_frame
  @rx_frame
end

#rx_overrunsObject (readonly)

Returns the value of attribute rx_overruns.



6
7
8
# File 'lib/ifconf/interface_statistics.rb', line 6

def rx_overruns
  @rx_overruns
end

#rx_packetsObject (readonly)

Returns the value of attribute rx_packets.



6
7
8
# File 'lib/ifconf/interface_statistics.rb', line 6

def rx_packets
  @rx_packets
end

#tx_bytesObject (readonly)

Returns the value of attribute tx_bytes.



6
7
8
# File 'lib/ifconf/interface_statistics.rb', line 6

def tx_bytes
  @tx_bytes
end

#tx_carrierObject (readonly)

Returns the value of attribute tx_carrier.



6
7
8
# File 'lib/ifconf/interface_statistics.rb', line 6

def tx_carrier
  @tx_carrier
end

#tx_droppedObject (readonly)

Returns the value of attribute tx_dropped.



6
7
8
# File 'lib/ifconf/interface_statistics.rb', line 6

def tx_dropped
  @tx_dropped
end

#tx_errorsObject (readonly)

Returns the value of attribute tx_errors.



6
7
8
# File 'lib/ifconf/interface_statistics.rb', line 6

def tx_errors
  @tx_errors
end

#tx_overrunsObject (readonly)

Returns the value of attribute tx_overruns.



6
7
8
# File 'lib/ifconf/interface_statistics.rb', line 6

def tx_overruns
  @tx_overruns
end

#tx_packetsObject (readonly)

Returns the value of attribute tx_packets.



6
7
8
# File 'lib/ifconf/interface_statistics.rb', line 6

def tx_packets
  @tx_packets
end

Class Method Details

.build(rx: {}, tx: {}, collisions: 0) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ifconf/interface_statistics.rb', line 16

def self.build(rx: {}, tx: {}, collisions: 0)
  new(
    rx_packets: rx.fetch(:packets, 0),
    rx_bytes: rx.fetch(:bytes, 0),
    rx_errors: rx.fetch(:errors, 0),
    rx_dropped: rx.fetch(:dropped, 0),
    rx_overruns: rx.fetch(:overruns, 0),
    rx_frame: rx.fetch(:frame, 0),
    tx_packets: tx.fetch(:packets, 0),
    tx_bytes: tx.fetch(:bytes, 0),
    tx_errors: tx.fetch(:errors, 0),
    tx_dropped: tx.fetch(:dropped, 0),
    tx_overruns: tx.fetch(:overruns, 0),
    tx_carrier: tx.fetch(:carrier, 0),
    collisions: collisions,
  )
end

.zeroObject



34
35
36
# File 'lib/ifconf/interface_statistics.rb', line 34

def self.zero
  build
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



96
97
98
# File 'lib/ifconf/interface_statistics.rb', line 96

def ==(other)
  other.is_a?(InterfaceStatistics) && to_h == other.to_h
end

#drop_rateObject



66
67
68
69
70
# File 'lib/ifconf/interface_statistics.rb', line 66

def drop_rate
  return 0.0 if total_packets.zero?

  total_dropped.to_f / total_packets
end

#error_rateObject



60
61
62
63
64
# File 'lib/ifconf/interface_statistics.rb', line 60

def error_rate
  return 0.0 if total_packets.zero?

  total_errors.to_f / total_packets
end

#hashObject



102
103
104
# File 'lib/ifconf/interface_statistics.rb', line 102

def hash
  to_h.hash
end

#healthy?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/ifconf/interface_statistics.rb', line 72

def healthy?
  @rx_errors.zero? && @tx_errors.zero?
end

#rx_bytes_humanObject



76
77
78
# File 'lib/ifconf/interface_statistics.rb', line 76

def rx_bytes_human
  humanize_bytes(@rx_bytes)
end

#to_hObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ifconf/interface_statistics.rb', line 84

def to_h
  {
    rx_packets: @rx_packets, rx_bytes: @rx_bytes,
    rx_errors: @rx_errors, rx_dropped: @rx_dropped,
    rx_overruns: @rx_overruns, rx_frame: @rx_frame,
    tx_packets: @tx_packets, tx_bytes: @tx_bytes,
    tx_errors: @tx_errors, tx_dropped: @tx_dropped,
    tx_overruns: @tx_overruns, tx_carrier: @tx_carrier,
    collisions: @collisions
  }
end

#total_bytesObject



48
49
50
# File 'lib/ifconf/interface_statistics.rb', line 48

def total_bytes
  @rx_bytes + @tx_bytes
end

#total_droppedObject



56
57
58
# File 'lib/ifconf/interface_statistics.rb', line 56

def total_dropped
  @rx_dropped + @tx_dropped
end

#total_errorsObject



52
53
54
# File 'lib/ifconf/interface_statistics.rb', line 52

def total_errors
  @rx_errors + @tx_errors
end

#total_packetsObject



44
45
46
# File 'lib/ifconf/interface_statistics.rb', line 44

def total_packets
  @rx_packets + @tx_packets
end

#tx_bytes_humanObject



80
81
82
# File 'lib/ifconf/interface_statistics.rb', line 80

def tx_bytes_human
  humanize_bytes(@tx_bytes)
end