Add minimal argument to get_core_stats

This commit is contained in:
Paweł Jastrzębski 2021-10-13 19:52:36 +02:00 committed by R. Miles McCain
parent 5966ea2f84
commit e577aa4997
2 changed files with 29 additions and 13 deletions

View File

@ -15,9 +15,10 @@ class DashboardApiView(ApiTokenRequiredMixin, DateRangeMixin, View):
Q(owner=request.user) | Q(collaborators__in=[request.user])
).distinct()
minimal = request.GET.get('minimal').lower() in ('1', 'true')
start = self.get_start_date()
end = self.get_end_date()
services_data = [s.get_core_stats(start, end) for s in services]
services_data = [s.get_core_stats(start, end, minimal) for s in services]
for service_data in services_data:
for key, value in service_data.items():
if isinstance(value, QuerySet):

View File

@ -107,21 +107,21 @@ class Service(models.Model):
start_time=timezone.now() - timezone.timedelta(days=1)
)
def get_core_stats(self, start_time=None, end_time=None):
def get_core_stats(self, start_time=None, end_time=None, minimal=False):
if start_time is None:
start_time = timezone.now() - timezone.timedelta(days=30)
if end_time is None:
end_time = timezone.now()
main_data = self.get_relative_stats(start_time, end_time)
main_data = self.get_relative_stats(start_time, end_time, minimal)
comparison_data = self.get_relative_stats(
start_time - (end_time - start_time), start_time
start_time - (end_time - start_time), start_time, minimal
)
main_data["compare"] = comparison_data
return main_data
def get_relative_stats(self, start_time, end_time):
def get_relative_stats(self, start_time, end_time, minimal=False):
Session = apps.get_model("analytics", "Session")
Hit = apps.get_model("analytics", "Hit")
@ -146,6 +146,28 @@ class Service(models.Model):
bounces = sessions.filter(is_bounce=True)
bounce_count = bounces.count()
avg_load_time = hits.aggregate(load_time__avg=models.Avg("load_time"))[
"load_time__avg"
]
avg_hits_per_session = hit_count / session_count if session_count > 0 else None
avg_session_duration = self._get_avg_session_duration(sessions, session_count)
if minimal:
return {
"currently_online": currently_online,
"session_count": session_count,
"hit_count": hit_count,
"has_hits": has_hits,
"bounce_rate_pct": bounce_count * 100 / session_count
if session_count > 0
else None,
"avg_session_duration": avg_session_duration,
"avg_load_time": avg_load_time,
"avg_hits_per_session": avg_hits_per_session,
"online": True,
}
locations = (
hits.values("location")
.annotate(count=models.Count("location"))
@ -192,17 +214,10 @@ class Service(models.Model):
.order_by("-count")
)
avg_load_time = hits.aggregate(load_time__avg=models.Avg("load_time"))[
"load_time__avg"
]
avg_hits_per_session = hit_count / session_count if session_count > 0 else None
avg_session_duration = self._get_avg_session_duration(sessions, session_count)
chart_data, chart_tooltip_format, chart_granularity = self._get_chart_data(
sessions, hits, start_time, end_time, tz_now
)
return {
"currently_online": currently_online,
"session_count": session_count,