Class: AmplitudeExperiment::DirectCohortDownloadApi

Inherits:
CohortDownloadApi show all
Defined in:
lib/experiment/cohort/cohort_download_api.rb

Overview

DirectCohortDownloadApi

Constant Summary

Constants inherited from CohortDownloadApi

CohortDownloadApi::COHORT_REQUEST_RETRY_DELAY_MILLIS, CohortDownloadApi::COHORT_REQUEST_TIMEOUT_MILLIS

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret_key, max_cohort_size, server_url, logger) ⇒ DirectCohortDownloadApi

Returns a new instance of DirectCohortDownloadApi.



20
21
22
23
24
25
26
27
# File 'lib/experiment/cohort/cohort_download_api.rb', line 20

def initialize(api_key, secret_key, max_cohort_size, server_url, logger)
  super()
  @api_key = api_key
  @secret_key = secret_key
  @max_cohort_size = max_cohort_size
  @server_url = server_url
  @logger = logger
end

Instance Method Details

#get_cohort(cohort_id, cohort = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/experiment/cohort/cohort_download_api.rb', line 29

def get_cohort(cohort_id, cohort = nil)
  @logger.debug("getCohortMembers(#{cohort_id}): start")
  errors = 0

  loop do
    begin
      last_modified = cohort.nil? ? nil : cohort.last_modified
      response = get_cohort_members_request(cohort_id, last_modified)
      @logger.debug("getCohortMembers(#{cohort_id}): status=#{response.code}")

      case response.code.to_i
      when 200
        cohort_info = JSON.parse(response.body)
        @logger.debug("getCohortMembers(#{cohort_id}): end - resultSize=#{cohort_info['size']}")
        return Cohort.new(
          cohort_info['cohortId'],
          cohort_info['lastModified'],
          cohort_info['size'],
          cohort_info['memberIds'].to_set,
          cohort_info['groupType']
        )
      when 204
        @logger.debug("getCohortMembers(#{cohort_id}): Cohort not modified")
        return nil
      when 413
        raise CohortTooLargeError.new(cohort_id, "Cohort exceeds max cohort size: #{response.code}")
      else
        raise HTTPErrorResponseError.new(response.code, cohort_id, "Unexpected response code: #{response.code}") if response.code.to_i != 202

      end
    rescue StandardError => e
      errors += 1 unless response && e.is_a?(HTTPErrorResponseError) && response.code.to_i == 429
      @logger.debug("getCohortMembers(#{cohort_id}): request-status error #{errors} - #{e}")
      raise e if errors >= 3 || e.is_a?(CohortTooLargeError)
    end

    sleep(COHORT_REQUEST_RETRY_DELAY_MILLIS / 1000.0)
  end
end