コード例 #1
0
    def test_execute_query_ocp_charge_with_delta(self):
        """Test that deltas work for charge."""
        url = reverse('reports-openshift-charges')
        client = APIClient()
        params = {
            'delta': 'cost',
            'filter[resolution]': 'daily',
            'filter[time_scope_value]': '-1',
            'filter[time_scope_units]': 'month'
        }
        url = url + '?' + urlencode(params, quote_via=quote_plus)
        response = client.get(url, **self.headers)
        self.assertEqual(response.status_code, 200)
        data = response.json()
        this_month_start = self.dh.this_month_start
        last_month_start = self.dh.last_month_start

        date_delta = relativedelta.relativedelta(months=1)

        def date_to_string(dt):
            return dt.strftime('%Y-%m-%d')

        def string_to_date(dt):
            return datetime.datetime.strptime(dt, '%Y-%m-%d').date()

        with tenant_context(self.tenant):
            current_total = OCPUsageLineItemDailySummary.objects\
                .filter(usage_start__gte=this_month_start)\
                .aggregate(
                    total=Sum(
                        F('pod_charge_cpu_core_hours') +  # noqa: W504
                        F('pod_charge_memory_gigabyte_hours')
                    )
                ).get('total')
            current_total = current_total if current_total is not None else 0

            current_totals = OCPUsageLineItemDailySummary.objects\
                .filter(usage_start__gte=this_month_start)\
                .annotate(**{'date': TruncDayString('usage_start')})\
                .values(*['date'])\
                .annotate(total=Sum(F('pod_charge_cpu_core_hours') + F('pod_charge_memory_gigabyte_hours')))

            prev_totals = OCPUsageLineItemDailySummary.objects\
                .filter(usage_start__gte=last_month_start)\
                .filter(usage_start__lt=this_month_start)\
                .annotate(**{'date': TruncDayString('usage_start')})\
                .values(*['date'])\
                .annotate(total=Sum(F('pod_charge_cpu_core_hours') + F('pod_charge_memory_gigabyte_hours')))

        current_totals = {total.get('date'): total.get('total')
                          for total in current_totals}
        prev_totals = {date_to_string(string_to_date(total.get('date')) + date_delta): total.get('total')
                       for total in prev_totals
                       if date_to_string(string_to_date(total.get('date')) + date_delta) in current_totals}

        prev_total = sum(prev_totals.values())
        prev_total = prev_total if prev_total is not None else 0

        expected_delta = current_total - prev_total
        delta = data.get('meta', {}).get('delta', {}).get('value')
        self.assertEqual(round(delta, 3), round(float(expected_delta), 3))
        for item in data.get('data'):
            date = item.get('date')
            expected_delta = current_totals.get(date, 0) - prev_totals.get(date, 0)
            values = item.get('values', [])
            delta_value = 0
            if values:
                delta_value = values[0].get('delta_value')
            self.assertEqual(round(delta_value, 3), round(float(expected_delta), 3))
コード例 #2
0
ファイル: tests_views.py プロジェクト: lcouzens/koku
    def test_execute_query_ocp_aws_storage_with_delta(self):
        """Test that deltas work for OpenShift on AWS storage."""
        url = reverse("reports-openshift-aws-storage")
        client = APIClient()
        params = {
            "delta": "usage",
            "filter[resolution]": "daily",
            "filter[time_scope_value]": "-1",
            "filter[time_scope_units]": "month",
        }
        url = url + "?" + urlencode(params, quote_via=quote_plus)
        response = client.get(url, **self.headers)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.data
        this_month_start = self.dh.this_month_start
        last_month_start = self.dh.last_month_start

        date_delta = relativedelta.relativedelta(months=1)

        def date_to_string(dt):
            return dt.strftime("%Y-%m-%d")

        def string_to_date(dt):
            return datetime.datetime.strptime(dt, "%Y-%m-%d").date()

        with tenant_context(self.tenant):
            current_total = (OCPAWSCostLineItemDailySummary.objects.filter(
                usage_start__gte=this_month_start).filter(
                    product_family__contains="Storage").aggregate(
                        usage=Sum(F("usage_amount"))).get("usage"))
            current_total = current_total if current_total is not None else 0

            current_totals = (OCPAWSCostLineItemDailySummary.objects.filter(
                usage_start__gte=this_month_start).filter(
                    product_family__contains="Storage").annotate(
                        **{
                            "date": TruncDayString("usage_start")
                        }).values(*["date"]).annotate(
                            usage=Sum(F("usage_amount"))))

            prev_totals = (OCPAWSCostLineItemDailySummary.objects.filter(
                usage_start__gte=last_month_start).filter(
                    usage_start__lt=this_month_start).filter(
                        product_family__contains="Storage").annotate(
                            **{
                                "date": TruncDayString("usage_start")
                            }).values(*["date"]).annotate(
                                usage=Sum(F("usage_amount"))))

            current_totals = {
                total.get("date"): total.get("usage")
                for total in current_totals
            }
            prev_total_dates = [
                total.get("date") for total in prev_totals if date_to_string(
                    string_to_date(total.get("date")) +
                    date_delta) in current_totals
            ]
            prev_totals = {
                date_to_string(string_to_date(total.get("date")) + date_delta):
                total.get("usage")
                for total in prev_totals if date_to_string(
                    string_to_date(total.get("date")) +
                    date_delta) in current_totals
            }

            prev_total = (OCPAWSCostLineItemDailySummary.objects.filter(
                usage_start__in=prev_total_dates).filter(
                    product_family__contains="Storage").aggregate(
                        usage=Sum(F("usage_amount"))).get("usage"))
            prev_total = prev_total if prev_total is not None else 0

        expected_delta = current_total - prev_total
        delta = data.get("meta", {}).get("delta", {}).get("value")
        self.assertEqual(delta, expected_delta)
        for item in data.get("data"):
            date = item.get("date")
            expected_delta = current_totals.get(date, 0) - prev_totals.get(
                date, 0)
            values = item.get("values", [])
            delta_value = 0
            if values:
                delta_value = values[0].get("delta_value")
            self.assertAlmostEqual(delta_value, expected_delta)
