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

Rework the histogram activity chart #5685

Closed
wants to merge 1 commit into from
Closed
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
60 changes: 56 additions & 4 deletions temba/flows/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
trim_flow_sessions,
update_session_wait_expires,
)
from .views import FlowCRUDL


class FlowTest(TembaTest, CRUDLTestMixin):
Expand Down Expand Up @@ -2888,9 +2887,6 @@ def test_results(self):
self.assertEqual("Color", counts[0]["name"])
self.assertEqual(2, counts[0]["total"])

FlowCRUDL.ActivityChart.HISTOGRAM_MIN = 0
FlowCRUDL.ActivityChart.PERIOD_MIN = 0
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are always zero...


# and some charts
response = self.client.get(reverse("flows.flow_activity_data", args=[flow.id]))
data = response.json()
Expand Down Expand Up @@ -2969,6 +2965,62 @@ def test_results(self):
response = self.client.get(reverse("flows.flow_category_counts", args=[flow.uuid]))
self.assertEqual(404, response.status_code)

def test_activity_data(self):
flow1 = self.create_flow("Test 1")

data_url = reverse("flows.flow_activity_data", args=[flow1.id])

self.assertRequestDisallowed(data_url, [None, self.agent])

# check with no data
response = self.assertReadFetch(data_url, [self.user, self.editor, self.admin])
self.assertEqual(
{
"min_date": 1729900800000,
"summary": {
"responses": 0,
"active": 0,
"waiting": 0,
"completed": 0,
"expired": 0,
"interrupted": 0,
"failed": 0,
"title": "0 Responses",
},
"dow": [
{"name": "Sunday", "msgs": 0, "y": 0},
{"name": "Monday", "msgs": 0, "y": 0},
{"name": "Tuesday", "msgs": 0, "y": 0},
{"name": "Wednesday", "msgs": 0, "y": 0},
{"name": "Thursday", "msgs": 0, "y": 0},
{"name": "Friday", "msgs": 0, "y": 0},
{"name": "Saturday", "msgs": 0, "y": 0},
],
"hod": [[i, 0] for i in range(24)],
"histogram": [],
"completion": {
"summary": [
{"name": "Active", "y": 0, "drilldown": None, "color": "#2387CA"},
{"name": "Completed", "y": 0, "drilldown": None, "color": "#8FC93A"},
{"name": "Interrupted, Expired and Failed", "y": 0, "drilldown": "incomplete", "color": "#CCC"},
],
"drilldown": [
{
"name": "Interrupted, Expired and Failed",
"id": "incomplete",
"innerSize": "50%",
"data": [
{"name": "Expired", "y": 0, "color": "#CCC"},
{"name": "Interrupted", "y": 0, "color": "#EEE"},
{"name": "Failed", "y": 0, "color": "#FEE"},
],
}
],
},
},
response.json(),
)

def test_activity(self):
flow = self.get_flow("favorites_v13")
flow_nodes = flow.get_definition()["nodes"]
Expand Down
83 changes: 41 additions & 42 deletions temba/flows/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
from django.conf import settings
from django.contrib.humanize.templatetags import humanize
from django.core.exceptions import ValidationError
from django.db.models import Max, Min, Sum
from django.db.models import Min, Sum
from django.db.models.functions import Lower
from django.http import Http404, HttpResponse, HttpResponseRedirect, JsonResponse
from django.urls import reverse
from django.utils import timezone
from django.utils.encoding import force_str
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _, ngettext_lazy as _p
Expand Down Expand Up @@ -1146,12 +1147,6 @@
)

class ActivityData(BaseReadView):
# the min number of responses to show a histogram
HISTOGRAM_MIN = 0

# the min number of responses to show the period charts
PERIOD_MIN = 0

permission = "flows.flow_results"

day_names = (
Expand All @@ -1164,7 +1159,7 @@
_("Saturday"),
)

