td-client 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 10f1e7ce3ea3f8b94d5c2d2cce131ef2968c534f
4
- data.tar.gz: 24fa95fd8ebb8751ba502265a7d15942e6f5a978
3
+ metadata.gz: 0a5a7d5958d73f2f32f21250831184d6da0f4ea2
4
+ data.tar.gz: '049a6d49493d8fdee00e30b6c28fef7c3657ea81'
5
5
  SHA512:
6
- metadata.gz: e3dce86a68061a523fd51215e1fb37d6f0b654660dbc4c2fd87420d0b155bf8bd5de084b8b0aa4b2a932f130012bf7d7ee49214bcb589fa2d3d4463ba72f8439
7
- data.tar.gz: 16ab57b46b6d3c208bcbeb690c1578b30e64d2643ff518f57889ca20c9980be6e387846d2d308b8dbcbdee63c3d25fea768d9c84dcfb3427c1ea63a0d4652e74
6
+ metadata.gz: eb17173daa47cd38d36555a4d4a5f05e92642d6eaec7724dd45e17ac3890c860f58bf62e907a7628e6ecf8a765b9f68807f1e19c5bae625376c4aa1b8b266f95
7
+ data.tar.gz: cbd7a86745ae12a10112556418013a74b4734d2e17b44a7661933b5fb7f81db5c2701ab55bbdd865cec0fe62e4af475f3336315eb1c2bb4a3d5835ee73b96e98
@@ -88,9 +88,14 @@ class Client
88
88
  raise NotFoundError, "Database '#{db_name}' does not exist"
89
89
  end
90
90
 
91
+ # @param [String] db
92
+ # @param [String] table
93
+ # @option params [Fixnum] :expire_days days to expire table
94
+ # @option params [Boolean] :include_v (true) include v column on Hive
95
+ # @option params [Boolean] :detect_schema (true) detect schema on import
91
96
  # @return [true]
92
- def create_log_table(db_name, table_name)
93
- @api.create_log_table(db_name, table_name)
97
+ def create_log_table(db_name, table_name, params={})
98
+ @api.create_log_table(db_name, table_name, params)
94
99
  end
95
100
 
96
101
  # Swap table names
@@ -111,6 +116,16 @@ class Client
111
116
  @api.update_schema(db_name, table_name, schema.to_json)
112
117
  end
113
118
 
119
+ # @param [String] db
120
+ # @param [String] table
121
+ # @option params [Fixnum] :expire_days days to expire table
122
+ # @option params [Boolean] :include_v (true) include v column on Hive
123
+ # @option params [Boolean] :detect_schema (true) detect schema on import
124
+ # @return [true]
125
+ def update_table(db_name, table_name, params={})
126
+ @api.update_table(db_name, table_name, params)
127
+ end
128
+
114
129
  # @param [String] db_name
115
130
  # @param [String] table_name
116
131
  # @param [Fixnum] expire_days
@@ -130,10 +145,10 @@ class Client
130
145
  # @return [Array] Tables
131
146
  def tables(db_name)
132
147
  m = @api.list_tables(db_name)
133
- m.map {|table_name, (type, schema, count, created_at, updated_at, estimated_storage_size, last_import, last_log_timestamp, expire_days)|
148
+ m.map {|table_name, (type, schema, count, created_at, updated_at, estimated_storage_size, last_import, last_log_timestamp, expire_days, include_v)|
134
149
  schema = Schema.new.from_json(schema)
135
150
  Table.new(self, db_name, table_name, type, schema, count, created_at, updated_at,
136
- estimated_storage_size, last_import, last_log_timestamp, expire_days)
151
+ estimated_storage_size, last_import, last_log_timestamp, expire_days, include_v)
137
152
  }
138
153
  end
139
154
 
@@ -6,7 +6,7 @@ module Job
6
6
  ##
7
7
 
8
8
  # @param [Fixnum] from
9
- # @param [Fixnum] to
9
+ # @param [Fixnum] to (to is inclusive)
10
10
  # @param [String] status
11
11
  # @param [Hash] conditions
12
12
  # @return [Array]
@@ -66,7 +66,7 @@ module Schedule
66
66
 
67
67
  # @param [String] name
68
68
  # @param [Fixnum] from
69
- # @param [Fixnum] to
69
+ # @param [Fixnum] to (to is exclusive)
70
70
  # @return [Array]
71
71
  def history(name, from=0, to=nil)
72
72
  params = {}
@@ -25,16 +25,18 @@ module Table
25
25
  estimated_storage_size = m['estimated_storage_size'].to_i
26
26
  schema = JSON.parse(m['schema'] || '[]')
27
27
  expire_days = m['expire_days']
