Skip to content

[COST-7430] Enhance Azure report query handling with subscription name fix#6037

Open
lcouzens wants to merge 3 commits intomainfrom
rank-merge-keys
Open

[COST-7430] Enhance Azure report query handling with subscription name fix#6037
lcouzens wants to merge 3 commits intomainfrom
rank-merge-keys

Conversation

@lcouzens
Copy link
Copy Markdown
Contributor

@lcouzens lcouzens commented May 1, 2026

The following addresses alert seen in stage: KeyError: 'subscription_name'

  • Added a new test to validate the execution of ranked queries with ordering by subscription_name, ensuring that subscription_name is included in the data rows.
  • Updated the ReportQueryHandler to handle cases where rank_group_by may include dimensions not selected in the main queryset, improving the accuracy of data merging.
  • Refactored the AzureReportQueryHandler to simplify the conditional logic for annotating subscription_name based on query_group_by.

This change improves the flexibility and correctness of Azure report queries, particularly in scenarios involving subscription-based data.

Jira Ticket

COST-####

Description

This change will ...

Testing

  1. Checkout Branch
  2. Restart Koku
  3. Hit endpoint or launch shell
    1. You should see ...
  4. Do more things...

Release Notes

  • proposed release note
* [COST-####](https://issues.redhat.com/browse/COST-####) Fix some things

…e ordering

- Added a new test to validate the execution of ranked queries with ordering by subscription_name, ensuring that subscription_name is included in the data rows.
- Updated the ReportQueryHandler to handle cases where rank_group_by may include dimensions not selected in the main queryset, improving the accuracy of data merging.
- Refactored the AzureReportQueryHandler to simplify the conditional logic for annotating subscription_name based on query_group_by.

This change improves the flexibility and correctness of Azure report queries, particularly in scenarios involving subscription-based data.
@lcouzens lcouzens requested review from a team as code owners May 1, 2026 09:15
@lcouzens lcouzens added the smoke-tests pr_check will run minimal required smokes. Used when changes hit multiple providers. label May 1, 2026
@github-actions github-actions Bot added the smokes-required Label to show that smokes tests should be run against these changes. label May 1, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request simplifies the annotation logic for subscription_name in Azure report query handlers and updates the _ranked_list function to handle dimensions that may not be present in the main queryset. A new test case is introduced to verify ranked queries ordered by subscription_name. Feedback suggests improving this test by explicitly asserting that subscription_name is included in the output data.

Comment on lines +789 to +796
def test_execute_query_limit_orderby_subscription_name(self):
"""Ranked query with order_by subscription_name must include subscription_name on data rows."""
url = "?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=monthly&filter[limit]=5&order_by[subscription_name]=asc&group_by[subscription_guid]=*" # noqa: E501
path = reverse("reports-azure-costs")
query_params = self.mocked_query_params(url, AzureCostView, path)
handler = AzureReportQueryHandler(query_params)
query_output = handler.execute_query()
self.assertIsNotNone(query_output.get("data"))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The test test_execute_query_limit_orderby_subscription_name currently only verifies that the data is not None. Since this PR specifically fixes a KeyError and ensures subscription_name is correctly handled in ranked queries (via the changes in _ranked_list), the test should explicitly verify that subscription_name is present in the resulting data rows. This provides better coverage and ensures the fix works as intended for ranked results.

Suggested change
def test_execute_query_limit_orderby_subscription_name(self):
"""Ranked query with order_by subscription_name must include subscription_name on data rows."""
url = "?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=monthly&filter[limit]=5&order_by[subscription_name]=asc&group_by[subscription_guid]=*" # noqa: E501
path = reverse("reports-azure-costs")
query_params = self.mocked_query_params(url, AzureCostView, path)
handler = AzureReportQueryHandler(query_params)
query_output = handler.execute_query()
self.assertIsNotNone(query_output.get("data"))
def test_execute_query_limit_orderby_subscription_name(self):
"""Ranked query with order_by subscription_name must include subscription_name on data rows."""
url = "?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=monthly&filter[limit]=5&order_by[subscription_name]=asc&group_by[subscription_guid]=*" # noqa: E501
path = reverse("reports-azure-costs")
query_params = self.mocked_query_params(url, AzureCostView, path)
handler = AzureReportQueryHandler(query_params)
query_output = handler.execute_query()
data = query_output.get("data")
self.assertIsNotNone(data)
for data_item in data:
for sub_item in data_item.get("subscription_guids", []):
self.assertIn("subscription_name", sub_item.get("values", [{}])[0])

@codecov
Copy link
Copy Markdown

codecov Bot commented May 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.3%. Comparing base (87ad6a1) to head (0eecea1).

Additional details and impacted files
@@           Coverage Diff           @@
##            main   #6037     +/-   ##
=======================================
- Coverage   94.3%   94.3%   -0.0%     
=======================================
  Files        361     361             
  Lines      31805   31808      +3     
  Branches    3484    3485      +1     
=======================================
  Hits       30007   30007             
+ Misses      1170    1169      -1     
- Partials     628     632      +4     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

lcouzens added 2 commits May 1, 2026 12:29
- Added new tests to validate the execution of queries in AzureReportQueryHandler and OCPAzureReportQueryHandler, ensuring that subscription names are included in the returned data when ordered by subscription_name.
- Improved assertions to check the structure and types of data returned, enhancing test coverage for scenarios involving subscription-based data.

These changes improve the robustness of the report query handling for Azure and OCP Azure, ensuring accurate data representation in the results.
@myersCody
Copy link
Copy Markdown
Contributor

/retest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

smoke-tests pr_check will run minimal required smokes. Used when changes hit multiple providers. smokes-required Label to show that smokes tests should be run against these changes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants