Пример #1
0
def get_wrapped_ledger_values(domain, case_ids, section_id, entry_ids=None):
    # todo: figure out why this causes circular import
    from corehq.apps.reports.commtrack.util import StockLedgerValueWrapper
    query = LedgerES().domain(domain).section(section_id).case(case_ids)
    if entry_ids:
        query = query.entry(entry_ids)

    return [StockLedgerValueWrapper.wrap(row) for row in query.run().hits]
Пример #2
0
def get_wrapped_ledger_values(domain, case_ids, section_id, entry_ids=None):
    # todo: figure out why this causes circular import
    from corehq.apps.reports.commtrack.util import StockLedgerValueWrapper

    query = LedgerES().domain(domain).section(section_id).case(case_ids)
    if entry_ids:
        query = query.entry(entry_ids)

    return [StockLedgerValueWrapper.wrap(row) for row in query.run().hits]
Пример #3
0
def get_aggregated_ledger_values(domain, case_ids, section_id, entry_ids=None):
    # todo: figure out why this causes circular import
    query = LedgerES().domain(domain).section(section_id).case(case_ids)
    if entry_ids:
        query = query.entry(entry_ids)

    terms = [AggregationTerm("entry_id", "entry_id")]
    return NestedTermAggregationsHelper(
        base_query=query, terms=terms, inner_most_aggregation=SumAggregation("balance", "balance")
    ).get_data()
Пример #4
0
def get_aggregated_ledger_values(domain, case_ids, section_id, entry_ids=None):
    # todo: figure out why this causes circular import
    query = LedgerES().domain(domain).section(section_id).case(case_ids)
    if entry_ids:
        query = query.entry(entry_ids)

    terms = [
        AggregationTerm('entry_id', 'entry_id'),
    ]
    return NestedTermAggregationsHelper(
        base_query=query,
        terms=terms,
        inner_most_aggregation=SumAggregation('balance', 'balance'),
    ).get_data()
Пример #5
0
def get_ledger_section_entry_combinations(domain):
    """Get all section / entry combinations in a domain.
    :returns: a generator of namedtuples with fields ``section_id``, ``entry_id``, ``doc_count``
    """
    terms = [
        AggregationTerm('section_id', 'section_id'),
        AggregationTerm('entry_id', 'entry_id'),
    ]
    query = LedgerES().domain(domain)
    return NestedTermAggregationsHelper(base_query=query, terms=terms).get_data()
Пример #6
0
    def _ledgers_per_case(self):
        results = (LedgerES(es_instance_alias=ES_EXPORT_INSTANCE).domain(
            self.domain).aggregation(
                TermsAggregation('by_case', 'case_id',
                                 size=100)).size(0).run())

        ledgers_per_case = results.aggregations.by_case
        case_ids = set()
        ledger_counts = []
        for case_id, ledger_count in ledgers_per_case.counts_by_bucket().items(
        ):
            case_ids.add(case_id)
            ledger_counts.append(ledger_count)

        if not case_ids:
            self.stdout.write("Domain has no ledgers")
            return

        avg_ledgers_per_case = sum(ledger_counts) // len(case_ids)
        case_types_result = CaseES(es_instance_alias=ES_EXPORT_INSTANCE)\
            .domain(self.domain).case_ids(case_ids)\
            .aggregation(TermsAggregation('types', 'type'))\
            .size(0).run()

        case_types = case_types_result.aggregations.types.keys

        self.stdout.write('\nCase Types with Ledgers')
        for type_ in case_types:
            self._print_value(
                'case_type', type_,
                CaseES().domain(self.domain).case_type(type_).count())
            if should_use_sql_backend(self.domain):
                db_name = get_db_aliases_for_partitioned_query()[
                    0]  # just query one shard DB
                results = (CommCareCaseSQL.objects.using(db_name).filter(
                    domain=self.domain, closed=True, type=type_).annotate(
                        lifespan=F('closed_on') - F('opened_on')).annotate(
                            avg_lifespan=Avg('lifespan')).values(
                                'avg_lifespan', flat=True))
                self._print_value('Average lifespan for "%s" cases' % type_,
                                  results[0]['avg_lifespan'])

            self._cases_created_per_user_per_month(type_)

        self._print_value('Average ledgers per case', avg_ledgers_per_case)

        if should_use_sql_backend(self.domain):
            stats = defaultdict(list)
            for db_name, case_ids_p in split_list_by_db_partition(case_ids):
                transactions_per_case_per_month = (
                    LedgerTransaction.objects.using(db_name).filter(
                        case_id__in=case_ids).annotate(
                            m=Month('server_date'),
                            y=Year('server_date')).values(
                                'case_id', 'y',
                                'm').annotate(count=Count('id')))
                for row in transactions_per_case_per_month:
                    month = date(row['y'], row['m'], 1)
                    stats[month].append(row['count'])
        else:
            transactions_per_case_per_month = (StockTransaction.objects.filter(
                case_id__in=case_ids).annotate(
                    m=Month('report__date'), y=Year('report__date')).values(
                        'case_id', 'y', 'm').annotate(count=Count('id')))

            stats = defaultdict(list)
            for row in transactions_per_case_per_month:
                month = date(row['y'], row['m'], 1)
                stats[month].append(row['count'])

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

        self._print_table(['Month', 'Ledgers updated per case'], final_stats)
Пример #7
0
def products_with_ledgers(domain, case_ids, section_id, entry_ids=None):
    # returns entry ids/product ids that have associated ledgers
    query = LedgerES().domain(domain).section(section_id).case(case_ids)
    if entry_ids:
        query = query.entry(entry_ids)
    return set(query.values_list('entry_id', flat=True))
Пример #8
0
def products_with_ledgers(domain, case_ids, section_id, entry_ids=None):
    # returns entry ids/product ids that have associated ledgers
    query = LedgerES().domain(domain).section(section_id).case(case_ids)
    if entry_ids:
        query = query.entry(entry_ids)
    return set(query.values_list('entry_id', flat=True))
Пример #9
0
def get_total_records(domain, case_ids, section_id, entry_ids=None):
    query = LedgerES().domain(domain).section(section_id).case(case_ids)
    if entry_ids:
        query = query.entry(entry_ids)
    return query.count()