Class: Spotlight::ReindexProgress

Inherits:
Object
  • Object
show all
Defined in:
app/models/spotlight/reindex_progress.rb

Overview

ReindexProgress is a class that models the progress of reindexing a list of resources

Instance Method Summary collapse

Constructor Details

#initialize(resource_list) ⇒ ReindexProgress

Returns a new instance of ReindexProgress.



5
6
7
8
9
10
11
# File 'app/models/spotlight/reindex_progress.rb', line 5

def initialize(resource_list)
  @resources = if resource_list.present?
                 resource_list.order('updated_at')
               else
                 Spotlight::Resource.none
               end
end

Instance Method Details

#as_jsonObject



48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/spotlight/reindex_progress.rb', line 48

def as_json(*)
  {
    recently_in_progress: recently_in_progress?,
    started_at: localized_start_time,
    finished_at: localized_finish_time,
    updated_at: localized_updated_time,
    total: total,
    completed: completed,
    errored: errored?
  }
end

#completedObject



40
41
42
# File 'app/models/spotlight/reindex_progress.rb', line 40

def completed
  @completed ||= completed_resources.map(&:last_indexed_count).sum
end

#errored?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/models/spotlight/reindex_progress.rb', line 44

def errored?
  resources.any?(&:errored?)
end

#finished?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'app/models/spotlight/reindex_progress.rb', line 27

def finished?
  completed_resources.present? && !any_waiting?
end

#finished_atObject



31
32
33
34
# File 'app/models/spotlight/reindex_progress.rb', line 31

def finished_at
  return unless finished?
  @finished ||= completed_resources.max_by(&:last_indexed_finished).last_indexed_finished
end

#recently_in_progress?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'app/models/spotlight/reindex_progress.rb', line 13

def recently_in_progress?
  any_waiting? || (!!finished_at && finished_at > Spotlight::Engine.config.reindex_progress_window.minutes.ago)
end

#started_atObject



17
18
19
20
21
# File 'app/models/spotlight/reindex_progress.rb', line 17

def started_at
  return unless resources.present?

  @started ||= resources.min_by(&:enqueued_at).enqueued_at
end

#totalObject



36
37
38
# File 'app/models/spotlight/reindex_progress.rb', line 36

def total
  @total ||= resources.map(&:last_indexed_estimate).sum
end

#updated_atObject



23
24
25
# File 'app/models/spotlight/reindex_progress.rb', line 23

def updated_at
  @updated ||= resources.maximum(:updated_at) || started_at
end