Class: Seo::VisitsSyncService

Inherits:
BaseService show all
Defined in:
app/services/seo/visits_sync_service.rb

Overview

Applies rolling first-party page-view counts from the bounded daily
aggregate to SiteMap data points and the sortable 30-day cache column.
Aggregate refresh is deliberately owned by separate, one-day Sidekiq jobs.

Examples:

Apply every SiteMap after aggregate coverage is complete

Seo::VisitsSyncService.new.call

Apply a bounded Sidekiq batch

Seo::VisitsSyncService.new(site_map_ids: [1, 2, 3], period_end: Date.current).call

Constant Summary collapse

BATCH_SIZE =
500

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #process, #tagged_logger

Constructor Details

#initialize(options = {}) ⇒ VisitsSyncService

Returns a new instance of VisitsSyncService.

Parameters:

  • options (Hash) (defaults to: {})

    configurable options

Options Hash (options):

  • site_map_ids (Array<Integer>)

    site maps to sync visits for

  • period_end (Date)

    last day of the period to sync (defaults to today)



19
20
21
22
23
24
# File 'app/services/seo/visits_sync_service.rb', line 19

def initialize(options = {})
  super
  @site_map_ids = options[:site_map_ids]
  @period_end = (options[:period_end] || Date.current).to_date
  @stats = { pages_updated: 0, errors: [], deferred: false }
end

Instance Method Details

#callHash

Returns page count plus a deferred flag when 90-day coverage is not
ready yet.

Returns:

  • (Hash)

    page count plus a deferred flag when 90-day coverage is not
    ready yet



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/services/seo/visits_sync_service.rb', line 28

def call
  counts_query = Seo::VisitPageDailyCountsQuery.new(period_end: @period_end)
  unless counts_query.coverage_complete?
    @logger.warn("[VisitsSyncService] Deferred: daily aggregate is incomplete through #{@period_end}")
    return @stats.merge(deferred: true)
  end

  recorded_at = Time.current
  batch_id = SecureRandom.uuid
  @logger.info("[VisitsSyncService] Applying visit counts through #{@period_end}")
  site_maps_scope.in_batches(of: BATCH_SIZE) do |batch|
    apply_batch(batch.pluck(:id, :path), counts_query, recorded_at:, batch_id:)
  end

  @logger.info("[VisitsSyncService] Completed: #{@stats[:pages_updated]} pages updated")
  @stats
end