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

Log endpoint uses multipart form #1228

Merged
merged 11 commits into from
Nov 24, 2020
16 changes: 8 additions & 8 deletions src/js/brim/ingest/detectFileTypes.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ingest from "./"
import itestFile from "../../test/itestFile"
import {itestFile} from "../../test/itestFile"

const json = itestFile("sample.ndjson")
const pcap = itestFile("sample.pcap")
Expand All @@ -8,14 +8,14 @@ const unknown = itestFile("plain.txt")
const zeek = itestFile("sample.tsv")

test("add file types", async () => {
const paths = [pcap, pcapng, zeek, json, unknown]
const types = await ingest.detectFileTypes(paths)
const files = [pcap, pcapng, zeek, json, unknown]
const types = await ingest.detectFileTypes(files)

expect(types).toEqual([
{type: "pcap", path: pcap},
{type: "pcap", path: pcapng},
{type: "log", path: zeek},
{type: "log", path: json},
{type: "log", path: unknown}
{type: "pcap", file: pcap},
{type: "pcap", file: pcapng},
{type: "log", file: zeek},
{type: "log", file: json},
{type: "log", file: unknown}
])
})
8 changes: 4 additions & 4 deletions src/js/brim/ingest/detectFileTypes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {FileListData} from "./fileList"
import detectFileType from "./detectFileType"

