Skip to content

Commit

Permalink
refactor(cli): Simplify URL building by wrapping every part
Browse files Browse the repository at this point in the history
  • Loading branch information
akash1810 committed Dec 13, 2023
1 parent ef05394 commit 73bb8f3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions cli/elk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getLink } from "./elk.ts";
Deno.test("getLink with simple input", () => {
const got = getLink("devx", { app: "riff-raff", stage: "PROD" });
const want =
"https://logs.gutools.co.uk/s/devx/app/discover#/?_g=(filters:!((query:(match_phrase:(app:'riff-raff'))),(query:(match_phrase:(stage:'PROD')))))";
"https://logs.gutools.co.uk/s/devx/app/discover#/?_g=(filters:!((query:(match_phrase:('app':'riff-raff'))),(query:(match_phrase:('stage':'PROD')))))";
assertEquals(got, want);
});

Expand All @@ -16,7 +16,7 @@ Deno.test("getLink with columns", () => {
"level",
]);
const want =
"https://logs.gutools.co.uk/s/devx/app/discover#/?_g=(filters:!((query:(match_phrase:(app:'riff-raff'))),(query:(match_phrase:(stage:'PROD')))))&_a=(columns:!(message,level))";
"https://logs.gutools.co.uk/s/devx/app/discover#/?_g=(filters:!((query:(match_phrase:('app':'riff-raff'))),(query:(match_phrase:('stage':'PROD')))))&_a=(columns:!('message','level'))";
assertEquals(got, want);
});

Expand All @@ -30,6 +30,6 @@ Deno.test("getLink with colon(:) input", () => {
stage: "PROD",
}, ["message", "gu:repo"]);
const want =
"https://logs.gutools.co.uk/s/devx/app/discover#/?_g=(filters:!((query:(match_phrase:('gu:repo.keyword':'guardian/amigo'))),(query:(match_phrase:(stage:'PROD')))))&_a=(columns:!(message,'gu:repo'))";
"https://logs.gutools.co.uk/s/devx/app/discover#/?_g=(filters:!((query:(match_phrase:('gu:repo.keyword':'guardian/amigo'))),(query:(match_phrase:('stage':'PROD')))))&_a=(columns:!('message','gu:repo'))";
assertEquals(got, want);
});
12 changes: 8 additions & 4 deletions cli/elk.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
function escapeColon(str: string): string {
return str.includes(":") ? `'${str}'` : str;
/**
* Wrap a string in single quotes so Kibana can parse it correctly
*/
function wrapString(str: string): string {
return `'${str}'`;
}

export function getLink(
space: string,
filters: Record<string, string>,
columns: string[] = [],
): string {
const kibanaFilters = Object.entries(filters).map(([key, value]) => {
return `(query:(match_phrase:(${escapeColon(key)}:'${value}')))`;
return `(query:(match_phrase:(${wrapString(key)}:${wrapString(value)})))`;
});

// The `#/` at the end is important for Kibana to correctly parse the query string
Expand All @@ -19,7 +23,7 @@ export function getLink(
_g: `(filters:!(${kibanaFilters.join(",")}))`,
}),
...(columns.length > 0 && {
_a: `(columns:!(${columns.map(escapeColon).join(",")}))`,
_a: `(columns:!(${columns.map(wrapString).join(",")}))`,
}),
};

Expand Down

0 comments on commit 73bb8f3

Please sign in to comment.