Skip to content

RHINENG-24787: simplify system load and go directly into SystemPlatfo… - #2158

Merged
MichaelMraka merged 1 commit into
RedHatInsights:masterfrom
MichaelMraka:pr3
Apr 17, 2026
Merged

RHINENG-24787: simplify system load and go directly into SystemPlatfo…#2158
MichaelMraka merged 1 commit into
RedHatInsights:masterfrom
MichaelMraka:pr3

Conversation

@MichaelMraka

@MichaelMraka MichaelMraka commented Apr 17, 2026

Copy link
Copy Markdown
Collaborator

…rmV2

Secure Coding Practices Checklist GitHub Link

Secure Coding Checklist

  • Input Validation
  • Output Encoding
  • Authentication and Password Management
  • Session Management
  • Access Control
  • Cryptographic Practices
  • Error Handling and Logging
  • Data Protection
  • Communication Security
  • System Configuration
  • Database Security
  • File Management
  • Memory Management
  • General Coding Practices

Summary by Sourcery

Simplify system data loading by querying system and patch data directly into the SystemPlatformV2 model and aligning the model structure with the underlying tables.

Enhancements:

  • Inline the SystemPlatformV2 loading logic to query system_inventory and system_patch directly, eliminating the separate loader module and its tests.
  • Update SystemPlatformV2 to use embedded SystemInventory and SystemPatch fields for more direct mapping to the joined table columns.

@MichaelMraka
MichaelMraka requested a review from a team as a code owner April 17, 2026 07:32
@sourcery-ai

sourcery-ai Bot commented Apr 17, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors system data loading to query SystemPlatformV2 directly via a joined system_inventory/system_patch GORM query and adjusts the composite model mapping accordingly, removing the now-redundant loader implementation and its tests.

File-Level Changes

Change Details Files
Load SystemPlatformV2 directly in loadSystemData using a single joined GORM query instead of a helper loader function.
  • Replace call to loadSystemPlatformV2 with an inline GORM query using Table("system_inventory si") and JOIN on system_patch.
  • Select combined columns from system_inventory and system_patch into a SystemPlatformV2 struct.
  • Filter by rh_account_id and inventory_id with proper casting to UUID.
  • Return the resulting SystemPlatformV2 pointer and query error directly from loadSystemData.
evaluator/evaluate.go
Adjust SystemPlatformV2 model to work with embedded GORM mappings for the joined query.
  • Mark Inventory field as embedded to flatten SystemInventory columns into SystemPlatformV2.
  • Mark Patch field as embedded to flatten SystemPatch columns into SystemPlatformV2.
  • Document continued requirement to treat SystemPlatformV2 as a composite type loaded via explicit joins rather than Model-based single-table operations.
base/models/models.go
Remove obsolete SystemPlatformV2 loader implementation and its tests.
  • Delete evaluator/system_platform_v2_load.go which contained the loadSystemPlatformV2 helper.
  • Delete evaluator/system_platform_v2_load_test.go that tested the removed loader logic.
evaluator/system_platform_v2_load.go
evaluator/system_platform_v2_load_test.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions

Copy link
Copy Markdown

SC Environment Impact Assessment

Overall Impact:NONE

No SC Environment-specific impacts detected in this PR.

What was checked

This PR was automatically scanned for:

  • Database migrations
  • ClowdApp configuration changes
  • Kessel integration changes
  • AWS service integrations (S3, RDS, ElastiCache)
  • Kafka topic changes
  • Secrets management changes
  • External dependencies

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 2 issues, and left some high level feedback:

  • The new loadSystemData query uses Find on a single SystemPlatformV2 instance, which silently returns a zero value when no row matches; if callers rely on a not-found error, consider switching to First or explicitly checking RowsAffected to preserve behavior.
  • By changing SystemPlatformV2 to use two embedded structs and selecting si.*, sp.* without aliases, overlapping column names from system_inventory and system_patch (e.g., id, timestamps, account IDs) may map unpredictably; it would be safer to add embeddedPrefix/explicit column tags or adjust the SELECT to alias fields to avoid collisions.
  • Inlining the system load SQL into loadSystemData removes the dedicated loader function and its tests; if this pattern will be reused, consider keeping a single helper for the join logic to avoid duplication and make future changes less error-prone.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `loadSystemData` query uses `Find` on a single `SystemPlatformV2` instance, which silently returns a zero value when no row matches; if callers rely on a not-found error, consider switching to `First` or explicitly checking `RowsAffected` to preserve behavior.
