class InsightsCloud::Async::InsightsFullSync
Public Instance Methods
logger()
click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 27 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 # This can be turned off when we enable automatic status syncs # This step will query cloud inventory to retrieve inventory uuids for each host ForemanTasks.sync_task(InventorySync::Async::InventoryHostsSync) perform_hits_sync InsightsRulesSync.perform_later end
perform_hits_sync()
click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 18 def perform_hits_sync hits = query_insights_hits uuids = hits.map { |hit| hit['uuid'] } setup_host_ids(uuids) replace_hits_data(hits) end
Private Instance Methods
host_id(uuid)
click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 67 def host_id(uuid) @host_ids[uuid] end
query_insights_hits()
click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 33 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
query_insights_rules()
click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 47 def query_insights_rules rules_response = RestClient::Request.execute( method: :get, url: InsightsCloud.rules_url, verify_ssl: ForemanRhCloud.verify_ssl_method, proxy: ForemanRhCloud.transformed_http_proxy_string(logger: logger), headers: { Authorization: "Bearer #{rh_credentials}", } ) JSON.parse(rules_response) end
replace_hits_data(hits)
click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 71 def replace_hits_data(hits) InsightsHit.transaction do # Reset hit counters to 0, they will be recreated later InsightsFacet.unscoped.update_all(hits_count: 0) InsightsHit.delete_all InsightsHit.create(hits.map { |hits_hash| to_model_hash(hits_hash) }.compact) end end
safe_results_match(results_url)
click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 102 def safe_results_match(results_url) match = results_url.match(/\/(?<id>[^\/]*)\/[^\/]*\/\z/) match || { id: nil } end
setup_host_ids(uuids)
click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 61 def setup_host_ids(uuids) @host_ids = Hash[ InsightsFacet.where(uuid: uuids).pluck(:uuid, :host_id) ] end
to_model_hash(hit_hash)
click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 80 def to_model_hash(hit_hash) hit_host_id = host_id(hit_hash['uuid']) 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'], rule_id: to_rule_id(hit_hash['results_url']), } end
to_rule_id(results_url)
click to toggle source
# File lib/insights_cloud/async/insights_full_sync.rb, line 98 def to_rule_id(results_url) URI.decode(safe_results_match(results_url)[:id] || '') end