/
datahub.js
330 lines (293 loc) · 9.22 KB
/
datahub.js
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
const EventEmitter = require('events')
const fetch = require('node-fetch')
const FormData = require('form-data')
const lodash = require('lodash')
const {File, xlsxParser} = require('data.js')
const XLSX = require('xlsx')
const toArray = require('stream-to-array')
const infer = require('tableschema').infer
const YAML = require('yamljs')
const {Agent} = require('./agent')
// TODO
// debug logging - and use to output what we are sending to APIs
// get user id from /auth/check when we login and store it and use it
// get dedicated auth token for the rawstore
// common error handling for fetch stuff ... (?)
class DataHub extends EventEmitter {
constructor({apiUrl, token, ownerid, owner, debug = false}) {
super()
this.apiUrl = apiUrl
this._token = token
this._debug = debug
this._ownerid = ownerid
this._owner = owner
this._agent = new Agent(apiUrl, {debug})
}
async push(dataset, options) {
let resources = lodash.clone(dataset.resources)
// Exclude remote Resources
resources = resources.filter(res => res.descriptor.pathType === 'local')
// Get Dataset itself (datapackage.json) as an (Inline) File
const _descriptor = lodash.cloneDeep(dataset.descriptor)
const dpJsonResource = File.load({
path: 'datapackage.json',
name: 'datapackage.json',
data: _descriptor
})
resources.push(dpJsonResource)
this._debugMsg('Getting rawstore upload creds')
const rawstoreUploadCreds = await this.rawstoreAuthorize(resources, options)
this._debugMsg('Uploading to rawstore with creds ...')
this._debugMsg(rawstoreUploadCreds)
// Upload - we do them in parallel
const uploads = resources.map(async resource => {
// TODO: annoying that the serves parses the s3 url so we have to unparse it!
const creds = rawstoreUploadCreds[resource.descriptor.path]
const formData = new FormData()
lodash.forEach(creds.upload_query, (v, k) => {
formData.append(k, v)
})
// We need to compute content length for S3 and don't want form-data to re-read entire stream to get length
// so we explicitly add it
// See https://github.com/alexindigo/form-data/blob/655b95988ef2ed3399f8796b29b2a8673c1df11c/lib/form_data.js#L82
formData.append('file', resource.stream(), {
knownLength: resource.size,
contentType: creds.upload_query['Content-Type']
})
const totalLength = formData.getLengthSync()
// Use straight fetch as not interacting with API but with external object store
const res = await fetch(creds.upload_url, {
method: 'POST',
body: formData,
headers: {
'Content-Length': totalLength
}
})
if (res.status > 204) {
const body = await res.text()
throw new Error(`Error uploading to rawstore for ${resource.descriptor.path} with code ${res.status} reason ${body}`)
}
// Finally add the path to file in the rawstore - this is use by makeSourceSpec
// TODO: should we use urljoin?
// eslint-disable-next-line camelcase
creds.rawstore_url = creds.upload_url + '/' + creds.upload_query.key
})
await Promise.all(uploads)
this._debugMsg('Uploads to rawstore: Complete')
this._debugMsg('Uploading to source spec store')
// Upload to SpecStore
const spec = await makeSourceSpec(rawstoreUploadCreds, this._ownerid, this._owner, dataset, options)
this._debugMsg('Calling source upload with spec')
this._debugMsg(spec)
const token = await this._authz('source')
const res = await this._fetch('/source/upload', token, {
method: 'POST',
body: spec
})
if (res.status === 200) {
const out = await res.json()
if (out.success) {
this._debugMsg(out)
return out
} else {
throw new Error(out.errors.join('\n'))
}
}
throw new Error(responseError(res))
}
async pushFlow(specPath){
let spec = {}
try {
spec = YAML.load(specPath)
} catch (err) {
throw new Error(err.message)
}
this._debugMsg('Calling source upload with spec')
this._debugMsg(spec)
const token = await this._authz('source')
const res = await this._fetch('/source/upload', token, {
method: 'POST',
body: spec
})
if (res.status === 200) {
const out = await res.json()
this._debugMsg(out)
return out
}
throw new Error(responseError(res))
}
async rawstoreAuthorize(resources, options={}) {
const fileData = {}
resources.forEach(resource => {
fileData[resource.descriptor.path] = {
length: resource.size,
md5: resource.hash,
// Not needed - optional in bitstore API
// type: 'binary/octet-stream',
name: resource.descriptor.name
}
})
const body = {
metadata: {
owner: this._ownerid,
findability: options.findability
},
filedata: fileData
}
const token = await this._authz('rawstore')
this._debugMsg('Calling rawstore authorize with')
this._debugMsg(body)
const res = await this._fetch('/rawstore/authorize', token, {
method: 'POST',
body
})
if (res.status === 200) {
const out = await res.json()
return out.filedata
}
throw new Error(await responseError(res))
}
async _authz(service) {
this._debugMsg(`Getting authz token for ${service} service`)
const res = await this._fetch(
`/auth/authorize?service=${service}`,
this._token
)
if (res.status !== 200) {
throw new Error(`Authz server: ${res.statusText}`)
}
return (await res.json()).token
}
close() {
this._agent.close()
}
_fetch(_url, token, opts = {}) {
opts.headers = opts.headers || {}
opts.headers['Auth-Token'] = token
return this._agent.fetch(_url, opts)
}
_debugMsg(msg_) {
if (this._debug) {
let msg = msg_
if (lodash.isObject(msg)) {
msg = JSON.stringify(msg, null, 2)
}
console.log('> [debug] ' + msg)
}
}
}
const makeSourceSpec = async (rawstoreResponse, ownerid, owner, dataset, options) => {
const resourceMapping = {}
lodash.forEach(rawstoreResponse, (uploadInfo, resourceName) => {
if (resourceName !== 'datapackage.json') {
resourceMapping[resourceName] = uploadInfo.rawstore_url
}
})
let processing = await processExcelSheets(dataset.resources, options.sheets)
processing = processing[0] ? processing : undefined
let outputs = handleOutputs(options.outputs)
outputs = outputs[0] ? outputs : undefined
const schedule = options.schedule
return {
meta: {
version: 1,
ownerid,
owner,
dataset: dataset.descriptor.name,
findability: options.findability
},
inputs: [
{
kind: 'datapackage',
// Above we set the "name" for the data package resource to be
// datapackage.json so we use that name to look it up in rawstoreResponse
url: rawstoreResponse['datapackage.json'].rawstore_url,
parameters: {
'resource-mapping': resourceMapping,
'descriptor': dataset.descriptor
}
}
],
outputs,
processing,
schedule
}
}
async function processExcelSheets(resources = [], sheets) {
const excelFormats = ['xls', 'xlsx']
const processingSteps = []
const steps = resources.map(async res => {
if (excelFormats.includes(res.descriptor.format)) {
const buffer = await res.buffer
const workbook = XLSX.read(buffer, {type: 'buffer', bookSheets: true})
if (sheets === 'all') {
sheets = Array.from(Array(workbook.SheetNames.length).keys())
// We are using sheet idx starting from 1 so we need to add 1 to each idx:
sheets = sheets.map(value => value + 1)
} else if (sheets) { // When sheets are indexes of sheets separated by ','
sheets = sheets.split(',').map(value => parseInt(value))
} else { // Default case
sheets = [1]
}
for (let i of sheets) {
const rows = await toArray(await xlsxParser(res, false, i-1))
const schema = await infer(rows)
const step = {
input: res.descriptor.name,
output: `${res.descriptor.name}-sheet-${i}`,
tabulator: {
sheet: i
},
schema
}
processingSteps.push(step)
}
}
})
await Promise.all(steps)
return processingSteps
}
function handleOutputs(outputsConfig = {}) {
const outputs = []
if (outputsConfig.zip) {
outputs.push({
kind: 'zip',
parameters: {
'out-file': 'dataset.zip'
}
})
}
if (outputsConfig.sqlite) {
outputs.push({
kind: 'sqlite'
})
}
return outputs
}
async function responseError(res) {
let message
let userError
if (res.status >= 400 && res.status < 500) {
let body
try {
body = await res.json()
} catch (err) {
body = {}
}
message = (body.error || {}).message
userError = true
} else {
message = await res.text()
userError = false
}
const err = new Error(message || `Response error - no information. Status code: ${res.status} - ${res.statusText}`)
err.status = res.status
err.userError = userError
return err
}
module.exports = {
DataHub,
processExcelSheets,
handleOutputs,
makeSourceSpec
}