Class: Embulk::Input::MixpanelApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/embulk/input/mixpanel_api/client.rb

Constant Summary collapse

ENDPOINT_EXPORT =
"https://data.mixpanel.com/api/2.0/export/".freeze
TIMEOUT_SECONDS =
3600
PING_TIMEOUT_SECONDS =
3
PING_RETRY_LIMIT =
3
PING_RETRY_WAIT =
2
SMALLSET_BYTE_RANGE =
"0-#{5 * 1024 * 1024}"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_secret, retryer = nil) ⇒ Client

Returns a new instance of Client.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/embulk/input/mixpanel_api/client.rb', line 39

def initialize(api_key, api_secret, retryer = nil)
  @api_key = api_key
  @api_secret = api_secret
  @retryer = retryer || PerfectRetry.new do |config|
    # for test
    config.limit = 0
    config.dont_rescues = [RuntimeError]
    config.log_level = nil
    config.logger = Embulk.logger
    config.raise_original_error = true
  end
end

Instance Attribute Details

#retryerObject (readonly)

Returns the value of attribute retryer.



17
18
19
# File 'lib/embulk/input/mixpanel_api/client.rb', line 17

def retryer
  @retryer
end

Class Method Details

.mixpanel_available?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/embulk/input/mixpanel_api/client.rb', line 19

def self.mixpanel_available?
  retryer = PerfectRetry.new do |config|
    config.limit = PING_RETRY_LIMIT
    config.sleep = PING_RETRY_WAIT
    config.logger = Embulk.logger
    config.log_level = nil
  end

  begin
    retryer.with_retry do
      client = HTTPClient.new
      client.connect_timeout = PING_TIMEOUT_SECONDS
      client.get("https://data.mixpanel.com")
    end
    true
  rescue PerfectRetry::TooManyRetry
    false
  end
end

Instance Method Details

#export(params = {}, &block) ⇒ Object



52
53
54
55
56
# File 'lib/embulk/input/mixpanel_api/client.rb', line 52

def export(params = {}, &block)
  retryer.with_retry do
    request(params, &block)
  end
end

#export_for_small_dataset(params = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/embulk/input/mixpanel_api/client.rb', line 58

def export_for_small_dataset(params = {})
  yesterday = Date.today - 1
  latest_tried_to_date = nil
  try_to_dates(params["from_date"]).each do |to_date|
    next if yesterday < to_date
    latest_tried_to_date = to_date
    params["to_date"] = to_date.strftime("%Y-%m-%d")
    records = retryer.with_retry do
      request_small_dataset(params, SMALLSET_BYTE_RANGE)
    end
    next if records.first.nil?
    return records
  end

  raise ConfigError.new "#{params["from_date"]}..#{latest_tried_to_date} has no record."
end

#try_to_dates(from_date) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/embulk/input/mixpanel_api/client.rb', line 75

def try_to_dates(from_date)
  try_to_dates = 5.times.map do |n|
    # from_date + 1, from_date + 10, from_date + 100, ... so on
    days = 1 * (10 ** n)
    Date.parse(from_date.to_s) + days
  end
  yesterday = Date.today - 1
  try_to_dates << yesterday
  try_to_dates.find_all {|date| date <= yesterday}.uniq
end