Class: LeanplumApi::DataExportAPI

Inherits:
API
  • Object
show all
Defined in:
lib/leanplum_api/data_export_api.rb

Constant Summary

Constants inherited from API

API::EXPORT_FINISHED, API::EXPORT_PENDING, API::EXPORT_RUNNING, API::SET_DEVICE_ATTRIBUTES, API::SET_USER_ATTRIBUTES, API::TRACK

Instance Method Summary collapse

Methods inherited from API

#delete_user, #export_user, #get_ab_test, #get_ab_tests, #get_message, #get_messages, #get_variant, #get_vars, #import_csv, #initialize, #reset_anomalous_users, #set_device_attributes, #set_user_attributes, #track_events, #track_multi, #user_attributes, #user_events

Constructor Details

This class inherits a constructor from LeanplumApi::API

Instance Method Details

#export_data(start_time, end_time = nil) ⇒ Object

Returns the jobId Leanplum has confirmed that using startTime and endTime, especially trying to be relatively up to the minute, leads to sort of unprocessed information that can be incomplete. They recommend using the automatic export to S3 if possible.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/leanplum_api/data_export_api.rb', line 13

def export_data(start_time, end_time = nil)
  LeanplumApi.configuration.logger.warn("You should probably use the direct S3 export instead of exportData")
  fail "Start time #{start_time} after end time #{end_time}" if end_time && start_time > end_time
  LeanplumApi.configuration.logger.info("Requesting data export from #{start_time} to #{end_time}...")

  # Because of open questions about how startTime and endTime work (or don't work, as the case may be), we
  # only want to pass the dates unless start and end times are specifically requested.
  params = { action: 'exportData', startDate: start_time.strftime('%Y%m%d') }
  params[:startTime] = start_time.strftime('%s') if start_time.is_a?(DateTime) || start_time.is_a?(Time)

  if end_time
    params[:endDate] = end_time.strftime('%Y%m%d')
    params[:endTime] = end_time.strftime('%s') if end_time.is_a?(DateTime) || end_time.is_a?(Time)
  end

  # Handle optional S3 export params
  if LeanplumApi.configuration.s3_bucket_name
    fail 's3_bucket_name set but s3_access_id not configured!' unless LeanplumApi.configuration.s3_access_id
    fail 's3_bucket_name set but s3_access_key not configured!' unless LeanplumApi.configuration.s3_access_key

    params.merge!(
      s3BucketName: LeanplumApi.configuration.s3_bucket_name,
      s3AccessId: LeanplumApi.configuration.s3_access_id,
      s3AccessKey: LeanplumApi.configuration.s3_access_key
    )
    params.merge!(s3ObjectPrefix: LeanplumApi.configuration.s3_object_prefix) if LeanplumApi.configuration.s3_object_prefix
  end

  data_export_connection.get(params).first['jobId']
end

#export_users(ab_test_id = nil, segment = nil) ⇒ Object

See leanplum docs. The segment syntax is identical to that produced by the “Insert Value” feature on the dashboard. Examples: ‘Country = “US”’, ‘= “US” and version = 1’.



65
66
67
# File 'lib/leanplum_api/data_export_api.rb', line 65

def export_users(ab_test_id = nil, segment = nil)
  data_export_connection.get(action: 'exportUsers', segment: segment, ab_test_id: ab_test_id).first['jobId']
end

#get_export_results(job_id) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/leanplum_api/data_export_api.rb', line 44

def get_export_results(job_id)
  response = data_export_connection.get(action: 'getExportResults', jobId: job_id).first

  if response['state'] == EXPORT_FINISHED
    LeanplumApi.configuration.logger.info("Export finished.")
    LeanplumApi.configuration.logger.debug("  Response: #{response}")
    {
      files: response['files'],
      number_of_sessions: response['numSessions'],
      number_of_bytes: response['numBytes'],
      state: response['state'],
      s3_copy_status: response['s3CopyStatus']
    }
  else
    { state: response['state'] }
  end
end

#wait_for_export_job(job_id, polling_interval = 60) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/leanplum_api/data_export_api.rb', line 69

def wait_for_export_job(job_id, polling_interval = 60)
  while get_export_results(job_id)[:state] != EXPORT_FINISHED
    LeanplumApi.configuration.logger.debug("Polling job #{job_id}: #{get_export_results(job_id)}")
    sleep(polling_interval)
  end

  get_export_results(job_id)
end