Skip to content

Commit 08106fc

Browse files
authored
[COST-4751] GCP Scrub disabled tags in trino. (#4966)
1 parent fd8c662 commit 08106fc

6 files changed

+17
-87
lines changed

koku/masu/database/gcp_report_db_accessor.py

-24
Original file line numberDiff line numberDiff line change
@@ -227,30 +227,6 @@ def populate_enabled_tag_keys(self, start_date, end_date, bill_ids):
227227
}
228228
self._prepare_and_execute_raw_sql_query(table_name, sql, sql_params)
229229

230-
def update_line_item_daily_summary_with_enabled_tags(self, start_date, end_date, bill_ids):
231-
"""Populate the enabled tag key table.
232-
233-
Args:
234-
start_date (datetime.date) The date to start populating the table.
235-
end_date (datetime.date) The date to end on.
236-
bill_ids (list) A list of bill IDs.
237-
238-
Returns
239-
(None)
240-
"""
241-
table_name = self._table_map["line_item_daily_summary"]
242-
sql = pkgutil.get_data(
243-
"masu.database", "sql/reporting_gcpcostentryline_item_daily_summary_update_enabled_tags.sql"
244-
)
245-
sql = sql.decode("utf-8")
246-
sql_params = {
247-
"start_date": start_date,
248-
"end_date": end_date,
249-
"bill_ids": bill_ids,
250-
"schema": self.schema,
251-
}
252-
self._prepare_and_execute_raw_sql_query(table_name, sql, sql_params)
253-
254230
def populate_gcp_topology_information_tables(self, provider, start_date, end_date, invoice_month_date):
255231
"""Populate the GCP topology table."""
256232
ctx = {

koku/masu/database/sql/reporting_gcpcostentryline_item_daily_summary_update_enabled_tags.sql

-17
This file was deleted.

koku/masu/database/trino_sql/reporting_gcpcostentrylineitem_daily_summary.sql

+15-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ INSERT INTO postgres.{{schema | sqlsafe}}.reporting_gcpcostentrylineitem_daily_s
2323
invoice_month,
2424
credit_amount
2525
)
26+
with cte_pg_enabled_keys as (
27+
select array_agg(key order by key) as keys
28+
from postgres.{{schema | sqlsafe}}.reporting_enabledtagkeys
29+
where enabled = true
30+
and provider_type = 'GCP'
31+
)
2632
SELECT uuid() as uuid,
2733
INTEGER '{{bill_id | sqlsafe}}' as cost_entry_bill_id,
2834
billing_account_id as account_id,
@@ -38,7 +44,12 @@ SELECT uuid() as uuid,
3844
json_extract_scalar(json_parse(system_labels), '$["compute.googleapis.com/machine_spec"]') as instance_type,
3945
max(usage_pricing_unit) as unit,
4046
cast(sum(usage_amount_in_pricing_units) AS decimal(24,9)) as usage_amount,
41-
json_parse(labels) as tags,
47+
cast(
48+
map_filter(
49+
cast(json_parse(labels) as map(varchar, varchar)),
50+
(k,v) -> contains(pek.keys, k)
51+
) as json
52+
) as tags,
4253
max(currency) as currency,
4354
cost_type as line_item_type,
4455
cast(sum(cost) AS decimal(24,9)) as unblended_cost,
@@ -47,6 +58,8 @@ SELECT uuid() as uuid,
4758
invoice_month,
4859
sum(((cast(COALESCE(json_extract_scalar(json_parse(credits), '$["amount"]'), '0')AS decimal(24,9)))*1000000)/1000000) as credit_amount
4960
FROM hive.{{schema | sqlsafe}}.{{table | sqlsafe}}
61+
CROSS JOIN
62+
cte_pg_enabled_keys as pek
5063
WHERE source = '{{source_uuid | sqlsafe}}'
5164
AND year = '{{year | sqlsafe}}'
5265
AND month = '{{month | sqlsafe}}'
@@ -61,6 +74,6 @@ GROUP BY billing_account_id,
6174
date(usage_end_time),
6275
location_region,
6376
json_extract_scalar(json_parse(system_labels), '$["compute.googleapis.com/machine_spec"]'),
64-
labels,
77+
16, -- matches column num of tag's map_filter
6578
cost_type,
6679
invoice_month

koku/masu/processor/gcp/gcp_report_parquet_summary_updater.py

-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ def update_summary_tables(self, start_date, end_date, **kwargs):
116116
accessor.populate_ui_summary_tables(start, end, self._provider.uuid)
117117
accessor.populate_tags_summary_table(bill_ids, start_date, end_date)
118118
accessor.populate_gcp_topology_information_tables(self._provider, start_date, end_date, invoice_month_date)
119-
accessor.update_line_item_daily_summary_with_enabled_tags(start_date, end_date, bill_ids)
120119
accessor.update_line_item_daily_summary_with_tag_mapping(start_date, end_date, bill_ids)
121120
for bill in bills:
122121
if bill.summary_data_creation_datetime is None:

koku/masu/test/database/test_gcp_report_db_accessor.py

-29
Original file line numberDiff line numberDiff line change
@@ -185,35 +185,6 @@ def test_populate_enabled_tag_keys(self):
185185
self.accessor.populate_enabled_tag_keys(start_date, end_date, bill_ids)
186186
self.assertNotEqual(EnabledTagKeys.objects.filter(provider_type=Provider.PROVIDER_GCP).count(), 0)
187187

