diff --git a/apps/zui/src/js/lib/Program.ts b/apps/zui/src/js/lib/Program.ts deleted file mode 100644 index 4f26017b6b..0000000000 --- a/apps/zui/src/js/lib/Program.ts +++ /dev/null @@ -1,69 +0,0 @@ -import {first, same} from "./Array" -import {onlyWhitespace, trim} from "./Str" -import {parse as parseAst} from "zed/compiler/parser/parser" -import ast from "../models/ast" - -export type Program = string - -export const parse = (string: Program) => { - let ast = null - let error = null - try { - ast = parseAst(string) - } catch (e) { - error = e - } - return [ast, error] -} - -export const hasGroupByProc = (program: Program) => { - const [ast] = parse(program) - if (!ast) return false - return !!getGroupByProc(ast) -} - -export const getGroupByProc = (astArg: any) => { - return ast(astArg).proc("Summarize") -} - -function joinProcs(procs: string[]) { - return "fork ( => " + procs.join(" => ") + " )" -} - -export function joinParts(filter: string, proc: string) { - const f = fmtProgram(filter) - return [f, proc].join(" | ") -} - -export function splitParts(program: string) { - const [_, ...procs] = program.split("|") - const p = trim(procs.join("|")) - - const [filter] = program.split("|") - const f = trim(filter) - - return [f, p] -} - -export function parallelizeProcs(programs: string[]) { - const filters = [] - const procs = [] - - for (const program of programs) { - const [filter, proc] = splitParts(program) - filters.push(filter) - procs.push(proc) - } - - if (!same(filters)) { - throw new Error( - `Filters must be the same in all programs: ${filters.join(", ")}` - ) - } - - return joinParts(first(filters), joinProcs(procs)) -} - -export function fmtProgram(string: string) { - return onlyWhitespace(string) ? "*" : string -} diff --git a/apps/zui/src/js/models/program.test.ts b/apps/zui/src/js/models/program.test.ts index 4cd9938254..5cd003db9d 100644 --- a/apps/zui/src/js/models/program.test.ts +++ b/apps/zui/src/js/models/program.test.ts @@ -1,5 +1,4 @@ import {createField, createRecord} from "@brimdata/zed-js" -import {joinParts, parallelizeProcs, splitParts} from "../lib/Program" import program from "./program" describe("excluding and including", () => { @@ -231,59 +230,6 @@ describe("#hasAnalytics()", () => { }) }) -describe("Get Parts of Program", () => { - const script = 'md5=="123" _path=="files" | count() by md5 | sort -r | head 1' - - test("get filter part", () => { - expect(splitParts(script)[0]).toBe('md5=="123" _path=="files"') - }) - - test("get filter part when none", () => { - expect(splitParts("* | count()")[0]).toBe("*") - }) - - test("get proc part", () => { - expect(splitParts(script)[1]).toBe("count() by md5 | sort -r | head 1") - }) - - test("get proc part when none", () => { - expect(splitParts('_path=="files"')[1]).toEqual("") - }) -}) - -describe("Join Parts of Program", () => { - const filter = 'md5=="123"' - const proc = "count() by _path" - - test("#joinParts", () => { - expect(joinParts(filter, proc)).toBe('md5=="123" | count() by _path') - }) - - test("#joinParts when empty filter", () => { - expect(joinParts("", proc)).toBe("* | count() by _path") - }) -}) - -describe("Parallelizing multiple programs", () => { - const a = 'md5=="123" | count()' - const b = 'md5=="123" | head 5' - const c = 'md5=="123" | count() by _path' - - test("#parallelizeProcs when programs have same filter", () => { - expect(parallelizeProcs([a, b, c])).toEqual( - 'md5=="123" | fork ( => count() => head 5 => count() by _path )' - ) - }) - - test("#parallelizeProcs when programs do not have same filter", () => { - expect(() => { - parallelizeProcs([a, b, c, '_path=="conn"']) - }).toThrow( - 'Filters must be the same in all programs: md5=="123", md5=="123", md5=="123", _path=="conn"' - ) - }) -}) - describe("extracting the first filter", () => { test("*", () => { expect(program("*").filter()).toEqual("*")