Skip to content

Commit 0e97ac4

Browse files
esebestosamdoranmyersCody
authored
[COST-4713] Handle Azure billingcurrency column (#4946)
Simplify leap year test failure fix. Co-authored-by: Sam Doran <[email protected]> Co-authored-by: Cody Myers <[email protected]>
1 parent 02078f8 commit 0e97ac4

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

koku/koku/test_pg_partition.py

+5-15
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
# Copyright 2021 Red Hat Inc.
33
# SPDX-License-Identifier: Apache-2.0
44
#
5-
import calendar
65
import datetime
76
import json
87
import uuid
9-
from datetime import timedelta
108

119
from django.db import connection as conn
1210
from django_tenants.utils import schema_context
@@ -25,12 +23,12 @@ def _execute(sql, params=None):
2523

2624

2725
def handle_leap_year_overflow(date, years):
26+
future_year = date.year + years
2827
try:
29-
new_date = date.replace(year=(date.year + years))
28+
return date.replace(year=future_year)
3029
except ValueError:
31-
# If an error occurs, set the day to the last day of the month
32-
new_date = (date + timedelta(days=31)).replace(year=(date.year + years), day=1)
33-
return new_date
30+
# If an error occurs, it's probably not a leap year in the future so roll back one day
31+
return (date - datetime.timedelta(days=1)).replace(year=future_year)
3432

3533

3634
def _get_table_partition_info(schema, table):
@@ -964,7 +962,6 @@ def test_repartition_all_tables(self):
964962
with schema_context(self.schema_name):
965963
aws_lids = AWSCostEntryLineItemDailySummary.objects.order_by("-usage_start")[0]
966964
aws_lids.usage_start = handle_leap_year_overflow(aws_lids.usage_start, 11)
967-
aws_lids.usage_start = aws_lids.usage_start.replace(year=(aws_lids.usage_start.year + 11))
968965
aws_lids.save()
969966
ocp_lids = OCPUsageLineItemDailySummary.objects.order_by("-usage_start")[0]
970967
ocp_lids.usage_start = handle_leap_year_overflow(aws_lids.usage_start, 11)
@@ -1031,14 +1028,7 @@ def test_repartition_all_tables(self):
10311028
if new_ocp_lids.usage_start.day < 28
10321029
else new_ocp_lids.usage_start.day - 1
10331030
)
1034-
if (
1035-
ocp_lids.usage_start.month == 2
1036-
and ocp_lids.usage_start.day == 28
1037-
and calendar.isleap(ocp_lids.usage_start.year)
1038-
):
1039-
new_ocp_lids.usage_start = ocp_lids.usage_start.replace(day=29)
1040-
else:
1041-
new_ocp_lids.usage_start = ocp_lids.usage_start.replace(day=new_day)
1031+
new_ocp_lids.usage_start = ocp_lids.usage_start.replace(day=new_day)
10421032
new_ocp_lids.save()
10431033

10441034
with conn.cursor() as cur:

koku/masu/database/trino_sql/reporting_ocpazurecostlineitem_daily_summary.sql

+4-4
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ SELECT cast(uuid() as varchar) as uuid,
164164
ELSE unitofmeasure
165165
END) as unit_of_measure,
166166
sum(coalesce(nullif(azure.quantity, 0), azure.usagequantity)) as usage_quantity,
167-
coalesce(nullif(azure.billingcurrencycode, ''), azure.currency) as currency,
167+
coalesce(nullif(azure.billingcurrencycode, ''), nullif(azure.currency, ''), azure.billingcurrency) as currency,
168168
sum(coalesce(nullif(azure.costinbillingcurrency, 0), azure.pretaxcost)) as pretax_cost,
169169
azure.tags,
170170
max(azure.resource_id_matched) as resource_id_matched,
@@ -183,7 +183,7 @@ GROUP BY coalesce(azure.date, azure.usagedatetime),
183183
coalesce(nullif(servicename, ''), metercategory),
184184
coalesce(nullif(subscriptionid, ''), subscriptionguid),
185185
azure.resourcelocation,
186-
coalesce(nullif(azure.billingcurrencycode, ''), azure.currency),
186+
coalesce(nullif(azure.billingcurrencycode, ''), nullif(azure.currency, ''), azure.billingcurrency),
187187
azure.tags
188188
;
189189

@@ -233,7 +233,7 @@ SELECT cast(uuid() as varchar) as uuid,
233233
ELSE unitofmeasure
234234
END) as unit_of_measure,
235235
sum(coalesce(nullif(azure.quantity, 0), azure.usagequantity)) as usage_quantity,
236-
coalesce(nullif(azure.billingcurrencycode, ''), azure.currency) as currency,
236+
coalesce(nullif(azure.billingcurrencycode, ''), nullif(azure.currency, ''), azure.billingcurrency) as currency,
237237
sum(coalesce(nullif(azure.costinbillingcurrency, 0), azure.pretaxcost)) as pretax_cost,
238238
json_format(
239239
cast(
@@ -260,7 +260,7 @@ GROUP BY coalesce(azure.date, azure.usagedatetime),
260260
coalesce(nullif(servicename, ''), metercategory),
261261
coalesce(nullif(subscriptionid, ''), subscriptionguid),
262262
azure.resourcelocation,
263-
coalesce(nullif(azure.billingcurrencycode, ''), azure.currency),
263+
coalesce(nullif(azure.billingcurrencycode, ''), nullif(azure.currency, ''), azure.billingcurrency),
264264
12, -- tags
265265
azure.matched_tag
266266
;

0 commit comments

Comments
 (0)