Class: Amazon::SalesRankMetrics
- Inherits:
-
Object
- Object
- Amazon::SalesRankMetrics
- Includes:
- Service
- Defined in:
- app/services/amazon/sales_rank_metrics.rb
Overview
Builds the bounded sales-rank summary used by an Amazon retailer's Trends
tab. It selects the best current listing/category pairs first, then fetches
history only for those pairs; loading the dashboard therefore stays bounded
as the daily data-point table grows.
Defined Under Namespace
Constant Summary collapse
- CURRENT_MAX_AGE =
14.days
- COMPARISON_DAYS =
30- COMPARISON_TOLERANCE_DAYS =
7
Instance Attribute Summary collapse
- #catalog ⇒ Object readonly
- #days ⇒ Object readonly
- #limit ⇒ Object readonly
Instance Method Summary collapse
Methods included from Service
#attributes_used, #build_result, call, #logger
Methods included from Service::Initializer
Instance Attribute Details
#catalog ⇒ Object (readonly)
55 |
# File 'app/services/amazon/sales_rank_metrics.rb', line 55 validates :catalog, presence: true |
#days ⇒ Object (readonly)
56 |
# File 'app/services/amazon/sales_rank_metrics.rb', line 56 validates :days, :limit, numericality: { only_integer: true, greater_than: 0 } |
#limit ⇒ Object (readonly)
56 |
# File 'app/services/amazon/sales_rank_metrics.rb', line 56 validates :days, :limit, numericality: { only_integer: true, greater_than: 0 } |
Instance Method Details
#call ⇒ Result
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'app/services/amazon/sales_rank_metrics.rb', line 59 def call raise ArgumentError, errors..to_sentence unless valid? points = current_points return Result.new(rows: [], window_days: days) if points.empty? histories = histories_for(points) catalog_items = CatalogItem.includes(store_item: :item) .where(id: points.map(&:catalog_item_id)) .index_by(&:id) browse_node_names = browse_node_names_for(points) rows = points.filter_map do |point| catalog_item = catalog_items[point.catalog_item_id] next unless catalog_item pair_history = histories.fetch(pair_key(point), []) category_name = category_name_for(point, browse_node_names) Row.new( catalog_item_id: catalog_item.id, item_id: catalog_item.item_id, sku: catalog_item.sku, asin: catalog_item.amazon_asin, amazon_url: catalog_item.amazon_product_url, category_kind: point.category_kind, category_id: point.product_category_id, category_name:, current_rank: point.rank, observed_on: point.observed_on, observed_at: point.observed_at, change_30_days: change_30_days(point, pair_history), points: pair_history.map { |history_point| [history_point.observed_on.iso8601, history_point.rank] } ) end Result.new(rows:, window_days: days) end |