Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Refactor analytics query parser to use descriptive operator names and update tests accordingly

Kiran K9e7bd009f1c8
diff --git a/apps/web/lib/analytics/utils/analytics-query-parser.ts b/apps/web/lib/analytics/utils/analytics-query-parser.ts
index 0593c46bc9..a3109a5412 100644
--- a/apps/web/lib/analytics/utils/analytics-query-parser.ts
+++ b/apps/web/lib/analytics/utils/analytics-query-parser.ts
@@ -2,7 +2,13 @@ import { EventsFilters } from "../types";
interface InternalFilter {
operand: string;
- operator: "=" | ">" | "<" | ">=" | "<=" | "!=";
+ operator:
+ | "equals"
+ | "notEquals"
+ | "greaterThan"
+ | "lessThan"
+ | "greaterThanOrEqual"
+ | "lessThanOrEqual";
value: string;
}
@@ -59,10 +65,12 @@ function parseCondition(condition: string): InternalFilter | null {
if (fieldOrMetadata.startsWith("metadata")) {
const keyPath = fieldOrMetadata.replace(/^metadata/, "");
- operand = keyPath
+ const extractedKey = keyPath
.replace(/^\[['"]|['"]\]$/g, "") // Remove leading [' or [" and trailing '] or "]
.replace(/\[['"]/g, ".") // Replace [' or [" with .
.replace(/['"]\]/g, ""); // Remove trailing '] or "]
+
+ operand = `metadata.${extractedKey}`;
} else {
operand = fieldOrMetadata;
}
@@ -78,19 +86,19 @@ function parseCondition(condition: string): InternalFilter | null {
function mapOperator(operator: string): InternalFilter["operator"] {
switch (operator) {
case ":":
- return "=";
+ return "equals";
case ">":
- return ">";
+ return "greaterThan";
case "<":
- return "<";
+ return "lessThan";
case ">=":
- return ">=";
+ return "greaterThanOrEqual";
case "<=":
- return "<=";
+ return "lessThanOrEqual";
case "!=":
- return "!=";
+ return "notEquals";
default:
// For unsupported operators, default to equals
- return "=";
+ return "equals";
}
}
diff --git a/apps/web/tests/misc/analytics-query-parser.test.ts b/apps/web/tests/misc/analytics-query-parser.test.ts
index 3f6c43283c..58aa830f65 100644
--- a/apps/web/tests/misc/analytics-query-parser.test.ts
+++ b/apps/web/tests/misc/analytics-query-parser.test.ts
@@ -51,14 +51,14 @@ describe("Analytics Query Parser", () => {
it("should parse simple nested property", () => {
const result = parseFiltersFromQuery("metadata['key']:value");
expect(result).toEqual([
- { operand: "key", operator: "=", value: "value" },
+ { operand: "metadata.key", operator: "=", value: "value" },
]);
});
it("should parse nested property with double quotes", () => {
const result = parseFiltersFromQuery('metadata["key"]:"quoted value"');
expect(result).toEqual([
- { operand: "key", operator: "=", value: "quoted value" },
+ { operand: "metadata.key", operator: "=", value: "quoted value" },
]);
});
@@ -67,7 +67,7 @@ describe("Analytics Query Parser", () => {
"metadata['level1']['level2']['level3']:value",
);
expect(result).toEqual([
- { operand: "level1.level2.level3", operator: "=", value: "value" },
+ { operand: "metadata.level1.level2.level3", operator: "=", value: "value" },
]);
});
@@ -76,7 +76,7 @@ describe("Analytics Query Parser", () => {
"metadata['user']['preferences']['theme']:dark",
);
expect(result).toEqual([
- { operand: "user.preferences.theme", operator: "=", value: "dark" },
+ { operand: "metadata.user.preferences.theme", operator: "=", value: "dark" },
]);
});
});
@@ -92,7 +92,7 @@ describe("Analytics Query Parser", () => {
it("should parse equals operator (:) for nested property", () => {
const result = parseFiltersFromQuery("metadata['key']:value");
expect(result).toEqual([
- { operand: "key", operator: "=", value: "value" },
+ { operand: "metadata.key", operator: "=", value: "value" },
]);
});
@@ -134,7 +134,7 @@ describe("Analytics Query Parser", () => {
it("should parse not equals operator for nested property", () => {
const result = parseFiltersFromQuery("metadata['status']!=completed");
expect(result).toEqual([
- { operand: "status", operator: "!=", value: "completed" },
+ { operand: "metadata.status", operator: "!=", value: "completed" },
]);
});
});
@@ -172,8 +172,8 @@ describe("Analytics Query Parser", () => {
"metadata['product_id']:123 AND metadata['category']:electronics",
);
expect(result).toEqual([
- { operand: "product_id", operator: "=", value: "123" },
- { operand: "category", operator: "=", value: "electronics" },
+ { operand: "metadata.product_id", operator: "=", value: "123" },
+ { operand: "metadata.category", operator: "=", value: "electronics" },
]);
});
@@ -183,7 +183,7 @@ describe("Analytics Query Parser", () => {
);
expect(result).toEqual([
{ operand: "amount", operator: ">", value: "100" },
- { operand: "user_type", operator: "=", value: "premium" },
+ { operand: "metadata.user_type", operator: "=", value: "premium" },
]);
});
});
@@ -263,7 +263,7 @@ describe("Analytics Query Parser", () => {
);
expect(result).toEqual([
{ operand: "amount", operator: ">=", value: "100" },
- { operand: "category", operator: "=", value: "electronics" },
+ { operand: "metadata.category", operator: "=", value: "electronics" },
{ operand: "status", operator: "=", value: "completed" },
]);
});
@@ -273,8 +273,8 @@ describe("Analytics Query Parser", () => {
"metadata['user_type']:premium AND metadata['region']:us AND amount>50",
);
expect(result).toEqual([
- { operand: "user_type", operator: "=", value: "premium" },
- { operand: "region", operator: "=", value: "us" },
+ { operand: "metadata.user_type", operator: "=", value: "premium" },
+ { operand: "metadata.region", operator: "=", value: "us" },
{ operand: "amount", operator: ">", value: "50" },
]);
});
@@ -284,8 +284,8 @@ describe("Analytics Query Parser", () => {
"metadata['source']:website AND metadata['campaign']:summer2024 AND status:qualified",
);
expect(result).toEqual([
- { operand: "source", operator: "=", value: "website" },
- { operand: "campaign", operator: "=", value: "summer2024" },
+ { operand: "metadata.source", operator: "=", value: "website" },
+ { operand: "metadata.campaign", operator: "=", value: "summer2024" },
{ operand: "status", operator: "=", value: "qualified" },
]);
});
diff --git a/packages/tinybird/pipes/v2_events.pipe b/packages/tinybird/pipes/v2_events.pipe
index 9d5ea0d03e..b7027b7942 100644
--- a/packages/tinybird/pipes/v2_events.pipe
+++ b/packages/tinybird/pipes/v2_events.pipe
@@ -136,8 +136,24 @@ SQL >
{% if defined(filters) %}
{% for item in JSON(filters, '[]') %}
- {% if item.get('operator', '') == 'equals' %}
- AND JSONExtractString(metadata, {{ item.get('operand', '') }}) = {{ item.get('value', '') }}
+ {% if item.get('operand', '').startswith('metadata.') %}
+ {% set metadataKey = item.get('operand', '').split('.')[1] %}
+ {% set operator = item.get('operator', 'equals') %}
+ {% set value = item.get('value', '') %}
+
+ {% if operator == 'equals' %}
+ AND JSONExtractString(metadata, {{ metadataKey }}) = {{ value }}
+ {% elif operator == 'notEquals' %}
+ AND JSONExtractString(metadata, {{ metadataKey }}) != {{ value }}
+ {% elif operator == 'greaterThan' %}
+ AND JSONExtractString(metadata, {{ metadataKey }}) > {{ value }}
+ {% elif operator == 'lessThan' %}
+ AND JSONExtractString(metadata, {{ metadataKey }}) < {{ value }}
+ {% elif operator == 'greaterThanOrEqual' %}
+ AND JSONExtractString(metadata, {{ metadataKey }}) >= {{ value }}
+ {% elif operator == 'lessThanOrEqual' %}
+ AND JSONExtractString(metadata, {{ metadataKey }}) <= {{ value }}
+ {% end %}
{% end %}
{% end %}
{% end %}
@@ -186,8 +202,24 @@ SQL >
{% if defined(filters) %}
{% for item in JSON(filters, '[]') %}
- {% if item.get('operator', '') == 'equals' %}
- AND JSONExtractString(metadata, {{ item.get('operand', '') }}) = {{ item.get('value', '') }}
+ {% if item.get('operand', '').startswith('metadata.') %}
+ {% set metadataKey = item.get('operand', '').split('.')[1] %}
+ {% set operator = item.get('operator', 'equals') %}
+ {% set value = item.get('value', '') %}
+
+ {% if operator == 'equals' %}
+ AND JSONExtractString(metadata, {{ metadataKey }}) = {{ value }}
+ {% elif operator == 'notEquals' %}
+ AND JSONExtractString(metadata, {{ metadataKey }}) != {{ value }}
+ {% elif operator == 'greaterThan' %}
+ AND JSONExtractString(metadata, {{ metadataKey }}) > {{ value }}
+ {% elif operator == 'lessThan' %}
+ AND JSONExtractString(metadata, {{ metadataKey }}) < {{ value }}
+ {% elif operator == 'greaterThanOrEqual' %}
+ AND JSONExtractString(metadata, {{ metadataKey }}) >= {{ value }}
+ {% elif operator == 'lessThanOrEqual' %}
+ AND JSONExtractString(metadata, {{ metadataKey }}) <= {{ value }}
+ {% end %}
{% end %}
{% end %}
{% end %}