def get_day_of_week_counts(self, exit_uuids: list) -> dict:
def get_day_of_week_counts(self, exit_uuids: list) -> dict[int, int]:
dow = (
self.object.path_counts.filter(from_uuid__in=exit_uuids)
.extra({"day": "extract(dow from period::timestamp)"})
Expand All @@ -1174,7 +1169,7 @@

return {int(d.get("day")): d.get("count") for d in dow}

def get_hour_of_day_counts(self, exit_uuids: list) -> dict:
def get_hour_of_day_counts(self, exit_uuids: list) -> dict[int, int]:
hod = (
self.object.path_counts.filter(from_uuid__in=exit_uuids)
.extra({"hour": "extract(hour from period::timestamp)"})
Expand All @@ -1185,14 +1180,21 @@

return {int(h.get("hour")): h.get("count") for h in hod}

def get_date_counts(self, exit_uuids: list, truncate: str) -> list[tuple]:
dates = (
self.object.path_counts.filter(from_uuid__in=exit_uuids)
.extra({"date": f"date_trunc('{truncate}', period::date)"})
.values("date")
.annotate(count=Sum("count"))
.order_by("date")
)

return [(d.get("date"), d.get("count")) for d in dates]

def render_to_response(self, context, **response_kwargs):
total_responses = 0
flow = self.object

exit_uuids = flow.metadata["waiting_exit_uuids"]
dates = self.object.path_counts.filter(from_uuid__in=exit_uuids).aggregate(Max("period"), Min("period"))
start_date = dates.get("period__min")
end_date = dates.get("period__max")

hour_dict = self.get_hour_of_day_counts(exit_uuids)
hours = []
Expand All @@ -1206,34 +1208,33 @@
dow.append({"name": x, "msgs": day_count})
total_responses += day_count

if total_responses > self.PERIOD_MIN:
dow = sorted(dow, key=lambda k: k["name"])
dow = [
{
"name": self.day_names[d["name"]],
"msgs": d["msgs"],
"y": 100 * float(d["msgs"]) / float(total_responses),
}
for d in dow
]

min_date = None
histogram = []

if total_responses > self.HISTOGRAM_MIN:
# our main histogram
date_range = end_date - start_date
histogram = self.object.path_counts.filter(from_uuid__in=exit_uuids)
dow = [
{
"name": self.day_names[d["name"]],
"msgs": d["msgs"],
"y": (100 * float(d["msgs"]) / float(total_responses)) if total_responses else 0,
}
for d in dow
]

# figure out the earliest date we have data for
min_date = self.object.path_counts.filter(from_uuid__in=exit_uuids).aggregate(Min("period"))
min_date = min_date.get("period__min")
if min_date:
min_date = min_date.date()
else:
min_date = timezone.now().date() - timedelta(days=30)

if date_range < timedelta(days=500):
histogram = histogram.extra({"bucket": "date_trunc('day', period)"})
min_date = end_date - timedelta(days=100)
else:
histogram = histogram.extra({"bucket": "date_trunc('week', period)"})
min_date = end_date - timedelta(days=500)
# bucket dates into months or weeks depending on the range
if min_date < timezone.now().date() - timedelta(days=365 * 3):
truncate = "month"

Check warning on line 1230 in temba/flows/views.py

View check run for this annotation

Codecov / codecov/patch

temba/flows/views.py#L1230

Added line #L1230 was not covered by tests
elif min_date < timezone.now().date() - timedelta(days=365):
truncate = "week"
else:
truncate = "day"

histogram = histogram.values("bucket").annotate(count=Sum("count")).order_by("bucket")
histogram = [[_["bucket"], _["count"]] for _ in histogram]
dates = self.get_date_counts(exit_uuids, truncate)
min_date = dates[0][0] if dates else timezone.now().date() - timedelta(days=30)

summary = {
"responses": total_responses,
Expand Down Expand Up @@ -1279,13 +1280,11 @@

return JsonResponse(
{
"start_date": start_date,
"end_date": end_date,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not used

"min_date": min_date,
"summary": summary,
"dow": dow,
"hod": hours,
"histogram": histogram,
"histogram": dates,
"completion": completion,
},
json_dumps_params={"indent": 2},
Expand Down
Loading