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]})
def bulk_cache_initialize(cls, labels): """ Pre-loads cached counts on a set of labels to avoid fetching counts individually for each label """ from casepro.statistics.models import TotalCount inbox_by_label = TotalCount.get_by_label(labels, TotalCount.TYPE_INBOX).scope_totals() archived_by_label = TotalCount.get_by_label(labels, TotalCount.TYPE_ARCHIVED).scope_totals() for label in labels: setattr(label, cls.INBOX_COUNT_CACHE_ATTR, inbox_by_label[label]) setattr(label, cls.ARCHIVED_COUNT_CACHE_ATTR, archived_by_label[label])
def bulk_cache_initialize(cls, labels): """ Pre-loads cached counts on a set of labels to avoid fetching counts individually for each label """ from casepro.statistics.models import TotalCount inbox_by_label = TotalCount.get_by_label( labels, TotalCount.TYPE_INBOX).scope_totals() archived_by_label = TotalCount.get_by_label( labels, TotalCount.TYPE_ARCHIVED).scope_totals() for label in labels: setattr(label, cls.INBOX_COUNT_CACHE_ATTR, inbox_by_label[label]) setattr(label, cls.ARCHIVED_COUNT_CACHE_ATTR, archived_by_label[label])
def _get_archived_count(self): from casepro.statistics.models import TotalCount return TotalCount.get_by_label( [self], TotalCount.TYPE_ARCHIVED).scope_totals()[self]
def _get_inbox_count(self, ): from casepro.statistics.models import TotalCount return TotalCount.get_by_label( [self], TotalCount.TYPE_INBOX).scope_totals()[self]
def _get_archived_count(self): from casepro.statistics.models import TotalCount return TotalCount.get_by_label([self], TotalCount.TYPE_ARCHIVED).scope_totals()[self]
def _get_inbox_count(self, ): from casepro.statistics.models import TotalCount return TotalCount.get_by_label([self], TotalCount.TYPE_INBOX).scope_totals()[self]
def render_as_json(self, partners, with_activity): if with_activity: # get reply statistics replies_total = TotalCount.get_by_partner( partners, DailyCount.TYPE_REPLIES).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 = TotalCount.get_by_partner( partners, DailyCount.TYPE_CASE_OPENED).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]})