class InsightsCloud::Async::InsightsFullSync

Public Instance Methods

logger() click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 17
def logger
  Foreman::Logging.logger('background')
end
perform() click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 8
def perform
  hits = query_insights_hits

  @hits_host_names = Hash[hits.map { |hit| [hit['hostname'], hit['uuid']] }]
  setup_host_names(@hits_host_names.keys)

  replace_hits_data(hits)
end

Private Instance Methods

host_id(host_name) click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 43
def host_id(host_name)
  @host_ids[host_name]
end
query_insights_hits() click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 23
def query_insights_hits
  hits_response = RestClient::Request.execute(
    method: :get,
    url: InsightsCloud.hits_export_url,
    verify_ssl: ForemanRhCloud.verify_ssl_method,
    proxy: ForemanRhCloud.transformed_http_proxy_string(logger: logger),
    headers: {
      Authorization: "Bearer #{rh_credentials}",
    }
  )

  JSON.parse(hits_response)
end
replace_hits_data(hits) click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 47
def replace_hits_data(hits)
  InsightsHit.transaction do
    # create new facets for hosts that are missing one
    hosts_with_existing_facets = InsightsFacet.where(host_id: @host_ids.values).pluck(:host_id)
    InsightsFacet.create(
      @host_ids.map do |host_name, host_id|
        unless hosts_with_existing_facets.include?(host_id)
          {
            host_id: host_id,
            uuid: @hits_host_names[host_name],
          }
        end
      end.compact
    )
    InsightsHit.delete_all
    InsightsHit.create(hits.map { |hits_hash| to_model_hash(hits_hash) }.compact)
  end
end
setup_host_names(host_names) click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 37
def setup_host_names(host_names)
  @host_ids = Hash[
    Host.unscoped.where(name: host_names).pluck(:name, :id)
  ]
end
to_model_hash(hit_hash) click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 66
def to_model_hash(hit_hash)
  hit_host_id = host_id(hit_hash['hostname'])

  return unless hit_host_id

  {
    host_id: hit_host_id,
    last_seen: DateTime.parse(hit_hash['last_seen']),
    publish_date: DateTime.parse(hit_hash['publish_date']),
    title: hit_hash['title'],
    solution_url: hit_hash['solution_url'],
    total_risk: hit_hash['total_risk'].to_i,
    likelihood: hit_hash['likelihood'].to_i,
    results_url: hit_hash['results_url'],
  }
end