Class: TrendImporter

Inherits:
Object
  • Object
show all
Defined in:
app/models/trend_importer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.update!Object


3
4
5
6
7
8
# File 'app/models/trend_importer.rb', line 3

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

Instance Method Details

#aggregate_countersObject


46
47
48
# File 'app/models/trend_importer.rb', line 46

def aggregate_counters

end

#check_valuesObject

Check for missing values Comparing a count prior to trying to recreating them all for efficiency sake


12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/trend_importer.rb', line 12

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

#update_trend_countersObject


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/trend_importer.rb', line 24

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