Skip to content

Commit

Permalink
feat(LogQL): add static method 'type' (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken authored Jul 1, 2024
1 parent 8fd889d commit 5bedadd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/logql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import { ParserExpression } from "./parserExpression";
export { StreamSelector, LineFilters, LabelFilters, ParserExpression };

export class LogQL {
static type(QL: string | LogQL) {
if (typeof QL === "string") {
return /^[a-zA-Z_\s]+\(/g.test(QL) ? "metric" : "query";
}

return QL.type;
}

#type: "metric" | "query" = "query";
#rawInit: string;

Expand All @@ -19,7 +27,7 @@ export class LogQL {
init?: string | string[] | StreamSelector | LineFilters | LabelFilters | ParserExpression
) {
if (typeof init === "string") {
this.#type = /^[a-zA-Z_\s]+\(/g.test(init) ? "metric" : "query";
this.#type = LogQL.type(init);

if (this.#type === "metric") {
this.#rawInit = init;
Expand Down
16 changes: 16 additions & 0 deletions src/logql/test/logql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ import { StreamSelector } from "../src/streamSelector";
import { LineFilters } from "../src/lineFilters";

describe("LogQL", () => {
describe("static type", () => {
it("should return 'metric' given a metric query", () => {
assert.strictEqual(
LogQL.type("count_over_time({ label = `value` } [5m])"),
"metric"
);
});

it("should return 'query' given a normal query with no aggregations etc", () => {
assert.strictEqual(
LogQL.type("{ label = `value` }"),
"query"
);
});
});

describe("constructor", () => {
it("should create a new instance of LogQL", () => {
const logql = new LogQL();
Expand Down

0 comments on commit 5bedadd

Please sign in to comment.