export default function(paths: string[]): Promise<FileListData> {
export default function(files: File[]): Promise<FileListData> {
return Promise.all(
paths.map(async (path) => {
const type = await detectFileType(path)
return {type, path}
files.map(async (file) => {
const type = await detectFileType(file.path)
return {type, file}
})
)
}
30 changes: 18 additions & 12 deletions src/js/brim/ingest/fileList.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,52 @@
import {IngestFileType} from "./detectFileType"
import lib from "../../lib"

export type FileListData = {type: IngestFileType; path: string}[]
export type FileListData = {type: IngestFileType; file: File}[]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if you considered extending the File interface by annotating it with the new type field? would save all the extra .file.s since it would be flattened, just a thought!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah true, I usually try to stay away from extending things like that. "Compose rather than inherit" mindset I guess. Is this something that is done conventionally in go?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the composition vs inheritance dilemma is the same in go. I do agree with the compose > inheritance mindset, but thought that in this case with just these two types, and one always depending on the other, extending would grant the small bonus of less typing as you access the imbedded File interface. I have no strong feelings in this case at all though!


export default function fileList(files: FileListData) {
export default function fileList(list: FileListData) {
return {
first() {
return files[0]
return list[0]
},

oneFile() {
return files.length === 1
return list.length === 1
},

multiple() {
return files.length > 1
return list.length > 1
},

paths(): string[] {
return files.map((f) => f.path)
return list.map((f) => f.file.path)
},

files(): File[] {
return list.map((f) => f.file)
},

any(type: string) {
return !!files.find((f) => f.type === type)
return !!list.find((f) => f.type === type)
},

allPcap() {
return files.every((f) => f.type === "pcap")
return list.every((f) => f.type === "pcap")
},

mixed() {
return !files.every((f) => f.type === files[0].type)
return !list.every((f) => f.type === list[0].type)
},

inSameDir() {
return files.every(
(f) => lib.file(f.path).dirName() === lib.file(files[0].path).dirName()
return list.every(
(item) =>
lib.file(item.file.path).dirName() ===
lib.file(list[0].file.path).dirName()
)
},

dirName() {
return lib.file(files[0].path).dirName()
return lib.file(list[0].file.path).dirName()
}
}
}
6 changes: 3 additions & 3 deletions src/js/brim/ingest/getParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type IngestParams = {
name: string
dataDir: string
endpoint: IngestFileType
paths: string[]
files: File[]
}

export type IngestParamsError = {
Expand All @@ -37,7 +37,7 @@ export default function getParams(

function getSpaceName() {
let name
if (files.oneFile()) name = lib.file(files.first().path).fileName()
if (files.oneFile()) name = lib.file(files.first().file.path).fileName()
else if (files.inSameDir()) name = files.dirName()
else name = generateDirName(now)

Expand All @@ -48,7 +48,7 @@ export default function getParams(
name: getSpaceName(),
dataDir: getDataDir(),
endpoint: files.first().type,
paths: files.paths()
files: files.files()
}
}

Expand Down
38 changes: 24 additions & 14 deletions src/js/brim/ingest/test.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,56 @@
import ingest from "./"

const fakeFile = (path: string): File => {
const f = new File([], "fake")
f.path = path
return f
}

test("one pcap default", () => {
const data = ingest.getParams([{type: "pcap", path: "/work/my.pcap"}])
const data = ingest.getParams([
{type: "pcap", file: fakeFile("/work/my.pcap")}
])

expect(data).toEqual({
dataDir: "",
name: "my.pcap",
endpoint: "pcap",
paths: ["/work/my.pcap"]
files: [fakeFile("/work/my.pcap")]
})
})

test("one zeek log default", () => {
const data = ingest.getParams([{type: "log", path: "/work/zeek.log"}])
const data = ingest.getParams([
{type: "log", file: fakeFile("/work/zeek.log")}
])

expect(data).toEqual({
name: "zeek.log",
dataDir: "",
endpoint: "log",
paths: ["/work/zeek.log"]
files: [fakeFile("/work/zeek.log")]
})
})

test("two zeek logs in same dir default", () => {
const data = ingest.getParams([
{type: "log", path: "/work/zeek-1.log"},
{type: "log", path: "/work/zeek-2.log"}
{type: "log", file: fakeFile("/work/zeek-1.log")},
{type: "log", file: fakeFile("/work/zeek-2.log")}
])

expect(data).toEqual({
name: "work",
dataDir: "",
endpoint: "log",
paths: ["/work/zeek-1.log", "/work/zeek-2.log"]
files: [fakeFile("/work/zeek-1.log"), fakeFile("/work/zeek-2.log")]
})
})

test("two zeek logs in different dir default", () => {
const data = ingest.getParams(
[
{type: "log", path: "/work/day-1/zeek.log"},
{type: "log", path: "/work/day-2/zeek.log"}
{type: "log", file: fakeFile("/work/day-1/zeek.log")},
{type: "log", file: fakeFile("/work/day-2/zeek.log")}
],
"",
[],
Expand All @@ -51,14 +61,14 @@ test("two zeek logs in different dir default", () => {
name: "zeek_1969-12-31_16:00:00",
dataDir: "",
endpoint: "log",
paths: ["/work/day-1/zeek.log", "/work/day-2/zeek.log"]
files: [fakeFile("/work/day-1/zeek.log"), fakeFile("/work/day-2/zeek.log")]
})
})

test("two pcaps", () => {
const data = ingest.getParams([
{type: "pcap", path: "/pcap-1"},
{type: "pcap", path: "/pcap-2"}
{type: "pcap", file: fakeFile("/pcap-1")},
{type: "pcap", file: fakeFile("/pcap-2")}
])

expect(data).toEqual({
Expand All @@ -68,8 +78,8 @@ test("two pcaps", () => {

test("1 pcap and 1 zeek", () => {
const data = ingest.getParams([
{type: "pcap", path: "/pcap-1"},
{type: "log", path: "/zeek-1"}
{type: "pcap", file: fakeFile("/pcap-1")},
{type: "log", file: fakeFile("/zeek-1")}
])

expect(data).toEqual({
Expand Down
10 changes: 5 additions & 5 deletions src/js/components/LoadFilesInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import useCallbackRef from "./hooks/useCallbackRef"
import useDropzone from "./hooks/useDropzone"

type Props = {
onChange: (e, arg1: string[]) => void
onChange: (e, files: File[]) => void
}

export default function LoadFilesInput({onChange}: Props) {
const [input, setInput] = useCallbackRef()

const [bindDropzone, dragging] = useDropzone((e: DragEvent) => {
const paths = Array.from(e.dataTransfer.files).map((f) => f.path)
onChange(e, paths)
const files = Array.from(e.dataTransfer.files)
onChange(e, files)
})

function _onChange(e: ChangeEvent<HTMLInputElement>) {
const paths = Array.from(e.target.files).map((f) => f.path)
onChange(e, paths)
const files = Array.from(e.target.files)
onChange(e, files)
}

function openDialog(_: MouseEvent) {
Expand Down
2 changes: 1 addition & 1 deletion src/js/flows/exportResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ export default (filePath: string): Thunk => (dispatch, getState) => {
format: "zng",
controlMessages: false
})
.then((resp) => saveToFile(resp.origResp, filePath))
.then((resp) => saveToFile(resp.origResp as Response, filePath))
}
4 changes: 2 additions & 2 deletions src/js/flows/getZealot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ConnectionStatuses from "../state/ConnectionStatuses"

const createBrimFetcher = (dispatch, getState) => {
return (hostPort: string) => {
const {promise, stream} = createFetcher(hostPort)
const {promise, ...rest} = createFetcher(hostPort)

const wrappedPromise = (args: FetchArgs): Promise<any> => {
return promise(args).catch((e) => {
Expand All @@ -22,7 +22,7 @@ const createBrimFetcher = (dispatch, getState) => {
})
}

return {promise: wrappedPromise, stream}
return {promise: wrappedPromise, ...rest}
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/js/flows/ingestFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Tab from "../state/Tab"
import fixtures from "../test/fixtures"
import ingestFiles from "./ingestFiles"
import initTestStore from "../test/initTestStore"
import itestFile from "../test/itestFile"
import {itestFile, itestFilePath} from "../test/itestFile"
import lib from "../lib"

let store, zealot
Expand Down Expand Up @@ -102,13 +102,13 @@ describe("success case", () => {
test("a json file with a custom types config", async () => {
zealot.stubStream("logs.post", [{type: "LogPostStatus"}, {type: "TaskEnd"}])

const contents = await lib.file(itestFile("sampleTypes.json")).read()
store.dispatch(Prefs.setJSONTypeConfig(itestFile("sampleTypes.json")))
const contents = await lib.file(itestFilePath("sampleTypes.json")).read()
store.dispatch(Prefs.setJSONTypeConfig(itestFilePath("sampleTypes.json")))

await store.dispatch(ingestFiles([itestFile("sample.ndjson")]))

expect(zealot.calls("logs.post")[0].args).toEqual({
paths: [itestFile("sample.ndjson")],
files: [itestFile("sample.ndjson")],
spaceId: "spaceId",
types: JSON.parse(contents)
})
Expand Down
Loading