Class: MaintenanceTasks::TaskDataIndex Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class that represents the data related to a Task. Such information can be sourced from a Task or from existing Run records for a Task that was since deleted. This class contains higher-level information about the Task, such as its status and category.

Instances of this class replace a Task class instance in cases where we don’t need the actual Task subclass.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, related_run = nil) ⇒ TaskDataIndex

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes a Task Data with a name and optionally a related run.

Parameters:

  • name (String)

    the name of the Task subclass.

  • related_run (MaintenanceTasks::Run) (defaults to: nil)

    optionally, a Run record to set for the Task.



55
56
57
58
# File 'app/models/maintenance_tasks/task_data_index.rb', line 55

def initialize(name, related_run = nil)
  @name = name
  @related_run = related_run
end

Instance Attribute Details

#nameString (readonly) Also known as: to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the name of the Task.

Returns:

  • (String)

    the name of the Task.



61
62
63
# File 'app/models/maintenance_tasks/task_data_index.rb', line 61

def name
  @name
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



62
63
64
# File 'app/models/maintenance_tasks/task_data_index.rb', line 62

def related_run
  @related_run
end

Class Method Details

.available_tasksArray<TaskDataIndex>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a list of sorted Task Data objects that represent the available Tasks.

Tasks are sorted by category, and within a category, by Task name. Determining a Task’s category requires their latest Run records. Two queries are done to get the currently active and completed Run records, and Task Data instances are initialized with these related run values.

Returns:



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

def available_tasks
  tasks = []

  task_names = Task.load_all.map(&:name)

  active_runs = Run.with_attached_csv.active.where(task_name: task_names)
  active_runs.each do |run|
    tasks << TaskDataIndex.new(run.task_name, run)
    task_names.delete(run.task_name)
  end

  completed_runs = Run.completed.where(task_name: task_names)
  last_runs = Run.with_attached_csv.where(id: completed_runs.select("MAX(id) as id").group(:task_name))
  task_names.map do |task_name|
    last_run = last_runs.find { |run| run.task_name == task_name }
    tasks << TaskDataIndex.new(task_name, last_run)
  end

  # We add an additional sorting key (status) to avoid possible
  # inconsistencies across database adapters when a Task has
  # multiple active Runs.
  tasks.sort_by! { |task| [task.name, task.status] }
end

Instance Method Details

#categorySymbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Retrieves the Task’s category, which is one of active, new, or completed.

Returns:

  • (Symbol)

    the category of the Task.



77
78
79
80
81
82
83
84
85
# File 'app/models/maintenance_tasks/task_data_index.rb', line 77

def category
  if related_run.present? && related_run.active?
    :active
  elsif related_run.nil?
    :new
  else
    :completed
  end
end

#statusString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the status of the latest active or completed Run, if present. If the Task does not have any Runs, the Task status is ‘new`.

Returns:

  • (String)

    the Task status.



70
71
72
# File 'app/models/maintenance_tasks/task_data_index.rb', line 70

def status
  related_run&.status || "new"
end