Class: MaintenanceTasks::TaskData Private

Inherits:
Object
  • Object
show all
Defined in:
app/models/maintenance_tasks/task_data.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.

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

API:

  • private

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, last_run = :none_passed) ⇒ TaskData

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 last_run.

Parameters:

  • the name of the Task subclass.

  • (defaults to: :none_passed)

    optionally, a Run record to set for the Task.

API:

  • private



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

def initialize(name, last_run = :none_passed)
  @name = name
  @last_run = last_run unless last_run == :none_passed
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:

  • the name of the Task.

API:

  • private



66
67
68
# File 'app/models/maintenance_tasks/task_data.rb', line 66

def name
  @name
end

Class Method Details

.available_tasksArray<TaskData>

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 require its latest Run record. To optimize calls to the database, a single query is done to get the last Run for each Task, and Task Data instances are initialized with these last_run values.

Returns:

  • the list of Task Data.

API:

  • private



41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/maintenance_tasks/task_data.rb', line 41

def available_tasks
  task_names = Task.available_tasks.map(&:name)
  available_task_runs = Run.where(task_name: task_names)
  last_runs = Run.with_attached_csv.where(
    id: available_task_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 }
    TaskData.new(task_name, last_run)
  end.sort_by!(&:name)
end

.find(name) ⇒ TaskData

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 by name, raising if the Task does not exist.

For the purpose of this method, a Task does not exist if it’s deleted and doesn’t have a Run. While technically, it could have existed and been deleted since, if it never had a Run we may as well consider it non-existent since we don’t have interesting data to show.

Parameters:

  • the name of the Task subclass.

Returns:

  • a Task Data instance.

Raises:

  • if the Task does not exist and doesn’t have a Run.

API:

  • private



25
26
27
28
29
# File 'app/models/maintenance_tasks/task_data.rb', line 25

def find(name)
  task_data = new(name)
  task_data.last_run || Task.named(name)
  task_data
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:

  • the category of the Task.

API:

  • private



128
129
130
131
132
133
134
135
136
# File 'app/models/maintenance_tasks/task_data.rb', line 128

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

#codeString?

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.

The Task’s source code.

Returns:

  • the contents of the file which defines the Task.

  • if the Task file was deleted.

API:

  • private



74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/maintenance_tasks/task_data.rb', line 74

def code
  return if deleted?

  task = Task.named(name)
  file = if Object.respond_to?(:const_source_location)
    Object.const_source_location(task.name).first
  else
    task.instance_method(:process).source_location.first
  end
  File.read(file)
end

#csv_task?Boolean

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 whether the Task inherits from CsvTask.

Returns:

  • whether the Task inherits from CsvTask.

API:

  • private



139
140
141
# File 'app/models/maintenance_tasks/task_data.rb', line 139

def csv_task?
  !deleted? && Task.named(name).has_csv_content?
end

#deleted?Boolean

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 whether the Task has been deleted.

Returns:

  • whether the Task has been deleted.

API:

  • private



110
111
112
113
114
115
# File 'app/models/maintenance_tasks/task_data.rb', line 110

def deleted?
  Task.named(name)
  false
rescue Task::NotFoundError
  true
end

#last_runMaintenanceTasks::Run?

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 latest Run associated with the Task.

Returns:

  • the Run record.

  • if there are no Runs associated with the Task.

API:

  • private



90
91
92
93
94
# File 'app/models/maintenance_tasks/task_data.rb', line 90

def last_run
  return @last_run if defined?(@last_run)

  @last_run = runs.first
end

#newMaintenanceTasks::Task?

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:

  • an instance of the Task class.

  • if the Task file was deleted.

API:

  • private



154
155
156
157
158
# File 'app/models/maintenance_tasks/task_data.rb', line 154

def new
  return if deleted?

  MaintenanceTasks::Task.named(name).new
end

#parameter_namesArray<String>

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 names of parameters the Task accepts.

Returns:

  • the names of parameters the Task accepts.

API:

  • private



144
145
146
147
148
149
150
# File 'app/models/maintenance_tasks/task_data.rb', line 144

def parameter_names
  if deleted?
    []
  else
    Task.named(name).attribute_names
  end
end

#previous_runsActiveRecord::Relation<MaintenanceTasks::Run>

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 set of Run records associated with the Task previous to the last Run. This collection represents a historic of past Runs for information purposes, since the base for Task Data information comes primarily from the last Run.

Returns:

  • the relation of record previous to the last Run.

API:

  • private



103
104
105
106
107
# File 'app/models/maintenance_tasks/task_data.rb', line 103

def previous_runs
  return Run.none unless last_run

  runs.where.not(id: last_run.id)
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.

The Task status. It returns the status of the last Run, if present. If the Task does not have any Runs, the Task status is new.

Returns:

  • the Task status.

API:

  • private



121
122
123
# File 'app/models/maintenance_tasks/task_data.rb', line 121

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