django.db.models.DecimalField - python examples

Here are the examples of the python api django.db.models.DecimalField taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

37 Examples 7

3 View Complete Implementation : converter.py
Copyright MIT License
Author : eamigo86
@convert_django_field.register(models.DecimalField)
@convert_django_field.register(models.FloatField)
@convert_django_field.register(models.DurationField)
def convert_field_to_float(field, registry=None, input_flag=None, nested_field=False):
    return Float(
        description=field.help_text or field.verbose_name,
        required=is_required(field) and input_flag == "create",
    )

3 View Complete Implementation : models.py
Copyright MIT License
Author : gnosis
    def with_balance(self):
        """
        :return: Queryset with the Safes and a `balance` attribute
        """
        return self.annotate(
            balance=Subquery(
                InternalTx.objects.balance_for_all_safes(
                ).filter(
                    to=OuterRef('address')
                ).values('balance').distinct(),
                models.DecimalField()))

3 View Complete Implementation : test_case.py
Copyright MIT License
Author : labd
    def astertModelDecimalField(self, field, max_digits, decimal_places, null=False, blank=False):
        self.astertEqual(field.__clast__, models.DecimalField)
        self.astertEqual(field.max_digits, max_digits)
        self.astertEqual(field.decimal_places, decimal_places)
        self.astertEqual(field.null, null)
        self.astertEqual(field.blank, blank)

3 View Complete Implementation : test.py
Copyright MIT License
Author : martsberger
    def test_pivot_aggregate(self):
        shirt_sales = ShirtSales.objects.all()

        data = ExpressionWrapper(F('units') * F('price'), output_field=DecimalField())
        pt = pivot(ShirtSales, 'store__region__name', 'shipped', data, Avg, default=0)

        for row in pt:
            region_name = row['store__region__name']
            for dt in (key for key in row.keys() if key != 'store__region__name'):
                spends = [ss.units * ss.price for ss in shirt_sales if force_text(ss.shipped) == force_text(dt) and ss.store.region.name == region_name]
                avg = sum(spends) / len(spends) if spends else 0
                self.astertAlmostEqual(row[dt], float(avg), places=4)

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_combine_different_types(self):
        msg = 'Expression contains mixed types. You must set output_field.'
        qs = Book.objects.annotate(sums=Sum('rating') + Sum('pages') + Sum('price'))
        with self.astertRaisesMessage(FieldError, msg):
            qs.first()
        with self.astertRaisesMessage(FieldError, msg):
            qs.first()

        b1 = Book.objects.annotate(sums=Sum(F('rating') + F('pages') + F('price'),
                                   output_field=IntegerField())).get(pk=self.b4.pk)
        self.astertEqual(b1.sums, 383)

        b2 = Book.objects.annotate(sums=Sum(F('rating') + F('pages') + F('price'),
                                   output_field=FloatField())).get(pk=self.b4.pk)
        self.astertEqual(b2.sums, 383.69)

        b3 = Book.objects.annotate(sums=Sum(F('rating') + F('pages') + F('price'),
                                   output_field=DecimalField())).get(pk=self.b4.pk)
        self.astertEqual(b3.sums, Approximate(Decimal("383.69"), places=2))

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_multi_arg_aggregate(self):
        clast MyMax(Max):
            output_field = DecimalField()

            def as_sql(self, compiler, connection):
                copy = self.copy()
                copy.set_source_expressions(copy.get_source_expressions()[0:1])
                return super(MyMax, copy).as_sql(compiler, connection)

        with self.astertRaisesMessage(TypeError, 'Complex aggregates require an alias'):
            Book.objects.aggregate(MyMax('pages', 'price'))

        with self.astertRaisesMessage(TypeError, 'Complex annotations require an alias'):
            Book.objects.annotate(MyMax('pages', 'price'))

        Book.objects.aggregate(max_field=MyMax('pages', 'price'))

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_decimal_field(self):
        field = models.DecimalField(max_digits=5, decimal_places=2)
        name, path, args, kwargs = field.deconstruct()
        self.astertEqual(path, "django.db.models.DecimalField")
        self.astertEqual(args, [])
        self.astertEqual(kwargs, {"max_digits": 5, "decimal_places": 2})

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_decimal_field_0_decimal_places(self):
        """
        A DecimalField with decimal_places=0 should work (#22272).
        """
        field = models.DecimalField(max_digits=5, decimal_places=0)
        name, path, args, kwargs = field.deconstruct()
        self.astertEqual(path, "django.db.models.DecimalField")
        self.astertEqual(args, [])
        self.astertEqual(kwargs, {"max_digits": 5, "decimal_places": 0})

3 View Complete Implementation : test_decimalfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_to_python(self):
        f = models.DecimalField(max_digits=4, decimal_places=2)
        self.astertEqual(f.to_python(3), Decimal('3'))
        self.astertEqual(f.to_python('3.14'), Decimal('3.14'))
        # to_python() converts floats and honors max_digits.
        self.astertEqual(f.to_python(3.1415926535897), Decimal('3.142'))
        self.astertEqual(f.to_python(2.4), Decimal('2.400'))
        # Uses default rounding of ROUND_HALF_EVEN.
        self.astertEqual(f.to_python(2.0625), Decimal('2.062'))
        self.astertEqual(f.to_python(2.1875), Decimal('2.188'))
        msg = "'abc' value must be a decimal number."
        with self.astertRaisesMessage(ValidationError, msg):
            f.to_python('abc')

3 View Complete Implementation : test_cast.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @skipUnlessDBFeature('supports_cast_with_precision')
    def test_cast_to_decimal_field(self):
        FloatModel.objects.create(f1=-1.934, f2=3.467)
        float_obj = FloatModel.objects.annotate(
            cast_f1_decimal=Cast('f1', models.DecimalField(max_digits=8, decimal_places=2)),
            cast_f2_decimal=Cast('f2', models.DecimalField(max_digits=8, decimal_places=1)),
        ).get()
        self.astertEqual(float_obj.cast_f1_decimal, decimal.Decimal('-1.93'))
        self.astertEqual(float_obj.cast_f2_decimal, decimal.Decimal('3.5'))
        author_obj = Author.objects.annotate(
            cast_alias_decimal=Cast('alias', models.DecimalField(max_digits=8, decimal_places=2)),
        ).get()
        self.astertEqual(author_obj.cast_alias_decimal, decimal.Decimal('1'))

3 View Complete Implementation : views.py
Copyright MIT License
Author : PacktPublishing
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        items = ShoppingCarsatem.objects.get_items(self.object)

        context['is_cart_empty'] = (items.count() == 0)

        order = items.aggregate(
            total_order=Sum(F('price_per_unit') * F('quansaty'),
                            output_field=DecimalField())
        )

        context['total_order'] = order['total_order']

        return context

3 View Complete Implementation : math.py
Copyright GNU Affero General Public License v3.0
Author : Unicaronas
    def as_postgresql(self, compiler, connection, **extra_context):
        # Cast FloatField to DecimalField as PostgreSQL doesn't support the
        # following function signatures:
        # - LOG(double, double)
        # - MOD(double, double)
        output_field = DecimalField(decimal_places=sys.float_info.dig, max_digits=1000)
        clone = self.copy()
        clone.set_source_expressions([
            Cast(expression, output_field) if isinstance(expression.output_field, FloatField)
            else expression for expression in self.get_source_expressions()
        ])
        return clone.as_sql(compiler, connection, **extra_context)

0 View Complete Implementation : filters.py
Copyright Apache License 2.0
Author : BeanWei
    @clastmethod
    def test(cls, field, request, params, model, admin_view, field_path):
        return isinstance(field, (models.DecimalField, models.FloatField, models.IntegerField))

0 View Complete Implementation : functions.py
Copyright MIT License
Author : bpgc-cte
    def __init__(self, expression, **extra):
        output_field = extra.pop('output_field', DecimalField())
        super(IntervalToSeconds, self).__init__(expression, output_field=output_field, **extra)

0 View Complete Implementation : models.py
Copyright MIT License
Author : gnosis
    def balance_for_all_safes(self):
        # Exclude `DELEGATE_CALL` and errored transactions from `balance` calculations

        # There must be at least 2 txs (one in and another out for this method to work
        # SELECT SUM(value) FROM "relay_internaltx" U0 WHERE U0."_from" = address
        # sum
        # -----
        # (1 row)

        # But Django uses group by
        # SELECT SUM(value) FROM "relay_internaltx" U0 WHERE U0."_from" = '0xE3726b0a9d59c3B28947Ae450e8B8FC864c7f77f' GROUP BY U0."_from"
        # sum
        # -----
        # (0 rows)

        # We would like to translate this query into Django (excluding errors and DELEGATE_CALLs),
        # but it's not working as Django always try to `GROUP BY` when using `annotate`
        # SELECT *, (SELECT SUM(CASE WHEN "to"=R.to THEN value ELSE -value END)
        # FROM relay_internaltx
        # WHERE "to"=R.to OR _from=R.to) FROM relay_internaltx R;

        outgoing_balance = self.filter(
            _from=OuterRef('to'), error=None
        ).exclude(
            call_type=EthereumTxCallType.DELEGATE_CALL.value
        ).order_by().values('_from').annotate(
            total=Coalesce(Sum('value'), 0)
        ).values('total')

        incoming_balance = self.filter(
            to=OuterRef('to'), error=None
        ).exclude(
            call_type=EthereumTxCallType.DELEGATE_CALL.value
        ).order_by().values('to').annotate(
            total=Coalesce(Sum('value'), 0)
        ).values('total')

        return self.annotate(balance=Subquery(incoming_balance, output_field=models.DecimalField()) -
                                     Subquery(outgoing_balance, output_field=models.DecimalField()))

