Class: PartyResearchRun

Inherits:
ApplicationRecord show all
Defined in:
app/models/party_research_run.rb

Overview

One row per "Enrich" click on a Party. Tracks which adapters were
selected, the Sidekiq job running them, and run-level state for UI
progress and cost accounting. Findings produced by adapters live on
PartyResearchFinding and persist independently — a run is just the
container for one batch of findings.

Constant Summary collapse

STATES =
%w[queued running completed failed].freeze

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

Belongs to collapse

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.most_recent(party) ⇒ Object

Latest run for this party regardless of state — used by the
duplicates/lead-qualify tab to surface "your auto-enrichment is
ready" to the sales manager.



29
30
31
# File 'app/models/party_research_run.rb', line 29

def self.most_recent(party)
  where(party: party).order(created_at: :desc).first
end

.most_recent_with_adapter(party, adapter) ⇒ Object

Most recent successful run that included the given adapter; nil if the
adapter has never been run on this party.



19
20
21
22
23
24
# File 'app/models/party_research_run.rb', line 19

def self.most_recent_with_adapter(party, adapter)
  where(party: party, state: 'completed')
    .where('adapters @> ?', [adapter].to_json)
    .order(finished_at: :desc)
    .first
end

.pendingActiveRecord::Relation<PartyResearchRun>

A relation of PartyResearchRuns that are pending. Active Record Scope

Returns:

See Also:



15
# File 'app/models/party_research_run.rb', line 15

scope :pending, -> { where(state: %w[queued running]) }

Instance Method Details

#adapters_listObject



33
34
35
# File 'app/models/party_research_run.rb', line 33

def adapters_list
  Array(adapters).map(&:to_s)
end

#complete!Object



41
42
43
# File 'app/models/party_research_run.rb', line 41

def complete!
  update!(state: 'completed', finished_at: Time.current)
end

#fail!(message) ⇒ Object



45
46
47
# File 'app/models/party_research_run.rb', line 45

def fail!(message)
  update!(state: 'failed', finished_at: Time.current, error_message: message.to_s.truncate(2000))
end

#findingsActiveRecord::Relation<PartyResearchFinding>

Returns:

See Also:



11
# File 'app/models/party_research_run.rb', line 11

has_many :findings, class_name: 'PartyResearchFinding', dependent: :destroy

#partyParty

Returns:

See Also:



9
# File 'app/models/party_research_run.rb', line 9

belongs_to :party

#requested_byAccount

Returns:

See Also:



10
# File 'app/models/party_research_run.rb', line 10

belongs_to :requested_by, class_name: 'Account', optional: true

#start!Object



37
38
39
# File 'app/models/party_research_run.rb', line 37

def start!
  update!(state: 'running', started_at: Time.current)
end