From d2b455dfdbc81561685aefac0ee8271a7b9de483 Mon Sep 17 00:00:00 2001 From: Shekar Siri Date: Tue, 20 May 2025 11:28:50 +0200 Subject: [PATCH] feat(product_analytics): table of cards testing fitlers --- backend/pkg/analytics/charts/metric_table.go | 26 +++++++++++--------- backend/pkg/analytics/charts/query.go | 17 +++++++++++++ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/backend/pkg/analytics/charts/metric_table.go b/backend/pkg/analytics/charts/metric_table.go index 2f011a79e..464da6b78 100644 --- a/backend/pkg/analytics/charts/metric_table.go +++ b/backend/pkg/analytics/charts/metric_table.go @@ -51,6 +51,7 @@ var propertySelectorMap = map[string]string{ var mainColumns = map[string]string{ "userBrowser": "$browser", "referrer": "$referrer", + "ISSUE": "issue_type", } func (t TableQueryBuilder) Execute(p Payload, conn db.Connector) (interface{}, error) { @@ -134,9 +135,20 @@ func (t TableQueryBuilder) buildQuery(r Payload, metricFormat string) (string, e originalMetricOf := r.MetricOf propertyName = originalMetricOf - durationConds, eventFilters := buildDurationWhere(s.Filter.Filters) + durationConds, _ := buildDurationWhere(s.Filter.Filters) + eventFilters, _ := filterOutTypes(s.Filter.Filters, []FilterType{FilterDuration, FilterUserId}) + _, sessionFilters := filterOutTypes(s.Filter.Filters, []FilterType{FilterUserId, FilterUserAnonymousId}) + + sessionConds, _ := buildEventConditions(sessionFilters, BuildConditionsOptions{ + DefinedColumns: map[string]string{ + "userId": "user_id", + }, + MainTableAlias: "sessions", + }) + eventConds, eventNames := buildEventConditions(eventFilters, BuildConditionsOptions{ DefinedColumns: mainColumns, + MainTableAlias: "main", }) baseWhereConditions := []string{ fmt.Sprintf("main.created_at >= toDateTime(%d/1000)", r.StartTimestamp), @@ -149,17 +161,7 @@ func (t TableQueryBuilder) buildQuery(r Payload, metricFormat string) (string, e baseWhereConditions = append(baseWhereConditions, cond) } - //baseWhereConditions := []string{ - // fmt.Sprintf("main.created_at >= toDateTime(%d/1000)", r.StartTimestamp), - // fmt.Sprintf("main.created_at <= toDateTime(%d/1000)", r.EndTimestamp), - // "sessions.duration > 0", - //} - // - - // - //if r.ProjectId > 0 { - // baseWhereConditions = append(baseWhereConditions, fmt.Sprintf("main.project_id = %d", r.ProjectId)) - //} + baseWhereConditions = append(baseWhereConditions, sessionConds...) var aggregationExpression string var aggregationAlias = "aggregation_id" diff --git a/backend/pkg/analytics/charts/query.go b/backend/pkg/analytics/charts/query.go index a770c7629..77e23b9e3 100644 --- a/backend/pkg/analytics/charts/query.go +++ b/backend/pkg/analytics/charts/query.go @@ -50,10 +50,12 @@ type BuildConditionsOptions struct { var propertyKeyMap = map[string]filterConfig{ "LOCATION": {LogicalProperty: "url_path"}, "FETCH": {LogicalProperty: "url_path"}, + "REQUEST": {LogicalProperty: "url_path"}, "CLICK": {LogicalProperty: "label"}, "INPUT": {LogicalProperty: "label"}, "fetchUrl": {LogicalProperty: "url_path"}, "fetchStatusCode": {LogicalProperty: "status", IsNumeric: true}, + //"ISSUE": {LogicalProperty: "issue_type"}, // TODO add more mappings as needed } @@ -466,3 +468,18 @@ func buildDurationWhere(filters []Filter) ([]string, []Filter) { } return conds, rest } + +func filterOutTypes(filters []Filter, typesToRemove []FilterType) (kept []Filter, removed []Filter) { + removeMap := make(map[FilterType]struct{}, len(typesToRemove)) + for _, t := range typesToRemove { + removeMap[t] = struct{}{} + } + for _, f := range filters { + if _, ok := removeMap[f.Type]; ok { + removed = append(removed, f) + } else { + kept = append(kept, f) + } + } + return +}