0 View Complete Implementation : converter.py
Copyright MIT License
Author : graphql-python
@convert_django_field.register(models.DecimalField)
@convert_django_field.register(models.FloatField)
@convert_django_field.register(models.DurationField)
def convert_field_to_float(field, registry=None):
    return Float(description=field.help_text, required=not field.null)

0 View Complete Implementation : test_decimalfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_default(self):
        f = models.DecimalField(default=Decimal('0.00'))
        self.astertEqual(f.get_default(), Decimal('0.00'))

0 View Complete Implementation : test_decimalfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_get_prep_value(self):
        f = models.DecimalField(max_digits=5, decimal_places=1)
        self.astertIsNone(f.get_prep_value(None))
        self.astertEqual(f.get_prep_value('2.4'), Decimal('2.4'))

0 View Complete Implementation : test_decimalfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_max_digits_validation(self):
        field = models.DecimalField(max_digits=2)
        expected_message = validators.DecimalValidator.messages['max_digits'] % {'max': 2}
        with self.astertRaisesMessage(ValidationError, expected_message):
            field.clean(100, None)

0 View Complete Implementation : test_decimalfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_max_decimal_places_validation(self):
        field = models.DecimalField(decimal_places=1)
        expected_message = validators.DecimalValidator.messages['max_decimal_places'] % {'max': 1}
        with self.astertRaisesMessage(ValidationError, expected_message):
            field.clean(Decimal('0.99'), None)

0 View Complete Implementation : test_decimalfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_max_whole_digits_validation(self):
        field = models.DecimalField(max_digits=3, decimal_places=1)
        expected_message = validators.DecimalValidator.messages['max_whole_digits'] % {'max': 2}
        with self.astertRaisesMessage(ValidationError, expected_message):
            field.clean(Decimal('999'), None)

0 View Complete Implementation : test_error_messages.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_decimal_field_raises_error_message(self):
        f = models.DecimalField()
        self._test_validation_messages(f, 'fõo', ["'fõo' value must be a decimal number."])

0 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_expression_on_aggregation(self):

        # Create a plain expression
        clast Greatest(Func):
            function = 'GREATEST'

            def as_sqlite(self, compiler, connection, **extra_context):
                return super().as_sql(compiler, connection, function='MAX', **extra_context)

        qs = Publisher.objects.annotate(
            price_or_median=Greatest(Avg('book__rating', output_field=DecimalField()), Avg('book__price'))
        ).filter(price_or_median__gte=F('num_awards')).order_by('num_awards')
        self.astertQuerysetEqual(
            qs, [1, 3, 7, 9], lambda v: v.num_awards)

        qs2 = Publisher.objects.annotate(
            rating_or_num_awards=Greatest(Avg('book__rating'), F('num_awards'),
                                          output_field=FloatField())
        ).filter(rating_or_num_awards__gt=F('num_awards')).order_by('num_awards')
        self.astertQuerysetEqual(
            qs2, [1, 3], lambda v: v.num_awards)

0 View Complete Implementation : views.py
Copyright MIT License
Author : PacktPublishing
def _prepare_order_data(cart):

    cart_items = ShoppingCarsatem.objects.values_list(
        'game__name',
        'price_per_unit',
        'game__id',
        'quansaty').filter(cart__id=cart.id)

    order = cart_items.aggregate(
        total_order=Sum(F('price_per_unit') * F('quansaty'),
                        output_field=DecimalField(decimal_places=2))
    )

    order_items = [OrderItem(*x)._asdict() for x in cart_items]

    order_customer = {
        'customer_id': cart.user.id,
        'email': cart.user.email,
        'name': f'{cart.user.first_name} {cart.user.last_name}'
    }

    order_dict = {
        'items': order_items,
        'order_customer': order_customer,
        'total': order['total_order']
    }

    return json.dumps(order_dict, cls=DjangoJSONEncoder)

0 View Complete Implementation : provider_map.py
Copyright GNU Affero General Public License v3.0
Author : project-koku
    def __init__(self, provider, report_type):
        """Constructor."""
        self._mapping = [
            {
                'provider': 'OCP_All',
                'alias': 'account_alias__account_alias',
                'annotations': {
                    'cluster': 'cluster_id',
                    'project': 'namespace',
                    'account': 'usage_account_id',
                    'service': 'product_code',
                    'az': 'availability_zone'
                },
                'end_date': 'usage_end',
                'filters': {
                    'project': {
                        'field': 'namespace',
                        'operation': 'icontains'
                    },
                    'cluster': [
                        {
                            'field': 'cluster_alias',
                            'operation': 'icontains',
                            'composition_key': 'cluster_filter'
                        },
                        {
                            'field': 'cluster_id',
                            'operation': 'icontains',
                            'composition_key': 'cluster_filter'
                        }
                    ],
                    'node': {
                        'field': 'node',
                        'operation': 'icontains'
                    },
                    'account': [
                        {
                            'field': 'account_alias__account_alias',
                            'operation': 'icontains',
                            'composition_key': 'account_filter'
                        },
                        {
                            'field': 'usage_account_id',
                            'operation': 'icontains',
                            'composition_key': 'account_filter'
                        }
                    ],
                    'service': {
                        'field': 'product_code',
                        'operation': 'icontains'
                    },
                    'product_family': {
                        'field': 'product_family',
                        'operation': 'icontains'
                    },
                    'az': {
                        'field': 'availability_zone',
                        'operation': 'icontains'
                    },
                    'region': {
                        'field': 'region',
                        'operation': 'icontains'
                    }
                },
                'group_by_options': ['account', 'service', 'region',
                                     'cluster', 'project', 'node', 'product_family'],
                'tag_column': 'tags',
                'report_type': {
                    'costs': {
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum(F('unblended_cost')),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                        },
                        'annotations': {
                            # Cost is the first column in annotations so that it
                            # can reference the original database column 'markup_cost'
                            # If cost comes after the markup_cost annotaton, then
                            # Django will reference the annotated value, which is
                            # a Sum() and things will break trying to add
                            # a column with the sum of another column.
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum(F('unblended_cost')),
                            'derived_cost': Value(0, output_field=DecimalField()),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD'))
                        },
                        'count': None,
                        'delta_key': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            )
                        },
                        'filter': [{}],
                        'cost_units_key': 'currency_code',
                        'cost_units_fallback': 'USD',
                        'sum_columns': ['cost', 'infrastructure_cost', 'derived_cost', 'markup_cost'],
                        'default_ordering': {'cost': 'desc'},
                    },
                    'costs_by_project': {
                        'tables': {
                            'query': OCPAllCostLineItemProjectDailySummary,
                            'total': OCPAllCostLineItemProjectDailySummary
                        },
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('pod_cost'), Value(0, output_field=DecimalField()))\
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                        },
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('pod_cost'), Value(0, output_field=DecimalField()))\
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD'))
                        },
                        'count': None,
                        'delta_key': {
                            'cost': Sum(
                                Coalesce(F('pod_cost'), Value(0, output_field=DecimalField()))\
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            )
                        },
                        'filter': [{}],
                        'cost_units_key': 'currency_code',
                        'cost_units_fallback': 'USD',
                        'sum_columns': ['infrastructure_cost', 'markup_cost',
                                        'derived_cost', 'cost'],
                        'default_ordering': {'cost': 'desc'},
                    },
                    'storage': {
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum(F('unblended_cost')),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'usage': Sum(F('usage_amount')),
                            'usage_units': Coalesce(Max('unit'), Value('GB-Mo'))
                        },
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum(F('unblended_cost')),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'usage': Sum(F('usage_amount')),
                            'usage_units': Coalesce(Max('unit'), Value('GB-Mo'))
                        },
                        'count': None,
                        'delta_key': {'usage': Sum('usage_amount')},
                        'filter': [{
                            'field': 'product_family',
                            'operation': 'contains',
                            'parameter': 'Storage'
                        }, ],
                        'cost_units_key': 'currency_code',
                        'cost_units_fallback': 'USD',
                        'usage_units_key': 'unit',
                        'usage_units_fallback': 'GB-Mo',
                        'sum_columns': ['usage', 'infrastructure_cost',
                                        'markup_cost', 'derived_cost', 'cost'],
                        'default_ordering': {'usage': 'desc'},
                    },
                    'storage_by_project': {
                        'tables': {
                            'query': OCPAllCostLineItemProjectDailySummary,
                            'total': OCPAllCostLineItemProjectDailySummary
                        },
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('pod_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'usage': Sum('usage_amount'),
                            'usage_units': Coalesce(Max('unit'), Value('GB-Mo'))
                        },
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('pod_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'usage': Sum('usage_amount'),
                            'usage_units': Coalesce(Max('unit'), Value('GB-Mo'))
                        },
                        'count': None,
                        'delta_key': {'usage': Sum('usage_amount')},
                        'filter': [{
                            'field': 'product_family',
                            'operation': 'contains',
                            'parameter': 'Storage'
                        }, ],
                        'cost_units_key': 'currency_code',
                        'cost_units_fallback': 'USD',
                        'usage_units_key': 'unit',
                        'usage_units_fallback': 'GB-Mo',
                        'sum_columns': ['usage', 'cost', 'infrastructure_cost', 'derived_cost',
                                        'markup_cost'],
                        'default_ordering': {'usage': 'desc'},
                    },
                    'instance_type': {
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum(F('unblended_cost')),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'count': Count('resource_id', distinct=True),
                            'usage': Sum(F('usage_amount')),
                            'usage_units': Coalesce(Max('unit'), Value('GB-Mo'))
                        },
                        'aggregate_key': 'usage_amount',
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum(F('unblended_cost')),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'count': Count('resource_id', distinct=True),
                            'count_units': Value('instances', output_field=CharField()),
                            'usage': Sum(F('usage_amount')),
                            'usage_units': Coalesce(Max('unit'), Value('Hrs'))
                        },
                        'count': 'resource_id',
                        'delta_key': {'usage': Sum('usage_amount')},
                        'filter': [{
                            'field': 'instance_type',
                            'operation': 'isnull',
                            'parameter': False
                        }, ],
                        'group_by': ['instance_type'],
                        'cost_units_key': 'currency_code',
                        'cost_units_fallback': 'USD',
                        'usage_units_key': 'unit',
                        'usage_units_fallback': 'Hrs',
                        'count_units_fallback': 'instances',
                        'sum_columns': ['usage', 'cost', 'infrastructure_cost',
                                        'markup_cost', 'derived_cost', 'count'],
                        'default_ordering': {'usage': 'desc'},
                    },
                    'instance_type_by_project': {
                        'tables': {
                            'query': OCPAllCostLineItemProjectDailySummary,
                            'total': OCPAllCostLineItemProjectDailySummary
                        },
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('pod_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'count': Count('resource_id', distinct=True),
                            'usage': Sum('usage_amount'),
                            'usage_units': Coalesce(Max('unit'), Value('GB-Mo'))
                        },
                        'aggregate_key': 'usage_amount',
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('pod_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'count': Count('resource_id', distinct=True),
                            'count_units': Value('instances', output_field=CharField()),
                            'usage': Sum('usage_amount'),
                            'usage_units': Coalesce(Max('unit'), Value('Hrs'))
                        },
                        'count': 'resource_id',
                        'delta_key': {'usage': Sum('usage_amount')},
                        'filter': [{
                            'field': 'instance_type',
                            'operation': 'isnull',
                            'parameter': False
                        }, ],
                        'group_by': ['instance_type'],
                        'cost_units_key': 'currency_code',
                        'cost_units_fallback': 'USD',
                        'usage_units_key': 'unit',
                        'usage_units_fallback': 'Hrs',
                        'count_units_fallback': 'instances',
                        'sum_columns': ['usage', 'cost', 'infrastructure_cost',
                                        'markup_cost', 'derived_cost', 'count'],
                        'default_ordering': {'usage': 'desc'},
                    },
                    'tags': {
                        'default_ordering': {'cost': 'desc'},
                    },
                },
                'start_date': 'usage_start',
                'tables': {
                    'query': OCPAllCostLineItemDailySummary,
                    'total': OCPAllCostLineItemDailySummary
                },
            }
        ]
        super().__init__(provider, report_type)