28
- result[name] = [type, schema, count, created_at, updated_at, estimated_storage_size, last_import, last_log_timestamp, expire_days]
28
+ include_v = m['include_v']
29
+ result[name] = [type, schema, count, created_at, updated_at, estimated_storage_size, last_import, last_log_timestamp, expire_days, include_v]
29
30
  }
30
31
  return result
31
32
  end
32
33
 
33
34
  # @param [String] db
34
35
  # @param [String] table
36
+ # @param [Hash] params
35
37
  # @return [true]
36
- def create_log_table(db, table)
37
- create_table(db, table, :log)
38
+ def create_log_table(db, table, params={})
39
+ create_table(db, table, :log, params)
38
40
  end
39
41
 
40
42
  # @param [String] db
@@ -81,9 +83,19 @@ module Table
81
83
  # @param [Fixnum] expire_days
82
84
  # @return [true]
83
85
  def update_expire(db, table, expire_days)
84
- code, body, res = post("/v3/table/update/#{e db}/#{e table}", {'expire_days'=>expire_days})
86
+ update_table(db, table, {:expire_days=>expire_days})
87
+ end
88
+
89
+ # @param [String] db
90
+ # @param [String] table
91
+ # @option params [Fixnum] :expire_days days to expire table
92
+ # @option params [Boolean] :include_v (true) include v column on Hive
93
+ # @option params [Boolean] :detect_schema (true) detect schema on import
94
+ # @return [true]
95
+ def update_table(db, table, params={})
96
+ code, body, res = post("/v3/table/update/#{e db}/#{e table}", params)
85
97
  if code != "200"
86
- raise_error("Update table expiration failed", res)
98
+ raise_error("Update table failed", res)
87
99
  end
88
100
  return true
89
101
  end
@@ -153,7 +153,7 @@ class Table < Model
153
153
  # @param [String] last_import
154
154
  # @param [String] last_log_timestamp
155
155
  # @param [Fixnum, String] expire_days
156
- def initialize(client, db_name, table_name, type, schema, count, created_at=nil, updated_at=nil, estimated_storage_size=nil, last_import=nil, last_log_timestamp=nil, expire_days=nil)
156
+ def initialize(client, db_name, table_name, type, schema, count, created_at=nil, updated_at=nil, estimated_storage_size=nil, last_import=nil, last_log_timestamp=nil, expire_days=nil, include_v=false)
157
157
  super(client)
158
158
  @database = nil
159
159
  @db_name = db_name
@@ -167,6 +167,7 @@ class Table < Model
167
167
  @last_import = last_import
168
168
  @last_log_timestamp = last_log_timestamp
169
169
  @expire_days = expire_days
170
+ @include_v = include_v
170
171
  end
171
172
 
172
173
  # @!attribute [r] type
@@ -175,7 +176,7 @@ class Table < Model
175
176
  # @!attribute [r] schema
176
177
  # @!attribute [r] count
177
178
  # @!attribute [r] estimated_storage_size
178
- attr_reader :type, :db_name, :table_name, :schema, :count, :estimated_storage_size
179
+ attr_reader :type, :db_name, :table_name, :schema, :count, :estimated_storage_size, :include_v
179
180
 
180
181
  alias database_name db_name
181
182
  alias name table_name
@@ -425,6 +426,7 @@ class Job < Model
425
426
  @db_name = db_name
426
427
  @duration = duration
427
428
  @num_records = num_records
429
+ @auto_update_status = true
428
430
  end
429
431
 
430
432
  # @!attribute [r] job_id
@@ -440,6 +442,16 @@ class Job < Model
440
442
  attr_reader :priority, :retry_limit, :org_name, :db_name
441
443
  attr_reader :duration, :num_records
442
444
 
445
+ # whether it update status if the job is not finished yet or not
446
+ def auto_update_status?
447
+ @auto_update_status
448
+ end
449
+
450
+ # set whether it update status if the job is not finished yet or not
451
+ def auto_update_status=(bool)
452
+ @auto_update_status = bool ? true : false
453
+ end
454
+
443
455
  # @option timeout [Integer,nil] timeout in second
444
456
  # @option wait_interval [Integer,nil] interval in second of polling the job status
445
457
  # @param detail [Boolean] update job detail or not
@@ -478,55 +490,55 @@ class Job < Model
478
490
 
479
491
  # @return [String]
480
492
  def query
481
- update_status! unless @query || finished?
493
+ update_status! unless @query || !@auto_update_status || finished?
482
494
  @query
483
495
  end
484
496
 
485
497
  # @return [String]
486
498
  def status
487
- update_status! unless @status || finished?
499
+ update_status! unless @status || !@auto_update_status || finished?
488
500
  @status
489
501
  end
490
502
 
491
503
  # @return [String]
492
504
  def url
