Exemple #1
0
        def get(self, request, *args, **kwargs):
            with_activity = str_to_bool(
                self.request.GET.get("with_activity", ""))
            labels = list(
                Label.get_all(self.request.org,
                              self.request.user).order_by("name"))
            Label.bulk_cache_initialize(labels)

            if with_activity:
                # get message statistics
                this_month = DailyCount.get_by_label(
                    labels, DailyCount.TYPE_INCOMING,
                    *month_range(0)).scope_totals()
                last_month = DailyCount.get_by_label(
                    labels, DailyCount.TYPE_INCOMING,
                    *month_range(-1)).scope_totals()

            def as_json(label):
                obj = label.as_json()
                if with_activity:
                    obj["activity"] = {
                        "this_month": this_month.get(label, 0),
                        "last_month": last_month.get(label, 0)
                    }
                return obj

            return JsonResponse({"results": [as_json(l) for l in labels]})
Exemple #2
0
        def get(self, request, *args, **kwargs):
            org = request.org
            partner_id = request.GET.get("partner")
            non_partner = str_to_bool(self.request.GET.get("non_partner", ""))
            with_activity = str_to_bool(
                self.request.GET.get("with_activity", ""))

            if non_partner:
                users = org.administrators.all()
            elif partner_id:
                users = Partner.objects.get(org=org, pk=partner_id).get_users()
            else:
                users = org.get_users()

            users = list(users.order_by("profile__full_name"))

            # get reply statistics
            if with_activity:
                replies_total = TotalCount.get_by_user(
                    org, users, DailyCount.TYPE_REPLIES).scope_totals()
                replies_this_month = DailyCount.get_by_user(
                    org, users, DailyCount.TYPE_REPLIES,
                    *month_range(0)).scope_totals()
                replies_last_month = DailyCount.get_by_user(
                    org, users, DailyCount.TYPE_REPLIES,
                    *month_range(-1)).scope_totals()

                cases_total = TotalCount.get_by_user(
                    org, users, DailyCount.TYPE_CASE_OPENED).scope_totals()
                cases_opened_this_month = DailyCount.get_by_user(
                    org, users, DailyCount.TYPE_CASE_OPENED,
                    *month_range(0)).scope_totals()
                cases_closed_this_month = DailyCount.get_by_user(
                    org, users, DailyCount.TYPE_CASE_CLOSED,
                    *month_range(0)).scope_totals()

            def as_json(user):
                obj = user.as_json(full=True, org=org)
                if with_activity:
                    obj.update({
                        "replies": {
                            "this_month": replies_this_month.get(user, 0),
                            "last_month": replies_last_month.get(user, 0),
                            "total": replies_total.get(user, 0),
                        },
                        "cases": {
                            "opened_this_month":
                            cases_opened_this_month.get(user, 0),
                            "closed_this_month":
                            cases_closed_this_month.get(user, 0),
                            "total":
                            cases_total.get(user, 0),
                        },
                    })

                return obj

            return JsonResponse({"results": [as_json(u) for u in users]})
Exemple #3
0
        def render_as_json(self, partners, with_activity):
            if with_activity:
                # get reply statistics
                total = DailyCount.get_by_partner(partners, DailyCount.TYPE_REPLIES, None, None).scope_totals()
                this_month = DailyCount.get_by_partner(partners, DailyCount.TYPE_REPLIES,
                                                       *month_range(0)).scope_totals()
                last_month = DailyCount.get_by_partner(partners, DailyCount.TYPE_REPLIES,
                                                       *month_range(-1)).scope_totals()

            def as_json(partner):
                obj = partner.as_json()
                if with_activity:
                    obj['replies'] = {
                        'this_month': this_month.get(partner, 0),
                        'last_month': last_month.get(partner, 0),
                        'total': total.get(partner, 0)
                    }
                return obj

            return JsonResponse({'results': [as_json(p) for p in partners]})
Exemple #4
0
    def get_data(self, request):
        now = timezone.now()

        since = month_range(-(self.num_months - 1))[0]  # last X months including this month
        totals = self.get_month_totals(request, since)
        totals_by_month = {t[0]: t[1] for t in totals}

        # generate category labels and series over last X months
        categories = []
        series = []
        this_month = now.date().month
        for m in reversed(range(0, -self.num_months, -1)):
            month = this_month + m
            if month < 1:
                month += 12
            categories.append(six.text_type(MONTH_NAMES[month - 1]))
            series.append(totals_by_month.get(month, 0))

        return {'categories': categories, 'series': series}
