Module: ForemanStatistics::TrendsHelper

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

Instance Method Summary collapse

Instance Method Details

#chart_data(trend, from = Setting[:max_trend], _to = Time.now.utc) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/helpers/foreman_statistics/trends_helper.rb', line 28

def chart_data(trend, from = Setting[:max_trend], _to = Time.now.utc)
  chart_colors = ['#4572A7', '#AA4643', '#89A54E', '#80699B', '#3D96AE', '#DB843D', '#92A8CD', '#A47D7C', '#B5CA92']
  values = trend.values
  labels = {}
  values.includes(:trendable).each { |v| labels[v.id] = [CGI.escapeHTML(v.to_label), trend_path(:id => v)] }
  values.includes(:trend_counters).where(['trend_counters.interval_end > ? or trend_counters.interval_end is null', from])
        .reorder('trend_counters.interval_start')
        .each_with_index.map do |value, idx|
    data = []
    value.trend_counters.each do |counter|
      # cut the left side of the graph
      interval_start = (counter.interval_start || from) > from ? counter.interval_start : from
      next_timestamp = counter.try(:interval_end) || Time.now.utc
      # transform the timestamp values to flot format - from seconds in Ruby to milliseconds in flot
      data << [interval_start.to_i * 1000, counter.count]
      data << [next_timestamp.to_i * 1000 - 1, counter.count]
    end
    { :label => labels[value.id][0], :href => labels[value.id][1], :data => data, :color => chart_colors[idx % chart_colors.size] } unless data.empty?
  end.compact
end

#rangeObject



49
50
51
# File 'app/helpers/foreman_statistics/trends_helper.rb', line 49

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

#trend_days_filter(trend) ⇒ Object



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

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 => range },
                          { :onchange => "$('#days_filter').submit();$(this).attr('disabled','disabled');;" })).html_safe)
  end
end

#trend_title(trend) ⇒ Object



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

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
# File 'app/helpers/foreman_statistics/trends_helper.rb', line 5

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