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 =

Valid run lifecycle states.

%w[queued running completed failed].freeze

Constants included from Models::Schedulable

Models::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 Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.most_recent(party) ⇒ PartyResearchRun?

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.

Parameters:

  • party (Party)

    party to look up runs for

Returns:



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

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

.most_recent_with_adapter(party, adapter) ⇒ PartyResearchRun?

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

Parameters:

  • party (Party)

    party to look up runs for

  • adapter (String)

    adapter name (matched against the adapters JSONB array)

Returns:



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

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:



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

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

Instance Method Details

#adapters_listArray<String>

Adapter names as plain strings.

Returns:

  • (Array<String>)


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

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

#complete!void

This method returns an undefined value.

Marks the run as completed.



56
57
58
# File 'app/models/party_research_run.rb', line 56

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

#fail!(message) ⇒ void

This method returns an undefined value.

Marks the run as failed with an error message.

Parameters:

  • message (Exception, String)

    failure reason (truncated to 2000 chars)



63
64
65
# File 'app/models/party_research_run.rb', line 63

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

#findingsActiveRecord::Relation<PartyResearchFinding>

Findings produced by this run's adapters.

Returns:

See Also:



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

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

#partyParty

Party the research run enriches.

Returns:

See Also:



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

belongs_to :party

#requested_byAccount

Account that clicked "Enrich".

Returns:

See Also:



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

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

#start!void

This method returns an undefined value.

Marks the run as running.



50
51
52
# File 'app/models/party_research_run.rb', line 50

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