Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove logs.post() and logs.postPaths() from Zealot #1772

Merged
merged 2 commits into from
Aug 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 42 additions & 33 deletions plugins/log-loader/index.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
import BrimApi from "../../src/js/api"
import {IngestParams} from "../../src/js/brim/ingest/getParams"
import errors from "../../src/js/errors"
import {forEach} from "lodash"
import {forEach, get} from "lodash"
import {Readable} from "stream"

export const activate = (api: BrimApi) => {
const load = async (
params: IngestParams & {poolId: string},
onProgressUpdate: (value: number | null) => void,
onWarning: (warning: string) => void,
onDetailUpdate: () => Promise<void>
onDetailUpdate: () => Promise<void>,
signal?: AbortSignal
): Promise<void> => {
const {poolId, fileListData} = params
const client = api.getZealot()

const files = fileListData.map((f) => f.file)

const stream = await client.logs.post({poolId, files})

const files = params.fileListData.map((f) => f.file)
const totalBytes = files.reduce((sum, file) => sum + file.size, 0)
const zealot = api.getZealot()
let readBytes = 0
onProgressUpdate(0)
// @ts-ignore
for await (const {type, ...status} of stream) {
switch (type) {
case "Error":
throw new Error(status.error)
case "UploadProgress":
onProgressUpdate(status.progress)
await onDetailUpdate()
break
case "LogPostResponse":
await onDetailUpdate()
forEach(status.warnings, onWarning)
break
case "LogPostWarning":
onWarning(status.warning)
break
case "TaskEnd":
if (status.error) {
throw errors.logsIngest(status.error.error)
}
break
}
for (const file of files) {
const progressUpdateTransformStream = new TransformStream({
transform(chunk, ctrl) {
ctrl.enqueue(chunk)
readBytes += chunk.byteLength
onProgressUpdate(readBytes / totalBytes)
}
})
const stream = file.stream().pipeThrough(progressUpdateTransformStream)
const res = await zealot.pools.add(params.poolId, {
data: nodeJSReadableStreamFromReadableStream(stream),
signal
})
const commitId = get(res, ["value", "commit"], "")
if (!commitId) throw new Error("No commit obtained from lake add")
forEach(get(res, ["value", "warnings"], []), onWarning)
await zealot.pools.commit(params.poolId, commitId, {
author: "brim",
message: "automatic import of " + file.path,
signal
})
}

await onDetailUpdate()
onProgressUpdate(1)
onProgressUpdate(null)
Expand All @@ -53,4 +49,17 @@ export const activate = (api: BrimApi) => {
})
}

function nodeJSReadableStreamFromReadableStream(
nwt marked this conversation as resolved.
Show resolved Hide resolved
stream: ReadableStream
): NodeJS.ReadableStream {
const reader = stream.getReader()
return new Readable({
read(_size) {
reader.read().then(({done, value}) => {
this.push(done ? null : value)
})
}
})
}

export const deactivate = () => {}