Skip to content

Commit a938e36

Browse files
myersCodylcouzens
andauthored
[COST-7202] Don't summarize vm tables 4 previous month finalization (#5888)
Co-authored-by: lcouzens <lcouzens@redhat.com>
1 parent 5a8227c commit a938e36

File tree

11 files changed

+66
-64
lines changed

11 files changed

+66
-64
lines changed

koku/api/report/test/util/model_bakery_loader.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from masu.database.ocp_report_db_accessor import OCPReportDBAccessor
3535
from masu.processor.tasks import update_cost_model_costs
3636
from masu.util.aws.insert_aws_org_tree import InsertAwsOrgTree
37+
from masu.util.common import SummaryRangeConfig
3738
from reporting.models import AWSAccountAlias
3839
from reporting.models import AWSOrganizationalUnit
3940

@@ -466,7 +467,9 @@ def load_openshift_data(self, cluster_id, on_cloud=False):
466467
report_period_ids, self.first_start_date, self.last_end_date, provider.uuid
467468
)
468469
with OCPReportDBAccessor(self.schema) as accessor:
469-
accessor.populate_ui_summary_tables(self.first_start_date, self.last_end_date, provider.uuid)
470+
accessor.populate_ui_summary_tables(
471+
SummaryRangeConfig(start_date=self.first_start_date, end_date=self.last_end_date), provider.uuid
472+
)
470473

471474
populate_ocp_topology(self.schema, provider, cluster_id)
472475

koku/masu/database/ocp_report_db_accessor.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ def get_report_periods_before_date(self, date):
9595
"""Get the report periods with report period before provided date."""
9696
return OCPUsageReportPeriod.objects.filter(report_period_start__lte=date)
9797

