Class: Railswatch::Widgets::CPULoad

Inherits:
ResourceChart show all
Defined in:
lib/railswatch/widgets/resource_chart.rb

Instance Attribute Summary

Attributes inherited from ResourceChart

#key, #server, #type

Attributes inherited from Chart

#description, #legend, #subtitle, #units

Attributes inherited from Base

#datasource

Instance Method Summary collapse

Methods inherited from ResourceChart

#data, #id, #signal

Methods inherited from Chart

#data, #id, #to_partial_path, #type

Methods inherited from Base

#to_partial_path

Constructor Details

#initialize(server) ⇒ CPULoad

Returns a new instance of CPULoad.



34
35
36
37
38
39
40
41
42
43
# File 'lib/railswatch/widgets/resource_chart.rb', line 34

def initialize(server)
  super(
    server,
    key: :cpu,
    type: 'Percentage',
    subtitle: 'CPU',
    description: 'CPU load average (1 min), average per 1 minute',
    legend: 'CPU',
  )
end

Instance Method Details

#format(measurement) ⇒ Object



45
46
47
# File 'lib/railswatch/widgets/resource_chart.rb', line 45

def format(measurement)
  measurement['one_min'].to_f.round(2)
end

#measureObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/railswatch/widgets/resource_chart.rb', line 49

def measure
  load_averages = Sys::CPU.load_avg
  {
    one_min: load_averages[0],
    five_min: load_averages[1],
    fifteen_min: load_averages[2]
  }
rescue StandardError => e
  ::Rails.logger.error "Error fetching CPU usage: #{e.message}"
  { one_min: 0.0, five_min: 0.0, fifteen_min: 0.0 }
end