0 View Complete Implementation : provider_map.py
Copyright GNU Affero General Public License v3.0
Author : project-koku
    def __init__(self, provider, report_type):
        """Constructor."""
        self._mapping = [
            {
                'provider': 'AWS',
                'alias': 'account_alias__account_alias',
                'annotations': {
                    'account': 'usage_account_id',
                    'service': 'product_code',
                    'az': 'availability_zone'
                },
                'end_date': 'usage_end',
                'filters': {
                    'account': [
                        {
                            'field': 'account_alias__account_alias',
                            'operation': 'icontains',
                            'composition_key': 'account_filter'
                        },
                        {
                            'field': 'usage_account_id',
                            'operation': 'icontains',
                            'composition_key': 'account_filter'
                        }
                    ],
                    'service': {
                        'field': 'product_code',
                        'operation': 'icontains'
                    },
                    'az': {
                        'field': 'availability_zone',
                        'operation': 'icontains'
                    },
                    'region': {
                        'field': 'region',
                        'operation': 'icontains'
                    },
                    'product_family': {
                        'field': 'product_family',
                        'operation': 'icontains'
                    }
                },
                'group_by_options': ['service', 'account', 'region', 'az', 'product_family'],
                'tag_column': 'tags',
                'report_type': {
                    'costs': {
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('unblended_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                        },
                        'aggregate_key': 'unblended_cost',
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('unblended_cost'),
                            'derived_cost': Value(0, output_field=DecimalField()),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD'))
                        },
                        'delta_key': {'cost': Sum(ExpressionWrapper(F('unblended_cost') + F('markup_cost'),
                                                                    output_field=DecimalField()))},
                        'filter': [{}],
                        'cost_units_key': 'currency_code',
                        'cost_units_fallback': 'USD',
                        'sum_columns': ['cost', 'infrastructure_cost', 'derived_cost', 'markup_cost'],
                        'default_ordering': {'cost': 'desc'},
                    },
                    'instance_type': {
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('unblended_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum('markup_cost'),
                            'count': Sum(Value(0, output_field=DecimalField())),
                            'usage': Sum('usage_amount'),
                        },
                        'aggregate_key': 'usage_amount',
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('unblended_cost'),
                            'derived_cost': Value(0, output_field=DecimalField()),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            # The summary table already already has counts
                            'count': Max('resource_count'),
                            'count_units': Value('instances', output_field=CharField()),
                            'usage': Sum('usage_amount'),
                            'usage_units': Coalesce(Max('unit'), Value('Hrs'))
                        },
                        'delta_key': {'usage': Sum('usage_amount')},
                        'filter': [{
                            'field': 'instance_type',
                            'operation': 'isnull',
                            'parameter': False
                        }, ],
                        'group_by': ['instance_type'],
                        'cost_units_key': 'currency_code',
                        'cost_units_fallback': 'USD',
                        'usage_units_key': 'unit',
                        'usage_units_fallback': 'Hrs',
                        'count_units_fallback': 'instances',
                        'sum_columns': ['usage', 'cost', 'infrastructure_cost',
                                        'derived_cost', 'markup_cost', 'count'],
                        'default_ordering': {'usage': 'desc'},
                    },
                    'storage': {
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('unblended_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum('markup_cost'),
                            'usage': Sum('usage_amount'),
                        },
                        'aggregate_key': 'usage_amount',
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('unblended_cost'),
                            'derived_cost': Value(0, output_field=DecimalField()),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'usage': Sum('usage_amount'),
                            'usage_units': Coalesce(Max('unit'), Value('GB-Mo'))
                        },
                        'delta_key': {'usage': Sum('usage_amount')},
                        'filter': [{
                            'field': 'product_family',
                            'operation': 'contains',
                            'parameter': 'Storage'
                        }, {
                            'field': 'unit',
                            'operation': 'exact',
                            'parameter': 'GB-Mo'
                        }],
                        'cost_units_key': 'currency_code',
                        'cost_units_fallback': 'USD',
                        'usage_units_key': 'unit',
                        'usage_units_fallback': 'GB-Mo',
                        'sum_columns': ['usage', 'cost', 'infrastructure_cost',
                                        'derived_cost', 'markup_cost'],
                        'default_ordering': {'usage': 'desc'},
                    },
                    'tags': {
                        'default_ordering': {'cost': 'desc'},
                    },
                },
                'start_date': 'usage_start',
                'tables': {
                    'query': AWSCostEntryLineItemDailySummary,
                },
            },
        ]

        self.views = {
            'costs': {
                'default': AWSCostSummary,
                'account': AWSCostSummaryByAccount,
                'region': AWSCostSummaryByRegion,
                'service': AWSCostSummaryByService,
                'product_family': AWSCostSummaryByService
            },
            'instance_type': {
                'default': AWSComputeSummary,
                'account': AWSComputeSummaryByAccount,
                'region': AWSComputeSummaryByRegion,
                'service': AWSComputeSummaryByService,
                'product_family': AWSComputeSummaryByService,
                'instance_type': AWSComputeSummary
            },
            'storage': {
                'default': AWSStorageSummary,
                'account': AWSStorageSummaryByAccount,
                'region': AWSStorageSummaryByRegion,
                'service': AWSStorageSummaryByService,
                'product_family': AWSStorageSummaryByService
            },
            'database': {
                'default': AWSDatabaseSummary,
                'service': AWSDatabaseSummary
            },
            'network': {
                'default': AWSNetworkSummary,
                'service': AWSNetworkSummary
            }
        }
        super().__init__(provider, report_type)

