class ForemanMaintain::Report

Attributes

data[RW]

Public Instance Methods

__run__(execution) click to toggle source

internal method called by executor

Calls superclass method ForemanMaintain::Executable#__run__
# File lib/foreman_maintain/report.rb, line 67
def __run__(execution)
  super
rescue Error::Fail => e
  set_fail(e.message)
rescue StandardError => e
  set_warn(e.message)
end
data_field(field_name) { || ... } click to toggle source
# File lib/foreman_maintain/report.rb, line 46
def data_field(field_name)
  self.data ||= {}
  value = yield
  self.data[field_name] = value
rescue StandardError
  nil
end
flatten(hash, prefix = '') click to toggle source
# File lib/foreman_maintain/report.rb, line 31
def flatten(hash, prefix = '')
  hash.each_with_object({}) do |(key, value), result|
    new_key = "#{prefix}#{prefix.empty? ? '' : flatten_separator}#{key}"
    if value.is_a? Hash
      result.merge!(flatten(value, new_key))
    else
      result[new_key] = value
    end
  end
end
flatten_separator() click to toggle source
# File lib/foreman_maintain/report.rb, line 75
def flatten_separator
  '|'
end
merge_data(prefix) { || ... } click to toggle source
# File lib/foreman_maintain/report.rb, line 54
def merge_data(prefix)
  self.data ||= {}
  data_hash = yield
  self.data.merge!(flatten(data_hash, prefix))
rescue StandardError
  nil
end
query(sql) click to toggle source
# File lib/foreman_maintain/report.rb, line 10
def query(sql)
  feature(:foreman_database).query(sql)
end
run() click to toggle source
# File lib/foreman_maintain/report.rb, line 62
def run
  raise NoMethodError, 'method not implemented on abstract report classes'
end
sql_as_count(selection, sql, cte: '') click to toggle source
# File lib/foreman_maintain/report.rb, line 18
def sql_as_count(selection, sql, cte: '')
  query = "#{cte} SELECT #{selection} AS COUNT FROM #{sql}"
  feature(:foreman_database).query(query).first['count'].to_i
rescue StandardError
  nil
end
sql_count(sql, column: '*', cte: '') click to toggle source
# File lib/foreman_maintain/report.rb, line 14
def sql_count(sql, column: '*', cte: '')
  sql_as_count("COUNT(#{column})", sql, cte: cte)
end
sql_setting(name) click to toggle source
# File lib/foreman_maintain/report.rb, line 25
def sql_setting(name)
  sql = "SELECT value FROM settings WHERE name = '#{name}'"
  result = feature(:foreman_database).query(sql).first
  (result || {})['value']
end
table_exists(table) click to toggle source
# File lib/foreman_maintain/report.rb, line 42
def table_exists(table)
  sql_count("information_schema.tables WHERE table_name = '#{table}'").positive?
end