Module: ForemanStatistics::TrendsHelper

Includes:
CommonParametersHelper
Defined in:
app/helpers/foreman_statistics/trends_helper.rb

Instance Method Summary collapse

Instance Method Details

#trend_chart_data(trend, from = Setting[:max_trend]) ⇒ Object

Returns data in format:

[

[time, <time_int>, <time_int>],
[trend_val1, <host_count>, <host_count>],
[trend_val2, 5, 2],
[trend_valx, 213, 3]

]



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/helpers/foreman_statistics/trends_helper.rb', line 39

def trend_chart_data(trend, from = Setting[:max_trend])
  data = {}
  names = {}
  trend.values.preload(:trendable).each { |value| names[value.id] = CGI.escapeHTML(value.to_label) }
  trend.values.preload(:trend_counters).joins(:trend_counters)
       .where(['trend_counters.interval_end > ? or trend_counters.interval_end is null', from])
       .reorder('trend_counters.interval_start')
       .each do |value|
    value.trend_counters.each do |counter|
      current_data = data[counter.interval_start.to_i] ||= {}
      next_timestamp = counter.try(:interval_end) || Time.now.utc
      next_data = data[next_timestamp.to_i] ||= {}
      current_data[value.id] = next_data[value.id] = counter.count
    end
  end
  times = data.keys.sort
  result = names.map { |id, label| [label].concat(times.map { |time| data[time][id].to_f }) }
  result.unshift(['time'].concat(times))
end

#trend_days_filter(trend) ⇒ Object



14
15
16
17
18
19
20
# File 'app/helpers/foreman_statistics/trends_helper.rb', line 14

def trend_days_filter(trend)
  form_tag trend, :id => 'days_filter', :method => :get, :class => 'form form-inline' do
    (:span, (_('Trend of the last %s days.') %
                        select(nil, 'range', 1..Setting[:max_trend], { :selected => trends_range },
                          { :onchange => "$('#days_filter').submit();$(this).attr('disabled','disabled');;" })).html_safe)
  end
end

#trend_title(trend) ⇒ Object



22
23
24
25
26
27
28
# File 'app/helpers/foreman_statistics/trends_helper.rb', line 22

def trend_title(trend)
  if trend.fact_value.blank?
    trend.to_label
  else
    "#{trend.type_name} - #{trend.to_label}"
  end
end

#trendable_typesObject



5
6
7
8
9
10
11
12
# File 'app/helpers/foreman_statistics/trends_helper.rb', line 5

def trendable_types
  existing = ForemanTrend.types.pluck(:trendable_type)
  options = {}
  options = { _('Environment') => 'ForemanPuppet::Environment' } if Foreman::Plugin.find(:foreman_puppet)
  options.merge!({ _('Operating system') => 'Operatingsystem', _('Model') => 'Model', _('Facts') => 'FactName',
                   _('Host group') => 'Hostgroup', _('Compute resource') => 'ComputeResource' })
  options.delete_if { |_k, v| existing.include?(v) }
end


59
60
61
# File 'app/helpers/foreman_statistics/trends_helper.rb', line 59

def trends_range
  params['range'].empty? ? Setting[:max_trend] : params['range'].to_i
end