0 View Complete Implementation : provider_map.py
Copyright GNU Affero General Public License v3.0
Author : project-koku
    def __init__(self, provider, report_type):
        """Constructor."""
        self._mapping = [
            {
                'provider': 'OCP_AZURE',
                'alias': 'subscription_guid',
                'annotations': {
                    'cluster': 'cluster_id',
                    'project': 'namespace',
                },
                'end_date': 'costentrybill__billing_period_end',
                'filters': {
                    'project': {'field': 'namespace', 'operation': 'icontains'},
                    'cluster': [
                        {
                            'field': 'cluster_alias',
                            'operation': 'icontains',
                            'composition_key': 'cluster_filter',
                        },
                        {
                            'field': 'cluster_id',
                            'operation': 'icontains',
                            'composition_key': 'cluster_filter',
                        },
                    ],
                    'node': {'field': 'node', 'operation': 'icontains'},
                    'subscription_guid': [
                        {
                            'field': 'subscription_guid',
                            'operation': 'icontains',
                            'composition_key': 'account_filter',
                        }
                    ],
                    'service_name': {'field': 'service_name', 'operation': 'icontains'},
                    'resource_location': {
                        'field': 'resource_location',
                        'operation': 'icontains',
                    },
                    'instance_type': {
                        'field': 'instance_type',
                        'operation': 'icontains',
                    },
                },
                'group_by_options': [
                    'cluster',  # ocp
                    'project',  # ocp
                    'node',  # ocp
                    'service_name',  # azure
                    'subscription_guid',  # azure
                    'resource_location',  # azure
                    'instance_type',  # azure
                ],
                'tag_column': 'tags',
                'report_type': {
                    'costs': {
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(
                                    F('pretax_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                                + Coalesce(
                                    F('markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'infrastructure_cost': Sum(F('pretax_cost')),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(
                                    F('markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                        },
                        'annotations': {
                            # Cost is the first column in annotations so that it
                            # can reference the original database column 'markup_cost'
                            # If cost comes after the markup_cost annotaton, then
                            # Django will reference the annotated value, which is
                            # a Sum() and things will break trying to add
                            # a column with the sum of another column.
                            'cost': Sum(
                                Coalesce(
                                    F('pretax_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                                + Coalesce(
                                    F('markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'infrastructure_cost': Sum(F('pretax_cost')),
                            'derived_cost': Value(0, output_field=DecimalField()),
                            'markup_cost': Sum(
                                Coalesce(
                                    F('markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'cost_units': Coalesce(Max('currency'), Value('USD')),
                        },
                        'count': None,
                        'delta_key': {
                            'cost': Sum(
                                Coalesce(
                                    F('pretax_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                                + Coalesce(
                                    F('markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            )
                        },
                        'filter': [{}],
                        'cost_units_key': 'currency',
                        'cost_units_fallback': 'USD',
                        'sum_columns': [
                            'cost',
                            'infrastructure_cost',
                            'derived_cost',
                            'markup_cost',
                        ],
                        'default_ordering': {'cost': 'desc'},
                    },
                    'costs_by_project': {
                        'tables': {
                            'query': OCPAzureCostLineItemProjectDailySummary,
                            'total': OCPAzureCostLineItemProjectDailySummary,
                        },
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(
                                    F('pod_cost'), Value(0, output_field=DecimalField())
                                )
                                + Coalesce(
                                    F('project_markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(
                                    F('project_markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                        },
                        'annotations': {
                            'cost': Sum(
                                Coalesce(
                                    F('pod_cost'), Value(0, output_field=DecimalField())
                                )
                                + Coalesce(
                                    F('project_markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(
                                    F('project_markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'cost_units': Coalesce(Max('currency'), Value('USD')),
                        },
                        'count': None,
                        'delta_key': {
                            'cost': Sum(
                                Coalesce(
                                    F('pod_cost'), Value(0, output_field=DecimalField())
                                )
                                + Coalesce(
                                    F('project_markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            )
                        },
                        'filter': [{}],
                        'cost_units_key': 'currency',
                        'cost_units_fallback': 'USD',
                        'sum_columns': [
                            'infrastructure_cost',
                            'markup_cost',
                            'derived_cost',
                            'cost',
                        ],
                        'default_ordering': {'cost': 'desc'},
                    },
                    'storage': {
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(
                                    F('pretax_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                                + Coalesce(
                                    F('markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'infrastructure_cost': Sum(F('pretax_cost')),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(
                                    F('markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'cost_units': Coalesce(Max('currency'), Value('USD')),
                            'usage': Sum(F('usage_quansaty')),
                            'usage_units': Coalesce(
                                Max('unit_of_measure'),
                                Value('Storage Type Placeholder'),
                            ),
                        },
                        'annotations': {
                            'cost': Sum(
                                Coalesce(
                                    F('pretax_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                                + Coalesce(
                                    F('markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'infrastructure_cost': Sum(F('pretax_cost')),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(
                                    F('markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'cost_units': Coalesce(Max('currency'), Value('USD')),
                            'usage': Sum(F('usage_quansaty')),
                            # FIXME: Waiting on MSFT for usage_units default
                            'usage_units': Coalesce(
                                Max('unit_of_measure'),
                                Value('Storage Type Placeholder'),
                            ),
                        },
                        'count': None,
                        'delta_key': {'usage': Sum('usage_quansaty')},
                        'filter': [
                            {
                                'field': 'service_name',
                                'operation': 'contains',
                                'parameter': 'Storage',
                            }
                        ],
                        'cost_units_key': 'currency',
                        'cost_units_fallback': 'USD',
                        'usage_units_key': 'unit_of_measure',
                        'usage_units_fallback': 'Storage Type Placeholder',  # FIXME
                        'sum_columns': [
                            'usage',
                            'infrastructure_cost',
                            'markup_cost',
                            'derived_cost',
                            'cost',
                        ],
                        'default_ordering': {'usage': 'desc'},
                    },
                    'storage_by_project': {
                        'tables': {
                            'query': OCPAzureCostLineItemProjectDailySummary,
                            'total': OCPAzureCostLineItemProjectDailySummary,
                        },
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(
                                    F('pod_cost'), Value(0, output_field=DecimalField())
                                )
                                + Coalesce(
                                    F('project_markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(
                                    F('project_markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'cost_units': Coalesce(Max('currency'), Value('USD')),
                            'usage': Sum('usage_quansaty'),
                            # FIXME: Waiting on MSFT for usage_units default
                            'usage_units': Coalesce(
                                Max('unit_of_measure'),
                                Value('Storage Type Placeholder'),
                            ),
                        },
                        'annotations': {
                            'cost': Sum(
                                Coalesce(
                                    F('pod_cost'), Value(0, output_field=DecimalField())
                                )
                                + Coalesce(
                                    F('project_markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(
                                    F('project_markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'cost_units': Coalesce(Max('currency'), Value('USD')),
                            'usage': Sum('usage_quansaty'),
                            'usage_units': Coalesce(
                                Max('unit_of_measure'),
                                Value('Storage Type Placeholder'),
                            ),
                        },
                        'count': None,
                        'delta_key': {'usage': Sum('usage_quansaty')},
                        'filter': [
                            {
                                'field': 'service_name',
                                'operation': 'contains',
                                'parameter': 'Storage',
                            }
                        ],
                        'cost_units_key': 'currency',
                        'cost_units_fallback': 'USD',
                        'usage_units_key': 'unit_of_measure',
                        'usage_units_fallback': 'Storage Type Placeholder',  # FIXME
                        'sum_columns': [
                            'usage',
                            'cost',
                            'infrastructure_cost',
                            'derived_cost',
                            'markup_cost',
                        ],
                        'default_ordering': {'usage': 'desc'},
                    },
                    'instance_type': {
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(
                                    F('pretax_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                                + Coalesce(
                                    F('markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'infrastructure_cost': Sum(F('pretax_cost')),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(
                                    F('markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'cost_units': Coalesce(Max('currency'), Value('USD')),
                            'count': Count('resource_id', distinct=True),
                            'usage': Sum(F('usage_quansaty')),
                            # FIXME: Waiting on MSFT for usage_units default
                            'usage_units': Coalesce(
                                Max('unit_of_measure'),
                                Value('Instance Type Placeholder'),
                            ),
                        },
                        'aggregate_key': 'usage_quansaty',
                        'annotations': {
                            'cost': Sum(
                                Coalesce(
                                    F('pretax_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                                + Coalesce(
                                    F('markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'infrastructure_cost': Sum(F('pretax_cost')),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(
                                    F('markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'cost_units': Coalesce(Max('currency'), Value('USD')),
                            'count': Count('resource_id', distinct=True),
                            'count_units': Value('instances', output_field=CharField()),
                            'usage': Sum(F('usage_quansaty')),
                            # FIXME: Waiting on MSFT for usage_units default
                            'usage_units': Coalesce(
                                Max('unit_of_measure'),
                                Value('Instance Type Placeholder'),
                            ),
                        },
                        'count': 'resource_id',
                        'delta_key': {'usage': Sum('usage_quansaty')},
                        'filter': [
                            {
                                'field': 'instance_type',
                                'operation': 'isnull',
                                'parameter': False,
                            }
                        ],
                        'group_by': ['instance_type'],
                        'cost_units_key': 'currency',
                        'cost_units_fallback': 'USD',
                        'usage_units_key': 'unit_of_measure',
                        'usage_units_fallback': 'Instance Type Placeholder',  # FIXME
                        'count_units_fallback': 'instances',
                        'sum_columns': [
                            'usage',
                            'cost',
                            'infrastructure_cost',
                            'markup_cost',
                            'derived_cost',
                            'count',
                        ],
                        'default_ordering': {'usage': 'desc'},
                    },
                    'instance_type_by_project': {
                        'tables': {
                            'query': OCPAzureCostLineItemProjectDailySummary,
                            'total': OCPAzureCostLineItemProjectDailySummary,
                        },
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(
                                    F('pod_cost'), Value(0, output_field=DecimalField())
                                )
                                + Coalesce(
                                    F('project_markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(
                                    F('project_markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'cost_units': Coalesce(Max('currency'), Value('USD')),
                            'count': Count('resource_id', distinct=True),
                            'usage': Sum('usage_quansaty'),
                            # FIXME: Waiting on MSFT for usage_units default
                            'usage_units': Coalesce(
                                Max('unit_of_measure'),
                                Value('Instance Type Placeholder'),
                            ),
                        },
                        'aggregate_key': 'usage_quansaty',
                        'annotations': {
                            'cost': Sum(
                                Coalesce(
                                    F('pod_cost'), Value(0, output_field=DecimalField())
                                )
                                + Coalesce(
                                    F('project_markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(
                                    F('project_markup_cost'),
                                    Value(0, output_field=DecimalField()),
                                )
                            ),
                            'cost_units': Coalesce(Max('currency'), Value('USD')),
                            'count': Count('resource_id', distinct=True),
                            'count_units': Value('instances', output_field=CharField()),
                            'usage': Sum('usage_quansaty'),
                            # FIXME: Waiting on MSFT for usage_units default
                            'usage_units': Coalesce(
                                Max('unit_of_measure'),
                                Value('Instance Type Placeholder'),
                            ),
                        },
                        'count': 'resource_id',
                        'delta_key': {'usage': Sum('usage_quansaty')},
                        'filter': [
                            {
                                'field': 'instance_type',
                                'operation': 'isnull',
                                'parameter': False,
                            }
                        ],
                        'group_by': ['instance_type'],
                        'cost_units_key': 'currency',
                        'cost_units_fallback': 'USD',
                        'usage_units_key': 'unit_of_measure',
                        'usage_units_fallback': 'Instance Type Placeholder',  # FIXME
                        'count_units_fallback': 'instances',
                        'sum_columns': [
                            'usage',
                            'cost',
                            'infrastructure_cost',
                            'markup_cost',
                            'derived_cost',
                            'count',
                        ],
                        'default_ordering': {'usage': 'desc'},
                    },
                    'tags': {'default_ordering': {'cost': 'desc'}},
                },
                'start_date': 'usage_start',
                'tables': {
                    'query': OCPAzureCostLineItemDailySummary,
                    'total': OCPAzureCostLineItemDailySummary,
                },
            }
        ]
        super().__init__(provider, report_type)

0 View Complete Implementation : provider_map.py
Copyright GNU Affero General Public License v3.0
Author : project-koku
    def __init__(self, provider, report_type):
        """Constructor."""
        self._mapping = [
            {
                'provider': 'AZURE',
                'alias': 'subscription_guid',  # FIXME: probably wrong
                'annotations': {},
                'end_date': 'costentrybill__billing_period_end',
                'filters': {
                    'subscription_guid': [
                        {
                            'field': 'subscription_guid',
                            'operation': 'icontains',
                            'composition_key': 'account_filter'
                        },
                    ],
                    'service_name': {
                        'field': 'service_name',
                        'operation': 'icontains'
                    },
                    'resource_location': {
                        'field': 'resource_location',
                        'operation': 'icontains'
                    },
                    'instance_type': {
                        'field': 'instance_type',
                        'operation': 'icontains'
                    }
                },
                'group_by_options': ['service_name', 'subscription_guid',
                                     'resource_location', 'instance_type'],
                'tag_column': 'tags',
                'report_type': {
                    'costs': {
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('pretax_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pretax_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                        },
                        'aggregate_key': 'pretax_cost',
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('pretax_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pretax_cost'),
                            'derived_cost': Value(0, output_field=DecimalField()),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency'), Value('USD'))
                        },
                        'delta_key': {
                            'cost': Sum(
                                Coalesce(F('pretax_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            )
                        },
                        'filter': [{}],
                        'cost_units_key': 'currency',
                        'cost_units_fallback': 'USD',
                        'sum_columns': ['cost', 'infrastructure_cost', 'derived_cost', 'markup_cost'],
                        'default_ordering': {'cost': 'desc'},
                    },
                    'instance_type': {
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('pretax_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pretax_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'count': Sum(Value(0, output_field=DecimalField())),
                            'usage': Sum('usage_quansaty'),
                        },
                        'aggregate_key': 'usage_quansaty',
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('pretax_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pretax_cost'),
                            'derived_cost': Value(0, output_field=DecimalField()),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency'), Value('USD')),
                            'count': Max('instance_count'),
                            'count_units': Value('instance_types', output_field=CharField()),
                            'usage': Sum('usage_quansaty'),
                            # FIXME: Waiting on MSFT for usage_units default
                            'usage_units': Coalesce(Max('unit_of_measure'), Value('Instance Type Placeholder'))
                        },
                        'delta_key': {'usage': Sum('usage_quansaty')},
                        'filter': [{
                            'field': 'instance_type',
                            'operation': 'isnull',
                            'parameter': False
                        }],
                        'group_by': ['instance_type'],
                        'cost_units_key': 'currency',
                        'cost_units_fallback': 'USD',
                        'usage_units_key': 'unit_of_measure',
                        'usage_units_fallback': 'Instance Type Placeholder',  # FIXME: Waiting on MSFT
                        'count_units_fallback': 'instances',
                        'sum_columns': ['usage', 'cost', 'infrastructure_cost',
                                        'derived_cost', 'markup_cost', 'count'],
                        'default_ordering': {'usage': 'desc'},
                    },
                    'storage': {
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('pretax_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'usage': Sum('usage_quansaty'),
                            'infrastructure_cost': Sum('pretax_cost'),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                        },
                        'aggregate_key': 'usage_quansaty',
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('pretax_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pretax_cost'),
                            'derived_cost': Value(0, output_field=DecimalField()),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency'), Value('USD')),
                            'usage': Sum('usage_quansaty'),
                            # FIXME: Waiting on MSFT for usage_units default
                            'usage_units': Coalesce(Max('unit_of_measure'), Value('Storage Type Placeholder'))
                        },
                        'delta_key': {'usage': Sum('usage_quansaty')},
                        'filter': [{
                            'field': 'service_name',
                            'operation': 'contains',
                            'parameter': 'Storage'
                        }],
                        'cost_units_key': 'currency',
                        'cost_units_fallback': 'USD',
                        'usage_units_key': 'unit_of_measure',
                        'usage_units_fallback': 'Storage Type Placeholder',  # FIXME
                        'sum_columns': ['usage', 'cost', 'infrastructure_cost', 'derived_cost', 'markup_cost'],
                        'default_ordering': {'usage': 'desc'},
                    },
                    'tags': {
                        'default_ordering': {'cost': 'desc'},
                    },
                },
                'start_date': 'costentrybill__billing_period_start',
                'tables': {
                    'query': AzureCostEntryLineItemDailySummary,
                },
            },
        ]
        super().__init__(provider, report_type)

0 View Complete Implementation : provider_map.py
Copyright GNU Affero General Public License v3.0
Author : project-koku
    def __init__(self, provider, report_type):
        """Constructor."""
        self._mapping = [
            {
                'provider': 'OCP',
                'annotations': {
                    'cluster': 'cluster_id',
                    'project': 'namespace'
                },
                'end_date': 'usage_end',
                'filters': {
                    'project': {
                        'field': 'namespace',
                        'operation': 'icontains'
                    },
                    'cluster': [
                        {
                            'field': 'cluster_alias',
                            'operation': 'icontains',
                            'composition_key': 'cluster_filter'
                        },
                        {
                            'field': 'cluster_id',
                            'operation': 'icontains',
                            'composition_key': 'cluster_filter'
                        }
                    ],
                    'pod': {
                        'field': 'pod',
                        'operation': 'icontains'
                    },
                    'node': {
                        'field': 'node',
                        'operation': 'icontains'
                    },
                    'infrastructures': {
                        'field': 'cluster_id',
                        'operation': 'exact',
                        'custom': ProviderAccessor('OCP').infrastructure_key_list
                    },
                },
                'group_by_options': ['cluster', 'project', 'node'],
                'tag_column': 'pod_labels',
                'report_type': {
                    'costs': {
                        'tables': {
                            'query': OCPUsageLineItemDailySummary
                        },
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('pod_charge_cpu_core_hours'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('pod_charge_memory_gigabyte_hours'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('persistentvolumeclaim_charge_gb_month'),
                                           Value(0, output_field=DecimalField()))
                                + Coalesce(F('infra_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('monthly_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum(
                                Coalesce(F('infra_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'derived_cost': Sum(
                                Coalesce(F('pod_charge_cpu_core_hours'),
                                         Value(0, output_field=DecimalField()))
                                + Coalesce(F('pod_charge_memory_gigabyte_hours'),
                                           Value(0, output_field=DecimalField()))
                                + Coalesce(F('persistentvolumeclaim_charge_gb_month'),
                                           Value(0, output_field=DecimalField()))
                                + Coalesce(F('monthly_cost'), Value(0, output_field=DecimalField()))

                            ),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                        },
                        'default_ordering': {'cost': 'desc'},
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('pod_charge_cpu_core_hours'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('pod_charge_memory_gigabyte_hours'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('persistentvolumeclaim_charge_gb_month'),
                                           Value(0, output_field=DecimalField()))
                                + Coalesce(F('infra_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('monthly_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum(
                                Coalesce(F('infra_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'derived_cost': Sum(
                                Coalesce(F('pod_charge_cpu_core_hours'),
                                         Value(0, output_field=DecimalField()))
                                + Coalesce(F('pod_charge_memory_gigabyte_hours'),
                                           Value(0, output_field=DecimalField()))
                                + Coalesce(F('persistentvolumeclaim_charge_gb_month'),
                                           Value(0, output_field=DecimalField()))
                                + Coalesce(F('monthly_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Value('USD', output_field=CharField())
                        },
                        'capacity_aggregate': {},
                        'delta_key': {
                            'cost': Sum(
                                Coalesce(F('pod_charge_cpu_core_hours'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('pod_charge_memory_gigabyte_hours'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('persistentvolumeclaim_charge_gb_month'),
                                           Value(0, output_field=DecimalField()))
                                + Coalesce(F('infra_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('monthly_cost'), Value(0, output_field=DecimalField()))
                            ),
                        },
                        'filter': [{}],
                        'cost_units_key': 'USD',
                        'sum_columns': ['cost', 'infrastructure_cost', 'derived_cost', 'markup_cost'],
                    },
                    'costs_by_project': {
                        'tables': {
                            'query': OCPUsageLineItemDailySummary
                        },
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('pod_charge_cpu_core_hours'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('pod_charge_memory_gigabyte_hours'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('persistentvolumeclaim_charge_gb_month'),
                                           Value(0, output_field=DecimalField()))
                                + Coalesce(F('project_infra_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum(
                                Coalesce(F('project_infra_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'derived_cost': Sum(
                                Coalesce(F('pod_charge_cpu_core_hours'),
                                         Value(0, output_field=DecimalField()))
                                + Coalesce(F('pod_charge_memory_gigabyte_hours'),
                                           Value(0, output_field=DecimalField()))
                                + Coalesce(F('persistentvolumeclaim_charge_gb_month'),
                                           Value(0, output_field=DecimalField()))
                            ),
                            'markup_cost': Sum(
                                Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                        },
                        'default_ordering': {'cost': 'desc'},
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('pod_charge_cpu_core_hours'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('pod_charge_memory_gigabyte_hours'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('persistentvolumeclaim_charge_gb_month'),
                                           Value(0, output_field=DecimalField()))
                                + Coalesce(F('project_infra_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum(
                                Coalesce(F('project_infra_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'derived_cost': Sum(
                                Coalesce(F('pod_charge_cpu_core_hours'),
                                         Value(0, output_field=DecimalField()))
                                + Coalesce(F('pod_charge_memory_gigabyte_hours'),
                                           Value(0, output_field=DecimalField()))
                                + Coalesce(F('persistentvolumeclaim_charge_gb_month'),
                                           Value(0, output_field=DecimalField()))
                            ),
                            'markup_cost': Sum(
                                Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Value('USD', output_field=CharField())
                        },
                        'capacity_aggregate': {},
                        'delta_key': {
                            'cost': Sum(
                                Coalesce(F('pod_charge_cpu_core_hours'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('pod_charge_memory_gigabyte_hours'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('persistentvolumeclaim_charge_gb_month'),
                                           Value(0, output_field=DecimalField()))
                                + Coalesce(F('project_infra_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                        },
                        'filter': [{}],
                        'cost_units_key': 'USD',
                        'sum_columns': ['cost', 'infrastructure_cost', 'derived_cost', 'markup_cost'],
                    },
                    'cpu': {
                        'aggregates': {
                            'cost': Sum('pod_charge_cpu_core_hours'),
                            'usage': Sum('pod_usage_cpu_core_hours'),
                            'request': Sum('pod_request_cpu_core_hours'),
                            'limit': Sum('pod_limit_cpu_core_hours'),
                            'infrastructure_cost': Sum(Value(0, output_field=DecimalField())),
                            'derived_cost': Sum('pod_charge_cpu_core_hours'),
                        },
                        'capacity_aggregate': {
                            'capacity': Max('cluster_capacity_cpu_core_hours')
                        },
                        'default_ordering': {'usage': 'desc'},
                        'annotations': {
                            'usage': Sum('pod_usage_cpu_core_hours'),
                            'request': Sum('pod_request_cpu_core_hours'),
                            'limit': Sum('pod_limit_cpu_core_hours'),
                            'capacity': {
                                'total': Max('total_capacity_cpu_core_hours'),
                                'cluster': Max('cluster_capacity_cpu_core_hours'),
                            },
                            'cost': Sum('pod_charge_cpu_core_hours'),
                            'infrastructure_cost': Value(0, output_field=DecimalField()),
                            'derived_cost': Sum('pod_charge_cpu_core_hours'),
                            'cost_units': Value('USD', output_field=CharField()),
                            'usage_units': Value('Core-Hours', output_field=CharField())
                        },
                        'delta_key': {
                            'usage': Sum('pod_usage_cpu_core_hours'),
                            'request': Sum('pod_request_cpu_core_hours'),
                            'cost': Sum('pod_charge_cpu_core_hours')
                        },
                        'filter': [
                            {
                                'field': 'data_source',
                                'operation': 'exact',
                                'parameter': 'Pod'
                            }
                        ],
                        'cost_units_key': 'USD',
                        'usage_units_key': 'Core-Hours',
                        'sum_columns': ['usage', 'request', 'limit', 'infrastructure_cost',
                                        'derived_cost', 'cost'],
                    },
                    'memory': {
                        'aggregates': {
                            'usage': Sum('pod_usage_memory_gigabyte_hours'),
                            'request': Sum('pod_request_memory_gigabyte_hours'),
                            'limit': Sum('pod_limit_memory_gigabyte_hours'),
                            'cost': Sum('pod_charge_memory_gigabyte_hours'),
                            'infrastructure_cost': Sum(Value(0, output_field=DecimalField())),
                            'derived_cost': Sum('pod_charge_memory_gigabyte_hours'),
                        },
                        'capacity_aggregate': {
                            'capacity': Max('cluster_capacity_memory_gigabyte_hours')
                        },
                        'default_ordering': {'usage': 'desc'},
                        'annotations': {
                            'usage': Sum('pod_usage_memory_gigabyte_hours'),
                            'request': Sum('pod_request_memory_gigabyte_hours'),
                            'limit': Sum('pod_limit_memory_gigabyte_hours'),
                            'capacity': {
                                'total': Max('total_capacity_memory_gigabyte_hours'),
                                'cluster': Max('cluster_capacity_memory_gigabyte_hours'),
                            },
                            'cost': Sum('pod_charge_memory_gigabyte_hours'),
                            'infrastructure_cost': Value(0, output_field=DecimalField()),
                            'derived_cost': Sum('pod_charge_memory_gigabyte_hours'),
                            'cost_units': Value('USD', output_field=CharField()),
                            'usage_units': Value('GB-Hours', output_field=CharField())
                        },
                        'delta_key': {
                            'usage': Sum('pod_usage_memory_gigabyte_hours'),
                            'request': Sum('pod_request_memory_gigabyte_hours'),
                            'cost': Sum('pod_charge_memory_gigabyte_hours')
                        },
                        'filter': [
                            {
                                'field': 'data_source',
                                'operation': 'exact',
                                'parameter': 'Pod'
                            }
                        ],
                        'cost_units_key': 'USD',
                        'usage_units_key': 'GB-Hours',
                        'sum_columns': ['usage', 'request', 'limit', 'infrastructure_cost',
                                        'derived_cost', 'cost'],
                    },
                    'volume': {
                        'tag_column': 'volume_labels',
                        'aggregates': {
                            'usage': Sum('persistentvolumeclaim_usage_gigabyte_months'),
                            'request': Sum('volume_request_storage_gigabyte_months'),
                            'cost': Sum('persistentvolumeclaim_charge_gb_month'),
                            'infrastructure_cost': Sum(Value(0, output_field=DecimalField())),
                            'derived_cost': Sum('persistentvolumeclaim_charge_gb_month'),
                        },
                        'capacity_aggregate': {
                            'capacity': Sum('persistentvolumeclaim_capacity_gigabyte_months')
                        },
                        'default_ordering': {'usage': 'desc'},
                        'annotations': {
                            'usage': Sum('persistentvolumeclaim_usage_gigabyte_months'),
                            'request': Sum('volume_request_storage_gigabyte_months'),
                            'capacity': {
                                'total': Sum('persistentvolumeclaim_capacity_gigabyte_months'),
                                'cluster': Sum('persistentvolumeclaim_capacity_gigabyte_months'),
                            },
                            'cost': Sum('persistentvolumeclaim_charge_gb_month'),
                            'infrastructure_cost': Value(0, output_field=DecimalField()),
                            'derived_cost': Sum('persistentvolumeclaim_charge_gb_month'),
                            'cost_units': Value('USD', output_field=CharField()),
                            'usage_units': Value('GB-Mo', output_field=CharField()),
                        },
                        'delta_key': {
                            'usage': Sum('persistentvolumeclaim_usage_gigabyte_months'),
                            'request': Sum('volume_request_storage_gigabyte_months'),
                            'cost': Sum('persistentvolumeclaim_charge_gb_month')
                        },
                        'filter': [
                            {
                                'field': 'data_source',
                                'operation': 'exact',
                                'parameter': 'Storage'
                            }
                        ],
                        'cost_units_key': 'USD',
                        'usage_units_key': 'GB-Mo',
                        'sum_columns': ['usage', 'request', 'infrastructure_cost',
                                        'derived_cost', 'cost'],
                    },
                    'tags': {
                        'default_ordering': {'cost': 'desc'},
                    },
                },
                'start_date': 'usage_start',
                'tables': {
                    'query': OCPUsageLineItemDailySummary,
                },
            },
        ]
        super().__init__(provider, report_type)

0 View Complete Implementation : provider_map.py
Copyright GNU Affero General Public License v3.0
Author : project-koku
    def __init__(self, provider, report_type):
        """Constructor."""
        self._mapping = [
            {
                'provider': 'OCP_AWS',
                'alias': 'account_alias__account_alias',
                'annotations': {
                    'cluster': 'cluster_id',
                    'project': 'namespace',
                    'account': 'usage_account_id',
                    'service': 'product_code',
                    'az': 'availability_zone'
                },
                'end_date': 'usage_end',
                'filters': {
                    'project': {
                        'field': 'namespace',
                        'operation': 'icontains'
                    },
                    'cluster': [
                        {
                            'field': 'cluster_alias',
                            'operation': 'icontains',
                            'composition_key': 'cluster_filter'
                        },
                        {
                            'field': 'cluster_id',
                            'operation': 'icontains',
                            'composition_key': 'cluster_filter'
                        }
                    ],
                    'node': {
                        'field': 'node',
                        'operation': 'icontains'
                    },
                    'account': [
                        {
                            'field': 'account_alias__account_alias',
                            'operation': 'icontains',
                            'composition_key': 'account_filter'
                        },
                        {
                            'field': 'usage_account_id',
                            'operation': 'icontains',
                            'composition_key': 'account_filter'
                        }
                    ],
                    'service': {
                        'field': 'product_code',
                        'operation': 'icontains'
                    },
                    'product_family': {
                        'field': 'product_family',
                        'operation': 'icontains'
                    },
                    'az': {
                        'field': 'availability_zone',
                        'operation': 'icontains'
                    },
                    'region': {
                        'field': 'region',
                        'operation': 'icontains'
                    }
                },
                'group_by_options': ['account', 'service', 'region',
                                     'cluster', 'project', 'node', 'product_family'],
                'tag_column': 'tags',
                'report_type': {
                    'costs': {
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum(F('unblended_cost')),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                        },
                        'annotations': {
                            # Cost is the first column in annotations so that it
                            # can reference the original database column 'markup_cost'
                            # If cost comes after the markup_cost annotaton, then
                            # Django will reference the annotated value, which is
                            # a Sum() and things will break trying to add
                            # a column with the sum of another column.
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum(F('unblended_cost')),
                            'derived_cost': Value(0, output_field=DecimalField()),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD'))
                        },
                        'count': None,
                        'delta_key': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            )
                        },
                        'filter': [{}],
                        'cost_units_key': 'currency_code',
                        'cost_units_fallback': 'USD',
                        'sum_columns': ['cost', 'infrastructure_cost', 'derived_cost', 'markup_cost'],
                        'default_ordering': {'cost': 'desc'},
                    },
                    'costs_by_project': {
                        'tables': {
                            'query': OCPAWSCostLineItemProjectDailySummary,
                            'total': OCPAWSCostLineItemProjectDailySummary
                        },
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('pod_cost'), Value(0, output_field=DecimalField()))\
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                        },
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('pod_cost'), Value(0, output_field=DecimalField()))\
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD'))
                        },
                        'count': None,
                        'delta_key': {
                            'cost': Sum(
                                Coalesce(F('pod_cost'), Value(0, output_field=DecimalField()))\
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            )
                        },
                        'filter': [{}],
                        'cost_units_key': 'currency_code',
                        'cost_units_fallback': 'USD',
                        'sum_columns': ['infrastructure_cost', 'markup_cost',
                                        'derived_cost', 'cost'],
                        'default_ordering': {'cost': 'desc'},
                    },
                    'storage': {
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum(F('unblended_cost')),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'usage': Sum(F('usage_amount')),
                            'usage_units': Coalesce(Max('unit'), Value('GB-Mo'))
                        },
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum(F('unblended_cost')),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'usage': Sum(F('usage_amount')),
                            'usage_units': Coalesce(Max('unit'), Value('GB-Mo'))
                        },
                        'count': None,
                        'delta_key': {'usage': Sum('usage_amount')},
                        'filter': [{
                            'field': 'product_family',
                            'operation': 'contains',
                            'parameter': 'Storage'
                        }, ],
                        'cost_units_key': 'currency_code',
                        'cost_units_fallback': 'USD',
                        'usage_units_key': 'unit',
                        'usage_units_fallback': 'GB-Mo',
                        'sum_columns': ['usage', 'infrastructure_cost',
                                        'markup_cost', 'derived_cost', 'cost'],
                        'default_ordering': {'usage': 'desc'},
                    },
                    'storage_by_project': {
                        'tables': {
                            'query': OCPAWSCostLineItemProjectDailySummary,
                            'total': OCPAWSCostLineItemProjectDailySummary
                        },
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('pod_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'usage': Sum('usage_amount'),
                            'usage_units': Coalesce(Max('unit'), Value('GB-Mo'))
                        },
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('pod_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'usage': Sum('usage_amount'),
                            'usage_units': Coalesce(Max('unit'), Value('GB-Mo'))
                        },
                        'count': None,
                        'delta_key': {'usage': Sum('usage_amount')},
                        'filter': [{
                            'field': 'product_family',
                            'operation': 'contains',
                            'parameter': 'Storage'
                        }, ],
                        'cost_units_key': 'currency_code',
                        'cost_units_fallback': 'USD',
                        'usage_units_key': 'unit',
                        'usage_units_fallback': 'GB-Mo',
                        'sum_columns': ['usage', 'cost', 'infrastructure_cost', 'derived_cost',
                                        'markup_cost'],
                        'default_ordering': {'usage': 'desc'},
                    },
                    'instance_type': {
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum(F('unblended_cost')),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'count': Count('resource_id', distinct=True),
                            'usage': Sum(F('usage_amount')),
                            'usage_units': Coalesce(Max('unit'), Value('GB-Mo'))
                        },
                        'aggregate_key': 'usage_amount',
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('unblended_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum(F('unblended_cost')),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'count': Count('resource_id', distinct=True),
                            'count_units': Value('instances', output_field=CharField()),
                            'usage': Sum(F('usage_amount')),
                            'usage_units': Coalesce(Max('unit'), Value('Hrs'))
                        },
                        'count': 'resource_id',
                        'delta_key': {'usage': Sum('usage_amount')},
                        'filter': [{
                            'field': 'instance_type',
                            'operation': 'isnull',
                            'parameter': False
                        }, ],
                        'group_by': ['instance_type'],
                        'cost_units_key': 'currency_code',
                        'cost_units_fallback': 'USD',
                        'usage_units_key': 'unit',
                        'usage_units_fallback': 'Hrs',
                        'count_units_fallback': 'instances',
                        'sum_columns': ['usage', 'cost', 'infrastructure_cost',
                                        'markup_cost', 'derived_cost', 'count'],
                        'default_ordering': {'usage': 'desc'},
                    },
                    'instance_type_by_project': {
                        'tables': {
                            'query': OCPAWSCostLineItemProjectDailySummary,
                            'total': OCPAWSCostLineItemProjectDailySummary
                        },
                        'aggregates': {
                            'cost': Sum(
                                Coalesce(F('pod_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'count': Count('resource_id', distinct=True),
                            'usage': Sum('usage_amount'),
                            'usage_units': Coalesce(Max('unit'), Value('GB-Mo'))
                        },
                        'aggregate_key': 'usage_amount',
                        'annotations': {
                            'cost': Sum(
                                Coalesce(F('pod_cost'), Value(0, output_field=DecimalField()))
                                + Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'infrastructure_cost': Sum('pod_cost'),
                            'derived_cost': Sum(Value(0, output_field=DecimalField())),
                            'markup_cost': Sum(
                                Coalesce(F('project_markup_cost'), Value(0, output_field=DecimalField()))
                            ),
                            'cost_units': Coalesce(Max('currency_code'), Value('USD')),
                            'count': Count('resource_id', distinct=True),
                            'count_units': Value('instances', output_field=CharField()),
                            'usage': Sum('usage_amount'),
                            'usage_units': Coalesce(Max('unit'), Value('Hrs'))
                        },
                        'count': 'resource_id',
                        'delta_key': {'usage': Sum('usage_amount')},
                        'filter': [{
                            'field': 'instance_type',
                            'operation': 'isnull',
                            'parameter': False
                        }, ],
                        'group_by': ['instance_type'],
                        'cost_units_key': 'currency_code',
                        'cost_units_fallback': 'USD',
                        'usage_units_key': 'unit',
                        'usage_units_fallback': 'Hrs',
                        'count_units_fallback': 'instances',
                        'sum_columns': ['usage', 'cost', 'infrastructure_cost',
                                        'markup_cost', 'derived_cost', 'count'],
                        'default_ordering': {'usage': 'desc'},
                    },
                    'tags': {
                        'default_ordering': {'cost': 'desc'},
                    },
                },
                'start_date': 'usage_start',
                'tables': {
                    'query': OCPAWSCostLineItemDailySummary,
                    'total': OCPAWSCostLineItemDailySummary
                },
            }
        ]
        super().__init__(provider, report_type)

0 View Complete Implementation : helpers.py
Copyright GNU Affero General Public License v3.0
Author : project-koku
    def _populate_daily_summary_table(self):
        """Populate the daily summary table."""
        OCPUsageLineItemDailySummary.objects.all().delete()
        included_fields = [
            'usage_start',
            'usage_end',
            'namespace',
            'pod',
            'node',
            'cluster_id',
            'cluster_alias',
            'node_capacity_cpu_cores',
            'pod_labels',
        ]
        annotations = {
            'pod_usage_cpu_core_hours': F('pod_usage_cpu_core_seconds') / 3600,
            'pod_request_cpu_core_hours': Sum(
                ExpressionWrapper(
                    F('pod_request_cpu_core_seconds') / 3600,
                    output_field=DecimalField()
                )
            ),
            'pod_limit_cpu_core_hours': Sum(
                ExpressionWrapper(
                    F('pod_limit_cpu_core_seconds') / 3600,
                    output_field=DecimalField()
                )
            ),
            'pod_usage_memory_gigabyte_hours': Sum(
                ExpressionWrapper(
                    F('pod_usage_memory_byte_seconds') / 3600,
                    output_field=DecimalField()
                )
            ) * math.pow(2, -30),
            'pod_request_memory_gigabyte_hours': Sum(
                ExpressionWrapper(
                    F('pod_request_memory_byte_seconds') / 3600,
                    output_field=DecimalField()
                )
            ) * math.pow(2, -30),
            'pod_limit_memory_gigabyte_hours': ExpressionWrapper(
                F('pod_limit_memory_byte_seconds') / 3600,
                output_field=DecimalField()
            ) * math.pow(2, -30),
            'node_capacity_cpu_core_hours': F('node_capacity_cpu_core_seconds') / 3600,
            'node_capacity_memory_gigabytes': F('node_capacity_memory_bytes') * math.pow(2, -30),
            'node_capacity_memory_gigabyte_hours': ExpressionWrapper(
                F('node_capacity_memory_byte_seconds') / 3600,
                output_field=DecimalField()
            ) * math.pow(2, -30),
            'cluster_capacity_cpu_core_hours': F('cluster_capacity_cpu_core_seconds') / 3600,
            'cluster_capacity_memory_gigabyte_hours': ExpressionWrapper(
                F('cluster_capacity_memory_byte_seconds') / 3600,
                output_field=DecimalField()
            ) * math.pow(2, -30),
            'total_capacity_cpu_core_hours': F('cluster_capacity_cpu_core_seconds') / 3600 * 2,
            'total_capacity_memory_gigabyte_hours': ExpressionWrapper(
                F('cluster_capacity_memory_byte_seconds') / 3600 * 2,
                output_field=DecimalField()
            ) * math.pow(2, -30),
            'data_source': Value('Pod', output_field=CharField())
        }

        entries = OCPUsageLineItemDaily.objects.values(*included_fields).annotate(**annotations)

        for entry in entries:
            summary = OCPUsageLineItemDailySummary(**entry)
            summary.save()

0 View Complete Implementation : helpers.py
Copyright GNU Affero General Public License v3.0
Author : project-koku
    def _populate_storage_daily_summary_table(self):
        """Populate the daily summary table."""
        included_fields = [
            'usage_start',
            'usage_end',
            'namespace',
            'pod',
            'node',
            'persistentvolumeclaim',
            'persistentvolume',
            'storageclast',
            'cluster_id',
            'cluster_alias'
        ]
        annotations = {
            'volume_labels': Coalesce(F('persistentvolume_labels'), F('persistentvolumeclaim_labels')),
            'persistentvolumeclaim_capacity_gigabyte': ExpressionWrapper(
                F('persistentvolumeclaim_capacity_bytes') * math.pow(2, -30),
                output_field=DecimalField()
            ),
            'persistentvolumeclaim_capacity_gigabyte_months': ExpressionWrapper(
                F('persistentvolumeclaim_capacity_byte_seconds') / 86400 * 30 * math.pow(2, -30),
                output_field=DecimalField()
            ),
            'volume_request_storage_gigabyte_months': ExpressionWrapper(
                F('volume_request_storage_byte_seconds') / 86400 * 30 * math.pow(2, -30),
                output_field=DecimalField()
            ),
            'persistentvolumeclaim_usage_gigabyte_months': ExpressionWrapper(
                F('persistentvolumeclaim_usage_byte_seconds') / 86400 * 30 * math.pow(2, -30),
                output_field=DecimalField()
            ),
            'data_source': Value('Storage', output_field=CharField())
        }

        entries = OCPStorageLineItemDaily.objects.values(*included_fields).annotate(**annotations)

        for entry in entries:
            summary = OCPUsageLineItemDailySummary(**entry)
            summary.save()

0 View Complete Implementation : ocp_report_db_accessor.py
Copyright GNU Affero General Public License v3.0
Author : project-koku
    def populate_markup_cost(self, infra_provider_markup, ocp_markup, cluster_id):
        """Set markup cost for OCP including infrastructure cost markup."""
        with schema_context(self.schema):
            OCPUsageLineItemDailySummary.objects.filter(cluster_id=cluster_id).update(
                markup_cost=(
                    (
                        Coalesce(
                            F('pod_charge_cpu_core_hours'),
                            Value(0, output_field=DecimalField())
                        )
                        + Coalesce(
                            F('pod_charge_memory_gigabyte_hours'),
                            Value(0, output_field=DecimalField())
                        )
                        + Coalesce(
                            F('persistentvolumeclaim_charge_gb_month'),
                            Value(0, output_field=DecimalField())
                        )

                    ) * ocp_markup
                    + (
                        Coalesce(
                            F('infra_cost'),
                            Value(0, output_field=DecimalField())
                        )
                    ) * infra_provider_markup
                )
            )
            OCPUsageLineItemDailySummary.objects.filter(cluster_id=cluster_id).update(
                project_markup_cost=(
                    (
                        Coalesce(
                            F('pod_charge_cpu_core_hours'),
                            Value(0, output_field=DecimalField())
                        )
                        + Coalesce(
                            F('pod_charge_memory_gigabyte_hours'),
                            Value(0, output_field=DecimalField())
                        )
                        + Coalesce(
                            F('persistentvolumeclaim_charge_gb_month'),
                            Value(0, output_field=DecimalField())
                        )

                    ) * ocp_markup
                    + (
                        Coalesce(F('project_infra_cost'), Value(0, output_field=DecimalField()))
                    ) * infra_provider_markup
                )
            )

0 View Complete Implementation : functions.py
Copyright MIT License
Author : rizwansoaib
    def __init__(self, expression, *, output_field=None, **extra):
        super().__init__(expression, output_field=output_field or DecimalField(), **extra)

0 View Complete Implementation : converter.py
Copyright MIT License
Author : tOgg1
@convert_django_field_to_input.register(models.DecimalField)
@convert_django_field_to_input.register(models.FloatField)
def convert_field_to_float(field, registry=None, required=None, field_many_to_many_extras=None, field_foreign_key_extras=None):
    return Float(description=field.help_text, required=is_required(field, required))

0 View Complete Implementation : converters.py
Copyright MIT License
Author : tr11
@convert_django_field.register(models.DecimalField)
@convert_django_field.register(models.DurationField)
def convert_force_string(field, _registry=None):
    return String(description=field.help_text, required=not field.null)

0 View Complete Implementation : math.py
Copyright GNU Affero General Public License v3.0
Author : Unicaronas
    def _resolve_output_field(self):
        has_decimals = any(isinstance(s.output_field, DecimalField) for s in self.get_source_expressions())
        return DecimalField() if has_decimals else FloatField()