Add minimal argument to get_core_stats
This commit is contained in:
parent
5966ea2f84
commit
e577aa4997
@ -15,9 +15,10 @@ class DashboardApiView(ApiTokenRequiredMixin, DateRangeMixin, View):
|
|||||||
Q(owner=request.user) | Q(collaborators__in=[request.user])
|
Q(owner=request.user) | Q(collaborators__in=[request.user])
|
||||||
).distinct()
|
).distinct()
|
||||||
|
|
||||||
|
minimal = request.GET.get('minimal').lower() in ('1', 'true')
|
||||||
start = self.get_start_date()
|
start = self.get_start_date()
|
||||||
end = self.get_end_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 service_data in services_data:
|
||||||
for key, value in service_data.items():
|
for key, value in service_data.items():
|
||||||
if isinstance(value, QuerySet):
|
if isinstance(value, QuerySet):
|
||||||
|
@ -107,21 +107,21 @@ class Service(models.Model):
|
|||||||
start_time=timezone.now() - timezone.timedelta(days=1)
|
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:
|
if start_time is None:
|
||||||
start_time = timezone.now() - timezone.timedelta(days=30)
|
start_time = timezone.now() - timezone.timedelta(days=30)
|
||||||
if end_time is None:
|
if end_time is None:
|
||||||
end_time = timezone.now()
|
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(
|
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
|
main_data["compare"] = comparison_data
|
||||||
|
|
||||||
return main_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")
|
Session = apps.get_model("analytics", "Session")
|
||||||
Hit = apps.get_model("analytics", "Hit")
|
Hit = apps.get_model("analytics", "Hit")
|
||||||
|
|
||||||
@ -146,6 +146,28 @@ class Service(models.Model):
|
|||||||
bounces = sessions.filter(is_bounce=True)
|
bounces = sessions.filter(is_bounce=True)
|
||||||
bounce_count = bounces.count()
|
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 = (
|
locations = (
|
||||||
hits.values("location")
|
hits.values("location")
|
||||||
.annotate(count=models.Count("location"))
|
.annotate(count=models.Count("location"))
|
||||||
@ -192,17 +214,10 @@ class Service(models.Model):
|
|||||||
.order_by("-count")
|
.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(
|
chart_data, chart_tooltip_format, chart_granularity = self._get_chart_data(
|
||||||
sessions, hits, start_time, end_time, tz_now
|
sessions, hits, start_time, end_time, tz_now
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"currently_online": currently_online,
|
"currently_online": currently_online,
|
||||||
"session_count": session_count,
|
"session_count": session_count,
|
||||||
|
Loading…
Reference in New Issue
Block a user