class PuppetHerald::Models::Report

A model for Report

Public Class Methods

create_from_yaml(yaml) click to toggle source

Creates a report from given YAML report file @param yaml [String] a puppet report YAML as string @return [Report] created report

# File lib/puppet-herald/models/report.rb, line 26
def create_from_yaml(yaml)
  parsed = parse_yaml yaml
  report = nil
  transaction do
    report = Report.new

    parse_properties parsed, report
    parse_logs parsed, report
    node = parse_node parsed, report

    report.save
    node.save
  end
  report
end
purge_older_then(date) click to toggle source

Purges older reports then given date

@param date [DateTime] a date that will be border to @return [Integer] number of

# File lib/puppet-herald/models/report.rb, line 46
def purge_older_then(date)
  deleted = 0
  query = ['"reports"."time" < ?', date]
  return 0 if where(query).count == 0
  transaction do
    idss = joins(:log_entries).where(query).collect(&:id).uniq
    PuppetHerald::Models::LogEntry.where(['"report_id" IN (?)', idss]).delete_all unless idss.empty?
    where(['"id" IN (?)', idss]).delete_all unless idss.empty?
    PuppetHerald::Models::Node.delete_empty
    deleted = idss.length
  end
  deleted
end
with_log_entries(id) click to toggle source

Gets a report with prefetched log entries @param id [Integer] a in of report to get @return [Report, nil] fetched report or nil

# File lib/puppet-herald/models/report.rb, line 19
def with_log_entries(id)
  Report.joins(:log_entries).includes(:log_entries).find_by_id(id)
end

Private Class Methods

copy_attrs(from, to, attrs) click to toggle source
# File lib/puppet-herald/models/report.rb, line 100
def copy_attrs(from, to, attrs)
  attrs.each do |at|
    value = from.send at
    to.send "#{at}=", value
  end
end
parse_logs(parsed, report) click to toggle source
# File lib/puppet-herald/models/report.rb, line 80
def parse_logs(parsed, report)
  parsed.logs.each do |in_log|
    log = LogEntry.new
    attr_to_copy = %w(level message source time)
    copy_attrs in_log, log, attr_to_copy
    if log.message.include?('(noop)') && report.status != 'failed'
      report.status = 'pending'
      parsed.status = 'pending'
    end
    log.report = report
    report.log_entries << log
  end
end
parse_node(parsed, report) click to toggle source
# File lib/puppet-herald/models/report.rb, line 62
def parse_node(parsed, report)
  node = Node.find_by_name(parsed.host)
  if node.nil?
    node = Node.new
    node.name = parsed.host
  end
  report.node = node
  node.reports << report
  node.status = parsed.status
  node.last_run = parsed.time
  node
end
parse_properties(parsed, report) click to toggle source
# File lib/puppet-herald/models/report.rb, line 75
def parse_properties(parsed, report)
  attr_to_copy = %w(status environment transaction_uuid time puppet_version kind host configuration_version)
  copy_attrs parsed, report, attr_to_copy
end
parse_yaml(yaml) click to toggle source
# File lib/puppet-herald/models/report.rb, line 94
def parse_yaml(yaml)
  require 'yaml'
  raw = YAML.parse yaml
  raw.to_ruby
end