From 8d49a588e41ff41c610eff349f9273d858e8820e Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Tue, 7 Jun 2022 19:17:55 +0200 Subject: [PATCH] feat(api): funnel widget --- api/chalicelib/core/custom_metrics.py | 7 ++++++- api/chalicelib/core/funnels.py | 16 ++++++++++++++++ api/schemas.py | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/api/chalicelib/core/custom_metrics.py b/api/chalicelib/core/custom_metrics.py index 3e7fc100a..f26fdb6a9 100644 --- a/api/chalicelib/core/custom_metrics.py +++ b/api/chalicelib/core/custom_metrics.py @@ -2,7 +2,7 @@ import json from typing import Union import schemas -from chalicelib.core import sessions +from chalicelib.core import sessions, funnels from chalicelib.utils import helper, pg_client from chalicelib.utils.TimeUTC import TimeUTC @@ -43,6 +43,11 @@ def __try_live(project_id, data: schemas.TryCustomMetricsPayloadSchema): def merged_live(project_id, data: schemas.TryCustomMetricsPayloadSchema): + if data.metric_type == schemas.MetricType.funnel: + if len(data.series) == 0: + return {} + return funnels.get_top_insights_on_the_fly_widget(project_id=project_id, data=data.series[0].filter) + series_charts = __try_live(project_id=project_id, data=data) if data.view_type == schemas.MetricTimeseriesViewType.progress or data.metric_type == schemas.MetricType.table: return series_charts diff --git a/api/chalicelib/core/funnels.py b/api/chalicelib/core/funnels.py index 16e95989d..e0eb99dc3 100644 --- a/api/chalicelib/core/funnels.py +++ b/api/chalicelib/core/funnels.py @@ -251,6 +251,22 @@ def get_top_insights_on_the_fly(funnel_id, user_id, project_id, data: schemas.Fu "totalDropDueToIssues": total_drop_due_to_issues}} +# def get_top_insights_on_the_fly_widget(project_id, data: schemas.FunnelInsightsPayloadSchema): +def get_top_insights_on_the_fly_widget(project_id, data: schemas.CustomMetricSeriesFilterSchema): + data.events = filter_stages(__parse_events(data.events)) + data.events = __fix_stages(data.events) + if len(data.events) == 0: + return {"stages": [], "totalDropDueToIssues": 0} + insights, total_drop_due_to_issues = significance.get_top_insights(filter_d=data.dict(), project_id=project_id) + insights = helper.list_to_camel_case(insights) + if len(insights) > 0: + if total_drop_due_to_issues > insights[0]["sessionsCount"]: + total_drop_due_to_issues = insights[0]["sessionsCount"] + insights[-1]["dropDueToIssues"] = total_drop_due_to_issues + return {"stages": insights, + "totalDropDueToIssues": total_drop_due_to_issues} + + def get_issues(project_id, user_id, funnel_id, range_value=None, start_date=None, end_date=None): f = get(funnel_id=funnel_id, project_id=project_id, user_id=user_id, flatten=False) if f is None: diff --git a/api/schemas.py b/api/schemas.py index cb83789cd..0902fb269 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -795,6 +795,7 @@ class MetricType(str, Enum): timeseries = "timeseries" table = "table" predefined = "predefined" + funnel = "funnel" class TableMetricOfType(str, Enum):