188-
def test_update_line_item_daily_summary_with_enabled_tags(self):
189-
"""Test that we filter the daily summary table's tags with only enabled tags."""
190-
start_date = self.dh.this_month_start.date()
191-
end_date = self.dh.this_month_end.date()
192-
193-
bills = self.accessor.bills_for_provider_uuid(self.gcp_provider_uuid, start_date)
194-
with schema_context(self.schema):
195-
GCPTagsSummary.objects.all().delete()
196-
key_to_keep = EnabledTagKeys.objects.filter(provider_type=Provider.PROVIDER_GCP).filter(key="app").first()
197-
EnabledTagKeys.objects.filter(provider_type=Provider.PROVIDER_GCP).update(enabled=False)
198-
EnabledTagKeys.objects.filter(provider_type=Provider.PROVIDER_GCP).filter(key="app").update(enabled=True)
199-
bill_ids = [bill.id for bill in bills]
200-
self.accessor.update_line_item_daily_summary_with_enabled_tags(start_date, end_date, bill_ids)
201-
tags = (
202-
GCPCostEntryLineItemDailySummary.objects.filter(
203-
usage_start__gte=start_date, cost_entry_bill_id__in=bill_ids
204-
)
205-
.values_list("tags")
206-
.distinct()
207-
)
208-
209-
for tag in tags:
210-
tag_dict = tag[0] if tag[0] is not None else {} # account for null tags value
211-
tag_keys = list(tag_dict.keys())
212-
if tag_keys:
213-
self.assertEqual([key_to_keep.key], tag_keys)
214-
else:
215-
self.assertEqual([], tag_keys)
216-
217188
def test_table_properties(self):
218189
self.assertEqual(self.accessor.line_item_daily_summary_table, GCPCostEntryLineItemDailySummary)
219190

koku/masu/test/processor/gcp/test_gcp_report_parquet_summary_updater.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,11 @@ def test_get_sql_inputs(self):
6666
@patch(
6767
"masu.processor.gcp.gcp_report_parquet_summary_updater.GCPReportDBAccessor.delete_line_item_daily_summary_entries_for_date_range_raw" # noqa: E501
6868
)
69-
@patch(
70-
"masu.processor.gcp.gcp_report_parquet_summary_updater.GCPReportDBAccessor.update_line_item_daily_summary_with_enabled_tags" # noqa: E501
71-
)
7269
@patch("masu.processor.gcp.gcp_report_parquet_summary_updater.GCPReportDBAccessor.populate_tags_summary_table")
7370
@patch(
7471
"masu.processor.gcp.gcp_report_parquet_summary_updater.GCPReportDBAccessor.populate_line_item_daily_summary_table_trino" # noqa: E501
7572
)
76-
def test_update_daily_summary_tables(
77-
self, mock_trino, mock_tag_update, mock_summary_update, mock_delete, mock_topo
78-
):
73+
def test_update_daily_summary_tables(self, mock_trino, mock_tag_update, mock_delete, mock_topo):
7974
"""Test that we run Trino summary."""
8075
start_str = self.dh.this_month_start.isoformat()
8176
end_str = self.dh.this_month_end.isoformat()
@@ -103,7 +98,6 @@ def test_update_daily_summary_tables(
10398
expected_start, expected_end, self.gcp_provider.uuid, current_bill_id, markup_value, start
10499
)
105100
mock_tag_update.assert_called_with(bill_ids, start, end)
106-
mock_summary_update.assert_called_with(start, end, bill_ids)
107101

108102
self.assertEqual(start_return, start)
109103
self.assertEqual(end_return, end)
@@ -114,16 +108,11 @@ def test_update_daily_summary_tables(
114108
@patch(
115109
"masu.processor.gcp.gcp_report_parquet_summary_updater.GCPReportDBAccessor.delete_line_item_daily_summary_entries_for_date_range_raw" # noqa: E501
116110
)
117-
@patch(
118-
"masu.processor.gcp.gcp_report_parquet_summary_updater.GCPReportDBAccessor.update_line_item_daily_summary_with_enabled_tags" # noqa: E501
119-
)
120111
@patch("masu.processor.gcp.gcp_report_parquet_summary_updater.GCPReportDBAccessor.populate_tags_summary_table")
121112
@patch(
122113
"masu.processor.gcp.gcp_report_parquet_summary_updater.GCPReportDBAccessor.populate_line_item_daily_summary_table_trino" # noqa: E501
123114
)
124-
def test_update_daily_summary_tables_no_invoice_month(
125-
self, mock_trino, mock_tag_update, mock_summary_update, mock_delete, mock_topo
126-
):
115+
def test_update_daily_summary_tables_no_invoice_month(self, mock_trino, mock_tag_update, mock_delete, mock_topo):
127116
"""Test that we run Trino summary."""
128117
start_str = self.dh.this_month_start.isoformat()
129118
end_str = self.dh.this_month_end.isoformat()
@@ -132,7 +121,6 @@ def test_update_daily_summary_tables_no_invoice_month(
132121
mock_delete.assert_not_called()
133122
mock_trino.assert_not_called()
134123
mock_tag_update.assert_not_called()
135-
mock_summary_update.assert_not_called()
136124

137125
self.assertEqual(start_return, start)
138126
self.assertEqual(end_return, end)

0 commit comments

Comments
 (0)