Revision e54016da
Added by Marek Hulán over 8 years ago
app/services/report_status_calculator.rb | ||
---|---|---|
@raw_status = options[:bit_field] || 0
|
||
end
|
||
|
||
# calculates the raw_status based on counters
|
||
def calculate
|
||
@raw_status = 0
|
||
counters.each do |type, value|
|
||
... | ... | |
raw_status
|
||
end
|
||
|
||
#returns metrics
|
||
#when no metric type is specific returns hash with all values
|
||
#passing a METRIC member will return its value
|
||
def status(type = nil)
|
||
calculate if raw_status == 0
|
||
raise(Foreman::Exception(N_("invalid type %s") % type)) if type && !Report::METRIC.include?(type)
|
||
counters = Hash.new(0)
|
||
(type.is_a?(String) ? [type] : Report::METRIC).each do |m|
|
||
counters[m] = (raw_status || 0) >> (Report::BIT_NUM * Report::METRIC.index(m)) & Report::MAX
|
||
# returns metrics (counters) based on raw_status (aka bit field)
|
||
# to get status of specific metric, @see #status_of
|
||
def status
|
||
@status ||= begin
|
||
calculate if raw_status == 0
|
||
counters = Hash.new(0)
|
||
Report::METRIC.each do |m|
|
||
counters[m] = (raw_status || 0) >> (Report::BIT_NUM * Report::METRIC.index(m)) & Report::MAX
|
||
end
|
||
counters
|
||
end
|
||
end
|
||
|
||
def status_of(counter)
|
||
raise(Foreman::Exception.new(N_("invalid type %s"), counter)) unless Report::METRIC.include?(counter)
|
||
status[counter]
|
||
end
|
||
|
||
# returns true if total error metrics are > 0
|
||
def error?
|
||
status_of('failed') + status_of('failed_restarts') > 0
|
||
end
|
||
|
||
# returns true if total action metrics are > 0
|
||
def changes?
|
||
status_of('applied') + status_of('restarted') > 0
|
||
end
|
||
|
||
# returns true if there are any changes pending
|
||
def pending?
|
||
status_of('pending') > 0
|
||
end
|
||
|
||
# generate dynamically methods for all metrics
|
||
# e.g. applied failed ...
|
||
Report::METRIC.each do |method|
|
||
define_method method do
|
||
status_of(method)
|
||
end
|
||
type.nil? ? counters : counters[type]
|
||
end
|
||
|
||
private
|
||
|
||
attr_reader :raw_status, :counters
|
||
end
|
||
end
|
Also available in: Unified diff
Fixes #10782 - global host status
Introduce new global host status that is composed of host substatuses.Each substatus defines a mapping to the global one which can result in
three values
Plugins can add their own substatuses. These are automatically
propagated also to API.
Thanks to Tomas Strachota who wrote the original code.