td-client 0.8.68 → 0.8.69
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/td/client.rb +250 -57
- data/lib/td/client/api.rb +125 -7
- data/lib/td/client/api/access_control.rb +14 -2
- data/lib/td/client/api/account.rb +4 -0
- data/lib/td/client/api/bulk_import.rb +41 -11
- data/lib/td/client/api/bulk_load.rb +205 -0
- data/lib/td/client/api/database.rb +6 -3
- data/lib/td/client/api/export.rb +5 -0
- data/lib/td/client/api/import.rb +7 -1
- data/lib/td/client/api/job.rb +49 -5
- data/lib/td/client/api/partial_delete.rb +6 -0
- data/lib/td/client/api/result.rb +7 -2
- data/lib/td/client/api/schedule.rb +17 -3
- data/lib/td/client/api/server_status.rb +1 -0
- data/lib/td/client/api/table.rb +41 -6
- data/lib/td/client/api/to_hash_struct.rb +82 -0
- data/lib/td/client/api/user.rb +27 -10
- data/lib/td/client/compat_gzip_reader.rb +3 -0
- data/lib/td/client/model.rb +209 -0
- data/lib/td/client/version.rb +1 -1
- data/spec/spec_helper.rb +14 -6
- data/spec/td/client/bulk_load_spec.rb +358 -0
- data/spec/td/client/job_api_spec.rb +10 -8
- data/spec/td/client_sched_spec.rb +1 -1
- metadata +96 -35
data/lib/td/client/api/table.rb
CHANGED
@@ -5,7 +5,8 @@ module Table
|
|
5
5
|
## Table API
|
6
6
|
##
|
7
7
|
|
8
|
-
#
|
8
|
+
# @param [String] db
|
9
|
+
# @return [Array]
|
9
10
|
def list_tables(db)
|
10
11
|
code, body, res = get("/v3/table/list/#{e db}")
|
11
12
|
if code != "200"
|
@@ -31,6 +32,10 @@ module Table
|
|
31
32
|
return result
|
32
33
|
end
|
33
34
|
|
35
|
+
# @param [String] db
|
36
|
+
# @param [String] table
|
37
|
+
# @param [String] type
|
38
|
+
# @return [true]
|
34
39
|
def create_log_or_item_table(db, table, type)
|
35
40
|
code, body, res = post("/v3/table/create/#{e db}/#{e table}/#{type}")
|
36
41
|
if code != "200"
|
@@ -40,17 +45,28 @@ module Table
|
|
40
45
|
end
|
41
46
|
private :create_log_or_item_table
|
42
47
|
|
43
|
-
#
|
48
|
+
# @param [String] db
|
49
|
+
# @param [String] table
|
50
|
+
# @return [true]
|
44
51
|
def create_log_table(db, table)
|
45
52
|
create_table(db, table, :log)
|
46
53
|
end
|
47
54
|
|
48
|
-
#
|
55
|
+
# @param [String] db
|
56
|
+
# @param [String] table
|
57
|
+
# @param [String] primary_key
|
58
|
+
# @param [String] primary_key_type
|
59
|
+
# @return [true]
|
49
60
|
def create_item_table(db, table, primary_key, primary_key_type)
|
50
61
|
params = {'primary_key' => primary_key, 'primary_key_type' => primary_key_type}
|
51
62
|
create_table(db, table, :item, params)
|
52
63
|
end
|
53
64
|
|
65
|
+
# @param [String] db
|
66
|
+
# @param [String] table
|
67
|
+
# @param [String] type
|
68
|
+
# @param [Hash] params
|
69
|
+
# @return [true]
|
54
70
|
def create_table(db, table, type, params={})
|
55
71
|
schema = schema.to_s
|
56
72
|
code, body, res = post("/v3/table/create/#{e db}/#{e table}/#{type}", params)
|
@@ -61,7 +77,10 @@ module Table
|
|
61
77
|
end
|
62
78
|
private :create_table
|
63
79
|
|
64
|
-
#
|
80
|
+
# @param [String] db
|
81
|
+
# @param [String] table1
|
82
|
+
# @param [String] table2
|
83
|
+
# @return [true]
|
65
84
|
def swap_table(db, table1, table2)
|
66
85
|
code, body, res = post("/v3/table/swap/#{e db}/#{e table1}/#{e table2}")
|
67
86
|
if code != "200"
|
@@ -70,7 +89,10 @@ module Table
|
|
70
89
|
return true
|
71
90
|
end
|
72
91
|
|
73
|
-
#
|
92
|
+
# @param [String] db
|
93
|
+
# @param [String] table
|
94
|
+
# @param [String] schema_json
|
95
|
+
# @return [true]
|
74
96
|
def update_schema(db, table, schema_json)
|
75
97
|
code, body, res = post("/v3/table/update-schema/#{e db}/#{e table}", {'schema'=>schema_json})
|
76
98
|
if code != "200"
|
@@ -79,6 +101,10 @@ module Table
|
|
79
101
|
return true
|
80
102
|
end
|
81
103
|
|
104
|
+
# @param [String] db
|
105
|
+
# @param [String] table
|
106
|
+
# @param [Fixnum] expire_days
|
107
|
+
# @return [true]
|
82
108
|
def update_expire(db, table, expire_days)
|
83
109
|
code, body, res = post("/v3/table/update/#{e db}/#{e table}", {'expire_days'=>expire_days})
|
84
110
|
if code != "200"
|
@@ -87,7 +113,9 @@ module Table
|
|
87
113
|
return true
|
88
114
|
end
|
89
115
|
|
90
|
-
#
|
116
|
+
# @param [String] db
|
117
|
+
# @param [String] table
|
118
|
+
# @return [Symbol]
|
91
119
|
def delete_table(db, table)
|
92
120
|
code, body, res = post("/v3/table/delete/#{e db}/#{e table}")
|
93
121
|
if code != "200"
|
@@ -98,6 +126,13 @@ module Table
|
|
98
126
|
return type
|
99
127
|
end
|
100
128
|
|
129
|
+
# @param [String] db
|
130
|
+
# @param [String] table
|
131
|
+
# @param [Fixnum] count
|
132
|
+
# @param [Fixnum] to
|
133
|
+
# @param [Fixnum] from
|
134
|
+
# @param [Proc] block
|
135
|
+
# @return [Array, nil]
|
101
136
|
def tail(db, table, count, to, from, &block)
|
102
137
|
params = {'format' => 'msgpack'}
|
103
138
|
params['count'] = count.to_s if count
|
@@ -0,0 +1,82 @@
|
|
1
|
+
class TreasureData::API
|
2
|
+
class ToHashStruct < Struct
|
3
|
+
module ClassModule
|
4
|
+
def parse_json(body)
|
5
|
+
begin
|
6
|
+
js = JSON.load(body)
|
7
|
+
rescue
|
8
|
+
raise "Unexpected API response: #{$!}"
|
9
|
+
end
|
10
|
+
unless js.is_a?(Hash)
|
11
|
+
raise "Unexpected API response: #{body}"
|
12
|
+
end
|
13
|
+
js
|
14
|
+
end
|
15
|
+
|
16
|
+
def from_json(json)
|
17
|
+
from_hash(parse_json(json))
|
18
|
+
end
|
19
|
+
|
20
|
+
def from_hash(hash)
|
21
|
+
return new if hash.nil?
|
22
|
+
new(*members.map { |sym|
|
23
|
+
v = hash[sym] || hash[sym.to_s]
|
24
|
+
model.key?(sym) ? model[sym].from_hash(v) : v
|
25
|
+
})
|
26
|
+
end
|
27
|
+
|
28
|
+
def model_property(key, klass)
|
29
|
+
model[key.to_sym] = klass
|
30
|
+
end
|
31
|
+
|
32
|
+
def model
|
33
|
+
@model ||= {}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
extend ClassModule
|
37
|
+
|
38
|
+
def to_h
|
39
|
+
self.class.members.inject({}) { |r, e|
|
40
|
+
v = obj_to_h(self[e])
|
41
|
+
r[e.to_s] = v unless v.nil?
|
42
|
+
r
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_json
|
47
|
+
to_h.to_json
|
48
|
+
end
|
49
|
+
|
50
|
+
def validate
|
51
|
+
validate_self
|
52
|
+
values.each do |v|
|
53
|
+
v.validate if v.is_a?(ToHashStruct)
|
54
|
+
end
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
58
|
+
def validate_self
|
59
|
+
# define as required
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def validate_presence_of(key)
|
65
|
+
unless self.send(key)
|
66
|
+
raise ArgumentError.new("#{key} required")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def obj_to_h(obj)
|
71
|
+
if obj.nil?
|
72
|
+
nil
|
73
|
+
elsif Array === obj
|
74
|
+
obj.map { |e| obj_to_h(e) }
|
75
|
+
elsif obj.respond_to?(:to_h)
|
76
|
+
obj.to_h
|
77
|
+
else
|
78
|
+
obj
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/lib/td/client/api/user.rb
CHANGED
@@ -5,7 +5,9 @@ module User
|
|
5
5
|
## User API
|
6
6
|
##
|
7
7
|
|
8
|
-
#
|
8
|
+
# @param [String] user
|
9
|
+
# @param [String] password
|
10
|
+
# @return [String] API key
|
9
11
|
def authenticate(user, password)
|
10
12
|
code, body, res = post("/v3/user/authenticate", {'user'=>user, 'password'=>password})
|
11
13
|
if code != "200"
|
@@ -20,7 +22,7 @@ module User
|
|
20
22
|
return apikey
|
21
23
|
end
|
22
24
|
|
23
|
-
#
|
25
|
+
# @return [Array]
|
24
26
|
def list_users
|
25
27
|
code, body, res = get("/v3/user/list")
|
26
28
|
if code != "200"
|
@@ -35,7 +37,11 @@ module User
|
|
35
37
|
return result
|
36
38
|
end
|
37
39
|
|
38
|
-
#
|
40
|
+
# @param [String] name
|
41
|
+
# @param [String] org
|
42
|
+
# @param [String] email
|
43
|
+
# @param [String] password
|
44
|
+
# @return [true]
|
39
45
|
def add_user(name, org, email, password)
|
40
46
|
params = {'organization'=>org, :email=>email, :password=>password}
|
41
47
|
code, body, res = post("/v3/user/add/#{e name}", params)
|
@@ -45,7 +51,8 @@ module User
|
|
45
51
|
return true
|
46
52
|
end
|
47
53
|
|
48
|
-
#
|
54
|
+
# @param [String] user
|
55
|
+
# @return [true]
|
49
56
|
def remove_user(user)
|
50
57
|
code, body, res = post("/v3/user/remove/#{e user}")
|
51
58
|
if code != "200"
|
@@ -54,7 +61,9 @@ module User
|
|
54
61
|
return true
|
55
62
|
end
|
56
63
|
|
57
|
-
#
|
64
|
+
# @param [String] user
|
65
|
+
# @param [String] email
|
66
|
+
# @return [true]
|
58
67
|
def change_email(user, email)
|
59
68
|
params = {'email' => email}
|
60
69
|
code, body, res = post("/v3/user/email/change/#{e user}", params)
|
@@ -64,7 +73,8 @@ module User
|
|
64
73
|
return true
|
65
74
|
end
|
66
75
|
|
67
|
-
#
|
76
|
+
# @param [String] user
|
77
|
+
# @return [Array<String>] API keys as array
|
68
78
|
def list_apikeys(user)
|
69
79
|
code, body, res = get("/v3/user/apikey/list/#{e user}")
|
70
80
|
if code != "200"
|
@@ -74,7 +84,8 @@ module User
|
|
74
84
|
return js['apikeys']
|
75
85
|
end
|
76
86
|
|
77
|
-
#
|
87
|
+
# @param [String] user
|
88
|
+
# @return [true]
|
78
89
|
def add_apikey(user)
|
79
90
|
code, body, res = post("/v3/user/apikey/add/#{e user}")
|
80
91
|
if code != "200"
|
@@ -83,7 +94,9 @@ module User
|
|
83
94
|
return true
|
84
95
|
end
|
85
96
|
|
86
|
-
#
|
97
|
+
# @param [String] user
|
98
|
+
# @param [String] apikey
|
99
|
+
# @return [true]
|
87
100
|
def remove_apikey(user, apikey)
|
88
101
|
params = {'apikey' => apikey}
|
89
102
|
code, body, res = post("/v3/user/apikey/remove/#{e user}", params)
|
@@ -93,7 +106,9 @@ module User
|
|
93
106
|
return true
|
94
107
|
end
|
95
108
|
|
96
|
-
#
|
109
|
+
# @param [String] user
|
110
|
+
# @param [String] password
|
111
|
+
# @return [true]
|
97
112
|
def change_password(user, password)
|
98
113
|
params = {'password' => password}
|
99
114
|
code, body, res = post("/v3/user/password/change/#{e user}", params)
|
@@ -103,7 +118,9 @@ module User
|
|
103
118
|
return true
|
104
119
|
end
|
105
120
|
|
106
|
-
#
|
121
|
+
# @param [String] old_password
|
122
|
+
# @param [String] password
|
123
|
+
# @return [true]
|
107
124
|
def change_my_password(old_password, password)
|
108
125
|
params = {'old_password' => old_password, 'password' => password}
|
109
126
|
code, body, res = post("/v3/user/password/change", params)
|
@@ -2,6 +2,9 @@
|
|
2
2
|
methods = Zlib::GzipReader.public_instance_methods
|
3
3
|
if !methods.include?(:readpartial) && !methods.include?('readpartial')
|
4
4
|
class Zlib::GzipReader
|
5
|
+
# @param [Fixnum] size
|
6
|
+
# @param [IO] out
|
7
|
+
# @return [String]
|
5
8
|
def readpartial(size, out=nil)
|
6
9
|
o = read(size)
|
7
10
|
if o
|
data/lib/td/client/model.rb
CHANGED
@@ -3,14 +3,24 @@ module TreasureData
|
|
3
3
|
|
4
4
|
|
5
5
|
class Model
|
6
|
+
# @param [TreasureData::Client] client
|
6
7
|
def initialize(client)
|
7
8
|
@client = client
|
8
9
|
end
|
9
10
|
|
11
|
+
# @!attribute [r] client
|
12
|
+
# @return [TreasureData::Client] client
|
10
13
|
attr_reader :client
|
11
14
|
end
|
12
15
|
|
13
16
|
class Account < Model
|
17
|
+
# @param [TreasureData::Client] client
|
18
|
+
# @param [String] account_id
|
19
|
+
# @param [Fixnum] plan
|
20
|
+
# @param [Fixnum] storage_size
|
21
|
+
# @param [Fixnum] guaranteed_cores
|
22
|
+
# @param [Fixnum] maximum_cores
|
23
|
+
# @param [String] created_at
|
14
24
|
def initialize(client, account_id, plan, storage_size=nil, guaranteed_cores=nil, maximum_cores=nil, created_at=nil)
|
15
25
|
super(client)
|
16
26
|
@account_id = account_id
|
@@ -21,12 +31,19 @@ class Account < Model
|
|
21
31
|
@created_at = created_at
|
22
32
|
end
|
23
33
|
|
34
|
+
# @!attribute [r] account_id
|
35
|
+
# @!attribute [r] plan
|
36
|
+
# @!attribute [r] storage_size
|
37
|
+
# @!attribute [r] guaranteed_cores
|
38
|
+
# @!attribute [r] maximum_cores
|
24
39
|
attr_reader :account_id, :plan, :storage_size, :guaranteed_cores, :maximum_cores
|
25
40
|
|
41
|
+
# @return <Time, nil>
|
26
42
|
def created_at
|
27
43
|
@created_at && !@created_at.empty? ? Time.parse(@created_at) : nil
|
28
44
|
end
|
29
45
|
|
46
|
+
# @return <String>
|
30
47
|
def storage_size_string
|
31
48
|
if @storage_size <= 1024 * 1024
|
32
49
|
return "0.0 GB"
|
@@ -44,6 +61,14 @@ class Database < Model
|
|
44
61
|
PERMISSIONS = [:administrator, :full_access, :import_only, :query_only]
|
45
62
|
PERMISSION_LIST_TABLES = [:administrator, :full_access]
|
46
63
|
|
64
|
+
# @param [TreasureData::Client] client
|
65
|
+
# @param [String] db_name
|
66
|
+
# @param [Array<Table>] tables
|
67
|
+
# @param [Fixnum] count
|
68
|
+
# @param [String] created_at
|
69
|
+
# @param [String] updated_at
|
70
|
+
# @param [String] org_name
|
71
|
+
# @param [String] permission
|
47
72
|
def initialize(client, db_name, tables=nil, count=nil, created_at=nil, updated_at=nil, org_name=nil, permission=nil)
|
48
73
|
super(client)
|
49
74
|
@db_name = db_name
|
@@ -54,45 +79,62 @@ class Database < Model
|
|
54
79
|
@permission = permission.to_sym
|
55
80
|
end
|
56
81
|
|
82
|
+
# @!attribute [r] org_name
|
83
|
+
# @!attribute [r] permission
|
84
|
+
# @!attribute [r] count
|
57
85
|
attr_reader :org_name, :permission, :count
|
58
86
|
|
87
|
+
# @return [String] db_name
|
59
88
|
def name
|
60
89
|
@db_name
|
61
90
|
end
|
62
91
|
|
92
|
+
# @return [Array<Table>]
|
63
93
|
def tables
|
64
94
|
update_tables! unless @tables
|
65
95
|
@tables
|
66
96
|
end
|
67
97
|
|
98
|
+
# @param [String] name
|
99
|
+
# @return [true]
|
68
100
|
def create_log_table(name)
|
69
101
|
@client.create_log_table(@db_name, name)
|
70
102
|
end
|
71
103
|
|
104
|
+
# @param [String] name
|
105
|
+
# @return [true]
|
72
106
|
def create_item_table(name)
|
73
107
|
@client.create_item_table(@db_name, name)
|
74
108
|
end
|
75
109
|
|
110
|
+
# @param [String] table_name
|
111
|
+
# @return [Table]
|
76
112
|
def table(table_name)
|
77
113
|
@client.table(@db_name, table_name)
|
78
114
|
end
|
79
115
|
|
116
|
+
# @return [Symbol]
|
80
117
|
def delete
|
81
118
|
@client.delete_database(@db_name)
|
82
119
|
end
|
83
120
|
|
121
|
+
# @param [String] q
|
122
|
+
# @return [Job]
|
84
123
|
def query(q)
|
85
124
|
@client.query(@db_name, q)
|
86
125
|
end
|
87
126
|
|
127
|
+
# @return [Time, nil]
|
88
128
|
def created_at
|
89
129
|
@created_at && !@created_at.empty? ? Time.parse(@created_at) : nil
|
90
130
|
end
|
91
131
|
|
132
|
+
# @return [Time, nil]
|
92
133
|
def updated_at
|
93
134
|
@updated_at && !@updated_at.empty? ? Time.parse(@updated_at) : nil
|
94
135
|
end
|
95
136
|
|
137
|
+
# @return [nil]
|
96
138
|
def update_tables!
|
97
139
|
@tables = @client.tables(@db_name)
|
98
140
|
# provide Table objects with a reference to the parent Database to avoid
|
@@ -105,6 +147,20 @@ class Database < Model
|
|
105
147
|
end
|
106
148
|
|
107
149
|
class Table < Model
|
150
|
+
# @param [TreasureData::Client] client
|
151
|
+
# @param [String] db_name
|
152
|
+
# @param [String] table_name
|
153
|
+
# @param [String] type
|
154
|
+
# @param [String] schema
|
155
|
+
# @param [Fixnum] count
|
156
|
+
# @param [String] created_at
|
157
|
+
# @param [String] updated_at
|
158
|
+
# @param [Fixnum] estimated_storage_size
|
159
|
+
# @param [String] last_import
|
160
|
+
# @param [String] last_log_timestamp
|
161
|
+
# @param [Fixnum, String] expire_days
|
162
|
+
# @param [String] primary_key
|
163
|
+
# @param [String] primary_key_type
|
108
164
|
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, primary_key=nil, primary_key_type=nil)
|
109
165
|
super(client)
|
110
166
|
@database = nil
|
@@ -123,65 +179,95 @@ class Table < Model
|
|
123
179
|
@primary_key_type = primary_key_type
|
124
180
|
end
|
125
181
|
|
182
|
+
# @!attribute [r] type
|
183
|
+
# @!attribute [r] db_name
|
184
|
+
# @!attribute [r] table_name
|
185
|
+
# @!attribute [r] schema
|
186
|
+
# @!attribute [r] count
|
187
|
+
# @!attribute [r] estimated_storage_size
|
188
|
+
# @!attribute [r] primary_key
|
189
|
+
# @!attribute [r] primary_key_type
|
126
190
|
attr_reader :type, :db_name, :table_name, :schema, :count, :estimated_storage_size, :primary_key, :primary_key_type
|
127
191
|
|
128
192
|
alias database_name db_name
|
129
193
|
alias name table_name
|
130
194
|
|
195
|
+
# @param [String] database
|
131
196
|
def database=(database)
|
132
197
|
@database = database if database.instance_of?(Database)
|
133
198
|
end
|
134
199
|
|
200
|
+
# @return [Time, nil]
|
135
201
|
def created_at
|
136
202
|
@created_at && !@created_at.empty? ? Time.parse(@created_at) : nil
|
137
203
|
end
|
138
204
|
|
205
|
+
# @return [Time, nil]
|
139
206
|
def updated_at
|
140
207
|
@updated_at && !@updated_at.empty? ? Time.parse(@updated_at) : nil
|
141
208
|
end
|
142
209
|
|
210
|
+
# @return [Time, nil]
|
143
211
|
def last_import
|
144
212
|
@last_import && !@last_import.empty? ? Time.parse(@last_import) : nil
|
145
213
|
end
|
146
214
|
|
215
|
+
# @return [Time, nil]
|
147
216
|
def last_log_timestamp
|
148
217
|
@last_log_timestamp && !@last_log_timestamp.empty? ? Time.parse(@last_log_timestamp) : nil
|
149
218
|
end
|
150
219
|
|
220
|
+
# @return [Fixnum, nil]
|
151
221
|
def expire_days
|
152
222
|
@expire_days ? @expire_days.to_i : nil
|
153
223
|
end
|
154
224
|
|
225
|
+
# @return [Database]
|
155
226
|
def database
|
156
227
|
update_database! unless @database
|
157
228
|
@database
|
158
229
|
end
|
159
230
|
|
160
231
|
# get the database's permission as if they were the table's
|
232
|
+
# @return [String]
|
161
233
|
def permission
|
162
234
|
database.permission
|
163
235
|
end
|
164
236
|
|
237
|
+
# @return [String]
|
165
238
|
def identifier
|
166
239
|
"#{@db_name}.#{@table_name}"
|
167
240
|
end
|
168
241
|
|
242
|
+
# @return [Symbol]
|
169
243
|
def delete
|
170
244
|
@client.delete_table(@db_name, @table_name)
|
171
245
|
end
|
172
246
|
|
247
|
+
# @param [Fixnum] count
|
248
|
+
# @param [Fixnum] to
|
249
|
+
# @param [Fixnum] from
|
250
|
+
# @return [Array, nil]
|
173
251
|
def tail(count, to=nil, from=nil)
|
174
252
|
@client.tail(@db_name, @table_name, count, to, from)
|
175
253
|
end
|
176
254
|
|
255
|
+
# @param [String] format
|
256
|
+
# @param [String, StringIO] stream
|
257
|
+
# @param [Fixnum] size
|
258
|
+
# @return [Float]
|
177
259
|
def import(format, stream, size)
|
178
260
|
@client.import(@db_name, @table_name, format, stream, size)
|
179
261
|
end
|
180
262
|
|
263
|
+
# @param [String] storage_type
|
264
|
+
# @param [Hash] opts
|
265
|
+
# @return [Job]
|
181
266
|
def export(storage_type, opts={})
|
182
267
|
@client.export(@db_name, @table_name, storage_type, opts)
|
183
268
|
end
|
184
269
|
|
270
|
+
# @return [String]
|
185
271
|
def estimated_storage_size_string
|
186
272
|
if @estimated_storage_size <= 1024*1024
|
187
273
|
return "0.0 GB"
|
@@ -201,14 +287,21 @@ end
|
|
201
287
|
|
202
288
|
class Schema
|
203
289
|
class Field
|
290
|
+
# @param [String] name
|
291
|
+
# @param [String] type
|
204
292
|
def initialize(name, type)
|
205
293
|
@name = name
|
206
294
|
@type = type
|
207
295
|
end
|
296
|
+
|
297
|
+
# @!attribute [r] name
|
298
|
+
# @!attribute [r] type
|
208
299
|
attr_reader :name
|
209
300
|
attr_reader :type
|
210
301
|
end
|
211
302
|
|
303
|
+
# @param [String] cols
|
304
|
+
# @return [Schema]
|
212
305
|
def self.parse(cols)
|
213
306
|
fields = cols.split(',').map {|col|
|
214
307
|
name, type, *_ = col.split(':')
|
@@ -217,16 +310,23 @@ class Schema
|
|
217
310
|
Schema.new(fields)
|
218
311
|
end
|
219
312
|
|
313
|
+
# @param [Array] fields
|
220
314
|
def initialize(fields=[])
|
221
315
|
@fields = fields
|
222
316
|
end
|
223
317
|
|
318
|
+
# @!attribute [r] fields
|
224
319
|
attr_reader :fields
|
225
320
|
|
321
|
+
# @param [String] name
|
322
|
+
# @param [String] type
|
323
|
+
# @return [Array]
|
226
324
|
def add_field(name, type)
|
227
325
|
@fields << Field.new(name, type)
|
228
326
|
end
|
229
327
|
|
328
|
+
# @param [Schema] schema
|
329
|
+
# @return [Schema]
|
230
330
|
def merge(schema)
|
231
331
|
nf = @fields.dup
|
232
332
|
schema.fields.each {|f|
|
@@ -239,10 +339,13 @@ class Schema
|
|
239
339
|
Schema.new(nf)
|
240
340
|
end
|
241
341
|
|
342
|
+
# @return [Array<Field>]
|
242
343
|
def to_json(*args)
|
243
344
|
@fields.map {|f| [f.name, f.type] }.to_json(*args)
|
244
345
|
end
|
245
346
|
|
347
|
+
# @param [Object] obj
|
348
|
+
# @return [self]
|
246
349
|
def from_json(obj)
|
247
350
|
@fields = obj.map {|f|
|
248
351
|
Field.new(f[0], f[1])
|
@@ -260,6 +363,24 @@ class Job < Model
|
|
260
363
|
STATUS_KILLED = "killed"
|
261
364
|
FINISHED_STATUS = [STATUS_SUCCESS, STATUS_ERROR, STATUS_KILLED]
|
262
365
|
|
366
|
+
# @param [TreasureData::Client] client
|
367
|
+
# @param [String] job_id
|
368
|
+
# @param [String] type
|
369
|
+
# @param [String] query
|
370
|
+
# @param [Fixnum] status
|
371
|
+
# @param [String] url
|
372
|
+
# @param [Boolean] debug
|
373
|
+
# @param [String] start_at
|
374
|
+
# @param [String] end_at
|
375
|
+
# @param [String] cpu_time
|
376
|
+
# @param [String] result_size
|
377
|
+
# @param [Array] result
|
378
|
+
# @param [String] result_url
|
379
|
+
# @param [Array] hive_result_schema
|
380
|
+
# @param [Fixnum] priority
|
381
|
+
# @param [Fixnum] retry_limit
|
382
|
+
# @param [String] org_name
|
383
|
+
# @param [String] db_name
|
263
384
|
def initialize(client, job_id, type, query, status=nil, url=nil, debug=nil, start_at=nil, end_at=nil, cpu_time=nil,
|
264
385
|
result_size=nil, result=nil, result_url=nil, hive_result_schema=nil, priority=nil, retry_limit=nil,
|
265
386
|
org_name=nil, db_name=nil)
|
@@ -282,6 +403,13 @@ class Job < Model
|
|
282
403
|
@db_name = db_name
|
283
404
|
end
|
284
405
|
|
406
|
+
# @!attribute [r] job_id
|
407
|
+
# @!attribute [r] type
|
408
|
+
# @!attribute [r] result_url
|
409
|
+
# @!attribute [r] priority
|
410
|
+
# @!attribute [r] retry_limit
|
411
|
+
# @!attribute [r] org_name
|
412
|
+
# @!attribute [r] db_name
|
285
413
|
attr_reader :job_id, :type, :result_url
|
286
414
|
attr_reader :priority, :retry_limit, :org_name, :db_name
|
287
415
|
|
@@ -293,51 +421,61 @@ class Job < Model
|
|
293
421
|
# TODO
|
294
422
|
end
|
295
423
|
|
424
|
+
# @return [String]
|
296
425
|
def query
|
297
426
|
update_status! unless @query || finished?
|
298
427
|
@query
|
299
428
|
end
|
300
429
|
|
430
|
+
# @return [String]
|
301
431
|
def status
|
302
432
|
update_status! unless @status || finished?
|
303
433
|
@status
|
304
434
|
end
|
305
435
|
|
436
|
+
# @return [String]
|
306
437
|
def url
|
307
438
|
update_status! unless @url || finished?
|
308
439
|
@url
|
309
440
|
end
|
310
441
|
|
442
|
+
# @return [Boolean]
|
311
443
|
def debug
|
312
444
|
update_status! unless @debug || finished?
|
313
445
|
@debug
|
314
446
|
end
|
315
447
|
|
448
|
+
# @return [Time, nil]
|
316
449
|
def start_at
|
317
450
|
update_status! unless @start_at || finished?
|
318
451
|
@start_at && !@start_at.empty? ? Time.parse(@start_at) : nil
|
319
452
|
end
|
320
453
|
|
454
|
+
# @return [Time, nil]
|
321
455
|
def end_at
|
322
456
|
update_status! unless @end_at || finished?
|
323
457
|
@end_at && !@end_at.empty? ? Time.parse(@end_at) : nil
|
324
458
|
end
|
325
459
|
|
460
|
+
# @return [String]
|
326
461
|
def cpu_time
|
327
462
|
update_status! unless @cpu_time || finished?
|
328
463
|
@cpu_time
|
329
464
|
end
|
330
465
|
|
466
|
+
# @return [Array]
|
331
467
|
def hive_result_schema
|
332
468
|
update_status! unless @hive_result_schema.instance_of? Array || finished?
|
333
469
|
@hive_result_schema
|
334
470
|
end
|
335
471
|
|
472
|
+
# @return [String]
|
336
473
|
def result_size
|
337
474
|
update_status! unless @result_size || finished?
|
338
475
|
@result_size
|
339
476
|
end
|
340
477
|
|
478
|
+
# @return [Array]
|
341
479
|
def result
|
342
480
|
unless @result
|
343
481
|
return nil unless finished?
|
@@ -346,11 +484,17 @@ class Job < Model
|
|
346
484
|
@result
|
347
485
|
end
|
348
486
|
|
487
|
+
# @param [String] format
|
488
|
+
# @param [IO] io
|
489
|
+
# @param [Proc] block
|
490
|
+
# @return [nil, String]
|
349
491
|
def result_format(format, io=nil, &block)
|
350
492
|
return nil unless finished?
|
351
493
|
@client.job_result_format(@job_id, format, io, &block)
|
352
494
|
end
|
353
495
|
|
496
|
+
# @yield [result]
|
497
|
+
# @return [nil]
|
354
498
|
def result_each_with_compr_size(&block)
|
355
499
|
if @result
|
356
500
|
@result.each(&block)
|
@@ -360,6 +504,8 @@ class Job < Model
|
|
360
504
|
nil
|
361
505
|
end
|
362
506
|
|
507
|
+
# @yield [result]
|
508
|
+
# @return [nil]
|
363
509
|
def result_each(&block)
|
364
510
|
if @result
|
365
511
|
@result.each(&block)
|
@@ -369,31 +515,37 @@ class Job < Model
|
|
369
515
|
nil
|
370
516
|
end
|
371
517
|
|
518
|
+
# @return [Boolean]
|
372
519
|
def finished?
|
373
520
|
update_progress! unless @status
|
374
521
|
FINISHED_STATUS.include?(@status)
|
375
522
|
end
|
376
523
|
|
524
|
+
# @return [Boolean]
|
377
525
|
def success?
|
378
526
|
update_progress! unless @status
|
379
527
|
@status == STATUS_SUCCESS
|
380
528
|
end
|
381
529
|
|
530
|
+
# @return [Boolean]
|
382
531
|
def error?
|
383
532
|
update_progress! unless @status
|
384
533
|
@status == STATUS_ERROR
|
385
534
|
end
|
386
535
|
|
536
|
+
# @return [Boolean]
|
387
537
|
def killed?
|
388
538
|
update_progress! unless @status
|
389
539
|
@status == STATUS_KILLED
|
390
540
|
end
|
391
541
|
|
542
|
+
# @return [Boolean]
|
392
543
|
def queued?
|
393
544
|
update_progress! unless @status
|
394
545
|
@status == STATUS_QUEUED
|
395
546
|
end
|
396
547
|
|
548
|
+
# @return [Boolean]
|
397
549
|
def running?
|
398
550
|
update_progress! unless @status
|
399
551
|
@status == STATUS_RUNNING
|
@@ -428,6 +580,9 @@ end
|
|
428
580
|
class ScheduledJob < Job
|
429
581
|
attr_reader :scheduled_at
|
430
582
|
|
583
|
+
# @param [TreasureData::Client] client
|
584
|
+
# @param [String] scheduled_at
|
585
|
+
# @param [...] args for Job#initialize
|
431
586
|
def initialize(client, scheduled_at, *super_args)
|
432
587
|
super(client, *super_args)
|
433
588
|
if scheduled_at.to_s.empty?
|
@@ -440,6 +595,18 @@ end
|
|
440
595
|
|
441
596
|
|
442
597
|
class Schedule < Model
|
598
|
+
# @param [TreasureData::Client] client
|
599
|
+
# @param [String] name
|
600
|
+
# @param [String] cron
|
601
|
+
# @param [String] query
|
602
|
+
# @param [Fixnum] database
|
603
|
+
# @param [String] result_url
|
604
|
+
# @param [String] timezone
|
605
|
+
# @param [String] delay
|
606
|
+
# @param [String] next_time
|
607
|
+
# @param [String] priority
|
608
|
+
# @param [String] retry_limit
|
609
|
+
# @param [String] org_name
|
443
610
|
def initialize(client, name, cron, query, database=nil, result_url=nil, timezone=nil, delay=nil, next_time=nil,
|
444
611
|
priority=nil, retry_limit=nil, org_name=nil)
|
445
612
|
super(client)
|
@@ -455,12 +622,25 @@ class Schedule < Model
|
|
455
622
|
@retry_limit = retry_limit
|
456
623
|
end
|
457
624
|
|
625
|
+
# @!attribute [r] name
|
626
|
+
# @!attribute [r] cron
|
627
|
+
# @!attribute [r] query
|
628
|
+
# @!attribute [r] database
|
629
|
+
# @!attribute [r] result_url
|
630
|
+
# @!attribute [r] delay
|
631
|
+
# @!attribute [r] priority
|
632
|
+
# @!attribute [r] retry_limit
|
633
|
+
# @!attribute [r] org_name
|
458
634
|
attr_reader :name, :cron, :query, :database, :result_url, :timezone, :delay, :priority, :retry_limit, :org_name
|
459
635
|
|
636
|
+
# @return [Time, nil]
|
460
637
|
def next_time
|
461
638
|
@next_time ? Time.parse(@next_time) : nil
|
462
639
|
end
|
463
640
|
|
641
|
+
# @param [String] time
|
642
|
+
# @param [Fixnum] num
|
643
|
+
# @return [Array]
|
464
644
|
def run(time, num)
|
465
645
|
@client.run_schedule(time, num)
|
466
646
|
end
|
@@ -468,17 +648,26 @@ end
|
|
468
648
|
|
469
649
|
|
470
650
|
class Result < Model
|
651
|
+
# @param [TreasureData::Client] client
|
652
|
+
# @param [String] name
|
653
|
+
# @param [String] url
|
654
|
+
# @param [String] org_name
|
471
655
|
def initialize(client, name, url, org_name)
|
472
656
|
super(client)
|
473
657
|
@name = name
|
474
658
|
@url = url
|
475
659
|
end
|
476
660
|
|
661
|
+
# @!attribute [r] name
|
662
|
+
# @!attribute [r] url
|
663
|
+
# @!attribute [r] org_name
|
477
664
|
attr_reader :name, :url, :org_name
|
478
665
|
end
|
479
666
|
|
480
667
|
|
481
668
|
class BulkImport < Model
|
669
|
+
# @param [TreasureData::Client] client
|
670
|
+
# @param [Hash] data
|
482
671
|
def initialize(client, data={})
|
483
672
|
super(client)
|
484
673
|
@name = data['name']
|
@@ -493,6 +682,16 @@ class BulkImport < Model
|
|
493
682
|
@error_parts = data['error_parts']
|
494
683
|
end
|
495
684
|
|
685
|
+
# @!attribute [r] name
|
686
|
+
# @!attribute [r] database
|
687
|
+
# @!attribute [r] table
|
688
|
+
# @!attribute [r] status
|
689
|
+
# @!attribute [r] job_id
|
690
|
+
# @!attribute [r] valid_records
|
691
|
+
# @!attribute [r] error_records
|
692
|
+
# @!attribute [r] valid_parts
|
693
|
+
# @!attribute [r] error_parts
|
694
|
+
# @!attribute [r] org_name
|
496
695
|
attr_reader :name
|
497
696
|
attr_reader :database
|
498
697
|
attr_reader :table
|
@@ -504,6 +703,7 @@ class BulkImport < Model
|
|
504
703
|
attr_reader :error_parts
|
505
704
|
attr_reader :org_name
|
506
705
|
|
706
|
+
# @return [Boolean]
|
507
707
|
def upload_frozen?
|
508
708
|
@upload_frozen
|
509
709
|
end
|
@@ -522,6 +722,11 @@ end
|
|
522
722
|
|
523
723
|
|
524
724
|
class AccessControl < Model
|
725
|
+
# @param [TreasureData::Client] client
|
726
|
+
# @param [String] subject
|
727
|
+
# @param [String] action
|
728
|
+
# @param [String] scope
|
729
|
+
# @param [Array] grant_option
|
525
730
|
def initialize(client, subject, action, scope, grant_option)
|
526
731
|
super(client)
|
527
732
|
@subject = subject
|
@@ -530,6 +735,10 @@ class AccessControl < Model
|
|
530
735
|
@grant_option = grant_option
|
531
736
|
end
|
532
737
|
|
738
|
+
# @!attribute [r] subject
|
739
|
+
# @!attribute [r] action
|
740
|
+
# @!attribute [r] scope
|
741
|
+
# @!attribute [r] grant_option
|
533
742
|
attr_reader :subject, :action, :scope, :grant_option
|
534
743
|
end
|
535
744
|
|