Sha256: 8b86508a58d7f433cfeaad33b65911f5db63c78446b8e07947aba980f7b67cb4

Contents?: true

Size: 1.71 KB

Versions: 5

Compression:

Stored size: 1.71 KB

Contents

class TrendImporter

  def self.update!
    importer = new
    importer.check_values
    importer.update_trend_counters
    importer.aggregate_counters
  end

  # Check for missing values
  # Comparing a count prior to trying to recreating them all for efficiency sake
  def check_values
    ForemanTrend.types.pluck(:trendable_type).each do |trend_type|
      changes = trend_type.constantize.pluck(:id) - ForemanTrend.has_value.where(:trendable_type => trend_type).pluck(:trendable_id)
      ForemanTrend.create_values(trend_type) unless changes.empty?
    end

    FactTrend.types.pluck(:trendable_id).each do |fact_name_id|
      changes = FactValue.where(:fact_name_id => fact_name_id).group(:value).pluck(:value) - FactTrend.has_value.where(:trendable_id => fact_name_id).pluck(:fact_value)
      FactTrend.create_values(fact_name_id) unless changes.empty?
    end
  end

  def update_trend_counters
    timestamp = Time.now
    counter_hash = Hash.new
    Trend.types.each do |trend|
      if trend.is_a? FactTrend
        counter_hash[trend.trendable_id]   = Host.joins(:fact_values).where(:fact_values => {:fact_name_id => trend.trendable_id}).group(:value).count
      else
        counter_hash[trend.trendable_type] = Host.group(trend.trendable_type.foreign_key.to_sym).count
      end
    end
    Trend.has_value.each do |trend|
      counter = trend.trend_counters.new
      counter.count = if trend.is_a? FactTrend
                        counter_hash[trend.trendable_id][trend.fact_value]
                      else
                        counter_hash[trend.trendable_type][trend.trendable_id]
                      end || 0
      counter.created_at = timestamp
      counter.save!
      end
  end

  def aggregate_counters

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
foreman_discovery-1.0.0 test/foreman_app/app/models/trend_importer.rb
foreman_discovery-1.0.0.rc4 test/foreman_app/app/models/trend_importer.rb
foreman_discovery-1.0.0.rc3 test/foreman_app/app/models/trend_importer.rb
foreman_discovery-1.0.0.rc2 test/foreman_app/app/models/trend_importer.rb
foreman_discovery-1.0.0.rc1 test/foreman_app/app/models/trend_importer.rb