Class: SeoBatchItem

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

Overview

Tracks one page's prompt and result within a SeoBatchJob.

The custom_id (e.g. "seo_page_123") is used to match Anthropic batch
results back to the corresponding SiteMap record.

Lifecycle:
pending -> submitted -> succeeded / errored / expired

== Schema Information

Table name: seo_batch_items
Database name: primary

id :bigint not null, primary key
error_message :text
result :jsonb
status :string default("pending"), not null
user_prompt :text
created_at :datetime not null
updated_at :datetime not null
custom_id :string not null
seo_batch_job_id :bigint not null
site_map_id :bigint not null

Indexes

index_seo_batch_items_on_custom_id (custom_id) UNIQUE
index_seo_batch_items_on_seo_batch_job_id_and_site_map_id (seo_batch_job_id,site_map_id) UNIQUE
index_seo_batch_items_on_site_map_id (site_map_id)
index_seo_batch_items_on_status (status)

Foreign Keys

fk_rails_... (seo_batch_job_id => seo_batch_jobs.id)
fk_rails_... (site_map_id => site_maps.id)

Constant Summary collapse

STATUSES =
%w[pending submitted succeeded errored expired].freeze

Instance Attribute Summary collapse

Belongs to 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::EventPublishable

#publish_event

Instance Attribute Details

#custom_idObject (readonly)



45
# File 'app/models/seo_batch_item.rb', line 45

validates :custom_id, presence: true, uniqueness: true

#statusObject (readonly)



46
# File 'app/models/seo_batch_item.rb', line 46

validates :status, inclusion: { in: STATUSES }

Class Method Details

.erroredActiveRecord::Relation<SeoBatchItem>

A relation of SeoBatchItems that are errored. Active Record Scope

Returns:

See Also:



51
# File 'app/models/seo_batch_item.rb', line 51

scope :errored,   -> { where(status: 'errored') }

.expiredActiveRecord::Relation<SeoBatchItem>

A relation of SeoBatchItems that are expired. Active Record Scope

Returns:

See Also:



52
# File 'app/models/seo_batch_item.rb', line 52

scope :expired,   -> { where(status: 'expired') }

.pendingActiveRecord::Relation<SeoBatchItem>

A relation of SeoBatchItems that are pending. Active Record Scope

Returns:

See Also:



48
# File 'app/models/seo_batch_item.rb', line 48

scope :pending,   -> { where(status: 'pending') }

.submittedActiveRecord::Relation<SeoBatchItem>

A relation of SeoBatchItems that are submitted. Active Record Scope

Returns:

See Also:



49
# File 'app/models/seo_batch_item.rb', line 49

scope :submitted, -> { where(status: 'submitted') }

.succeededActiveRecord::Relation<SeoBatchItem>

A relation of SeoBatchItems that are succeeded. Active Record Scope

Returns:

See Also:



50
# File 'app/models/seo_batch_item.rb', line 50

scope :succeeded, -> { where(status: 'succeeded') }

Instance Method Details

#apply_result!(analysis_json) ⇒ Object

Apply a successful analysis result to the SiteMap.



55
56
57
58
59
60
61
62
63
# File 'app/models/seo_batch_item.rb', line 55

def apply_result!(analysis_json)
  analysis = analysis_json.is_a?(String) ? JSON.parse(analysis_json) : analysis_json
  analysis['analyzed_at'] = Time.current.iso8601
  analysis['model'] = seo_batch_job.model
  analysis['batch_job_id'] = seo_batch_job_id

  site_map.update!(seo_report: analysis)
  update!(status: 'succeeded', result: analysis)
end

#mark_errored!(message) ⇒ Object



65
66
67
# File 'app/models/seo_batch_item.rb', line 65

def mark_errored!(message)
  update!(status: 'errored', error_message: message)
end

#mark_expired!Object



69
70
71
# File 'app/models/seo_batch_item.rb', line 69

def mark_expired!
  update!(status: 'expired')
end

#seo_batch_jobSeoBatchJob



42
# File 'app/models/seo_batch_item.rb', line 42

belongs_to :seo_batch_job

#site_mapSiteMap

Returns:

See Also:



43
# File 'app/models/seo_batch_item.rb', line 43

belongs_to :site_map