def rows(self):
     appointments_sent = self.unique('case_data')
     appointments_confirmed = self.unique('case_data', check_response_state=True)
     reminders_sent = self.unique('session_data')
     reminders_confirmed = self.unique('session_data', check_response_state=True)
     return [[
         appointments_sent,
         div(appointments_confirmed, appointments_sent, percent=True),
         div(reminders_confirmed, reminders_sent, percent=True),
         div(reminders_sent, appointments_sent),
         div(reminders_confirmed, appointments_sent),
     ]]
 def rows(self):
     appointments_sent = self.unique('case_data')
     appointments_confirmed = self.unique('case_data',
                                          check_response_state=True)
     reminders_sent = self.unique('session_data')
     reminders_confirmed = self.unique('session_data',
                                       check_response_state=True)
     return [[
         appointments_sent,
         div(appointments_confirmed, appointments_sent, percent=True),
         div(reminders_confirmed, reminders_sent, percent=True),
         div(reminders_sent, appointments_sent),
         div(reminders_confirmed, appointments_sent),
     ]]
 def percent_over_time(self, field_name):
     values = []
     start, end = self.datespan.startdate_utc, self.datespan.enddate_utc
     while start <= end:
         base_query = self.gen_base_query(start)
         num = self.unique(field_name, True, base_query=base_query)
         denom = self.unique(field_name, base_query=base_query)
         values.append({
             "x": int(unix_time_millis(start)),
             "y": float(div(num, denom)) * 100,
         })
         start += timedelta(days=1)
     return values
 def percent_over_time(self, field_name):
     values = []
     start, end = self.datespan.startdate_utc, self.datespan.enddate_utc
     while start <= end:
         base_query = self.gen_base_query(start)
         num = self.unique(field_name, True, base_query=base_query)
         denom = self.unique(field_name, base_query=base_query)
         values.append({
             "x": int(unix_time_millis(start)),
             "y": float(div(num, denom)) * 100,
         })
         start += timedelta(days=1)
     return values
    def rows(self):
        def row(header, mw_val, case_val):
            return [_(header), mw_val, case_val, mw_val + case_val]

        def verified_numbered_users(owner_type, ids=None, check_filters=False):
            if not ids and not check_filters:
                data = get_db().view('sms/verified_number_by_domain',
                    reduce=True,
                    startkey=[self.domain, owner_type],
                    endkey=[self.domain, owner_type, {}],
                ).one()
                return data["value"] if data else 0
            else:
                owners = get_db().view('sms/verified_number_by_domain',
                    reduce=False,
                    startkey=[self.domain, owner_type],
                    endkey=[self.domain, owner_type, {}],
                ).all()
                return len(filter(lambda oid: oid in ids, [o["key"][2] for o in owners]))

        owner_ids = self.combined_user_ids if self.users_by_group else []
        case_ids = self.cases_by_case_group if self.cases_by_case_group else []

        check_filters = True if owner_ids or case_ids else False
        number = row("Number", verified_numbered_users("CommCareUser", owner_ids, check_filters=check_filters),
                     verified_numbered_users("CommCareCase", case_ids, check_filters=check_filters))

        def get_actives(recipient_type):
            return len(self.active_query(recipient_type)['facets']['couch_recipient']['terms'])

        active = row(_("Number Active"), get_actives("commcareuser"), get_actives("commcarecase"))

        perc_active = [_("% Active"),
                       div(active[1], number[1], True), div(active[2], number[2], True), div(active[3], number[3], True)]

        facets = self.messages_query()['facets']
        to_users, to_cases = 0, 0
        for term in facets['couch_recipient_doc_type']['terms']:
            if term['term'] == 'commcarecase':
                to_cases = term['count']
            elif term['term'] == 'commcareuser':
                to_users = term['count']
        messages = row(_("Number of SMS Messages, incoming and outgoing"), to_users, to_cases)

        avg_per_user = [_("Avg SMS Messages per User"),
                        div(messages[1], number[1]), div(messages[2], number[2]), div(messages[3], number[3])]
        avg_per_act_user = [_("Avg SMS Messages per Active User"),
                            div(messages[1], active[1]), div(messages[2], active[2]), div(messages[3], active[3])]

        return [number, active, perc_active, messages, avg_per_user, avg_per_act_user]
    def rows(self):
        def row(header, mw_val, case_val):
            return [_(header), mw_val, case_val, mw_val + case_val]

        def verified_numbered_users(owner_type, ids=None, check_filters=False):
            if not ids and not check_filters:
                data = get_db().view(
                    'sms/verified_number_by_domain',
                    reduce=True,
                    startkey=[self.domain, owner_type],
                    endkey=[self.domain, owner_type, {}],
                ).one()
                return data["value"] if data else 0
            else:
                owners = get_db().view(
                    'sms/verified_number_by_domain',
                    reduce=False,
                    startkey=[self.domain, owner_type],
                    endkey=[self.domain, owner_type, {}],
                ).all()
                return len(
                    filter(lambda oid: oid in ids,
                           [o["key"][2] for o in owners]))

        owner_ids = self.combined_user_ids if self.users_by_group else []
        case_ids = self.cases_by_case_group if self.cases_by_case_group else []

        check_filters = True if owner_ids or case_ids else False
        number = row(
            "Number",
            verified_numbered_users("CommCareUser",
                                    owner_ids,
                                    check_filters=check_filters),
            verified_numbered_users("CommCareCase",
                                    case_ids,
                                    check_filters=check_filters))

        def get_actives(recipient_type):
            return len(
                self.active_query(recipient_type)['facets']['couch_recipient']
                ['terms'])

        active = row(_("Number Active"), get_actives("commcareuser"),
                     get_actives("commcarecase"))

        perc_active = [
            _("% Active"),
            div(active[1], number[1], True),
            div(active[2], number[2], True),
            div(active[3], number[3], True)
        ]

        facets = self.messages_query()['facets']
        to_users, to_cases = 0, 0
        for term in facets['couch_recipient_doc_type']['terms']:
            if term['term'] == 'commcarecase':
                to_cases = term['count']
            elif term['term'] == 'commcareuser':
                to_users = term['count']
        messages = row(_("Number of SMS Messages, incoming and outgoing"),
                       to_users, to_cases)

        avg_per_user = [
            _("Avg SMS Messages per User"),
            div(messages[1], number[1]),
            div(messages[2], number[2]),
            div(messages[3], number[3])
        ]
        avg_per_act_user = [
            _("Avg SMS Messages per Active User"),
            div(messages[1], active[1]),
            div(messages[2], active[2]),
            div(messages[3], active[3])
        ]

        return [
            number, active, perc_active, messages, avg_per_user,
            avg_per_act_user
        ]