Class: ServiceStatus::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/service_status/status.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version, boot_time) ⇒ Status

Returns a new instance of Status.



13
14
15
16
17
18
19
20
21
22
# File 'lib/service_status/status.rb', line 13

def initialize(name, version, boot_time)
  @boot_time = boot_time
  @name = name
  @version = version
  @hostname = Socket.gethostname
  @checks = []
  @timestamp = Time.now.strftime('%Y-%m-%d %T')
  @stats = []
  @errors = []
end

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



11
12
13
# File 'lib/service_status/status.rb', line 11

def checks
  @checks
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



11
12
13
# File 'lib/service_status/status.rb', line 11

def hostname
  @hostname
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/service_status/status.rb', line 11

def name
  @name
end

#statsObject (readonly)

Returns the value of attribute stats.



11
12
13
# File 'lib/service_status/status.rb', line 11

def stats
  @stats
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



11
12
13
# File 'lib/service_status/status.rb', line 11

def timestamp
  @timestamp
end

#versionObject (readonly)

Returns the value of attribute version.



11
12
13
# File 'lib/service_status/status.rb', line 11

def version
  @version
end

Instance Method Details

#add_check(name, successful, description = nil) ⇒ Object



24
25
26
27
28
29
# File 'lib/service_status/status.rb', line 24

def add_check(name, successful, description = nil)
  check = { name: name, successful: successful }
  check[:description] = description if description
  @checks << check
  @errors << name unless successful
end

#add_http_get_check(name, url) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/service_status/status.rb', line 31

def add_http_get_check(name, url)
  successful = true
  uri = URI(url)
  begin
    res = Net::HTTP.get_response(uri)
    successful = res.is_a?(Net::HTTPSuccess)
  rescue
    successful = false
  end
  add_check(name, successful)
end

#add_stat(name, value, description = nil) ⇒ Object



43
44
45
46
47
# File 'lib/service_status/status.rb', line 43

def add_stat(name, value, description = nil)
  stat = { name: name, value: value }
  stat[:description] = description if description
  @stats << stat
end

#disk_usageObject



66
67
68
# File 'lib/service_status/status.rb', line 66

def disk_usage
  format('%01d%%', (disk_used / disk_size) * 100)
end

#online?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/service_status/status.rb', line 53

def online?
  @errors.empty?
end

#statusObject



49
50
51
# File 'lib/service_status/status.rb', line 49

def status
  online? ? 'online' : 'offline'
end

#to_json(*a) ⇒ Object

rubocop:disable MethodLength



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/service_status/status.rb', line 71

def to_json(*a)
  {
    name: name,
    version: version,
    hostname: hostname,
    stats: stats,
    checks: checks,
    timestamp: timestamp,
    uptime: uptime,
    diskusage: disk_usage,
    status: status
  }.to_json(*a)
end

#uptimeObject



57
58
59
60
61
62
63
64
# File 'lib/service_status/status.rb', line 57

def uptime
  total_seconds = Time.now - @boot_time
  seconds = total_seconds % 60
  minutes = (total_seconds / 60) % 60
  hours = total_seconds / (60 * 60)
  days = total_seconds / (60 * 60 * 24)
  format('%01dd:%02d:%02d:%02d', days, hours, minutes, seconds)
end