embulk-plugin-input-sfdc-event-log-files 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 82ff6152830fd1e9fe317091a30325d28e322848
4
- data.tar.gz: dd78b5bbb4e11d215be0570023304385225ddf4f
3
+ metadata.gz: 26d4a69db8632348b043774556a10b3c7bbff8e3
4
+ data.tar.gz: a2a7cfd359eb9a89625a4cc0f1bdd0229f236fc3
5
5
  SHA512:
6
- metadata.gz: 3cd3a3c7acbe65789601d8b158cfbde4d37528e406187f9d43ddebbd3144760e0f434377497e1675c9df3e1d86a2a570e5731ef996d910c065e19e0e8e83332c
7
- data.tar.gz: 38118c1e77adf1d538e1bf3b82a710427f52192ab331f3c90230c538d22f3def32216efa274527aee8b1a078a4148a6e5a04631562737fe236e758d158c43e0c
6
+ metadata.gz: d54660a917a3d419f3adef4a7f3cd9e3f7cd155323a04c3b26a6e3cdd0746c518793c1cca543a48caf9f0e5901cfa7b451a8eaefd84bd416a563aefdae5a8273
7
+ data.tar.gz: 51b9b0c0724aa171876c611af1df816535a4a267dfb8286484413b97f05238693899bc11d277c8242ae5e956aceed1bc6087ecf4d049622b09e3860a69dd394d
data/README.md CHANGED
@@ -3,4 +3,6 @@ embulk-plugin-input-sfdc-event-log-files
3
3
 
4
4
  Embulk plugin for Salesforce.com Event Log Files input
5
5
 
6
- To be written...
6
+ At this moment this plugin is a sample implementation of InputPlugin.
7
+ Without GuessPlugin implementation you cannot create proper schema configuration.
8
+ Stay tuned.
@@ -20,17 +20,23 @@ module Embulk
20
20
  'last_log_date' => config.param('last_log_date', :string, default: '0001-01-01T00:00:00Z'),
21
21
  'max_retry_times' => config.param('max_retry_times', :integer, default: 2),
22
22
  }
23
- idx = -1
24
- schema = config.param('schema', :array).map { |c| idx += 1; Column.new(idx, c['name'], c['type'].to_sym) }
25
23
  threads = config.param('threads', :integer, default: 2)
26
- task['client'] = client = HTTPClient.new
24
+ idx = -1
25
+ schema = config.param('schema', :array, default: []).map { |c| idx += 1; Column.new(idx, c['name'], c['type'].to_sym) }
27
26
 
28
27
  begin
29
- oauth(task)
30
- task['records'] = query(task)
28
+ client = HTTPClient.new
29
+ parsed = oauth(client, task)
30
+ task['instance_url'] = parsed['instance_url']
31
+ task['access_token'] = parsed['access_token']
32
+ init_client(client, task)
31
33
 
32
- reports = yield(task, schema, threads)
34
+ task['records'] = query(client, task)
33
35
 
36
+ if schema.empty?
37
+ raise 'empty schema given'
38
+ end
39
+ reports = yield(task, schema, threads)
34
40
  last_log_date_report = reports.max_by { |report|
35
41
  report['last_log_date']
36
42
  }
@@ -41,10 +47,15 @@ module Embulk
41
47
  end
42
48
  end
43
49
 
50
+ def init_client(client, task)
51
+ client.base_url = task['instance_url']
52
+ client.default_header = { 'Authorization' => 'Bearer ' + task['access_token'] }
53
+ client
54
+ end
55
+
44
56
  private
45
57
 
46
- def oauth(task)
47
- client = task['client']
58
+ def oauth(client, task)
48
59
  params = {
49
60
  :grant_type => 'password',
50
61
  :client_id => task['oauth_client_id'],
@@ -54,15 +65,11 @@ module Embulk
54
65
  }
55
66
  with_retry(task) {
56
67
  res = client.post(task['login_url'] + '/services/oauth2/token', params, :Accept => 'application/json; charset=UTF-8')
57
- parsed = JSON.parse(res.body)
58
- client.base_url = parsed['instance_url']
59
- client.default_header = { 'Authorization' => 'Bearer ' + parsed['access_token'] }
60
- nil
68
+ JSON.parse(res.body)
61
69
  }
62
70
  end
63
71
 
64
- def query(task)
65
- client = task['client']
72
+ def query(client, task)
66
73
  query = "Select LogDate, EventType, LogFile from EventLogFile Where LogDate > #{task['last_log_date']}"
67
74
  with_retry(task) {
68
75
  res = client.get('/services/data/v32.0/query/', :q => query)
@@ -83,30 +90,34 @@ module Embulk
83
90
  end
84
91
  end
85
92
 
86
- def initialize(task, schema, index, page_builder)
87
- @schema = schema
88
- @page_builder = page_builder
89
- @records = task['records']
90
- @last_log_date = Time.parse(task['last_log_date'])
91
- @client = task['client']
92
- end
93
+ attr_reader :task
94
+ attr_reader :schema
95
+ attr_reader :page_builder
93
96
 
94
97
  def run
95
- columns = @schema.map { |c| c.name }
96
- @page_builder.finish
97
- @records.each do |record|
98
+ client = self.class.init_client(HTTPClient.new, task)
99
+ records = task['records']
100
+ last_log_date = Time.parse(task['last_log_date'])
101
+ columns = schema.map { |c| c.name }
102
+ records.each do |record|
98
103
  event_type = record['EventType']
99
- @last_log_date = [@last_log_date, Time.parse(record['LogDate']).to_i].max
104
+ last_log_date = [last_log_date, Time.parse(record['LogDate']).to_i].max
100
105
  log_file = record['LogFile']
101
- CSV.parse(@client.get_content(log_file), headers: true) do |row|
102
- row['TIMESTAMP'] = Time.parse(row['TIMESTAMP']).to_i
103
- @page_builder.add(row.to_hash.values_at(*columns))
106
+ log_body = client.get_content(log_file)
107
+ CSV.parse(log_body, headers: true) do |row|
108
+ if row['TIMESTAMP']
109
+ begin
110
+ row['TIMESTAMP'] = Time.parse(row['TIMESTAMP']).to_i
111
+ rescue
112
+ # ignore
113
+ end
114
+ end
115
+ page_builder.add(row.to_hash.values_at(*columns).map(&:to_s))
104
116
  end
105
117
  end
106
- @page_builder.finish unless @records.empty?
107
-
118
+ page_builder.finish unless records.empty?
108
119
  commit_report = {
109
- 'last_log_date' => @last_log_date.xmlschema
120
+ 'last_log_date' => last_log_date.xmlschema
110
121
  }
111
122
  commit_report
112
123
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-plugin-input-sfdc-event-log-files
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroshi Nakamura