forked from fhirbase/fhirbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulk.go
372 lines (278 loc) · 8.36 KB
/
bulk.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
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
urlPkg "net/url"
"os"
"path"
"regexp"
"strconv"
"strings"
"sync"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/vbauerster/mpb"
"github.com/vbauerster/mpb/decor"
)
func UnknownTotalCounter(unit int, pairFormat string, wcc ...decor.WC) decor.Decorator {
var wc decor.WC
for _, widthConf := range wcc {
wc = widthConf
}
wc.Init()
d := &unknownTotalDecorator{
WC: wc,
unit: unit,
pairFormat: pairFormat,
}
return d
}
type unknownTotalDecorator struct {
WC decor.WC
unit int
pairFormat string
completeMsg *string
}
func (d *unknownTotalDecorator) Decor(st *decor.Statistics) string {
if st.Completed && d.completeMsg != nil {
return d.WC.FormatMsg(*d.completeMsg)
}
var str string
switch d.unit {
case decor.UnitKiB:
str = fmt.Sprintf(d.pairFormat, decor.CounterKiB(st.Current))
case decor.UnitKB:
str = fmt.Sprintf(d.pairFormat, decor.CounterKB(st.Current))
default:
str = fmt.Sprintf(d.pairFormat, st.Current)
}
return d.WC.FormatMsg(str)
}
func (d *unknownTotalDecorator) Syncable() (bool, chan int) {
return d.WC.Syncable()
}
var matchNonDigits, _ = regexp.Compile("[^\\d]")
func getBulkDataFiles(pingURL string) ([]string, error) {
fmt.Println("Waiting for Bulk Data API server to prepare files...")
client := &http.Client{}
req, err := http.NewRequest("GET", pingURL, nil)
req.Header.Add("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "error while pinging Bulk Data API server")
}
for i := 1; resp.StatusCode != 200; i++ {
// progress := int64(-1)
// xprogress := matchNonDigits.ReplaceAllString(resp.Header.Get("X-Progress"), "")
// if len(xprogress) > 0 {
// progress, err = strconv.ParseInt(xprogress, 10, 64)
// if err != nil {
// progress = -1
// }
// }
if resp.StatusCode > 299 || resp.StatusCode < 200 {
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("got %d response while pinging Bulk Data API server; cannot read response body", resp.StatusCode)
}
return nil, fmt.Errorf("got %d response while pinging Bulk Data API server. Response Body:\n%s", resp.StatusCode, respBody)
}
if i%5 == 0 {
fmt.Println("still waiting...")
}
resp.Body.Close()
time.Sleep(1000 * time.Millisecond)
req, err = http.NewRequest("GET", pingURL, nil)
req.Header.Add("Accept", "application/json")
resp, err = client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "error while pinging Bulk Data API server")
}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "error while reading response")
}
iter := jsoniter.ConfigDefault.BorrowIterator(body)
defer jsoniter.ConfigDefault.ReturnIterator(iter)
obj := iter.Read()
if obj == nil {
return nil, errors.Wrap(iter.Error, "cannot parse server response")
}
fileURLs := make([]string, 0)
objMap, ok := obj.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("expecting JSON object at the top level")
}
output := objMap["output"]
if output == nil {
return nil, fmt.Errorf("expecting to have 'output' attribute")
}
outputArr, ok := output.([]interface{})
if !ok {
return nil, fmt.Errorf("'output' attribute is not an JSON Array")
}
for _, v := range outputArr {
item, ok := v.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("got non-object in 'output' array")
}
url := item["url"]
if url == nil {
return nil, fmt.Errorf("cannot get 'url' attribute in item of 'output' array")
}
urlString, ok := url.(string)
if !ok {
return nil, fmt.Errorf("'url' attribute is not a string")
}
fileURLs = append(fileURLs, urlString)
}
return fileURLs, nil
}
func stripURL(url string, length int) string {
if len(url) < length {
return strings.Repeat(" ", length-len(url)) + url
}
return "..." + url[len(url)-length-3:len(url)]
}
func startDlWorker(n uint, bars *mpb.Progress, jobs chan string, results chan interface{}, wg *sync.WaitGroup) {
wg.Add(1)
go func() {
defer wg.Done()
client := &http.Client{}
for url := range jobs {
parsedURL, err := urlPkg.Parse(url)
fileName := path.Base(parsedURL.EscapedPath())
if err != nil {
results <- errors.Wrap(err, "cannot parse URL")
continue
}
targetFile, err := ioutil.TempFile("", fileName)
if err != nil {
results <- errors.Wrap(err, "cannot create temp file")
continue
}
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Accept-Encoding", "gzip")
resp, err := client.Do(req)
if err != nil {
results <- errors.Wrap(err, "cannot perform HTTP request")
continue
}
if resp.StatusCode != 200 {
results <- fmt.Errorf("got non-200 response while downloading %s", url)
}
contentLengthHeader := resp.Header.Get("Content-Length")
size, err := strconv.ParseInt(contentLengthHeader, 10, 64)
counterDecorator := decor.CountersKibiByte("%6.1f / %6.1f", decor.WCSyncWidth)
if err != nil {
size = 0
counterDecorator = UnknownTotalCounter(decor.UnitKiB, "%6.1f / ???", decor.WCSyncWidth)
}
name := stripURL(url, 25)
bar := bars.AddBar(size, mpb.BarPriority(int(n)),
mpb.BarRemoveOnComplete(),
mpb.PrependDecorators(
decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}),
counterDecorator,
),
mpb.AppendDecorators(
decor.EwmaETA(decor.ET_STYLE_MMSS, 1024*4, decor.WCSyncWidth),
decor.AverageSpeed(decor.UnitKiB, " % .2f"),
),
)
reader := bar.ProxyReader(resp.Body)
totalWritten, err := io.Copy(targetFile, reader)
if err != nil {
results <- errors.Wrap(err, "cannot copy HTTP response body to temporary file")
}
bar.SetTotal(totalWritten, true)
results <- targetFile
}
}()
}
func downloadFiles(fileURLs []string, numWorkers uint) ([]*os.File, error) {
doneWg := new(sync.WaitGroup)
bars := mpb.New(mpb.WithWidth(64), mpb.WithWaitGroup(doneWg))
jobs := make(chan string, len(fileURLs))
results := make(chan interface{}, len(fileURLs))
files := make([]*os.File, 0)
fmt.Printf("Start downloading %d files in %d threads\n", len(fileURLs), numWorkers)
for _, url := range fileURLs {
jobs <- url
}
close(jobs)
for i := uint(0); i < numWorkers; i++ {
startDlWorker(i, bars, jobs, results, doneWg)
}
bars.Wait()
close(results)
for res := range results {
err, ok := res.(error)
if ok {
fmt.Printf("Got an error while downloading file: %s", err.Error())
} else {
f, ok := res.(*os.File)
if ok {
files = append(files, f)
} else {
fmt.Printf("got result of unknown type: %v", res)
}
}
}
fmt.Printf("Finished downloading, got %d files\n", len(files))
return files, nil
}
func getBulkData(url string, numWorkers uint, acceptHdr string) ([]*os.File, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Prefer", "respond-async")
req.Header.Add("Accept", acceptHdr)
resp, err := client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "cannot perform HTTP query")
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
respBody, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("expected 20x response, got %d; response body is: %s", resp.StatusCode, respBody)
}
pingURL := resp.Header.Get("Content-Location")
if len(pingURL) == 0 {
return nil, fmt.Errorf("No Content-Location header was returned by Bulk Data API server")
}
fileURLs, err := getBulkDataFiles(pingURL)
if err != nil {
return nil, errors.Wrap(err, "Cannot get list of files to download")
}
return downloadFiles(fileURLs, numWorkers)
}
// BulkGetCommand loads data from Bulk Data Endpoint and saves it to local filesystem
func BulkGetCommand(c *cli.Context) error {
if c.NArg() < 2 {
cli.ShowCommandHelpAndExit(c, "bulkget", 1)
return nil
}
numWorkers := c.Uint("numdl")
acceptHdr := c.String("accept-header")
bulkURL := c.Args().Get(0)
destPath := c.Args().Get(1)
fileHndlrs, err := getBulkData(bulkURL, numWorkers, acceptHdr)
if err != nil {
return err
}
for _, f := range fileHndlrs {
fn := f.Name()
fbn := path.Base(fn)
err := os.Rename(f.Name(), path.Join(destPath, fbn))
if err != nil {
fmt.Printf("Error moving %s to %s: %v", f.Name(), path.Join(destPath, fbn), err)
}
}
return nil
}