fix: analytics export and send month and year when dimension in date (#1039)

* fix: send month and year when dimension in date

* fix: export csv email

* fix: export for segment and fix segment for date values
This commit is contained in:
pablohashescobar 2023-05-15 11:37:07 +05:30 committed by GitHub
parent 9fdc56db0f
commit 63075a6a0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 26 deletions

View File

@ -27,6 +27,7 @@ row_mapping = {
"completed_at": "Completed At", "completed_at": "Completed At",
"created_at": "Created At", "created_at": "Created At",
"issue_count": "Issue Count", "issue_count": "Issue Count",
"priority": "Priority",
"effort": "Effort", "effort": "Effort",
} }
@ -48,9 +49,6 @@ def analytic_export_task(email, data, slug):
key = "count" if y_axis == "issue_count" else "effort" key = "count" if y_axis == "issue_count" else "effort"
if segment: if segment:
row_zero = [
row_mapping.get(x_axis, "X-Axis"),
]
segment_zero = [] segment_zero = []
for item in distribution: for item in distribution:
current_dict = distribution.get(item) current_dict = distribution.get(item)
@ -58,18 +56,28 @@ def analytic_export_task(email, data, slug):
segment_zero.append(current.get("segment")) segment_zero.append(current.get("segment"))
segment_zero = list(set(segment_zero)) segment_zero = list(set(segment_zero))
row_zero = row_zero + segment_zero row_zero = (
[
row_mapping.get(x_axis, "X-Axis"),
]
+ [
row_mapping.get(y_axis, "Y-Axis"),
]
+ segment_zero
)
rows = [] rows = []
for item in distribution: for item in distribution:
generated_row = [] generated_row = [
item,
]
data = distribution.get(item) data = distribution.get(item)
for segment in segment_zero[1:]: for segment in segment_zero[1:]:
value = [x for x in data if x.get("segment") == segment] value = [x for x in data if x.get("segment") == segment]
if len(value): if len(value):
generated_row.append(value[0].get(key)) generated_row.append(value[0].get(key))
else: else:
generated_row.append("") generated_row.append("0")
rows.append(tuple(generated_row)) rows.append(tuple(generated_row))
@ -86,7 +94,7 @@ def analytic_export_task(email, data, slug):
html_content = render_to_string("emails/exports/analytics.html", {}) html_content = render_to_string("emails/exports/analytics.html", {})
text_content = strip_tags(html_content) text_content = strip_tags(html_content)
csv_buffer.seek(0)
msg = EmailMultiAlternatives( msg = EmailMultiAlternatives(
subject, text_content, settings.EMAIL_FROM, [email] subject, text_content, settings.EMAIL_FROM, [email]
) )
@ -125,13 +133,13 @@ def analytic_export_task(email, data, slug):
text_content = strip_tags(html_content) text_content = strip_tags(html_content)
csv_buffer.seek(0)
msg = EmailMultiAlternatives( msg = EmailMultiAlternatives(
subject, text_content, settings.EMAIL_FROM, [email] subject, text_content, settings.EMAIL_FROM, [email]
) )
msg.attach(f"{slug}-analytics.csv", csv_buffer.read()) msg.attach(f"{slug}-analytics.csv", csv_buffer.read())
msg.send(fail_silently=False) msg.send(fail_silently=False)
except Exception as e: except Exception as e:
print(e) print(e)
capture_exception(e) capture_exception(e)

View File

@ -3,13 +3,16 @@ from itertools import groupby
# Django import # Django import
from django.db import models from django.db import models
from django.db.models import Count, DateField, F, Sum, Value, Case, When, Q from django.db.models import Count, F, Sum, Value, Case, When, CharField
from django.db.models.functions import Cast, Concat, Coalesce from django.db.models.functions import Coalesce, ExtractMonth, ExtractYear, Concat
def build_graph_plot(queryset, x_axis, y_axis, segment=None): def build_graph_plot(queryset, x_axis, y_axis, segment=None):
if x_axis in ["created_at", "completed_at"]: if x_axis in ["created_at", "start_date", "target_date", "completed_at"]:
queryset = queryset.annotate(dimension=Cast(x_axis, DateField())) year = ExtractYear(x_axis)
month = ExtractMonth(x_axis)
dimension = Concat(year, Value("-"), month, output_field=CharField())
queryset = queryset.annotate(dimension=dimension)
x_axis = "dimension" x_axis = "dimension"
else: else:
queryset = queryset.annotate(dimension=F(x_axis)) queryset = queryset.annotate(dimension=F(x_axis))
@ -18,34 +21,41 @@ def build_graph_plot(queryset, x_axis, y_axis, segment=None):
if x_axis in ["created_at", "start_date", "target_date", "completed_at"]: if x_axis in ["created_at", "start_date", "target_date", "completed_at"]:
queryset = queryset.exclude(x_axis__is_null=True) queryset = queryset.exclude(x_axis__is_null=True)
if segment in ["created_at", "start_date", "target_date", "completed_at"]:
year = ExtractYear(segment)
month = ExtractMonth(segment)
dimension = Concat(year, Value("-"), month, output_field=CharField())
queryset = queryset.annotate(segmented=dimension)
segment = "segmented"
queryset = queryset.values(x_axis) queryset = queryset.values(x_axis)
# Group queryset by x_axis field # Group queryset by x_axis field
if y_axis == "issue_count": if y_axis == "issue_count":
queryset = ( queryset = queryset.annotate(
queryset.annotate( is_null=Case(
is_null=Case( When(dimension__isnull=True, then=Value("None")),
When(dimension__isnull=True, then=Value("None")), default=Value("not_null"),
default=Value("not_null"), output_field=models.CharField(max_length=8),
output_field=models.CharField(max_length=8), ),
), dimension_ex=Coalesce("dimension", Value("null")),
dimension_ex=Coalesce("dimension", Value("null")), ).values("dimension")
)
.values("dimension")
)
if segment: if segment:
queryset = queryset.annotate(segment=F(segment)).values("dimension", "segment") queryset = queryset.annotate(segment=F(segment)).values(
"dimension", "segment"
)
else: else:
queryset = queryset.values("dimension") queryset = queryset.values("dimension")
queryset = queryset.annotate(count=Count("*")).order_by("dimension") queryset = queryset.annotate(count=Count("*")).order_by("dimension")
if y_axis == "effort": if y_axis == "effort":
queryset = queryset.annotate(effort=Sum("estimate_point")).order_by(x_axis) queryset = queryset.annotate(effort=Sum("estimate_point")).order_by(x_axis)
if segment: if segment:
queryset = queryset.annotate(segment=F(segment)).values("dimension", "segment", "effort") queryset = queryset.annotate(segment=F(segment)).values(
"dimension", "segment", "effort"
)
else: else:
queryset = queryset.values("dimension", "effort") queryset = queryset.values("dimension", "effort")