Module: ReportCommon

Included in:
Host::Managed, Report
Defined in:
app/models/report_common.rb

Constant Summary collapse

METRIC =
%w[applied restarted failed failed_restarts skipped pending]
BIT_NUM =
6
MAX =

maximum value per metric

(1 << BIT_NUM) -1

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


6
7
8
9
10
11
12
13
14
15
# File 'app/models/report_common.rb', line 6

def self.included(base)
  base.class_eval do
    # search for a metric - e.g.:
    # Report.with("failed") --> all reports which have a failed counter > 0
    # Report.with("failed",20) --> all reports which have a failed counter > 20
    scope :with, lambda { |*arg| {
      :conditions => "(#{report_status} >> #{BIT_NUM*METRIC.index(arg[0])} & #{MAX}) > #{arg[1] || 0}"}
    }
  end
end

Instance Method Details

#changes?Boolean

returns true if total action metrics are > 0


31
32
33
# File 'app/models/report_common.rb', line 31

def changes?
  %w[applied restarted].sum {|f| status f} > 0
end

#error?Boolean

returns true if total error metrics are > 0


26
27
28
# File 'app/models/report_common.rb', line 26

def error?
  %w[failed failed_restarts].sum {|f| status f} > 0
end

#pending?Boolean

returns true if there are any changes pending


36
37
38
# File 'app/models/report_common.rb', line 36

def pending?
  pending > 0
end

#status(type = nil) ⇒ Object

returns metrics when no metric type is specific returns hash with all values passing a METRIC member will return its value


43
44
45
46
47
48
49
50
# File 'app/models/report_common.rb', line 43

def status(type = nil)
  raise(N_("invalid type %s") % type) if type and not METRIC.include?(type)
  h = {}
  (type.is_a?(String) ? [type] : METRIC).each do |m|
    h[m] = (read_attribute(self.class.report_status) || 0) >> (BIT_NUM*METRIC.index(m)) & MAX
  end
  type.nil? ? h : h[type]
end