コード例 #3
0
    def test_execute_query_ocp_aws_storage_with_delta(self):
        """Test that deltas work for OpenShift on AWS storage."""
        url = reverse('reports-openshift-aws-storage')
        client = APIClient()
        params = {
            'delta': 'usage',
            'filter[resolution]': 'daily',
            'filter[time_scope_value]': '-1',
            'filter[time_scope_units]': 'month'
        }
        url = url + '?' + urlencode(params, quote_via=quote_plus)
        response = client.get(url, **self.headers)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        data = response.data
        this_month_start = self.dh.this_month_start
        last_month_start = self.dh.last_month_start

        date_delta = relativedelta.relativedelta(months=1)

        def date_to_string(dt):
            return dt.strftime('%Y-%m-%d')

        def string_to_date(dt):
            return datetime.datetime.strptime(dt, '%Y-%m-%d').date()

        with tenant_context(self.tenant):
            current_total = OCPAWSCostLineItemDailySummary.objects\
                .filter(usage_start__gte=this_month_start)\
                .filter(product_family__contains='Storage')\
                .aggregate(usage=Sum(F('usage_amount')))\
                .get('usage')
            current_total = current_total if current_total is not None else 0

            current_totals = OCPAWSCostLineItemDailySummary.objects\
                .filter(usage_start__gte=this_month_start)\
                .filter(product_family__contains='Storage')\
                .annotate(**{'date': TruncDayString('usage_start')})\
                .values(*['date'])\
                .annotate(usage=Sum(F('usage_amount')))

            prev_totals = OCPAWSCostLineItemDailySummary.objects\
                .filter(usage_start__gte=last_month_start)\
                .filter(usage_start__lt=this_month_start)\
                .filter(product_family__contains='Storage')\
                .annotate(**{'date': TruncDayString('usage_start')})\
                .values(*['date'])\
                .annotate(usage=Sum(F('usage_amount')))

            current_totals = {
                total.get('date'): total.get('usage')
                for total in current_totals
            }
            prev_total_dates = [
                total.get('date') for total in prev_totals if date_to_string(
                    string_to_date(total.get('date')) +
                    date_delta) in current_totals
            ]
            prev_totals = {
                date_to_string(string_to_date(total.get('date')) + date_delta):
                total.get('usage')
                for total in prev_totals if date_to_string(
                    string_to_date(total.get('date')) +
                    date_delta) in current_totals
            }

            prev_total = OCPAWSCostLineItemDailySummary.objects\
                .filter(usage_start__in=prev_total_dates)\
                .filter(product_family__contains='Storage')\
                .aggregate(usage=Sum(F('usage_amount')))\
                .get('usage')
            prev_total = prev_total if prev_total is not None else 0

        expected_delta = current_total - prev_total
        delta = data.get('meta', {}).get('delta', {}).get('value')
        self.assertEqual(delta, expected_delta)
        for item in data.get('data'):
            date = item.get('date')
            expected_delta = current_totals.get(date, 0) - prev_totals.get(
                date, 0)
            values = item.get('values', [])
            delta_value = 0
            if values:
                delta_value = values[0].get('delta_value')
            self.assertAlmostEqual(delta_value, expected_delta)