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 =

Statuses.

%w[pending submitted succeeded errored expired].freeze

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

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

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#custom_idObject (readonly)

custom_id identifies a page's item within ONE batch (results lookups are
job-scoped), so uniqueness is per job — a global rule made every page that
ever appeared in a batch uncollectable by later batches.

Validations:



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

validates :custom_id, presence: true, uniqueness: { scope: :seo_batch_job_id }

#statusString

Returns:

  • (String)


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

validates :status, inclusion: { in: STATUSES }

Class Method Details

.erroredActiveRecord::Relation<SeoBatchItem>

A relation of SeoBatchItems that are errored. Active Record Scope

Returns:

See Also:



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

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

.expiredActiveRecord::Relation<SeoBatchItem>

A relation of SeoBatchItems that are expired. Active Record Scope

Returns:

See Also:



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

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

.pendingActiveRecord::Relation<SeoBatchItem>

A relation of SeoBatchItems that are pending. Active Record Scope

Returns:

See Also:



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

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

.submittedActiveRecord::Relation<SeoBatchItem>

A relation of SeoBatchItems that are submitted. Active Record Scope

Returns:

See Also:



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

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

.succeededActiveRecord::Relation<SeoBatchItem>

A relation of SeoBatchItems that are succeeded. Active Record Scope

Returns:

See Also:



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

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

Instance Method Details

#apply_result!(analysis_json) ⇒ void

This method returns an undefined value.

Apply a successful analysis result to the SiteMap.

Parameters:

  • analysis_json (String, Hash)


64
65
66
67
68
69
70
71
72
# File 'app/models/seo_batch_item.rb', line 64

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) ⇒ void

This method returns an undefined value.

Parameters:

  • message (String)


76
77
78
# File 'app/models/seo_batch_item.rb', line 76

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

#mark_expired!void

This method returns an undefined value.



81
82
83
# File 'app/models/seo_batch_item.rb', line 81

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

#seo_batch_jobSeoBatchJob

Returns:



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

belongs_to :seo_batch_job

#site_mapSiteMap

Returns:



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

belongs_to :site_map