Skip to content

Add Advisor IopRecommendationsCell #1052

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

Merged
merged 1 commit into from
Aug 1, 2025

Conversation

mhuth
Copy link
Contributor

@mhuth mhuth commented Jul 31, 2025

Adds the IopRecommendationsCell needed for producing a count of the number of hits for each Insights host, as per the screenshot:

Screenshot from 2025-07-31 17-44-54

No automated tests at this stage. Just manual testing via the browser.

Summary by Sourcery

Implement a new Advisor recommendations cell that uses the useAPI hook to fetch and display the number of hits for each Insights host and link to the host's Insights page, replacing the previous Scalprum-based implementation.

New Features:

  • Add IopRecommendationsCell that fetches hit counts via useAPI and renders them as links

Enhancements:

  • Remove ScalprumProvider wrapper and IopRecommendationsCellWrapped component
  • Use foremanUrl helper to construct host Insights page URLs

Tests:

  • No automated tests added; functionality verified via manual browser testing

Copy link

sourcery-ai bot commented Jul 31, 2025

Reviewer's Guide

Replaces the previous Scalprum-based IopRecommendationsCell with a direct API-backed implementation that fetches the hit count via useAPI, constructs the Foreman Insights link with foremanUrl, and updates RecommendationsCell to use the new component.

Sequence diagram for IopRecommendationsCell API-backed hit count fetch

sequenceDiagram
    participant RecommendationsCell
    participant IopRecommendationsCell
    participant API as Insights API
    participant Browser
    RecommendationsCell->>IopRecommendationsCell: Render with hostDetails
    IopRecommendationsCell->>API: GET /insights_cloud/api/insights/v1/system/:uuid
    API-->>IopRecommendationsCell: { hits }
    IopRecommendationsCell->>Browser: Render <a href=...>{hits}</a>
Loading

File-Level Changes

Change Details Files
New direct API-powered IopRecommendationsCell replaces the Scalprum wrapper
  • Removed IopRecommendationsCellWrapped and ScalprumProvider/ScalprumComponent usage
  • Refactored IopRecommendationsCell to extract UUID, call useAPI, and retrieve hits
  • Encoded hostname and built hitsUrl with foremanUrl
  • Returned '—' when no UUID or hits, otherwise rendered clickable hits count
webpack/ForemanColumnExtensions/index.js
Imported foremanUrl and useAPI helpers
  • Added import for foremanUrl
  • Added import for useAPI
webpack/ForemanColumnExtensions/index.js
Updated RecommendationsCell to use the new IopRecommendationsCell directly
  • Removed IopRecommendationsCellWrapped from the conditional
  • Switched to invoking IopRecommendationsCell directly when useLocalAdvisorEngine is true
webpack/ForemanColumnExtensions/index.js

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

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @mhuth - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `webpack/ForemanColumnExtensions/index.js:30` </location>
<code_context>
-      <ScalprumComponent scope={scope} module={module} />
-    </span>
-  );
+  const hostname = hostDetails?.name;
+  const encodedHostname = encodeURIComponent(hostname);
+  const hitsUrl = foremanUrl(`/new/hosts/${encodedHostname}#/Insights`);
+  return !hits ? '—' : <a href={hitsUrl}>{hits}</a>;
</code_context>

<issue_to_address>
No fallback for missing or empty hostname before encoding.

Add a check or default value for hostDetails.name to prevent generating invalid URLs when the hostname is missing or empty.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
  const hostname = hostDetails?.name;
  const encodedHostname = encodeURIComponent(hostname);
  const hitsUrl = foremanUrl(`/new/hosts/${encodedHostname}#/Insights`);
  return !hits ? '—' : <a href={hitsUrl}>{hits}</a>;
=======
  const hostname = hostDetails?.name && hostDetails.name.trim() !== '' ? hostDetails.name : 'unknown-host';
  const encodedHostname = encodeURIComponent(hostname);
  const hitsUrl = foremanUrl(`/new/hosts/${encodedHostname}#/Insights`);
  return !hits ? '—' : <a href={hitsUrl}>{hits}</a>;
>>>>>>> REPLACE

</suggested_fix>

### Comment 2
<location> `webpack/ForemanColumnExtensions/index.js:28` </location>
<code_context>
+  if (!uuid) return '—';
+  const key = `HOST_RECS_COUNT_${uuid}`;
+  const response = useAPI('get', `/insights_cloud/api/insights/v1/system/${uuid}`, {key,});
+  const hits = response.response?.hits;

-  return (
-    <span className="rh-cloud-insights-recommendations-cell">
-      <ScalprumComponent scope={scope} module={module} />
-    </span>
-  );
+  const hostname = hostDetails?.name;
+  const encodedHostname = encodeURIComponent(hostname);
+  const hitsUrl = foremanUrl(`/new/hosts/${encodedHostname}#/Insights`);
+  return !hits ? '—' : <a href={hitsUrl}>{hits}</a>;
 };

</code_context>

<issue_to_address>
Falsy check on hits may hide valid zero values.

Since 0 is falsy in JavaScript, this check will display '—' for 0 hits. Use a more precise condition, such as 'hits == null' or 'typeof hits !== "number"', to ensure zero is handled correctly.
</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.

Copy link
Collaborator

@jeremylenz jeremylenz left a comment

Choose a reason for hiding this comment

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

Thanks @mhuth ! Works well, just resolve the eslint problems and we're good.

@mhuth mhuth force-pushed the iop-recommendations-cell branch from 77ce4df to ad69fe2 Compare July 31, 2025 20:57
@mhuth mhuth force-pushed the iop-recommendations-cell branch from ad69fe2 to fd2cfd4 Compare July 31, 2025 21:03
Copy link
Member

@chris1984 chris1984 left a comment

Choose a reason for hiding this comment

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

Looks fine to me, @jeremylenz did you want to do a final review?

@jeremylenz jeremylenz merged commit 345aa65 into theforeman:develop Aug 1, 2025
23 checks passed
@mhuth mhuth deleted the iop-recommendations-cell branch August 2, 2025 07:11
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