Remove basic option from API

For simplicity
This commit is contained in:
Paweł Jastrzębski 2022-05-12 12:10:44 +02:00
parent ca97453c3e
commit d9bbeea892
2 changed files with 15 additions and 30 deletions

View File

@ -113,21 +113,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, basic=False): def get_core_stats(self, start_time=None, end_time=None):
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, basic) main_data = self.get_relative_stats(start_time, end_time)
comparison_data = self.get_relative_stats( comparison_data = self.get_relative_stats(
start_time - (end_time - start_time), start_time, basic start_time - (end_time - start_time), start_time
) )
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, basic=False): def get_relative_stats(self, start_time, end_time):
Session = apps.get_model("analytics", "Session") Session = apps.get_model("analytics", "Session")
Hit = apps.get_model("analytics", "Hit") Hit = apps.get_model("analytics", "Hit")
@ -152,28 +152,6 @@ 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 basic:
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"))
@ -220,6 +198,14 @@ 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
) )

View File

@ -36,17 +36,16 @@
<p>Service data can be accessed via API on url:</p> <p>Service data can be accessed via API on url:</p>
<code>{{script_protocol}}{{request.get_host}}{% url 'api:services' %}?uuid={{object.uuid}}</code> <code>{{script_protocol}}{{request.get_host}}{% url 'api:services' %}?uuid={{object.uuid}}</code>
<p> <p>
There are 3 optional query parameters: There are 2 optional query parameters:
<ul> <ul>
<li>startDate - to set start date in format YYYY-MM-DD</li> <li>startDate - to set start date in format YYYY-MM-DD</li>
<li>endDate - to set end date in format YYYY-MM-DD</li> <li>endDate - to set end date in format YYYY-MM-DD</li>
<li>basic - to get only basic data set to '1' or 'true'</li>
</ul> </ul>
</p> </p>
<p>Example using HTTPie:</p> <p>Example using HTTPie:</p>
<code>http get '{{script_protocol}}{{request.get_host}}{% url 'api:services' %}?uuid={{object.uuid}}&startDate=2021-01-01&endDate=2050-01-01&basic=1' 'Authorization:Token {{request.user.api_token}}'</code> <code>http get '{{script_protocol}}{{request.get_host}}{% url 'api:services' %}?uuid={{object.uuid}}&startDate=2021-01-01&endDate=2050-01-01' 'Authorization:Token {{request.user.api_token}}'</code>
<p>Example using cURL:</p> <p>Example using cURL:</p>
<code>curl -H 'Authorization:Token {{request.user.api_token}}' '{{script_protocol}}{{request.get_host}}{% url 'api:services' %}?uuid={{object.uuid}}&startDate=2021-01-01&endDate=2050-01-01&basic=1'</code> <code>curl -H 'Authorization:Token {{request.user.api_token}}' '{{script_protocol}}{{request.get_host}}{% url 'api:services' %}?uuid={{object.uuid}}&startDate=2021-01-01&endDate=2050-01-01'</code>
</div> </div>
</div> </div>
{% endblock %} {% endblock %}