Skip to content

Commit

Permalink
refactor(chalice): changed funnels&webVitals users count
Browse files Browse the repository at this point in the history
  • Loading branch information
tahayk committed Aug 14, 2024
1 parent 9754996 commit 2e037f2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion api/chalicelib/core/custom_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __get_funnel_chart(project_id: int, data: schemas.CardFunnel, user_id: int =

return funnels.get_top_insights_on_the_fly_widget(project_id=project_id,
data=data.series[0].filter,
metric_of=data.metric_of)
metric_format=data.metric_format)


def __get_errors_list(project_id, user_id, data: schemas.CardSchema):
Expand Down
8 changes: 4 additions & 4 deletions api/chalicelib/core/funnels.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ def __fix_stages(f_events: List[schemas.SessionSearchEventSchema2]):

# 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.CardSeriesFilterSchema,
metric_of: schemas.MetricOfFunnels):
metric_format: schemas.MetricExtendedFormatType):
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,
project_id=project_id,
metric_of=metric_of)
metric_format=metric_format)
insights = helper.list_to_camel_case(insights)
if len(insights) > 0:
if metric_of == schemas.MetricOfFunnels.SESSION_COUNT and total_drop_due_to_issues > (
if metric_format == schemas.MetricFormatType.SESSION_COUNT and total_drop_due_to_issues > (
insights[0]["sessionsCount"] - insights[-1]["sessionsCount"]):
total_drop_due_to_issues = insights[0]["sessionsCount"] - insights[-1]["sessionsCount"]
elif metric_of == schemas.MetricOfFunnels.USER_COUNT and total_drop_due_to_issues > (
elif metric_format == schemas.MetricExtendedFormatType.USER_COUNT and total_drop_due_to_issues > (
insights[0]["usersCount"] - insights[-1]["usersCount"]):
total_drop_due_to_issues = insights[0]["usersCount"] - insights[-1]["usersCount"]
insights[-1]["dropDueToIssues"] = total_drop_due_to_issues
Expand Down
12 changes: 7 additions & 5 deletions api/chalicelib/core/significance.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,10 @@ def count_users(rows, n_stages, user_key="user_uuid"):
return users_count


def get_stages(stages, rows, metric_of=schemas.MetricOfFunnels.SESSION_COUNT):
def get_stages(stages, rows,
metric_format: schemas.MetricExtendedFormatType = schemas.MetricExtendedFormatType.SESSION_COUNT):
n_stages = len(stages)
if metric_of == "sessionCount":
if metric_format == schemas.MetricExtendedFormatType.SESSION_COUNT:
base_counts = count_sessions(rows, n_stages)
else:
base_counts = count_users(rows, n_stages, user_key="user_id")
Expand All @@ -456,7 +457,7 @@ def get_stages(stages, rows, metric_of=schemas.MetricOfFunnels.SESSION_COUNT):
"dropDueToIssues": 0
}
)
if metric_of == "sessionCount":
if metric_format == schemas.MetricExtendedFormatType.SESSION_COUNT:
stages_list[-1]["sessionsCount"] = base_counts[i + 1]
else:
stages_list[-1]["usersCount"] = base_counts[i + 1]
Expand Down Expand Up @@ -543,7 +544,8 @@ def get_issues(stages, rows, first_stage=None, last_stage=None, drop_only=False)
return n_critical_issues, issues_dict, total_drop_due_to_issues


def get_top_insights(filter_d: schemas.CardSeriesFilterSchema, project_id, metric_of: schemas.MetricOfFunnels):
def get_top_insights(filter_d: schemas.CardSeriesFilterSchema, project_id,
metric_format: schemas.MetricExtendedFormatType):
output = []
stages = filter_d.events

Expand All @@ -554,7 +556,7 @@ def get_top_insights(filter_d: schemas.CardSeriesFilterSchema, project_id, metri
# The result of the multi-stage query
rows = get_stages_and_events(filter_d=filter_d, project_id=project_id)
# Obtain the first part of the output
stages_list = get_stages(stages, rows, metric_of=metric_of)
stages_list = get_stages(stages, rows, metric_format=metric_format)
if len(rows) == 0:
return stages_list, 0

Expand Down
4 changes: 2 additions & 2 deletions api/schemas/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ class MetricOfWebVitals(str, Enum):
AVG_VISITED_PAGES = "avgVisitedPages"
COUNT_REQUESTS = "countRequests"
COUNT_SESSIONS = "countSessions"
COUNT_USERS = "countUsers"
COUNT_USERS = "userCount"


class MetricOfTable(str, Enum):
Expand All @@ -1024,7 +1024,6 @@ class MetricOfTimeseries(str, Enum):

class MetricOfFunnels(str, Enum):
SESSION_COUNT = MetricOfTimeseries.SESSION_COUNT.value
USER_COUNT = MetricOfTimeseries.USER_COUNT.value


class MetricOfHeatMap(str, Enum):
Expand Down Expand Up @@ -1189,6 +1188,7 @@ class CardFunnel(__CardSchema):
metric_type: Literal[MetricType.FUNNEL]
metric_of: MetricOfFunnels = Field(default=MetricOfFunnels.SESSION_COUNT)
view_type: MetricOtherViewType = Field(...)
metric_format: MetricExtendedFormatType = Field(default=MetricExtendedFormatType.SESSION_COUNT)

@model_validator(mode="before")
@classmethod
Expand Down
2 changes: 1 addition & 1 deletion ee/api/chalicelib/core/custom_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __get_funnel_chart(project_id: int, data: schemas.CardFunnel, user_id: int =

return funnels.get_top_insights_on_the_fly_widget(project_id=project_id,
data=data.series[0].filter,
metric_of=data.metric_of)
metric_format=data.metric_format)


def __get_errors_list(project_id, user_id, data: schemas.CardSchema):
Expand Down

0 comments on commit 2e037f2

Please sign in to comment.