def get_locale_insights()

in pontoon/insights/utils.py [0:0]


def get_locale_insights(query_filters=None):
    """Get data required by the Locale Insights tab.

    :param django.db.models.Q query_filters: filters insights by given query_filters.

    TODO: Refactor as get_insights(locale, project)
    """
    start_date = get_insight_start_date()
    snapshots = LocaleInsightsSnapshot.objects.filter(created_at__gte=start_date)

    if query_filters:
        snapshots = snapshots.filter(query_filters)

    insights = (
        snapshots
        # Truncate to month and add to select list
        .annotate(month=TruncMonth("created_at"))
        # Group By month
        .values("month")
        # Select the avg/sum of the grouping
        .annotate(unreviewed_lifespan_avg=Avg("unreviewed_suggestions_lifespan"))
        .annotate(
            time_to_review_suggestions_avg=Avg(
                "time_to_review_suggestions",
                filter=Q(time_to_review_suggestions__isnull=False),
            )
        )
        .annotate(
            time_to_review_pretranslations_avg=Avg(
                "time_to_review_pretranslations",
                filter=Q(time_to_review_pretranslations__isnull=False),
            )
        )
        .annotate(completion_avg=Avg("completion"))
        .annotate(human_translations_sum=Sum("human_translations"))
        .annotate(machinery_sum=Sum("machinery_translations"))
        .annotate(new_source_strings_sum=Sum("new_source_strings"))
        .annotate(unreviewed_avg=Avg("unreviewed_strings"))
        .annotate(peer_approved_sum=Sum("peer_approved"))
        .annotate(self_approved_sum=Sum("self_approved"))
        .annotate(rejected_sum=Sum("rejected"))
        .annotate(new_suggestions_sum=Sum("new_suggestions"))
        .annotate(
            pretranslations_chrf_score_avg=Avg(
                "pretranslations_chrf_score",
                filter=Q(pretranslations_chrf_score__isnull=False),
            )
        )
        .annotate(pretranslations_approved_sum=Sum("pretranslations_approved"))
        .annotate(pretranslations_rejected_sum=Sum("pretranslations_rejected"))
        .annotate(pretranslations_new_sum=Sum("pretranslations_new"))
        # Select month and values
        .values(
            "month",
            "unreviewed_lifespan_avg",
            "time_to_review_suggestions_avg",
            "time_to_review_pretranslations_avg",
            "completion_avg",
            "human_translations_sum",
            "machinery_sum",
            "new_source_strings_sum",
            "unreviewed_avg",
            "peer_approved_sum",
            "self_approved_sum",
            "rejected_sum",
            "new_suggestions_sum",
            "pretranslations_chrf_score_avg",
            "pretranslations_approved_sum",
            "pretranslations_rejected_sum",
            "pretranslations_new_sum",
        )
        .order_by("month")
    )

    output = {}
    latest = snapshots.latest("created_at") if snapshots else None

    if latest:
        output.update(
            {
                "total_users": {
                    "managers": latest.total_managers,
                    "reviewers": latest.total_reviewers,
                    "contributors": latest.total_contributors,
                },
                "active_users_last_month": latest.active_users_last_month,
                "active_users_last_3_months": latest.active_users_last_3_months,
                "active_users_last_6_months": latest.active_users_last_6_months,
                "active_users_last_12_months": latest.active_users_last_12_months,
            }
        )
    else:
        output.update(
            {
                "total_users": active_users_default(),
                "active_users_last_month": active_users_default(),
                "active_users_last_3_months": active_users_default(),
                "active_users_last_6_months": active_users_default(),
                "active_users_last_12_months": active_users_default(),
            }
        )

    output.update(
        {
            "dates": [convert_to_unix_time(x["month"]) for x in insights],
            "unreviewed_lifespans": [
                x["unreviewed_lifespan_avg"].days for x in insights
            ],
            "time_to_review_suggestions": json.dumps(
                [
                    get_time_to_review(x["time_to_review_suggestions_avg"])
                    for x in insights
                ]
            ),
            "time_to_review_suggestions_12_month_avg": get_time_to_review_12_month_avg(
                "suggestions", query_filters
            ),
            "time_to_review_pretranslations": json.dumps(
                [
                    get_time_to_review(x["time_to_review_pretranslations_avg"])
                    for x in insights
                ]
            ),
            "time_to_review_pretranslations_12_month_avg": get_time_to_review_12_month_avg(
                "pretranslations", query_filters
            ),
            "translation_activity": {
                "completion": [round(x["completion_avg"], 2) for x in insights],
                "human_translations": [x["human_translations_sum"] for x in insights],
                "machinery_translations": [x["machinery_sum"] for x in insights],
                "new_source_strings": [x["new_source_strings_sum"] for x in insights],
            },
            "review_activity": {
                "unreviewed": [int(round(x["unreviewed_avg"])) for x in insights],
                "peer_approved": [x["peer_approved_sum"] for x in insights],
                "self_approved": [x["self_approved_sum"] for x in insights],
                "rejected": [x["rejected_sum"] for x in insights],
                "new_suggestions": [x["new_suggestions_sum"] for x in insights],
            },
            "pretranslation_quality": {
                "approval_rate": json.dumps([get_approval_rate(x) for x in insights]),
                "chrf_score": json.dumps([get_chrf_score(x) for x in insights]),
                "approved": [x["pretranslations_approved_sum"] for x in insights],
                "rejected": [x["pretranslations_rejected_sum"] for x in insights],
                "new": [x["pretranslations_new_sum"] for x in insights],
            },
        }
    )

    return output