Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Round sessions value #2925

Merged
merged 6 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions ee/clickhouse/queries/sessions/average.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ee.clickhouse.sql.sessions.average_per_period import AVERAGE_PER_PERIOD_SQL
from ee.clickhouse.sql.sessions.no_events import SESSIONS_NO_EVENTS_SQL
from posthog.models import Filter, Team
from posthog.queries.sessions.sessions import scale_time_series
from posthog.utils import append_data, friendly_time


Expand Down Expand Up @@ -44,6 +45,8 @@ def calculate_avg(self, filter: Filter, team: Team):
response = sync_execute(final_query, params)
values = self.clean_values(filter, response)
time_series_data = append_data(values, interval=filter.interval, math=None)
scaled_data, _ = scale_time_series(time_series_data["data"])
time_series_data.update({"data": scaled_data})
# calculate average
total = sum(val[1] for val in values)

Expand All @@ -69,7 +72,7 @@ def _format_avg(self, avg: float):
avg_split = avg_formatted.split(" ")
time_series_data = {}
time_series_data.update(
{"label": "Average Duration of Session ({})".format(avg_split[1]), "count": int(avg_split[0]),}
{"label": "Average Session Length ({})".format(avg_split[1]), "count": int(avg_split[0]),}
)
time_series_data.update({"chartLabel": "Average Duration of Session (seconds)"})
time_series_data.update({"chartLabel": "Average Session Length ({})".format(avg_split[1])})
return time_series_data
23 changes: 21 additions & 2 deletions posthog/queries/sessions/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ def _determineInterval(interval):
values = [(key, round(value[0])) if len(value) > 0 else (key, 0) for key, value in df_dates.iterrows()]

time_series_data = append_data(values, interval=filter.interval, math=None)
scaled_data, label = scale_time_series(time_series_data["data"])
time_series_data.update({"data": scaled_data})
# calculate average
totals = [sum(x) for x in list(zip(*time_series_avg))[2:4]]
overall_average = (totals[0] / totals[1]) if totals else 0
Expand All @@ -160,12 +162,12 @@ def _determineInterval(interval):

time_series_data.update(
{
"label": "Average Duration of Session ({})".format(avg_split[1]),
"label": "Average Session Length ({})".format(avg_split[1]),
"count": int(avg_split[0]),
"aggregated_value": int(avg_split[0]),
}
)
time_series_data.update({"chartLabel": "Average Duration of Session (seconds)"})
time_series_data.update({"chartLabel": "Average Session Length ({})".format(label)})
result = [time_series_data]
return result

Expand Down Expand Up @@ -193,3 +195,20 @@ def _session_dist(self, base_query: Query, params: QueryParams) -> List[Dict[str
for index in range(len(DIST_LABELS))
]
return result


def scale_time_series(data: List[float]) -> Tuple[List, str]:
_len = len([value for value in data if value > 0])
if _len == 0:
return data, "seconds"

avg = sum(data) / _len
minutes = avg // 60.0
hours = minutes // 60.0

if hours > 0:
return [round(value / 3600, 2) for value in data], "hours"
elif minutes > 0:
return [round(value / 60, 2) for value in data], "minutes"
else:
return data, "seconds"
21 changes: 11 additions & 10 deletions posthog/queries/sessions/test/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ def test_sessions_avg_length(self):

self.assertEqual(response[0]["count"], 3) # average length of all sessions
# time series
self.assertEqual(response[0]["data"][0], 240)
self.assertEqual(response[0]["data"][1], 120)
self.assertEqual(response[0]["data"][0], 4.0)
self.assertEqual(response[0]["data"][1], 2.0)
self.assertEqual(response[0]["labels"][0], "Sat. 14 January")
self.assertEqual(response[0]["labels"][1], "Sun. 15 January")
self.assertEqual(response[0]["days"][0], "2012-01-14")
self.assertEqual(response[0]["days"][1], "2012-01-15")
self.assertEqual(response[0]["chartLabel"], "Average Session Length (minutes)")

def test_sessions_avg_length_interval(self):
with freeze_time("2012-01-14T03:21:34.000Z"):
Expand Down Expand Up @@ -72,8 +73,8 @@ def test_sessions_avg_length_interval(self):
self.team,
)

self.assertEqual(month_response[0]["data"][0], 180)
self.assertEqual(month_response[0]["data"][2], 180)
self.assertEqual(month_response[0]["data"][0], 3.0)
self.assertEqual(month_response[0]["data"][2], 3.0)
self.assertEqual(month_response[0]["labels"][0], "Tue. 31 January")
self.assertEqual(month_response[0]["labels"][1], "Wed. 29 February")
self.assertEqual(month_response[0]["days"][0], "2012-01-31")
Expand All @@ -86,8 +87,8 @@ def test_sessions_avg_length_interval(self):
),
self.team,
)
self.assertEqual(week_response[0]["data"][1], 240.0)
self.assertEqual(week_response[0]["data"][3], 120.0)
self.assertEqual(week_response[0]["data"][1], 4.0)
self.assertEqual(week_response[0]["data"][3], 2.0)
self.assertEqual(week_response[0]["labels"][0], "Sun. 1 January")
self.assertEqual(week_response[0]["labels"][1], "Sun. 8 January")
self.assertEqual(week_response[0]["days"][0], "2012-01-01")
Expand All @@ -100,8 +101,8 @@ def test_sessions_avg_length_interval(self):
),
self.team,
)
self.assertEqual(hour_response[0]["data"][3], 240.0)
self.assertEqual(hour_response[0]["data"][27], 120.0)
self.assertEqual(hour_response[0]["data"][3], 4.0)
self.assertEqual(hour_response[0]["data"][27], 2.0)
self.assertEqual(hour_response[0]["labels"][0], "Wed. 14 March, 00:00")
self.assertEqual(hour_response[0]["labels"][1], "Wed. 14 March, 01:00")
self.assertEqual(hour_response[0]["days"][0], "2012-03-14 00:00:00")
Expand Down Expand Up @@ -141,8 +142,8 @@ def test_compare(self):
# Run without anything to compare to
compare_response = sessions().run(filter=filter, team=self.team)

self.assertEqual(compare_response[0]["data"][5], 120.0)
self.assertEqual(compare_response[1]["data"][4], 240.0)
self.assertEqual(compare_response[0]["data"][5], 2.0)
self.assertEqual(compare_response[1]["data"][4], 4.0)

def test_sessions_count_buckets_default(self):
with freeze_time("2012-01-11T01:25:30.000Z"):
Expand Down