Class: Amazon::SalesRankBackfill

Inherits:
Object
  • Object
show all
Includes:
Service
Defined in:
app/services/amazon/sales_rank_backfill.rb

Overview

Projects retained Product Pricing SalesRankings payloads from
edi_communication_logs into AmazonSalesRankDataPoint. Preview is the default
and counts the exact window before the write loop. Execution is resumable and
idempotent when the same batch UUID is reused.

This service deliberately ends before the forward collector's launch date:
live ingestion owns newer rows, while an imported batch stays independently removable with
AmazonSalesRankDataPoint.where(source_batch_id: batch_id).delete_all.

Defined Under Namespace

Classes: Result

Constant Summary collapse

REPORTING_TIME_ZONE =
'America/Chicago'
LIVE_COLLECTION_START_ON =
Date.new(2026, 7, 25)
UUID_PATTERN =
/\A[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\z/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Service

#attributes_used, #build_result, call, #logger

Methods included from Service::Initializer

#initialize

Instance Attribute Details

#end_onObject (readonly)



39
# File 'app/services/amazon/sales_rank_backfill.rb', line 39

validates :start_on, :end_on, presence: true

#start_onObject (readonly)



39
# File 'app/services/amazon/sales_rank_backfill.rb', line 39

validates :start_on, :end_on, presence: true

Instance Method Details

#callResult

Returns exact preview counts, plus write count when execute is true.

Returns:

  • (Result)

    exact preview counts, plus write count when execute is true

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/services/amazon/sales_rank_backfill.rb', line 44

def call
  raise ArgumentError, errors.full_messages.to_sentence unless valid?

  counts = preview_counts
  return build_result_from(counts, recorded_points: nil, executed: false) unless execute

  recorded_points = 0
  processed_logs = 0
  source_scope.includes(edi_documents: :catalog_item).find_each(batch_size: 500) do |ecl|
    ecl.edi_documents.filter_map(&:catalog_item).uniq(&:id).each do |catalog_item|
      result = Amazon::RecordSalesRanks.call(
        catalog_item:,
        payload: ecl.data,
        source: 'product_pricing',
        observed_at: ecl.created_at,
        source_batch_id: batch_id
      )
      recorded_points += result.recorded_count
    end

    processed_logs += 1
    logger.info("[Amazon::SalesRankBackfill] processed #{processed_logs}/#{counts[:source_logs]} logs") if (processed_logs % 1_000).zero?
  end

  build_result_from(counts, recorded_points:, executed: true)
end