From 63075a6a0d01d2f10e755765e053bae23d8ce0a1 Mon Sep 17 00:00:00 2001 From: pablohashescobar <118773738+pablohashescobar@users.noreply.github.com> Date: Mon, 15 May 2023 11:37:07 +0530 Subject: [PATCH] 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 --- .../plane/bgtasks/analytic_plot_export.py | 24 ++++++---- apiserver/plane/utils/analytics_plot.py | 46 +++++++++++-------- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/apiserver/plane/bgtasks/analytic_plot_export.py b/apiserver/plane/bgtasks/analytic_plot_export.py index f34f28c62..ca8ee1ab6 100644 --- a/apiserver/plane/bgtasks/analytic_plot_export.py +++ b/apiserver/plane/bgtasks/analytic_plot_export.py @@ -27,6 +27,7 @@ row_mapping = { "completed_at": "Completed At", "created_at": "Created At", "issue_count": "Issue Count", + "priority": "Priority", "effort": "Effort", } @@ -48,9 +49,6 @@ def analytic_export_task(email, data, slug): key = "count" if y_axis == "issue_count" else "effort" if segment: - row_zero = [ - row_mapping.get(x_axis, "X-Axis"), - ] segment_zero = [] for item in distribution: current_dict = distribution.get(item) @@ -58,18 +56,28 @@ def analytic_export_task(email, data, slug): segment_zero.append(current.get("segment")) 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 = [] for item in distribution: - generated_row = [] + generated_row = [ + item, + ] data = distribution.get(item) for segment in segment_zero[1:]: value = [x for x in data if x.get("segment") == segment] if len(value): generated_row.append(value[0].get(key)) else: - generated_row.append("") + generated_row.append("0") 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", {}) text_content = strip_tags(html_content) - + csv_buffer.seek(0) msg = EmailMultiAlternatives( subject, text_content, settings.EMAIL_FROM, [email] ) @@ -125,13 +133,13 @@ def analytic_export_task(email, data, slug): text_content = strip_tags(html_content) + csv_buffer.seek(0) msg = EmailMultiAlternatives( subject, text_content, settings.EMAIL_FROM, [email] ) msg.attach(f"{slug}-analytics.csv", csv_buffer.read()) msg.send(fail_silently=False) - except Exception as e: print(e) capture_exception(e) diff --git a/apiserver/plane/utils/analytics_plot.py b/apiserver/plane/utils/analytics_plot.py index 408b41d4b..838f535ff 100644 --- a/apiserver/plane/utils/analytics_plot.py +++ b/apiserver/plane/utils/analytics_plot.py @@ -3,13 +3,16 @@ from itertools import groupby # Django import from django.db import models -from django.db.models import Count, DateField, F, Sum, Value, Case, When, Q -from django.db.models.functions import Cast, Concat, Coalesce +from django.db.models import Count, F, Sum, Value, Case, When, CharField +from django.db.models.functions import Coalesce, ExtractMonth, ExtractYear, Concat def build_graph_plot(queryset, x_axis, y_axis, segment=None): - if x_axis in ["created_at", "completed_at"]: - queryset = queryset.annotate(dimension=Cast(x_axis, DateField())) + if x_axis in ["created_at", "start_date", "target_date", "completed_at"]: + year = ExtractYear(x_axis) + month = ExtractMonth(x_axis) + dimension = Concat(year, Value("-"), month, output_field=CharField()) + queryset = queryset.annotate(dimension=dimension) x_axis = "dimension" else: 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"]: 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) # Group queryset by x_axis field if y_axis == "issue_count": - queryset = ( - queryset.annotate( - is_null=Case( - When(dimension__isnull=True, then=Value("None")), - default=Value("not_null"), - output_field=models.CharField(max_length=8), - ), - dimension_ex=Coalesce("dimension", Value("null")), - ) - .values("dimension") - ) + queryset = queryset.annotate( + is_null=Case( + When(dimension__isnull=True, then=Value("None")), + default=Value("not_null"), + output_field=models.CharField(max_length=8), + ), + dimension_ex=Coalesce("dimension", Value("null")), + ).values("dimension") if segment: - queryset = queryset.annotate(segment=F(segment)).values("dimension", "segment") + queryset = queryset.annotate(segment=F(segment)).values( + "dimension", "segment" + ) else: queryset = queryset.values("dimension") queryset = queryset.annotate(count=Count("*")).order_by("dimension") - if y_axis == "effort": queryset = queryset.annotate(effort=Sum("estimate_point")).order_by(x_axis) if segment: - queryset = queryset.annotate(segment=F(segment)).values("dimension", "segment", "effort") + queryset = queryset.annotate(segment=F(segment)).values( + "dimension", "segment", "effort" + ) else: queryset = queryset.values("dimension", "effort")