Exemple #5
0
        def render_as_json(self, partners, with_activity):
            if with_activity:
                # get reply statistics
                replies_total = DailyCount.get_by_partner(
                    partners, DailyCount.TYPE_REPLIES, None,
                    None).scope_totals()
                replies_this_month = DailyCount.get_by_partner(
                    partners, DailyCount.TYPE_REPLIES,
                    *month_range(0)).scope_totals()
                replies_last_month = DailyCount.get_by_partner(
                    partners, DailyCount.TYPE_REPLIES,
                    *month_range(-1)).scope_totals()
                average_referral_response_time_this_month = DailySecondTotalCount.get_by_partner(
                    partners, DailySecondTotalCount.TYPE_TILL_REPLIED,
                    *month_range(0))
                average_referral_response_time_this_month = average_referral_response_time_this_month.scope_averages(
                )
                average_closed_this_month = DailySecondTotalCount.get_by_partner(
                    partners, DailySecondTotalCount.TYPE_TILL_CLOSED,
                    *month_range(0))
                average_closed_this_month = average_closed_this_month.scope_averages(
                )

                # get cases statistics
                cases_total = DailyCount.get_by_partner(
                    partners, DailyCount.TYPE_CASE_OPENED, None,
                    None).scope_totals()
                cases_opened_this_month = DailyCount.get_by_partner(
                    partners, DailyCount.TYPE_CASE_OPENED,
                    *month_range(0)).scope_totals()
                cases_closed_this_month = DailyCount.get_by_partner(
                    partners, DailyCount.TYPE_CASE_CLOSED,
                    *month_range(0)).scope_totals()

            def as_json(partner):
                obj = partner.as_json()
                if with_activity:
                    obj.update({
                        "replies": {
                            "this_month":
                            replies_this_month.get(partner, 0),
                            "last_month":
                            replies_last_month.get(partner, 0),
                            "total":
                            replies_total.get(partner, 0),
                            "average_referral_response_time_this_month":
                            humanize_seconds(
                                average_referral_response_time_this_month.get(
                                    partner, 0)),
                        },
                        "cases": {
                            "average_closed_this_month":
                            humanize_seconds(
                                average_closed_this_month.get(partner, 0)),
                            "opened_this_month":
                            cases_opened_this_month.get(partner, 0),
                            "closed_this_month":
                            cases_closed_this_month.get(partner, 0),
                            "total":
                            cases_total.get(partner, 0),
                        },
                    })
                return obj

            return JsonResponse({"results": [as_json(p) for p in partners]})
Exemple #6
0
        def get(self, request, *args, **kwargs):
            org = request.org
            partner_id = request.GET.get('partner')
            non_partner = str_to_bool(self.request.GET.get('non_partner', ''))
            with_activity = str_to_bool(
                self.request.GET.get('with_activity', ''))

            if non_partner:
                users = org.administrators.all()
            elif partner_id:
                users = Partner.objects.get(org=org, pk=partner_id).get_users()
            else:
                users = org.get_users()

            users = list(users.order_by('profile__full_name'))

            # get reply statistics
            if with_activity:
                replies_total = DailyCount.get_by_user(org, users,
                                                       DailyCount.TYPE_REPLIES,
                                                       None,
                                                       None).scope_totals()
                replies_this_month = DailyCount.get_by_user(
                    org, users, DailyCount.TYPE_REPLIES,
                    *month_range(0)).scope_totals()
                replies_last_month = DailyCount.get_by_user(
                    org, users, DailyCount.TYPE_REPLIES,
                    *month_range(-1)).scope_totals()

                cases_total = DailyCount.get_by_user(
                    org, users, DailyCount.TYPE_CASE_OPENED, None,
                    None).scope_totals()
                cases_opened_this_month = DailyCount.get_by_user(
                    org, users, DailyCount.TYPE_CASE_OPENED,
                    *month_range(0)).scope_totals()
                cases_closed_this_month = DailyCount.get_by_user(
                    org, users, DailyCount.TYPE_CASE_CLOSED,
                    *month_range(0)).scope_totals()

            def as_json(user):
                obj = user.as_json(full=True, org=org)
                if with_activity:
                    obj.update({
                        'replies': {
                            'this_month': replies_this_month.get(user, 0),
                            'last_month': replies_last_month.get(user, 0),
                            'total': replies_total.get(user, 0)
                        },
                        'cases': {
                            'opened_this_month':
                            cases_opened_this_month.get(user, 0),
                            'closed_this_month':
                            cases_closed_this_month.get(user, 0),
                            'total':
                            cases_total.get(user, 0)
                        },
                    })

                return obj

            return JsonResponse({'results': [as_json(u) for u in users]})
Exemple #7
0
        def get(self, request, *args, **kwargs):
            with_activity = str_to_bool(self.request.GET.get('with_activity', ''))
            labels = list(Label.get_all(self.request.org, self.request.user).order_by('name'))
            Label.bulk_cache_initialize(labels)

            if with_activity:
                # get message statistics
                this_month = DailyCount.get_by_label(labels, DailyCount.TYPE_INCOMING, *month_range(0)).scope_totals()
                last_month = DailyCount.get_by_label(labels, DailyCount.TYPE_INCOMING, *month_range(-1)).scope_totals()

            def as_json(label):
                obj = label.as_json()
                if with_activity:
                    obj['activity'] = {
                        'this_month': this_month.get(label, 0),
                        'last_month': last_month.get(label, 0),
                    }
                return obj

            return JsonResponse({'results': [as_json(l) for l in labels]})