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

(RHINENG-12028): serialize_host refactoring #2240

Conversation

strider
Copy link
Contributor

@strider strider commented Feb 5, 2025

Overview

This PR is being created to address RHINENG-12028.

See my comment with all the explanations about this refactoring and improvements that can be done, in the Jira ticket above or in the commit message.

PR Checklist

  • Keep PR title short, ideally under 72 characters
  • Descriptive comments provided in complex code blocks
  • Include raw query examples in the PR description, if adding/modifying SQL query
  • Tests: validate optimal/expected output
  • Tests: validate exceptions and failure scenarios
  • Tests: edge cases
  • Recovers or fails gracefully during potential resource outages (e.g. DB, Kafka)
  • Uses type hinting, if convenient
  • Documentation, if this PR changes the way other services interact with host inventory
  • Links to related PRs

Secure Coding Practices Documentation Reference

You can find documentation on this checklist here.

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

@strider strider force-pushed the RHINENG-12028-Serialize_host_refactoring branch 5 times, most recently from 89b4136 to 524217f Compare February 6, 2025 11:11
@strider strider marked this pull request as ready for review February 6, 2025 11:53
@strider strider requested a review from a team as a code owner February 6, 2025 11:53
@strider strider force-pushed the RHINENG-12028-Serialize_host_refactoring branch 4 times, most recently from 3c5f7e1 to daad157 Compare February 10, 2025 08:19
Copy link
Contributor

@jpramos123 jpramos123 left a comment

Choose a reason for hiding this comment

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

LGTM, just need the get_staleness_timestamps to be moved to global scope.

app/serialization.py Outdated Show resolved Hide resolved
app/serialization.py Outdated Show resolved Hide resolved
@kruai
Copy link
Collaborator

kruai commented Feb 10, 2025

I agree with @jpramos123's comments, but I think the code looks good beyond that. Good work!

@thearifismail
Copy link
Contributor

LGTM to except the moving of get_stale_timestamps function.

@strider strider reopened this Feb 11, 2025
@strider
Copy link
Contributor Author

strider commented Feb 11, 2025

Thanks @thearifismail, @kruai, and @jpramos123 for your comments.

I'll address that asap.

@strider strider force-pushed the RHINENG-12028-Serialize_host_refactoring branch from daad157 to c752431 Compare February 11, 2025 12:26
@strider strider changed the title techdebt(RHINENG-12028): serialize_host refactoring attempt (RHINENG-12028): serialize_host refactoring Feb 11, 2025
@jpramos123 jpramos123 force-pushed the RHINENG-12028-Serialize_host_refactoring branch from c752431 to 6220f6b Compare February 11, 2025 20:48
@jpramos123
Copy link
Contributor

/retest

@strider strider force-pushed the RHINENG-12028-Serialize_host_refactoring branch from 6220f6b to 700ebd7 Compare February 12, 2025 07:18
Issues identified:

  Repetitive staleness calculation:
  * The stale_timestamp, stale_warning_timestamp, and culled_timestamp
    assignments are redundant for edge and conventional hosts.
  * We should extract this logic into a helper function.

  Field processing in a long if-else chain:
  * The current approach of checking each field separately makes it
    harder to read.
  * Using a dictionary comprehension may make it cleaner.

  Unused arguments (additional_fields defaulted to tuple()):
  * We should use None as a default, and convert it to a tuple inside
    the function.

  Better separation of concerns:
  * Extract helper functions to make the serialize_host function more modular.

Improvements Proposal:

  Encapsulating the staleness calculation:
  * Extracting the calculation into a get_staleness_timestamps() helper
    function to avoid duplicate logic.

  Reducing repetitive "if" Checks:
  * By using a dictionary field_mapping to map field names to their
    corresponding serialization logic.
  * By doing this, we eliminate long "if" chains, making it cleaner and
    easier to modify.

  Optimizing System Profile management:
  * By using a dictionary comprehension to filter system_profile_facts
    efficiently.

  Improving readability and Maintainability:
  * By bringing a better logical separation of concerns.
  * By bringing an easier way to extend the host data (we just need to
    modify the field_mapping and add/remove fields).

Signed-off-by: Gaël Chamoulaud <[email protected]>

rh-pre-commit.version: 2.3.2
rh-pre-commit.check-secrets: ENABLED
@strider strider force-pushed the RHINENG-12028-Serialize_host_refactoring branch from 700ebd7 to 7d4e3a7 Compare February 12, 2025 13:27
@jpramos123 jpramos123 merged commit 44c2aec into RedHatInsights:master Feb 12, 2025
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants