-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathapi_schedule.go
415 lines (391 loc) · 12.4 KB
/
api_schedule.go
1
2
3
4
5
6
7
8
9
10
11
12
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//
// Treasure Data API client for Go
//
// Copyright (C) 2014 Treasure Data, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package td_client
import (
"fmt"
"net/url"
"strconv"
"time"
)
type ScheduleElement struct {
Id int
ExecutingUserId int
Description string
Name string
Cron string
Type string
Query string
Timezone string
Delay int
Database string
UserName string
Priority int
RetryLimit int
Result string
NextTime string
CreatedAt time.Time
}
type ListScheduleResult []ScheduleElement
var listScheduleSchema = map[string]interface{}{
"schedules": []map[string]interface{}{
{
"id": 0,
"executing_user_id": 0,
"description": Optional{"", nil},
"name": Optional{"", nil},
"cron": Optional{"", "?"},
"timezone": "",
"delay": 0,
"created_at": time.Time{},
"type": "",
"query": Optional{"", nil},
"database": Optional{"", "?"},
"user_name": Optional{"", nil},
"priority": 0,
"retry_limit": 0,
"result": Optional{"", "?"},
"next_time": Optional{"", "?"},
},
},
}
var scheduleResultSchema = map[string]interface{}{
"id": 0,
"name": Optional{"", nil},
"cron": Optional{"", "?"},
"timezone": "",
"delay": 0,
"created_at": time.Time{},
"type": "",
"query": Optional{"", nil},
"database": Optional{"", "?"},
"user_name": Optional{"", nil},
"priority": 0,
"retry_limit": 0,
"result": Optional{"", "?"},
"start": Optional{"", "?"},
}
type ScheduleResult struct {
ID string
Name string
Cron string
Type string
Query string
Timezone string
Delay int
Database string
UserName string
Priority int
RetryLimit int
Result string
Start string
CreatedAt time.Time
}
type DeleteScheduleResult struct {
Name string
Cron string
Type string
Query string
Timezone string
Delay int
Database string
UserName string
CreatedAt time.Time
}
var deleteScheduleSchema = map[string]interface{}{
"name": "",
"cron": Optional{"", "?"},
"timezone": "",
"delay": 0,
"created_at": time.Time{},
"type": "",
"query": "",
"database": "",
"user_name": "",
}
type RunScheduleResultList []RunScheduleResult
type RunScheduleResult struct {
ID string
Type string
ScheduledAt time.Time
}
var runScheduleResultSchema = map[string]interface{}{
"jobs": []map[string]interface{}{
{
"job_id": int64(0),
"scheduled_at": Optional{time.Time{}, time.Time{}},
"type": "",
},
},
}
type ScheduleHistoryElement struct {
ID string
Query string
Type string
URL string
Database string
Status string
StartAt time.Time
EndAt time.Time
ScheduledAt time.Time
CreatedAt time.Time
UpdatedAt time.Time
UserName string
CPUTime float64
Duration float64
ResultSize int
NumRecords int
Result string
Priority int
RetryLimit int
HiveResultSchema []interface{}
Organization string
}
type ScheduleHistoryElementList []ScheduleHistoryElement
type ScheduleHistoryList struct {
History ScheduleHistoryElementList
Count int
From int
To int
}
var scheduleHistorySchema = map[string]interface{}{
"history": []map[string]interface{}{
{
"job_id": "",
"type": Optional{"", "?"},
"database": "",
"status": "",
"query": "",
"start_at": Optional{time.Time{}, time.Time{}},
"end_at": Optional{time.Time{}, time.Time{}},
"scheduled_at": Optional{time.Time{}, time.Time{}},
"created_at": time.Time{},
"updated_at": time.Time{},
"duration": Optional{0., 0.},
"cpu_time": Optional{0., 0.},
"result_size": Optional{0, 0},
"num_records": Optional{0, 0},
"user_name": "",
"result": Optional{"", "?"},
"url": "",
"hive_result_schema": Optional{EmbeddedJSON([]interface{}{}), nil},
"organization": Optional{"", "?"},
"priority": 0,
"retry_limit": 0,
},
},
"count": Optional{0, 0},
"to": Optional{0, 0},
"from": Optional{0, 0},
}
func (client *TDClient) ListSchedules() (*ListScheduleResult, error) {
resp, err := client.get("/v3/schedule/list", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, client.buildError(resp, -1, "List schedules failed", nil)
}
js, err := client.checkedJson(resp, listScheduleSchema)
if err != nil {
return nil, err
}
schedules := js["schedules"].([]map[string]interface{})
listScheduleResult := make(ListScheduleResult, len(schedules))
for i, v := range schedules {
listScheduleResult[i] = ScheduleElement{
Id: v["id"].(int),
ExecutingUserId: v["executing_user_id"].(int),
Description: v["description"].(string),
Name: v["name"].(string),
Cron: v["cron"].(string),
Type: v["type"].(string),
Query: v["query"].(string),
Timezone: v["timezone"].(string),
Delay: v["delay"].(int),
Database: v["database"].(string),
UserName: v["user_name"].(string),
Priority: v["priority"].(int),
RetryLimit: v["retry_limit"].(int),
Result: v["result"].(string),
NextTime: v["next_time"].(string),
CreatedAt: v["created_at"].(time.Time),
}
}
return &listScheduleResult, nil
}
func (client *TDClient) CreateSchedule(scheduleName string, options map[string]string) (*ScheduleResult, error) {
resp, err := client.post(fmt.Sprintf("/v3/schedule/create/%s", url.QueryEscape(scheduleName)), dictToValues(options))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, client.buildError(resp, -1, "Failed for create schedule", nil)
}
schedule, err := client.checkedJson(resp, scheduleResultSchema)
if err != nil {
return nil, err
}
createScheduleResult := ScheduleResult{
ID: strconv.Itoa(schedule["id"].(int)),
Name: schedule["name"].(string),
Cron: schedule["cron"].(string),
Type: schedule["type"].(string),
Query: schedule["query"].(string),
Timezone: schedule["timezone"].(string),
Delay: schedule["delay"].(int),
Database: schedule["database"].(string),
UserName: schedule["user_name"].(string),
Priority: schedule["priority"].(int),
RetryLimit: schedule["retry_limit"].(int),
Result: schedule["result"].(string),
Start: schedule["start"].(string),
CreatedAt: schedule["created_at"].(time.Time),
}
return &createScheduleResult, nil
}
func (client *TDClient) DeleteSchedule(scheduleName string) (*DeleteScheduleResult, error) {
resp, err := client.post(fmt.Sprintf("/v3/schedule/delete/%s", url.QueryEscape(scheduleName)), nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, client.buildError(resp, -1, "Failed for delete schedule", nil)
}
schedule, err := client.checkedJson(resp, deleteScheduleSchema)
if err != nil {
return nil, err
}
deleteScheduleResult := DeleteScheduleResult{
Name: schedule["name"].(string),
Cron: schedule["cron"].(string),
Type: schedule["type"].(string),
Query: schedule["query"].(string),
Timezone: schedule["timezone"].(string),
Delay: schedule["delay"].(int),
Database: schedule["database"].(string),
UserName: schedule["user_name"].(string),
CreatedAt: schedule["created_at"].(time.Time),
}
return &deleteScheduleResult, nil
}
func (client *TDClient) UpdateSchedule(scheduleName string, options map[string]string) (*ScheduleResult, error) {
resp, err := client.post(fmt.Sprintf("/v3/schedule/update/%s", url.QueryEscape(scheduleName)), dictToValues(options))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, client.buildError(resp, -1, "Failed for update schedule", nil)
}
schedule, err := client.checkedJson(resp, scheduleResultSchema)
if err != nil {
return nil, err
}
updateScheduleResult := ScheduleResult{
ID: strconv.Itoa(schedule["id"].(int)),
Name: schedule["name"].(string),
Cron: schedule["cron"].(string),
Type: schedule["type"].(string),
Query: schedule["query"].(string),
Timezone: schedule["timezone"].(string),
Delay: schedule["delay"].(int),
Database: schedule["database"].(string),
UserName: schedule["user_name"].(string),
Priority: schedule["priority"].(int),
RetryLimit: schedule["retry_limit"].(int),
Result: schedule["result"].(string),
Start: schedule["start"].(string),
CreatedAt: schedule["created_at"].(time.Time),
}
return &updateScheduleResult, nil
}
func (client *TDClient) RunSchedule(scheduleName string, runTime string, options map[string]string) (*RunScheduleResultList, error) {
resp, err := client.post(fmt.Sprintf("/v3/schedule/run/%s/%s", url.QueryEscape(scheduleName), url.QueryEscape(runTime)), dictToValues(options))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, client.buildError(resp, -1, "Failed for run schedule", nil)
}
js, err := client.checkedJson(resp, runScheduleResultSchema)
if err != nil {
return nil, err
}
jobs := js["jobs"].([]map[string]interface{})
runResultList := make(RunScheduleResultList, len(jobs))
for i, v := range jobs {
runResultList[i] = RunScheduleResult{
ID: strconv.FormatInt(v["job_id"].(int64), 10),
Type: v["type"].(string),
ScheduledAt: v["scheduled_at"].(time.Time),
}
}
return &runResultList, nil
}
func (client *TDClient) ScheduleHistory(scheduleName string, options map[string]string) (*ScheduleHistoryList, error) {
resp, err := client.get(fmt.Sprintf("/v3/schedule/history/%s", scheduleName), dictToValues(options))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, client.buildError(resp, -1, "Failed for get schedule history", nil)
}
js, err := client.checkedJson(resp, scheduleHistorySchema)
if err != nil {
return nil, err
}
scheduleHistoryList := ScheduleHistoryList{}
history := js["history"].([]map[string]interface{})
scheduleHistory := make(ScheduleHistoryElementList, len(history))
for i, v := range history {
hiveResultSchema, _ := v["hive_result_schema"].([]interface{})
scheduleHistory[i] = ScheduleHistoryElement{
ID: v["job_id"].(string),
Query: v["query"].(string),
Type: v["type"].(string),
Status: v["status"].(string),
ScheduledAt: v["scheduled_at"].(time.Time),
CreatedAt: v["created_at"].(time.Time),
UpdatedAt: v["updated_at"].(time.Time),
StartAt: v["start_at"].(time.Time),
EndAt: v["end_at"].(time.Time),
Duration: v["duration"].(float64),
CPUTime: v["cpu_time"].(float64),
ResultSize: v["result_size"].(int),
NumRecords: v["num_records"].(int),
Result: v["result"].(string),
Priority: v["priority"].(int),
RetryLimit: v["retry_limit"].(int),
HiveResultSchema: hiveResultSchema,
UserName: v["user_name"].(string),
URL: v["url"].(string),
Database: v["database"].(string),
Organization: v["organization"].(string),
}
}
scheduleHistoryList.History = scheduleHistory
scheduleHistoryList.From = js["from"].(int)
scheduleHistoryList.To = js["to"].(int)
scheduleHistoryList.Count = js["count"].(int)
return &scheduleHistoryList, nil
}