def _cases_created_per_user_per_month(self, case_type=None):
        query = (CaseES(es_instance_alias=ES_EXPORT_INSTANCE).domain(
            self.domain).opened_range(
                gte=self.date_start, lt=self.date_end).aggregation(
                    TermsAggregation('cases_per_user', 'owner_id',
                                     size=100).aggregation(
                                         DateHistogram('cases_by_date',
                                                       'opened_on',
                                                       interval='month'))))
        if case_type:
            query = query.case_type(case_type)

        results = query.size(0).run()

        stats = defaultdict(list)
        cases_per_user = results.aggregations.cases_per_user
        for bucket in cases_per_user.buckets_list:
            counts_by_date = {
                b['key_as_string']: b['doc_count']
                for b in bucket.cases_by_date.normalized_buckets
            }
            for key, count in counts_by_date.items():
                stats[key].append(count)

        final_stats = []
        for month, case_count_list in sorted(list(stats.items()),
                                             key=lambda r: r[0]):
            final_stats.append(
                (month, sum(case_count_list) // len(case_count_list)))

        suffix = ''
        if case_type:
            suffix = '(case type: %s)' % case_type
        self._print_table(
            ['Month', 'Cases created per user %s' % suffix], final_stats)
    def _cases_updated_per_user_per_month(self):
        results = (CaseES(es_instance_alias=ES_EXPORT_INSTANCE).domain(
            self.domain).active_in_range(
                gte=self.date_start, lt=self.date_end).aggregation(
                    TermsAggregation(
                        'cases_per_user', 'owner_id', size=100).aggregation(
                            NestedAggregation(
                                'actions', 'actions').aggregation(
                                    DateHistogram(
                                        'cases_by_date',
                                        'server_date',
                                        interval='month')))).size(0).run())

        stats = defaultdict(list)
        cases_per_user = results.aggregations.cases_per_user
        for bucket in cases_per_user.buckets_list:
            counts_by_date = {
                b['key_as_string']: b['doc_count']
                for b in bucket.actions.cases_by_date.normalized_buckets
            }
            for key, count in counts_by_date.items():
                stats[key].append(count)

        final_stats = []
        for month, case_count_list in sorted(list(stats.items()),
                                             key=lambda r: r[0]):
            final_stats.append(
                (month, sum(case_count_list) // len(case_count_list)))

        self._print_table(['Month', 'Cases updated per user'], final_stats)
示例#3
0
 def user_query(self):
     # partially inspired by ApplicationStatusReport.user_query
     mobile_user_and_group_slugs = set(
         self.request.GET.getlist(ExpandedMobileWorkerFilter.slug)
     ) or set(['t__0'])  # default to all mobile workers on initial load
     user_query = ExpandedMobileWorkerFilter.user_es_query(
         self.domain,
         mobile_user_and_group_slugs,
         self.request.couch_user,
     )
     user_query = user_query.size(0)
     user_query = user_query.aggregations([
         DateHistogram('last_submission', 'reporting_metadata.last_submission_for_user.submission_date', '1d'),
         DateHistogram('last_sync', 'reporting_metadata.last_sync_for_user.sync_date', '1d')
     ])
     return user_query