Example #1
0
 def create_case_url(index):
     """
         Given an index for a cell in a the row, creates the link to the case list page for that cell
     """
     url_params = {"individual": owner_id} if index not in (4, 5) else {}
     url_params.update(url_args)
     url_params.update({"search_query": search_strings[index]})
     return util.numcell('<a href="%s?%s" target="_blank">%s</a>' % (cl_url, urlencode(url_params, True), row[index]), row[index])
Example #2
0
def _get_commcare_version(app_version_info):
    commcare_version = (_("Unknown CommCare Version"), 0)
    if app_version_info:
        version_text = ('CommCare {}'.format(app_version_info.commcare_version)
                        if app_version_info.commcare_version else
                        _("Unknown CommCare Version"))
        commcare_version = (version_text, app_version_info.commcare_version
                            or 0)
    return numcell(commcare_version[0], commcare_version[1], convert="float")
Example #3
0
def _get_commcare_version(app_version_info):
    commcare_version = (_("Unknown CommCare Version"), 0)
    if app_version_info:
        version_text = (
            'CommCare {}'.format(app_version_info.commcare_version)
            if app_version_info.commcare_version
            else _("Unknown CommCare Version")
        )
        commcare_version = (version_text, app_version_info.commcare_version or 0)
    return numcell(commcare_version[0], commcare_version[1], convert="float")
Example #4
0
    def rows(self):
        domains = [
            res['_source']
            for res in self.es_results.get('hits', {}).get('hits', [])
        ]

        for dom in domains:
            if 'name' in dom:  # for some reason when using the statistical facet, ES adds an empty dict to hits
                first_form_default_message = _("No Forms")
                if dom.get("cp_last_form", None):
                    first_form_default_message = _("Unable to parse date")

                yield [
                    self.get_name_or_link(dom),
                    numcell(
                        dom.get("cp_n_active_cc_users",
                                _("Not yet calculated"))),
                    numcell(dom.get("cp_n_cc_users", _("Not yet calculated"))),
                    numcell(
                        dom.get("cp_n_active_cases", _("Not yet calculated"))),
                    numcell(dom.get("cp_n_cases", _("Not yet calculated"))),
                    numcell(dom.get("cp_n_forms", _("Not yet calculated"))),
                    format_date(dom.get("cp_first_form"),
                                first_form_default_message),
                    format_date(dom.get("cp_last_form"), _("No forms")),
                    numcell(dom.get("cp_n_web_users", _("Not yet calculated")))
                ]
Example #5
0
        def submit_history_link(owner_id, val, param='select_mw'):
            """
                takes a row, and converts certain cells in the row to links that link to the submit history report
            """
            fs_url = reverse('project_report_dispatcher', args=(self.domain, 'submit_history'))
            start_date, end_date = dates_for_linked_reports()
            url_args = {
                param: owner_id,
                "startdate": start_date,
                "enddate": end_date,
            }

            return util.numcell('<a href="%s?%s" target="_blank">%s</a>' % (fs_url, urlencode(url_args, True), val), val)
Example #6
0
    def rows(self):
        domains = [res['_source'] for res in self.es_results.get('hits', {}).get('hits', [])]

        for dom in domains:
            if dom.has_key('name'): # for some reason when using the statistical facet, ES adds an empty dict to hits
                yield [
                    self.get_name_or_link(dom),
                    numcell(dom.get("cp_n_active_cc_users", _("Not yet calculated"))),
                    numcell(dom.get("cp_n_cc_users", _("Not yet calculated"))),
                    numcell(dom.get("cp_n_active_cases", _("Not yet calculated"))),
                    numcell(dom.get("cp_n_cases", _("Not yet calculated"))),
                    numcell(dom.get("cp_n_forms", _("Not yet calculated"))),
                    format_date(dom.get("cp_first_form"), _("No forms")),
                    format_date(dom.get("cp_last_form"), _("No forms")),
                    numcell(dom.get("cp_n_web_users", _("Not yet calculated")))
                ]
Example #7
0
    def rows(self):
        domains = [res['_source'] for res in self.es_results.get('hits', {}).get('hits', [])]

        for dom in domains:
            if 'name' in dom: # for some reason when using the statistical facet, ES adds an empty dict to hits
                first_form_default_message = _("No Forms")
                if dom.get("cp_last_form", None):
                    first_form_default_message = _("Unable to parse date")

                yield [
                    self.get_name_or_link(dom),
                    numcell(dom.get("cp_n_active_cc_users", _("Not yet calculated"))),
                    numcell(dom.get("cp_n_cc_users", _("Not yet calculated"))),
                    numcell(dom.get("cp_n_active_cases", _("Not yet calculated"))),
                    numcell(dom.get("cp_n_cases", _("Not yet calculated"))),
                    numcell(dom.get("cp_n_forms", _("Not yet calculated"))),
                    format_date(dom.get("cp_first_form"), first_form_default_message),
                    format_date(dom.get("cp_last_form"), _("No forms")),
                    numcell(dom.get("cp_n_web_users", _("Not yet calculated")))
                ]
