Class: Seo::RecommendationExtractorService
- Inherits:
-
BaseService
- Object
- BaseService
- Seo::RecommendationExtractorService
- Defined in:
- app/services/seo/recommendation_extractor_service.rb
Overview
Extracts individual recommendations from a SiteMap's seo_report JSONB
into SiteMapRecommendation records with smart merge on re-analysis.
Smart merge rules:
- Completed/ignored recommendations with matching fingerprint are preserved
- Pending/accepted/in_progress recommendations get updated details
- New fingerprints create pending recommendations
- Old fingerprints not in the new analysis are marked stale
Constant Summary collapse
- CATEGORY_MAPPINGS =
{ 'priority_actions' => { category: 'priority_action', title_key: 'action', impact_key: 'impact', effort_key: 'effort' }, 'internal_linking' => { category: 'internal_linking', title_proc: ->(item) { "Link to: #{item['target_title']}" }, impact_key: nil, effort_key: nil }, 'faq_recommendations' => { category: 'faq_recommendation', title_key: 'question', impact_key: nil, effort_key: nil }, 'content_recommendations' => { category: 'content_recommendation', title_key: 'recommendation', impact_key: nil, effort_key: nil }, 'technical_recommendations' => { category: 'technical_recommendation', title_proc: ->(item) { item.is_a?(String) ? item : item['recommendation'] }, impact_key: nil, effort_key: nil }, 'structured_data_recommendations' => { category: 'structured_data', title_proc: ->(item) { "#{item['action']&.capitalize} #{item['schema_type']}" }, impact_key: nil, effort_key: nil }, 'aio_recommendations' => { category: 'aio_recommendation', title_key: 'recommendation', impact_key: 'impact', effort_key: nil }, 'people_also_ask_content' => { category: 'people_also_ask', title_key: 'question', impact_key: nil, effort_key: nil }, 'paid_organic_synergy' => { category: 'paid_organic_synergy', title_proc: ->(item) { "#{item['action']} — #{item['keyword']}" }, fingerprint_proc: ->(item) { item['keyword'].to_s.downcase.strip }, impact_key: 'impact', effort_key: nil } }.freeze
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ RecommendationExtractorService
constructor
A new instance of RecommendationExtractorService.
-
#process ⇒ Object
rubocop:disable Metrics/AbcSize -- orchestrates merge, stale, cannibalization sync.
Methods inherited from BaseService
#log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger
Constructor Details
#initialize(options = {}) ⇒ RecommendationExtractorService
Returns a new instance of RecommendationExtractorService.
75 76 77 78 79 |
# File 'app/services/seo/recommendation_extractor_service.rb', line 75 def initialize( = {}) super @site_map = [:site_map] raise ArgumentError, 'site_map is required' unless @site_map end |
Instance Method Details
#process ⇒ Object
rubocop:disable Metrics/AbcSize -- orchestrates merge, stale, cannibalization sync
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'app/services/seo/recommendation_extractor_service.rb', line 81 def process # rubocop:disable Metrics/AbcSize -- orchestrates merge, stale, cannibalization sync report = @site_map.seo_report new_fingerprints = Set.new stats = { extracted: 0, updated: 0, preserved: 0, skipped_redundant: 0 } next_generation = (@site_map.recommendations.maximum(:analysis_generation) || 0) + 1 existing_by_fingerprint = @site_map.recommendations.index_by(&:fingerprint) if report.present? CATEGORY_MAPPINGS.each do |report_key, mapping| items = report[report_key] next if items.blank? Array.wrap(items).each do |item| title = extract_title(item, mapping) next if title.blank? category = mapping[:category] fingerprint_source = mapping[:fingerprint_proc] ? mapping[:fingerprint_proc].call(item) : title fingerprint = SiteMapRecommendation.generate_fingerprint(category, fingerprint_source) impact = mapping[:impact_key] && item.is_a?(Hash) ? item[mapping[:impact_key]] : nil effort = mapping[:effort_key] && item.is_a?(Hash) ? item[mapping[:effort_key]] : nil details = item.is_a?(Hash) ? item : { 'text' => item } annotate_internal_link_target!(details) if category == 'internal_linking' existing = existing_by_fingerprint[fingerprint] if existing new_fingerprints << fingerprint if existing.resolved? stats[:preserved] += 1 else existing.update!( title: title, details: details, impact: normalize_level(impact), effort: normalize_level(effort), analysis_generation: next_generation ) stats[:updated] += 1 end elsif redundant_with_resolved?(title) stats[:skipped_redundant] += 1 else new_fingerprints << fingerprint @site_map.recommendations.create!( category: category, title: title, details: details, impact: normalize_level(impact), effort: normalize_level(effort), fingerprint: fingerprint, analysis_generation: next_generation, shared: SiteMapRecommendation.shared_category?(category) ) stats[:extracted] += 1 end end end end sync_cannibalization_recommendations( new_fingerprints, existing_by_fingerprint, next_generation, stats ) stale_count = mark_stale(existing_by_fingerprint, new_fingerprints) stats[:stale] = stale_count log_info "Extracted #{stats[:extracted]} new, updated #{stats[:updated]}, " \ "preserved #{stats[:preserved]} resolved, skipped #{stats[:skipped_redundant]} redundant-vs-resolved, " \ "marked #{stale_count} stale for SiteMap #{@site_map.id}" stats end |