- By changing `SystemPlatformV2` to use two `embedded` structs and selecting `si.*, sp.*` without aliases, overlapping column names from `system_inventory` and `system_patch` (e.g., `id`, timestamps, account IDs) may map unpredictably; it would be safer to add `embeddedPrefix`/explicit column tags or adjust the SELECT to alias fields to avoid collisions.
- Inlining the system load SQL into `loadSystemData` removes the dedicated loader function and its tests; if this pattern will be reused, consider keeping a single helper for the join logic to avoid duplication and make future changes less error-prone.

## Individual Comments

### Comment 1
<location path="evaluator/evaluate.go" line_range="684-689" />
<code_context>
-	}
-	return v2, nil
+	var system models.SystemPlatformV2
+	err := database.DB.Table("system_inventory si").
+		Select("si.*, sp.*").
+		Joins("JOIN system_patch sp ON sp.system_id = si.id AND sp.rh_account_id = si.rh_account_id").
+		Where("si.rh_account_id = ?", accountID).
+		Where("si.inventory_id = ?::uuid", inventoryID).
+		Find(&system).Error
+	return &system, err
 }
</code_context>
<issue_to_address>
**issue:** Consider how multiple system_patch rows for a single inventory/system are handled in this join.

Because the join doesn’t disambiguate among multiple `system_patch` rows for the same system, `Find(&system)` will iterate over all matches and the final struct will be populated by whichever row is returned last. If you need a specific patch (e.g., the latest), add an appropriate `ORDER BY` + `LIMIT 1` or additional predicates on fields like `sp.id`/`sp.created_at`/`sp.reported_at` to make the selection deterministic.
</issue_to_address>

### Comment 2
<location path="base/models/models.go" line_range="132-133" />
<code_context>
 type SystemPlatformV2 struct {
-	Inventory SystemInventory
-	Patch     SystemPatch
+	Inventory SystemInventory `gorm:"embedded"`
+	Patch     SystemPatch     `gorm:"embedded"`
 }

</code_context>
<issue_to_address>
**suggestion (bug_risk):** Embedded structs without prefixes may lead to column name collisions in the join-based load.

Because both `SystemInventory` and `SystemPatch` are embedded without `embeddedPrefix`, the `Select("si.*, sp.*")` will cause any shared column names (e.g., `id`, `rh_account_id`, timestamps) to map to a single field, with the last-scanned table winning. If you need both values, add distinct `embeddedPrefix` values and update the `Select` to use matching aliases (e.g., `si.id AS inventory_id`).

Suggested implementation:

```golang
type SystemPlatformV2 struct {
	Inventory SystemInventory `gorm:"embedded;embeddedPrefix:inventory_"`
	Patch     SystemPatch     `gorm:"embedded;embeddedPrefix:patch_"`
}

```

` since we don't see that code here.

Here are the changes:

<file_operations>
<file_operation operation="edit" file_path="base/models/models.go">
<<<<<<< SEARCH
type SystemPlatformV2 struct {
	Inventory SystemInventory `gorm:"embedded"`
	Patch     SystemPatch     `gorm:"embedded"`
}
=======
type SystemPlatformV2 struct {
	Inventory SystemInventory `gorm:"embedded;embeddedPrefix:inventory_"`
	Patch     SystemPatch     `gorm:"embedded;embeddedPrefix:patch_"`
}
>>>>>>> REPLACE
</file_operation>
</file_operations>

<additional_changes>
Any code that loads `SystemPlatformV2` via joins and `Select("si.*, sp.*")` must be updated to:

1. Explicitly alias columns to match the new prefixes. For example:
   - `si.id AS inventory_id`, `si.rh_account_id AS inventory_rh_account_id`, etc.
   - `sp.id AS patch_id`, `sp.rh_account_id AS patch_rh_account_id`, etc.
2. Ensure the table aliases (`si`, `sp`) and column lists cover all fields that GORM will map into the embedded structs, or use `SELECT si.*` into a separate model and then map manually if needed.

Look for any queries that:
- Use `SystemPlatformV2` as the destination model, and
- Use `Select("si.*, sp.*")` or similar unaliased selects with joins on `system_inventory` and `system_patch`,
and update those `Select` clauses accordingly.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread evaluator/evaluate.go
Comment thread base/models/models.go
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 59.03%. Comparing base (89a4dc5) to head (0b282fc).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2158      +/-   ##
==========================================
- Coverage   59.29%   59.03%   -0.26%     
==========================================
  Files         135      134       -1     
  Lines        8748     8688      -60     
==========================================
- Hits         5187     5129      -58     
  Misses       3022     3022              
+ Partials      539      537       -2     
Flag Coverage Δ
unittests 59.03% <100.00%> (-0.26%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TenSt TenSt self-assigned this Apr 17, 2026
@MichaelMraka
MichaelMraka merged commit 0459095 into RedHatInsights:master Apr 17, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants