Class: Pimon::Probe::Uptime

Inherits:
Object
  • Object
show all
Defined in:
lib/pimon/probe/uptime.rb

Class Method Summary collapse

Class Method Details

.check(date = Time.now) ⇒ Object



6
7
8
# File 'lib/pimon/probe/uptime.rb', line 6

def self.check(date = Time.now)
  OpenStruct.new(date: date.strftime('%Y-%m-%d %H:%M:%S'), probe_name: 'uptime', value: formatted_uptime)
end

.formatted_uptimeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pimon/probe/uptime.rb', line 10

def self.formatted_uptime
  days, hours, minutes, seconds = Sys::Uptime.dhms.map(&:to_i)

  uptime = ''
  { day: days, hour: hours, minute: minutes, second: seconds }.each do |time_unit, value|
    next if value == 0
    case time_unit
    when :day
      uptime = "#{days}#{pluralize(value, time_unit)}"
    when :second
      uptime = "#{insert_word_connector(uptime, ' and ')}#{value}#{pluralize(value, time_unit)}"
    when :hour, :minute
      uptime = "#{insert_word_connector(uptime, ', ')}#{value}#{pluralize(value, time_unit)}"
    end
  end
  uptime
end

.insert_word_connector(text, connector) ⇒ Object



32
33
34
# File 'lib/pimon/probe/uptime.rb', line 32

def self.insert_word_connector(text, connector)
  "#{text}#{text == '' ? '' : connector}"
end

.pluralize(count, word) ⇒ Object



28
29
30
# File 'lib/pimon/probe/uptime.rb', line 28

def self.pluralize(count, word)
  " #{word}#{count == 1 ? '' : 's'}"
end

.unitObject



36
37
38
# File 'lib/pimon/probe/uptime.rb', line 36

def self.unit
  nil
end