Class: ForemanStatistics::TrendImporter

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.should_create_counter?(latest_counter, new_count, _timestamp) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
# File 'app/services/foreman_statistics/trend_importer.rb', line 55

def self.should_create_counter?(latest_counter, new_count, _timestamp)
  return true if latest_counter.nil?

  latest_counter.count != new_count
end

.update!Object



3
4
5
6
7
8
# File 'app/services/foreman_statistics/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



61
# File 'app/services/foreman_statistics/trend_importer.rb', line 61

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/services/foreman_statistics/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
45
46
47
48
49
50
51
52
53
# File 'app/services/foreman_statistics/trend_importer.rb', line 24

def update_trend_counters
  timestamp = Time.now.utc
  counter_hash = {}
  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|
    new_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

    latest_counter = trend.trend_counters.order(:created_at).last
    if latest_counter
      latest_counter.interval_end = timestamp
      latest_counter.save!
    end

    next unless self.class.should_create_counter?(latest_counter, new_count, timestamp)

    trend.trend_counters.create!  :count => new_count,
                                  :created_at => timestamp,
                                  :interval_start => timestamp
  end
end