Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include provider in OCP/cloud tag sql params #5261

Merged
merged 11 commits into from
Aug 22, 2024
3 changes: 2 additions & 1 deletion koku/api/report/test/util/model_bakery_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,8 @@ def load_openshift_on_cloud_data(self, provider_type, cluster_id, bills, report_
with dbaccessor(self.schema) as accessor:
# update tags
cls_method = getattr(accessor, tags_update_method)
cls_method([bill.id for bill in bills], self.first_start_date, self.last_end_date)
for report_period in report_periods:
cls_method([bill.id for bill in bills], self.first_start_date, self.last_end_date, report_period.id)

# update ui tables
sql_params = {
Expand Down
10 changes: 8 additions & 2 deletions koku/masu/database/aws_report_db_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,15 @@ def back_populate_ocp_infrastructure_costs(self, start_date, end_date, report_pe
}
self._prepare_and_execute_raw_sql_query(table_name, sql, sql_params)

def populate_ocp_on_aws_tag_information(self, bill_ids, start_date, end_date):
def populate_ocp_on_aws_tag_information(self, bill_ids, start_date, end_date, current_ocp_report_period_id):
lcouzens marked this conversation as resolved.
Show resolved Hide resolved
"""Populate the line item aggregated totals data table."""
sql_params = {"schema": self.schema, "bill_ids": bill_ids, "start_date": start_date, "end_date": end_date}
sql_params = {
"schema": self.schema,
"bill_ids": bill_ids,
"start_date": start_date,
"end_date": end_date,
"report_period_id": current_ocp_report_period_id,
lcouzens marked this conversation as resolved.
Show resolved Hide resolved
}
# Tag Summary
sql = pkgutil.get_data("masu.database", "sql/reporting_ocpawstags_summary.sql")
sql = sql.decode("utf-8")
Expand Down
10 changes: 8 additions & 2 deletions koku/masu/database/azure_report_db_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,15 @@ def get_bill_query_before_date(self, date):
"""Get the cost entry bill objects with billing period before provided date."""
return AzureCostEntryBill.objects.filter(billing_period_start__lte=date)

def populate_ocp_on_azure_tag_information(self, bill_ids, start_date, end_date):
def populate_ocp_on_azure_tag_information(self, bill_ids, start_date, end_date, report_period_id):
"""Populate the line item aggregated totals data table."""
sql_params = {"schema": self.schema, "bill_ids": bill_ids, "start_date": start_date, "end_date": end_date}
sql_params = {
"schema": self.schema,
"bill_ids": bill_ids,
"start_date": start_date,
"end_date": end_date,
"report_period_id": report_period_id,
}
# Tag summary
sql = pkgutil.get_data("masu.database", "sql/reporting_ocpazuretags_summary.sql")
sql = sql.decode("utf-8")
Expand Down
3 changes: 2 additions & 1 deletion koku/masu/database/gcp_report_db_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,14 @@ def get_openshift_on_cloud_matched_tags_trino(
)
return [json.loads(result[0]) for result in results]

def populate_ocp_on_gcp_tag_information(self, gcp_bill_ids, start_date, end_date):
def populate_ocp_on_gcp_tag_information(self, gcp_bill_ids, start_date, end_date, report_period_id):
"""Populate the line item aggregated totals data table."""
sql_params = {
"schema": self.schema,
"gcp_bill_ids": gcp_bill_ids,
"start_date": start_date,
"end_date": end_date,
"report_period_id": report_period_id,
}
# Tag Summary
sql = pkgutil.get_data("masu.database", "sql/gcp/openshift/reporting_ocpgcptags_summary.sql")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ WITH cte_tag_value AS (
jsonb_each_text(li.tags) labels
WHERE li.usage_start >= {{start_date}}
AND li.usage_start <= {{end_date}}
AND li.report_period_id = {{report_period_id}}
AND value IS NOT NULL
AND li.tags ?| (SELECT array_agg(DISTINCT key) FROM {{schema | sqlsafe}}.reporting_enabledtagkeys WHERE enabled=true AND provider_type = 'GCP')
{% if bill_ids %}
Expand Down
1 change: 1 addition & 0 deletions koku/masu/database/sql/reporting_ocpawstags_summary.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ WITH cte_tag_value AS (
jsonb_each_text(li.tags) labels
WHERE li.usage_start >= {{start_date}}
AND li.usage_start <= {{end_date}}
AND li.report_period_id = {{report_period_id}}
AND li.tags ?| (SELECT array_agg(DISTINCT key) FROM {{schema | sqlsafe}}.reporting_enabledtagkeys WHERE enabled=true AND provider_type='AWS')
{% if bill_ids %}
AND li.cost_entry_bill_id IN {{ bill_ids | inclause }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ WITH cte_tag_value AS (
jsonb_each_text(li.tags) labels
WHERE li.usage_start >= {{start_date}}
AND li.usage_start <= {{end_date}}
AND li.report_period_id = {{report_period_id}}
AND li.tags ?| (SELECT array_agg(DISTINCT key) FROM {{schema | sqlsafe}}.reporting_enabledtagkeys WHERE enabled=true AND provider_type='Azure')
{% if bill_ids %}
AND li.cost_entry_bill_id IN {{ bill_ids | inclause }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def update_aws_summary_tables(self, openshift_provider_uuid, aws_provider_uuid,
sql_params["start_date"] = start
sql_params["end_date"] = end
accessor.back_populate_ocp_infrastructure_costs(start, end, current_ocp_report_period_id)
accessor.populate_ocp_on_aws_tag_information(aws_bill_ids, start, end)
accessor.populate_ocp_on_aws_tag_information(aws_bill_ids, start, end, current_ocp_report_period_id)
accessor.populate_ocp_on_aws_ui_summary_tables_trino(
start, end, openshift_provider_uuid, aws_provider_uuid
)
Expand Down Expand Up @@ -434,7 +434,9 @@ def update_azure_summary_tables(self, openshift_provider_uuid, azure_provider_uu
sql_params["start_date"] = start
sql_params["end_date"] = end
accessor.back_populate_ocp_infrastructure_costs(start, end, current_ocp_report_period_id)
accessor.populate_ocp_on_azure_tag_information(azure_bill_ids, start, end)
accessor.populate_ocp_on_azure_tag_information(
azure_bill_ids, start, end, current_ocp_report_period_id
)
accessor.populate_ocp_on_azure_ui_summary_tables_trino(
start, end, openshift_provider_uuid, azure_provider_uuid
)
Expand Down Expand Up @@ -555,7 +557,7 @@ def update_gcp_summary_tables(self, openshift_provider_uuid, gcp_provider_uuid,
accessor.populate_ocp_on_gcp_ui_summary_tables_trino(
start, end, openshift_provider_uuid, gcp_provider_uuid
)
accessor.populate_ocp_on_gcp_tag_information(gcp_bill_ids, start, end)
accessor.populate_ocp_on_gcp_tag_information(gcp_bill_ids, start, end, current_ocp_report_period_id)

with OCPReportDBAccessor(self._schema) as ocp_accessor:
sql_params["source_type"] = "GCP"
Expand Down
8 changes: 4 additions & 4 deletions koku/masu/processor/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ def update_summary_tables( # noqa: C901
msg = f"Task {task_name} already running for {cache_args}. Requeuing."
if rate_limited:
msg = f"Schema {schema} is currently rate limited. Requeuing."
LOG.debug(log_json(tracing_id, msg=msg))
LOG.info(log_json(tracing_id, msg=msg))
update_summary_tables.s(
schema,
provider_type,
Expand Down Expand Up @@ -722,8 +722,6 @@ def update_openshift_on_cloud( # noqa: C901
"""Update OpenShift on Cloud for a specific OpenShift and cloud source."""
# Get latest manifest id for running OCP provider
ocp_manifest_id = get_latest_openshift_on_cloud_manifest(start_date, openshift_provider_uuid)
# Set OpenShift summary started time
set_summary_timestamp(ManifestState.START, ocp_manifest_id)
task_name = "masu.processor.tasks.update_openshift_on_cloud"
if is_ocp_on_cloud_summary_disabled(schema_name):
msg = f"OCP on Cloud summary disabled for {schema_name}."
Expand All @@ -747,7 +745,7 @@ def update_openshift_on_cloud( # noqa: C901
msg = f"Task {task_name} already running for {cache_args}. Requeuing."
if rate_limited:
msg = f"Schema {schema_name} is currently rate limited. Requeuing."
LOG.debug(log_json(tracing_id, msg=msg))
LOG.info(log_json(tracing_id, msg=msg))
update_openshift_on_cloud.s(
schema_name,
openshift_provider_uuid,
Expand All @@ -763,6 +761,8 @@ def update_openshift_on_cloud( # noqa: C901
return
worker_cache.lock_single_task(task_name, cache_args, timeout=timeout)

# Set OpenShift summary started time
set_summary_timestamp(ManifestState.START, ocp_manifest_id)
ctx = {
"schema": schema_name,
"ocp_provider_uuid": openshift_provider_uuid,
Expand Down
5 changes: 4 additions & 1 deletion koku/masu/test/database/test_aws_report_db_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ def test_populate_ocp_on_aws_tag_information(self, mock_unleash):
"""
mock_unleash.return_value = True
populated_keys = []
report_period_id = 1
with schema_context(self.schema):
enabled_tags = EnabledTagKeys.objects.filter(provider_type=Provider.PROVIDER_AWS, enabled=True)
for enabled_tag in enabled_tags:
Expand All @@ -446,7 +447,9 @@ def test_populate_ocp_on_aws_tag_information(self, mock_unleash):
parent_key, parent_obj, parent_count = populated_keys[0]
child_key, child_obj, child_count = populated_keys[1]
TagMapping.objects.create(parent=parent_obj, child=child_obj)
self.accessor.populate_ocp_on_aws_tag_information(bill_ids, self.dh.this_month_start, self.dh.today)
self.accessor.populate_ocp_on_aws_tag_information(
bill_ids, self.dh.this_month_start, self.dh.today, report_period_id
)
expected_parent_count = parent_count + child_count
actual_parent_count = OCPAWSCostLineItemProjectDailySummaryP.objects.filter(
tags__has_key=parent_key, usage_start__gte=self.dh.this_month_start, usage_start__lte=self.dh.today
Expand Down
5 changes: 4 additions & 1 deletion koku/masu/test/database/test_azure_report_db_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ def test_populate_ocp_on_aZURE_tag_information(self, mock_unleash):
"""
mock_unleash.return_value = True
populated_keys = []
report_period_id = 1
with schema_context(self.schema):
enabled_tags = EnabledTagKeys.objects.filter(provider_type=Provider.PROVIDER_AZURE, enabled=True)
for enabled_tag in enabled_tags:
Expand All @@ -369,7 +370,9 @@ def test_populate_ocp_on_aZURE_tag_information(self, mock_unleash):
parent_key, parent_obj, parent_count = populated_keys[0]
child_key, child_obj, child_count = populated_keys[1]
TagMapping.objects.create(parent=parent_obj, child=child_obj)
self.accessor.populate_ocp_on_azure_tag_information(bill_ids, self.dh.this_month_start, self.dh.today)
self.accessor.populate_ocp_on_azure_tag_information(
bill_ids, self.dh.this_month_start, self.dh.today, report_period_id
)
expected_parent_count = parent_count + child_count
actual_parent_count = OCPAzureCostLineItemProjectDailySummaryP.objects.filter(
tags__has_key=parent_key, usage_start__gte=self.dh.this_month_start, usage_start__lte=self.dh.today
Expand Down
8 changes: 6 additions & 2 deletions koku/masu/test/database/test_gcp_report_db_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,12 @@ def test_populate_ocp_gcp_ui_summary_tables(self, mock_sql):
@patch("masu.database.gcp_report_db_accessor.GCPReportDBAccessor._execute_raw_sql_query")
def test_populate_ocp_on_gcp_tag_information_assert_call(self, mock_trino):
"""Test that we construst our SQL and execute our query."""
report_period_id = 1
start_date = self.dh.this_month_start.date()
end_date = self.dh.this_month_end.date()

mock_gcp_bills = [Mock(), Mock()]
self.accessor.populate_ocp_on_gcp_tag_information(mock_gcp_bills, start_date, end_date)
self.accessor.populate_ocp_on_gcp_tag_information(mock_gcp_bills, start_date, end_date, report_period_id)
mock_trino.assert_called()

@patch("masu.database.gcp_report_db_accessor.GCPReportDBAccessor.schema_exists_trino")
Expand Down Expand Up @@ -493,6 +494,7 @@ def test_populate_ocp_on_gcp_tag_information(self, mock_unleash):
"""
mock_unleash.return_value = True
populated_keys = []
report_period_id = 1
with schema_context(self.schema):
enabled_tags = EnabledTagKeys.objects.filter(provider_type=Provider.PROVIDER_GCP, enabled=True)
for enabled_tag in enabled_tags:
Expand All @@ -514,7 +516,9 @@ def test_populate_ocp_on_gcp_tag_information(self, mock_unleash):
parent_key, parent_obj, parent_count = populated_keys[0]
child_key, child_obj, child_count = populated_keys[1]
TagMapping.objects.create(parent=parent_obj, child=child_obj)
self.accessor.populate_ocp_on_gcp_tag_information(bill_ids, self.dh.this_month_start, self.dh.today)
self.accessor.populate_ocp_on_gcp_tag_information(
bill_ids, self.dh.this_month_start, self.dh.today, report_period_id
)
expected_parent_count = parent_count + child_count
actual_parent_count = OCPGCPCostLineItemProjectDailySummaryP.objects.filter(
tags__has_key=parent_key, usage_start__gte=self.dh.this_month_start, usage_start__lte=self.dh.today
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def test_update_gcp_summary_tables(
mock_ui_summary.assert_called_with(
start_date, end_date, self.ocpgcp_provider_uuid, self.gcp_test_provider_uuid
)
mock_tag_summary.assert_called_with([1, 2], start_date, end_date)
mock_tag_summary.assert_called_with([1, 2], start_date, end_date, current_ocp_report_period_id)

@patch(
"masu.processor.ocp.ocp_cloud_parquet_summary_updater.OCPReportDBAccessor.populate_ocp_on_all_ui_summary_tables" # noqa: E501
Expand Down Expand Up @@ -379,7 +379,7 @@ def test_update_gcp_summary_tables_with_string_dates(
start_date, end_date, self.ocpgcp_provider_uuid, self.gcp_test_provider_uuid
)

mock_tag_summary.assert_called_with([1, 2], start_date, end_date)
mock_tag_summary.assert_called_with([1, 2], start_date, end_date, current_ocp_report_period_id)

@patch("masu.processor.ocp.ocp_cloud_parquet_summary_updater.OCPReportDBAccessor.get_cluster_for_provider")
@patch(
Expand Down
Loading