493
- update_status! unless @url || finished?
505
+ update_status! unless @url || !@auto_update_status || finished?
494
506
  @url
495
507
  end
496
508
 
497
509
  # @return [Boolean]
498
510
  def debug
499
- update_status! unless @debug || finished?
511
+ update_status! unless @debug || !@auto_update_status || finished?
500
512
  @debug
501
513
  end
502
514
 
503
515
  # @return [Time, nil]
504
516
  def start_at
505
- update_status! unless @start_at || finished?
517
+ update_status! unless @start_at || !@auto_update_status || finished?
506
518
  @start_at && !@start_at.empty? ? Time.parse(@start_at) : nil
507
519
  end
508
520
 
509
521
  # @return [Time, nil]
510
522
  def end_at
511
- update_status! unless @end_at || finished?
523
+ update_status! unless @end_at || !@auto_update_status || finished?
512
524
  @end_at && !@end_at.empty? ? Time.parse(@end_at) : nil
513
525
  end
514
526
 
515
527
  # @return [String]
516
528
  def cpu_time
517
- update_status! unless @cpu_time || finished?
529
+ update_status! unless @cpu_time || !@auto_update_status || finished?
518
530
  @cpu_time
519
531
  end
520
532
 
521
533
  # @return [Array]
522
534
  def hive_result_schema
523
- update_status! unless @hive_result_schema.instance_of? Array || finished?
535
+ update_status! unless @hive_result_schema.instance_of?(Array) || !@auto_update_status || finished?
524
536
  @hive_result_schema
525
537
  end
526
538
 
527
539
  # @return [String]
528
540
  def result_size
529
- update_status! unless @result_size || finished?
541
+ update_status! unless @result_size || !@auto_update_status || finished?
530
542
  @result_size
531
543
  end
532
544
 
@@ -1,5 +1,5 @@
1
1
  module TreasureData
2
2
  class Client
3
- VERSION = '1.0.4'
3
+ VERSION = '1.0.5'
4
4
  end
5
5
  end
@@ -35,6 +35,56 @@ describe 'Job Model' do
35
35
  end
36
36
  end
37
37
 
38
+ describe '#auto_update_status' do
39
+ let(:client) { Client.authenticate('user', 'password') }
40
+ let(:job_id) { 12345678 }
41
+ let(:job) { Job.new(client, job_id, nil, nil) }
42
+ let(:format) { 'json' }
43
+ let(:io) { StringIO.new }
44
+ before { allow(job).to receive(:finished?) { false } }
45
+
46
+ it 'can set' do
47
+ expect(job.auto_update_status?).to eq true
48
+ job.auto_update_status = false
49
+ expect(job.auto_update_status?).to eq false
50
+ job.auto_update_status = true
51
+ expect(job.auto_update_status?).to eq true
52
+ end
53
+
54
+ it 'calls API if auto_update_status=true' do
55
+ job.auto_update_status = true
56
+ result_job = {
57
+ 'job_id' => job_id,
58
+ 'status' => 'queued',
59
+ 'created_at' => Time.now,
60
+ }
61
+ stub_request(:get, "https://api.treasuredata.com/v3/job/show/#{job_id}").
62
+ to_return(:body => result_job.to_json)
63
+ expect(job.query).to be_nil
64
+ expect(job.status).to eq "queued"
65
+ expect(job.url).to be_nil
66
+ expect(job.debug).to be_nil
67
+ expect(job.start_at).to be_nil
68
+ expect(job.end_at).to be_nil
69
+ expect(job.cpu_time).to be_nil
70
+ expect(job.hive_result_schema).to be_nil
71
+ expect(job.result_size).to be_nil
72
+ end
73
+
74
+ it "doesn't call API if auto_update_status=false" do
75
+ job.auto_update_status = false
76
+ expect(job.query).to be_nil
77
+ expect(job.status).to be_nil
78
+ expect(job.url).to be_nil
79
+ expect(job.debug).to be_nil
80
+ expect(job.start_at).to be_nil
81
+ expect(job.end_at).to be_nil
82
+ expect(job.cpu_time).to be_nil
83
+ expect(job.hive_result_schema).to be_nil
84
+ expect(job.result_size).to be_nil
85
+ end
86
+ end
87
+
38
88
  describe '#result_raw' do
39
89
  let(:client) { Client.authenticate('user', 'password') }
40
90
  let(:job_id) { 12345678 }
@@ -30,6 +30,13 @@ describe 'Table API' do
30
30
  expect(api.create_log_table(db_name, table_name)).to be true
31
31
  end
32
32
 
33
+ it 'should create a new table with params' do
34
+ stub_api_request(:post, "/v3/table/create/#{e db_name}/#{e(table_name)}/log").
35
+ with(:body => {'include_v' => 'false'}).
36
+ to_return(:body => {'database' => db_name, 'table' => table_name, 'type' => 'log', 'include_v' => 'false'}.to_json)
37
+ expect(api.create_log_table(db_name, table_name, include_v: false)).to be true
38
+ end
39
+
33
40
  it 'should return 400 error with invalid name' do
34
41
  invalid_name = 'a'
35
42
  err_msg = "Name must be 3 to 256 characters, got #{invalid_name.length} characters. name = '#{invalid_name}'"
@@ -52,6 +59,52 @@ describe 'Table API' do
52
59
  end
53
60
  end
54
61
 
62
+ describe "'create_log_table' client API" do
63
+ it 'should return 404 error if the database does not exist' do
64
+ err_msg = "Create log table failed: Couldn't find UserDatabase with name = #{db_name}"
65
+ stub_api_request(:post, "/v3/table/create/#{e db_name}/#{e(table_name)}/log").
66
+ to_return(:status => 404, :body => {'message' => err_msg}.to_json)
67
+
68
+ expect {
69
+ client.create_log_table(db_name, table_name)
70
+ }.to raise_error(TreasureData::NotFoundError, /#{err_msg}/)
71
+ end
72
+
73
+ it 'should create a new table if the database exists' do
74
+ stub_api_request(:post, "/v3/table/create/#{e db_name}/#{e(table_name)}/log").
75
+ to_return(:body => {'database' => db_name, 'table' => table_name, 'type' => 'log'}.to_json)
76
+ expect(client.create_log_table(db_name, table_name)).to be true
77
+ end
78
+
79
+ it 'should create a new table with params' do
80
+ stub_api_request(:post, "/v3/table/create/#{e db_name}/#{e(table_name)}/log").
81
+ with(:body => {'include_v' => 'false'}).
82
+ to_return(:body => {'database' => db_name, 'table' => table_name, 'type' => 'log', 'include_v' => 'false'}.to_json)
83
+ expect(client.create_log_table(db_name, table_name, include_v: false)).to be true
84
+ end
85
+
86
+ it 'should return 400 error with invalid name' do
87
+ invalid_name = 'a'
88
+ err_msg = "Name must be 3 to 256 characters, got #{invalid_name.length} characters. name = '#{invalid_name}'"
89
+ stub_api_request(:post, "/v3/table/create/#{e db_name}/#{e invalid_name}/log").
90
+ to_return(:status => 400, :body => {'message' => err_msg}.to_json)
91
+
92
+ expect {
93
+ client.create_log_table(db_name, invalid_name)
94
+ }.to raise_error(TreasureData::APIError, /#{err_msg}/)
95
+ end
96
+
97
+ it 'should return 409 error with duplicated name' do
98
+ err_msg = "Table #{table_name} already exists"
99
+ stub_api_request(:post, "/v3/table/create/#{e db_name}/#{e table_name}/log").
100
+ to_return(:status => 409, :body => {'message' => err_msg}.to_json)
101
+
102
+ expect {
103
+ client.create_log_table(db_name, table_name)
104
+ }.to raise_error(TreasureData::AlreadyExistsError, /#{err_msg}/)
105
+ end
106
+ end
107
+
55
108
  describe "'list_tables' API" do
56
109
  it 'should list the tables in a Hash whose values include type, count, created_at, updated_at, schema, ...' do
57
110
  tables = [
@@ -179,6 +232,31 @@ describe 'Table API' do
179
232
  end
180
233
  end
181
234
 
235
+ describe 'handle include_v' do
236
+ it 'should set/unset include_v flag' do
237
+ stub_api_request(:get, '/v3/table/list/db').
238
+ to_return(:body => {'tables' => [
239
+ {'name' => 'table', 'type' => 'log', 'include_v' => true},
240
+ ]}.to_json)
241
+
242
+ table = client.table('db', 'table')
243
+ expect(table.include_v).to eq true
244
+
245
+ stub_api_request(:get, '/v3/table/list/db').
246
+ to_return(:body => {'tables' => [
247
+ {'name' => 'table', 'type' => 'log', 'include_v' => false},
248
+ ]}.to_json)
249
+
250
+ stub_api_request(:post, '/v3/table/update/db/table').
251
+ with(:body => {'include_v' => "false"}).
252
+ to_return(:body => {"database"=>"db","table"=>"table","type"=>"log"}.to_json)
253
+ api.update_table('db', 'table', include_v: "false")
254
+
255
+ table = client.table('db', 'table')
256
+ expect(table.include_v).to eq false
257
+ end
258
+ end
259
+
182
260
  describe 'tail' do
183
261
  let :packed do
184
262
  s = StringIO.new
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: td-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Treasure Data, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-21 00:00:00.000000000 Z
11
+ date: 2018-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack