Class: ForemanTasks::Task::Summarizer

Inherits:
Object
  • Object
show all
Defined in:
app/models/foreman_tasks/task/summarizer.rb

Defined Under Namespace

Classes: Record, RecordWithResult

Constant Summary collapse

ENSURED_STATE_KEYS =
%w[running paused stopped scheduled].freeze
ENSURED_RESULT_KEYS =
%w[success error warning].freeze

Instance Method Summary collapse

Constructor Details

#initialize(scope = Task.authorized, recent_timeframe = 24) ⇒ Summarizer

Returns a new instance of Summarizer.



50
51
52
53
# File 'app/models/foreman_tasks/task/summarizer.rb', line 50

def initialize(scope = Task.authorized, recent_timeframe = 24)
  @recent_timeframe = recent_timeframe.hours
  @scope = scope
end

Instance Method Details

#latest_tasks_in_errors_warning(limit = 5) ⇒ Object



59
60
61
# File 'app/models/foreman_tasks/task/summarizer.rb', line 59

def latest_tasks_in_errors_warning(limit = 5)
  @scope.where('result in (?)', %w[error warning]).order('started_at DESC').limit(limit)
end

#summarize_by_statusObject



55
56
57
# File 'app/models/foreman_tasks/task/summarizer.rb', line 55

def summarize_by_status
  aggregated_scope.where("result <> 'success'")
end

#summaryObject

Returns summary of tasks count, grouped by ‘state` and `result`, if form of:

{ 'running'   => { recent: 3, total: 6 },
  'paused'    => { recent: 1, total: 3 },
  'stopped'   => { recent: 3, total: 7,
                   by_result: {
                     'success' => { recent: 2, total: 4 },
                     'warning' => { recent: 1, total: 2 },
                     'error'   => { recent: 0, total: 1 },
                   }}
  'scheduled' => { recent: 0, total: 3 }}


75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/foreman_tasks/task/summarizer.rb', line 75

def summary
  @summary = Hash.new { |h, state| h[state] = Record.new }
  ENSURED_STATE_KEYS.each do |state|
    @summary[state] = state == 'stopped' ? RecordWithResult.new : Record.new
  end

  add_to_summary(aggregated_scope, :total)
  add_to_summary(aggregated_recent_scope, :recent)

  @summary.each.with_object({}) do |(key, record), hash|
    hash[key] = record.to_h
  end
end