Example #8
0
    def rows(self):
        domains = [
            res['_source']
            for res in self.es_results.get('hits', {}).get('hits', [])
        ]

        for dom in domains:
            if dom.has_key(
                    'name'
            ):  # for some reason when using the statistical facet, ES adds an empty dict to hits
                yield [
                    self.get_name_or_link(dom),
                    numcell(
                        dom.get("cp_n_active_cc_users",
                                _("Not yet calculated"))),
                    numcell(dom.get("cp_n_cc_users", _("Not yet calculated"))),
                    numcell(
                        dom.get("cp_n_active_cases", _("Not yet calculated"))),
                    numcell(dom.get("cp_n_cases", _("Not yet calculated"))),
                    numcell(dom.get("cp_n_forms", _("Not yet calculated"))),
                    format_date(dom.get("cp_first_form"), _("No forms")),
                    format_date(dom.get("cp_last_form"), _("No forms")),
                    numcell(dom.get("cp_n_web_users", _("Not yet calculated")))
                ]
Example #9
0
    def rows(self):
        duration = (self.datespan.enddate - self.datespan.startdate) + datetime.timedelta(days=1) # adjust bc inclusive
        avg_datespan = DateSpan(self.datespan.startdate - (duration * self.num_avg_intervals),
                                self.datespan.startdate - datetime.timedelta(days=1))

        form_data = self.es_form_submissions()
        submissions_by_user = dict([(t["term"], t["count"]) for t in form_data["facets"]["form.meta.userID"]["terms"]])
        avg_form_data = self.es_form_submissions(datespan=avg_datespan)
        avg_submissions_by_user = dict([(t["term"], t["count"]) for t in avg_form_data["facets"]["form.meta.userID"]["terms"]])

        if self.view_by == 'groups':
            active_users_by_group = dict([(g, len(filter(lambda u: submissions_by_user.get(u['user_id']), users)))
                                          for g, users in self.users_by_group.iteritems()])
        else:
            last_form_by_user = self.es_last_submissions()

        case_creation_data = self.es_case_queries('opened_on', 'opened_by')
        creations_by_user = dict([(t["term"].lower(), t["count"])
                                  for t in case_creation_data["facets"]["opened_by"]["terms"]])

        case_closure_data = self.es_case_queries('closed_on', 'closed_by')
        closures_by_user = dict([(t["term"].lower(), t["count"])
                                 for t in case_closure_data["facets"]["closed_by"]["terms"]])

        active_case_data = self.es_active_cases()
        actives_by_owner = dict([(t["term"].lower(), t["count"])
                                 for t in active_case_data["facets"]["owner_id"]["terms"]])

        total_case_data = self.es_total_cases()
        totals_by_owner = dict([(t["term"].lower(), t["count"])
                                for t in total_case_data["facets"]["owner_id"]["terms"]])

        def dates_for_linked_reports(case_list=False):
            start_date = self.datespan.startdate_param
            end_date = self.datespan.enddate if not case_list else self.datespan.enddate + datetime.timedelta(days=1)
            end_date = end_date.strftime(self.datespan.format)
            return start_date, end_date

        def submit_history_link(owner_id, val, param='select_mw'):
            """
                takes a row, and converts certain cells in the row to links that link to the submit history report
            """
            fs_url = reverse('project_report_dispatcher', args=(self.domain, 'submit_history'))
            start_date, end_date = dates_for_linked_reports()
            url_args = {
                param: owner_id,
                "startdate": start_date,
                "enddate": end_date,
            }

            return util.numcell('<a href="%s?%s" target="_blank">%s</a>' % (fs_url, urlencode(url_args, True), val), val)

        def add_case_list_links(owner_id, row):
            """
                takes a row, and converts certain cells in the row to links that link to the case list page
            """
            cl_url = reverse('project_report_dispatcher', args=(self.domain, 'case_list'))
            url_args = {
                "ufilter": range(4), # include all types of users in case list report
            }

            start_date, end_date = dates_for_linked_reports(case_list=True)
            start_date_sub1 = self.datespan.startdate - datetime.timedelta(days=1)
            start_date_sub1 = start_date_sub1.strftime(self.datespan.format)
            search_strings = {
                4: "opened_by: %s AND opened_on: [%s TO %s]" % (owner_id, start_date, end_date), # cases created
                5: "closed_by: %s AND closed_on: [%s TO %s]" % (owner_id, start_date, end_date), # cases closed
                7: "opened_on: [* TO %s] AND NOT closed_on: [* TO %s]" % (end_date, start_date_sub1), # total cases
            }
            if today_or_tomorrow(self.datespan.enddate):
                search_strings[6] = "modified_on: [%s TO %s]" % (start_date, end_date) # active cases

            if self.case_type:
                for index, search_string in search_strings.items():
                    search_strings[index] = search_string + " AND type.exact: %s" % self.case_type

            def create_case_url(index):
                """
                    Given an index for a cell in a the row, creates the link to the case list page for that cell
                """
                url_params = {"individual": owner_id} if index not in (4, 5) else {}
                url_params.update(url_args)
                url_params.update({"search_query": search_strings[index]})
                return util.numcell('<a href="%s?%s" target="_blank">%s</a>' % (cl_url, urlencode(url_params, True), row[index]), row[index])

            for i in search_strings:
                row[i] = create_case_url(i)
            return row

        def group_cell(group_id, group_name):
            """
                takes group info, and creates a cell that links to the user status report focused on the group
            """
            us_url = reverse('project_report_dispatcher', args=(self.domain, 'worker_activity'))
            start_date, end_date = dates_for_linked_reports()
            url_args = {
                "group": group_id,
                "startdate": start_date,
                "enddate": end_date,
            }
            return util.format_datatables_data(
                '<a href="%s?%s" target="_blank">%s</a>' % (us_url, urlencode(url_args, True), group_name),
                group_name
            )

        rows = []
        NO_FORMS_TEXT = _('None')
        if self.view_by == 'groups':
            for group, users in self.users_by_group.iteritems():
                group_name, group_id = tuple(group.split('|'))
                if group_name == 'no_group':
                    continue

                case_sharing_groups = set(reduce(operator.add, [u['group_ids'] for u in users], []))
                active_cases = sum([int(actives_by_owner.get(u["user_id"].lower(), 0)) for u in users]) + \
                    sum([int(actives_by_owner.get(g_id, 0)) for g_id in case_sharing_groups])
                total_cases = sum([int(totals_by_owner.get(u["user_id"].lower(), 0)) for u in users]) + \
                    sum([int(totals_by_owner.get(g_id, 0)) for g_id in case_sharing_groups])
                active_users = int(active_users_by_group.get(group, 0))
                total_users = len(self.users_by_group.get(group, []))

                rows.append([
                    group_cell(group_id, group_name),
                    submit_history_link(group_id,
                            sum([int(submissions_by_user.get(user["user_id"], 0)) for user in users]), param="group"),
                    util.numcell(sum([int(avg_submissions_by_user.get(user["user_id"], 0)) for user in users]) / self.num_avg_intervals),
                    util.numcell("%s / %s" % (active_users, total_users),
                                 int((float(active_users)/total_users) * 10000) if total_users else -1),
                    util.numcell(sum([int(creations_by_user.get(user["user_id"].lower(), 0)) for user in users])),
                    util.numcell(sum([int(closures_by_user.get(user["user_id"].lower(), 0)) for user in users])),
                    util.numcell(active_cases),
                    util.numcell(total_cases),
                    util.numcell((float(active_cases)/total_cases) * 100 if total_cases else 'nan', convert='float'),
                ])

        else:
            for user in self.users_to_iterate:
                active_cases = int(actives_by_owner.get(user["user_id"].lower(), 0)) + \
                    sum([int(actives_by_owner.get(group_id, 0)) for group_id in user["group_ids"]])
                total_cases = int(totals_by_owner.get(user["user_id"].lower(), 0)) + \
                    sum([int(totals_by_owner.get(group_id, 0)) for group_id in user["group_ids"]])

                rows.append(add_case_list_links(user['user_id'], [
                    user["username_in_report"],
                    submit_history_link(user['user_id'], submissions_by_user.get(user["user_id"], 0)),
                    util.numcell(int(avg_submissions_by_user.get(user["user_id"], 0)) / self.num_avg_intervals),
                    last_form_by_user.get(user["user_id"]) or NO_FORMS_TEXT,
                    int(creations_by_user.get(user["user_id"].lower(),0)),
                    int(closures_by_user.get(user["user_id"].lower(), 0)),
                    util.numcell(active_cases) if not today_or_tomorrow(self.datespan.enddate) else active_cases,
                    total_cases,
                    util.numcell((float(active_cases)/total_cases) * 100 if total_cases else 'nan', convert='float'),
                ]))

        self.total_row = [_("Total")]
        summing_cols = [1, 2, 4, 5, 6, 7]
        for col in range(1, len(self.headers)):
            if col in summing_cols:
                self.total_row.append(sum(filter(lambda x: not math.isnan(x), [row[col].get('sort_key', 0) for row in rows])))
            else:
                self.total_row.append('---')

        if self.view_by == 'groups':
            def parse(str):
                num, denom = tuple(str.split('/'))
                num = int(num.strip())
                denom = int(denom.strip())
                return num, denom

            def add(result_tuple, str):
                num, denom = parse(str)
                return num + result_tuple[0], denom + result_tuple[1]

            self.total_row[3] = '%s / %s' % reduce(add, [row[3]["html"] for row in rows], (0, 0))
        else:
            num = len(filter(lambda row: row[3] != NO_FORMS_TEXT, rows))
            self.total_row[3] = '%s / %s' % (num, len(rows))

        return rows
Example #10
0
def _get_build_version(app_version_info):
    build_version = (_("Unknown"), 0)
    if app_version_info and app_version_info.build_version:
        build_version = (app_version_info.build_version, app_version_info.build_version)
    return numcell(build_version[0], value=build_version[1])