Class: Webhookdb::Replicator::TransistorEpisodeStatsV1::EpisodeStatsBackfiller

Inherits:
Backfiller
  • Object
show all
Defined in:
lib/webhookdb/replicator/transistor_episode_stats_v1.rb

Instance Method Summary collapse

Methods inherited from Backfiller

#_fetch_backfill_page_with_retry, #backfill, do_retry_wait, #max_backfill_retry_attempts, #wait_for_retry_attempt

Constructor Details

#initialize(episode_svc:, episode_stats_svc:, episode_id:, episode_created_at:) ⇒ EpisodeStatsBackfiller

Returns a new instance of EpisodeStatsBackfiller.



98
99
100
101
102
103
104
# File 'lib/webhookdb/replicator/transistor_episode_stats_v1.rb', line 98

def initialize(episode_svc:, episode_stats_svc:, episode_id:, episode_created_at:)
  @episode_svc = episode_svc
  @episode_stats_svc = episode_stats_svc
  @episode_id = episode_id
  @episode_created_at = episode_created_at
  super()
end

Instance Method Details

#fetch_backfill_page(_pagination_token, **_kwargs) ⇒ Object



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
# File 'lib/webhookdb/replicator/transistor_episode_stats_v1.rb', line 111

def fetch_backfill_page(_pagination_token, **_kwargs)
  analytics_url = "https://api.transistor.fm/v1/analytics/episodes/" + @episode_id
  # The "downloads" stat gets collected daily but will not change retroactively for a past date.
  # If there are already rows in the enrichment table matching the episode_id, we want to check
  # the date of the last entry so that we don't have to upsert information that we know will not
  # be changed. We allow for a two day buffer before the date of the last entry to account for changes
  # that may occur on the day of a new entry, while the downloads are accruing.
  latest_update = @episode_stats_svc.admin_dataset(timeout: :fast) do |ds|
    ds.where(episode_id: @episode_id).max(:date)
  end
  start_date = latest_update.nil? ? @episode_created_at : (latest_update - 2.days)
  request_body = {
    start_date: start_date.strftime("%d-%m-%Y"),
    end_date: Time.now.strftime("%d-%m-%Y"),
  }
  response = Webhookdb::Http.get(
    analytics_url,
    headers: {"x-api-key" => @episode_svc.service_integration.backfill_key},
    body: request_body,
    logger: @episode_stats_svc.logger,
    timeout: Webhookdb::Transistor.http_timeout,
  )
  data = response.parsed_response.dig("data", "attributes", "downloads") || []
  return data, nil
end

#handle_item(item) ⇒ Object



106
107
108
109
# File 'lib/webhookdb/replicator/transistor_episode_stats_v1.rb', line 106

def handle_item(item)
  item["episode_id"] = @episode_id
  @episode_stats_svc.upsert_webhook_body(item)
end