98-
def populate_ui_summary_tables(self, start_date, end_date, source_uuid, tables=UI_SUMMARY_TABLES):
98+
def populate_ui_summary_tables(self, summary_range: SummaryRangeConfig, source_uuid, tables=UI_SUMMARY_TABLES):
9999
"""Populate our UI summary tables (formerly materialized views)."""
100100
sql_params = {
101-
"start_date": start_date,
102-
"end_date": end_date,
101+
"start_date": summary_range.start_date,
102+
"end_date": summary_range.end_date,
103103
"schema": self.schema,
104104
"source_uuid": source_uuid,
105105
}
@@ -114,6 +114,9 @@ def populate_ui_summary_tables(self, start_date, end_date, source_uuid, tables=U
114114
sql_params["month"] = start_date.strftime("%m")
115115

116116
self._populate_gpu_ui_summary_table_with_usage_only(sql_params)
117+
if summary_range.summarize_previous_month and not summary_range.is_current_month:
118+
# Don't resummarize virtualization UI table if we are summarizing previous month
119+
return
117120
self._populate_virtualization_ui_summary_table(sql_params)
118121

119122
def _populate_gpu_ui_summary_table_with_usage_only(self, params):
@@ -529,9 +532,6 @@ def populate_distributed_cost_sql(
529532
}
530533
# Handle distributions that require full month data
531534
if config.requires_full_month:
532-
# Skip full-month distributions on subsequent days when iterating day-by-day
533-
if summary_range.skip_full_month:
534-
continue
535535
sql_params["start_date"] = summary_range.start_of_month
536536
sql_params["end_date"] = summary_range.end_of_month
537537
if summary_range.is_current_month:
@@ -540,6 +540,7 @@ def populate_distributed_cost_sql(
540540
if dh.now_utc.day in [1, 2, 3]:
541541
sql_params["start_date"] = summary_range.start_of_previous_month
542542
sql_params["end_date"] = summary_range.end_of_previous_month
543+
summary_range.summarize_previous_month = True
543544
else:
544545
msg = f"Skipping {cost_model_key} distribution requires full month"
545546
LOG.info(log_json(msg=msg, context={"schema": self.schema, "cost_model_key": cost_model_key}))

koku/masu/processor/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,6 @@ def is_feature_cost_4403_ec2_compute_cost_enabled(schema): # pragma: no cover
141141
return UNLEASH_CLIENT.is_enabled(unleash_flag, context, fallback_development_true)
142142

143143

144-
def is_customer_cost_model_large(schema): # pragma: no cover
145-
"""Flag the customer as having a large amount of data for cost model updates."""
146-
context = {"schema": schema}
147-
return UNLEASH_CLIENT.is_enabled("cost-management.backend.large-customer-cost-model", context)
148-
149-
150144
def is_tag_processing_disabled(schema): # pragma: no cover
151145
"""Flag the customer as tag processing disabled."""
152146
context = {"schema": schema}

koku/masu/processor/cost_model_cost_updater.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from koku.cache import invalidate_cache_for_tenant_and_cache_key
1111
from koku.cache import invalidate_view_cache_for_tenant_and_source_type
1212
from koku.cache import TAG_MAPPING_PREFIX
13-
from masu.processor import is_customer_cost_model_large
1413
from masu.processor.aws.aws_cost_model_cost_updater import AWSCostModelCostUpdater
1514
from masu.processor.azure.azure_cost_model_cost_updater import AzureCostModelCostUpdater
1615
from masu.processor.gcp.gcp_cost_model_cost_updater import GCPCostModelCostUpdater
@@ -93,11 +92,7 @@ def update_cost_model_costs(self, start_date=None, end_date=None):
9392
end_date = dh.today.date()
9493
summary_range = SummaryRangeConfig(start_date=start_date, end_date=end_date)
9594
if self._updater:
96-
if is_customer_cost_model_large(self._schema):
97-
for day_range in summary_range.iter_days():
98-
self._updater.update_summary_cost_model_costs(day_range)
99-
else:
100-
self._updater.update_summary_cost_model_costs(summary_range)
95+
self._updater.update_summary_cost_model_costs(summary_range)
10196
invalidate_view_cache_for_tenant_and_source_type(self._schema, self._provider.type)
10297
# Invalidate the tag_rate_map for tag mapping
10398
invalidate_cache_for_tenant_and_cache_key(self._schema, TAG_MAPPING_PREFIX)

koku/masu/processor/ocp/ocp_cloud_parquet_summary_updater.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from masu.util.aws.common import get_bills_from_provider as aws_get_bills_from_provider
2525
from masu.util.azure.common import get_bills_from_provider as azure_get_bills_from_provider
2626
from masu.util.common import date_range_pair
27+
from masu.util.common import SummaryRangeConfig
2728
from masu.util.gcp.common import get_bills_from_provider as gcp_get_bills_from_provider
2829
from masu.util.ocp.common import get_cluster_alias_from_cluster_id
2930
from masu.util.ocp.common import get_cluster_id_from_provider
@@ -170,7 +171,9 @@ def update_summary_tables(self, start_date, end_date, ocp_provider_uuid, infra_p
170171

171172
# Update the UI tables for the OpenShift provider
172173
with OCPReportDBAccessor(self._schema) as ocp_accessor:
173-
ocp_accessor.populate_ui_summary_tables(start_date, end_date, ocp_provider_uuid)
174+
ocp_accessor.populate_ui_summary_tables(
175+
SummaryRangeConfig(start_date=start_date, end_date=end_date), ocp_provider_uuid
176+
)
174177

175178
def update_aws_summary_tables(self, openshift_provider_uuid, aws_provider_uuid, start_date, end_date):
176179
"""Update operations specifically for OpenShift on AWS."""

koku/masu/processor/ocp/ocp_cost_model_cost_updater.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -541,9 +541,7 @@ def distribute_costs_and_update_ui_summary(self, summary_range: SummaryRangeConf
541541
summary_range, self._provider_uuid, self._distribution_info
542542
)
543543
for month_range in summary_range.iter_summary_range_by_month():
544-
accessor.populate_ui_summary_tables(
545-
month_range.summary_start, month_range.summary_end, self._provider.uuid
546-
)
544+
accessor.populate_ui_summary_tables(month_range, self._provider.uuid)
547545

548546
if report_period := accessor.report_periods_for_provider_uuid(
549547
self._provider_uuid, month_range.summary_start

koku/masu/processor/ocp/ocp_report_parquet_summary_updater.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from masu.database.ocp_report_db_accessor import OCPReportDBAccessor
1818
from masu.processor.ocp.ocp_cloud_updater_base import OCPCloudUpdaterBase
1919
from masu.util.common import date_range_pair
20+
from masu.util.common import SummaryRangeConfig
2021
from masu.util.ocp.common import get_cluster_alias_from_cluster_id
2122
from masu.util.ocp.common import get_cluster_id_from_provider
2223
from reporting.provider.ocp.models import UI_SUMMARY_TABLES
@@ -130,7 +131,9 @@ def update_summary_tables(self, start_date, end_date, **kwargs):
130131
accessor.populate_line_item_daily_summary_table_trino(
131132
start, end, report_period_id, self._cluster_id, self._cluster_alias, self._provider.uuid
132133
)
133-
accessor.populate_ui_summary_tables(start, end, self._provider.uuid)
134+
accessor.populate_ui_summary_tables(
135+
SummaryRangeConfig(start_date=start, end_date=end), self._provider.uuid
136+
)
134137

135138
# This will process POD and STORAGE together
136139
LOG.info(

koku/masu/test/database/test_ocp_report_db_accessor.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,23 @@ def test_no_tag_rates(self, mock_psql):
13421342
)
13431343
mock_psql.assert_not_called()
13441344

1345+
@patch("masu.database.ocp_report_db_accessor.OCPReportDBAccessor._populate_virtualization_ui_summary_table")
1346+
@patch("masu.database.ocp_report_db_accessor.OCPReportDBAccessor._populate_gpu_ui_summary_table_with_usage_only")
1347+
@patch("masu.database.ocp_report_db_accessor.OCPReportDBAccessor._prepare_and_execute_raw_sql_query")
1348+
def test_populate_ui_summary_tables_skips_virtualization_for_previous_month(
1349+
self, mock_psql, mock_gpu_populate, mock_virt_populate
1350+
):
1351+
"""Test that virtualization UI table is not populated when summarizing previous month (not current month)."""
1352+
start_date = self.dh.last_month_start.date()
1353+
end_date = self.dh.last_month_end.date()
1354+
summary_range = SummaryRangeConfig(start_date=start_date, end_date=end_date, summarize_previous_month=True)
1355+
source_uuid = self.ocp_provider_uuid
1356+
with self.accessor as acc:
1357+
acc.populate_ui_summary_tables(summary_range, source_uuid, tables=[])
1358+
mock_psql.assert_not_called()
1359+
mock_gpu_populate.assert_called_once()
1360+
mock_virt_populate.assert_not_called()
1361+
13451362
@patch("masu.database.ocp_report_db_accessor.OCPReportDBAccessor.schema_exists_trino", return_value=False)
13461363
@patch("masu.database.ocp_report_db_accessor.OCPReportDBAccessor._prepare_and_execute_raw_sql_query")
13471364
def test__populate_virtualization_ui_summary_table_no_trino_schema(self, mock_psql, mock_schema_exists):

koku/masu/test/util/test_common.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -664,14 +664,14 @@ def test_iter_summary_range_by_month_with_extended_summary_dates(self):
664664
self.assertEqual(ranges[1].summary_start, date(2025, 2, 1))
665665
self.assertEqual(ranges[1].summary_end, date(2025, 2, 3))
666666

667-
def test_iter_summary_range_by_month_preserves_skip_full_month(self):
668-
"""Test iter_summary_range_by_month preserves skip_full_month flag."""
667+
def test_iter_summary_range_by_month_preserves_summarize_previous_month(self):
668+
"""Test iter_summary_range_by_month preserves summarize_previous_month flag."""
669669
config = common_utils.SummaryRangeConfig(
670-
start_date=date(2025, 1, 1), end_date=date(2025, 2, 3), skip_full_month=True
670+
start_date=date(2025, 1, 1), end_date=date(2025, 2, 3), summarize_previous_month=True
671671
)
672672

673673
for range_config in config.iter_summary_range_by_month():
674-
self.assertTrue(range_config.skip_full_month)
674+
self.assertTrue(range_config.summarize_previous_month)
675675

676676

677677
class NamedTemporaryGZipTests(TestCase):

koku/masu/util/common.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -532,10 +532,10 @@ class SummaryRangeConfig(BaseModel):
532532
end_date: date
533533
summary_starts: list[date] = Field(default_factory=list)
534534
summary_ends: list[date] = Field(default_factory=list)
535-
skip_full_month: bool = Field(
535+
summarize_previous_month: bool = Field(
536536
default=False,
537-
description="When True, skip distributions that require full month data. "
538-
"Set automatically by iter_days() for days after the first.",
537+
description="When True, indicates we are summarizing previous month data (e.g., GPU finalization). "
538+
"Used to skip certain UI table updates that don't need to be reprocessed.",
539539
)
540540

541541
@field_validator("start_date", "end_date", mode="before")
@@ -591,18 +591,6 @@ def summary_start(self) -> date:
591591
def summary_end(self) -> date:
592592
return max(self.summary_ends)
593593

594-
def iter_days(self) -> Iterator[Self]:
595-
"""Yield a SummaryRangeConfig for each day in the range.
596-
597-
Useful for splitting processing into individual days for large customers.
598-
The first day will have skip_full_month=False (runs full-month distributions),
599-
subsequent days will have skip_full_month=True (skips full-month distributions).
600-
"""
601-
first_day = True
602-
for day in DateHelper().list_days(self.start_date, self.end_date):
603-
yield SummaryRangeConfig(start_date=day, end_date=day, skip_full_month=not first_day)
604-
first_day = False
605-
606594
def iter_summary_range_by_month(self) -> Iterator[Self]:
607595
"""Yield a SummaryRangeConfig for each month in the summary range.
608596
@@ -616,4 +604,6 @@ def iter_summary_range_by_month(self) -> Iterator[Self]:
616604
- SummaryRangeConfig(2025-02-01, 2025-02-03)
617605
"""
618606
for start, end in DateHelper().list_month_tuples(self.summary_start, self.summary_end):
619-
yield SummaryRangeConfig(start_date=start, end_date=end, skip_full_month=self.skip_full_month)
607+
yield SummaryRangeConfig(
608+
start_date=start, end_date=end, summarize_previous_month=self.summarize_previous_month
609+
)

0 commit comments

Comments
 (0)