Class: Nosey::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/nosey/report.rb

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Report

Returns a new instance of Report.

Yields:

  • (_self)

Yield Parameters:

  • _self (Nosey::Report)

    the object that the method was called on



5
6
7
8
# File 'lib/nosey/report.rb', line 5

def initialize
  yield self if block_given?
  self
end

Instance Method Details

#probe_setsObject

Grab some probe_sets or an array



16
17
18
# File 'lib/nosey/report.rb', line 16

def probe_sets
  @probe_sets ||= Array.new
end

#probe_sets=(probe_sets) ⇒ Object

Make sure we end up with a flat array of probe_sets



11
12
13
# File 'lib/nosey/report.rb', line 11

def probe_sets=(probe_sets)
  @probe_sets = Array(probe_sets).flatten
end

#resetObject

Reset all the counters in each probe.



36
37
38
# File 'lib/nosey/report.rb', line 36

def reset
  probe_sets.each(&:reset)
end

#to_hashObject

Hash representation of all the probe_sets. This gives is an intermediate format that we can parse from other systems or code that needs reporting data for formatting, or whatever.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/nosey/report.rb', line 23

def to_hash
  # Drop the probes into the report
  probe_sets.inject({}) { |report, set|
    report[set.name.to_s] = set.probes.inject({}) { |memo, (_, probe)|
      # round the values to 2 decimal places
      memo[probe.name] = (probe.value.to_f * 100).round().to_f/100
      memo
    }
    report
  }
end

#to_sObject

String representation of all the probe_sets that’s suitable for flushing out over a socket.



42
43
44
# File 'lib/nosey/report.rb', line 42

def to_s
  Psych.dump to_hash
end