Class: PartyResearchRun
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- PartyResearchRun
- 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
-
#party ⇒ Party
Party the research run enriches.
-
#requested_by ⇒ Account
Account that clicked "Enrich".
Has many collapse
-
#findings ⇒ ActiveRecord::Relation<PartyResearchFinding>
Findings produced by this run's adapters.
Class Method Summary collapse
-
.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.
-
.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.
-
.pending ⇒ ActiveRecord::Relation<PartyResearchRun>
A relation of PartyResearchRuns that are pending.
Instance Method Summary collapse
-
#adapters_list ⇒ Array<String>
Adapter names as plain strings.
-
#complete! ⇒ void
Marks the run as completed.
-
#fail!(message) ⇒ void
Marks the run as failed with an error message.
-
#start! ⇒ void
Marks the run as running.
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
Methods included from Models::Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
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.
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.
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 |
.pending ⇒ ActiveRecord::Relation<PartyResearchRun>
A relation of PartyResearchRuns that are pending. Active Record Scope
19 |
# File 'app/models/party_research_run.rb', line 19 scope :pending, -> { where(state: %w[queued running]) } |
Instance Method Details
#adapters_list ⇒ Array<String>
Adapter names as plain strings.
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.
63 64 65 |
# File 'app/models/party_research_run.rb', line 63 def fail!() update!(state: 'failed', finished_at: Time.current, error_message: .to_s.truncate(2000)) end |
#findings ⇒ ActiveRecord::Relation<PartyResearchFinding>
Findings produced by this run's adapters.
14 |
# File 'app/models/party_research_run.rb', line 14 has_many :findings, class_name: 'PartyResearchFinding', dependent: :destroy |
#party ⇒ Party
Party the research run enriches.
10 |
# File 'app/models/party_research_run.rb', line 10 belongs_to :party |
#requested_by ⇒ Account
Account that clicked "Enrich".
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 |