Exemplo n.º 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]})
Exemplo n.º 2
0
 def get_summary(self, org):
     return {
         'total_incoming': DailyCount.get_by_org([org], DailyCount.TYPE_INCOMING).total(),
         'total_replies': DailyCount.get_by_org([org], DailyCount.TYPE_REPLIES).total(),
         'cases_open': Case.objects.filter(org=org, closed_on=None).count(),
         'cases_closed': Case.objects.filter(org=org).exclude(closed_on=None).count()
     }
Exemplo n.º 3
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]})
Exemplo n.º 4
0
    def clear_labels(self):
        """
        Removes all labels from this message
        """
        from casepro.statistics.models import DailyCount, datetime_to_date

        day = datetime_to_date(self.created_on, self.org)
        for label in self.labels.all():
            DailyCount.record_removal(day, DailyCount.TYPE_INCOMING, label)

        Labelling.objects.filter(message=self).delete()
Exemplo n.º 5
0
 def get_summary(self, org):
     return {
         "total_incoming":
         DailyCount.get_by_org([org], DailyCount.TYPE_INCOMING).total(),
         "total_replies":
         DailyCount.get_by_org([org], DailyCount.TYPE_REPLIES).total(),
         "cases_open":
         Case.objects.filter(org=org, closed_on=None).count(),
         "cases_closed":
         Case.objects.filter(org=org).exclude(closed_on=None).count(),
     }
Exemplo n.º 6
0
    def unlabel(self, *labels):
        """
        Removes the given labels from this message
        """
        from casepro.statistics.models import DailyCount, datetime_to_date

        existing_labellings = Labelling.objects.filter(
            message=self, label__in=labels).select_related("label")

        day = datetime_to_date(self.created_on, self.org)
        for labelling in existing_labellings:
            DailyCount.record_removal(day, DailyCount.TYPE_INCOMING,
                                      labelling.label)

        Labelling.objects.filter(
            id__in=[l.id for l in existing_labellings]).delete()
Exemplo n.º 7
0
        def get(self, request, *args, **kwargs):
            with_activity = str_to_bool(self.request.GET.get('with_activity', ''))
            labels = Label.get_all(self.request.org, self.request.user).order_by('name')

            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['messages'] = {
                        '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]})
Exemplo n.º 8
0
    def label(self, *labels):
        """
        Adds the given labels to this message
        """
        from casepro.profiles.models import Notification
        from casepro.statistics.models import DailyCount, datetime_to_date

        existing_label_ids = Labelling.objects.filter(
            message=self, label__in=labels).values_list("label", flat=True)
        add_labels = [l for l in labels if l.id not in existing_label_ids]
        new_labellings = [Labelling(message=self, label=l) for l in add_labels]
        Labelling.objects.bulk_create(new_labellings)

        day = datetime_to_date(self.created_on, self.org)
        for label in add_labels:
            DailyCount.record_item(day, DailyCount.TYPE_INCOMING, label)

        # notify all users who watch these labels
        for watcher in set(User.objects.filter(watched_labels__in=labels)):
            Notification.new_message_labelling(self.org, watcher, self)
Exemplo n.º 9
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]})
Exemplo n.º 10
0
 def get_summary(self, partner):
     return {
         "total_replies":
         DailyCount.get_by_partner([partner],
                                   DailyCount.TYPE_REPLIES).total(),
         "cases_open":
         Case.objects.filter(org=partner.org,
                             assignee=partner,
                             closed_on=None).count(),
         "cases_closed":
         Case.objects.filter(
             org=partner.org,
             assignee=partner).exclude(closed_on=None).count(),
     }
Exemplo n.º 11
0
 def get_summary(self, org, user):
     return {
         "total_replies":
         DailyCount.get_by_user(org, [user], DailyCount.TYPE_REPLIES,
                                None, None).total()
     }
Exemplo n.º 12
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]})
Exemplo n.º 13
0
 def get_summary(self, label):
     return {
         'total_messages': DailyCount.get_by_label([label], DailyCount.TYPE_INCOMING).total()
     }
Exemplo n.º 14
0
 def evaluate(self, org: Org) -> int:
     return DailyCount.get_by_org([org],
                                  DailyCount.TYPE_REPLIES,
                                  since=days_ago(90)).total()
Exemplo n.º 15
0
 def evaluate(self, org: Org) -> int:
     return DailyCount.get_by_org([org], DailyCount.TYPE_REPLIES).total()
Exemplo n.º 16
0
    def create_msgs(self, orgs):
        self._log("Creating messages. Press Ctrl+C to stop...\n")

        # cache labels and contacts for each org
        for org in orgs:
            org._contacts = list(org.contacts.order_by("id"))
            org._labels = list(org.labels.order_by("id"))

        last_backend_id = Message.objects.order_by("backend_id").values_list("backend_id", flat=True).last()
        backend_id_start = last_backend_id + 1 if last_backend_id else 1

        BATCH_SIZE = 100

        num_created = 0
        try:
            msg_batch = []

            while True:
                org = self.random_choice(orgs, bias=BIASED)
                backend_id = backend_id_start + num_created

                msg, labels = self.generate_msg(org, backend_id)
                msg._labels = labels

                msg_batch.append(msg)

                num_created += 1

                if len(msg_batch) == BATCH_SIZE:
                    Message.objects.bulk_create(msg_batch)

                    labelling_batch = []
                    count_batch = []
                    for msg in msg_batch:
                        count_batch.append(
                            DailyCount(
                                day=datetime_to_date(msg.created_on, org),
                                item_type=DailyCount.TYPE_INCOMING,
                                scope=DailyCount.encode_scope(org),
                                count=1,
                            )
                        )

                        for label in msg._labels:
                            labelling_batch.append(Labelling.create(label, msg))
                            count_batch.append(
                                DailyCount(
                                    day=datetime_to_date(msg.created_on, org),
                                    item_type=DailyCount.TYPE_INCOMING,
                                    scope=DailyCount.encode_scope(label),
                                    count=1,
                                )
                            )

                    Labelling.objects.bulk_create(labelling_batch)
                    DailyCount.objects.bulk_create(count_batch)

                    msg_batch = []

                    self._log(f" > Created {num_created} messages\n")
        except KeyboardInterrupt:
            pass

        self._log(" > Squashing counts...\n")

        squash_counts()

        self._log(self.style.SUCCESS("OK") + "\n")
Exemplo n.º 17
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]})
Exemplo n.º 18
0
 def get_summary(self, partner):
     return {
         'total_replies': DailyCount.get_by_partner([partner], DailyCount.TYPE_REPLIES).total(),
         'cases_open': Case.objects.filter(org=partner.org, assignee=partner, closed_on=None).count(),
         'cases_closed': Case.objects.filter(org=partner.org, assignee=partner).exclude(closed_on=None).count()
     }