Skip to content

Conversation

@jrafanie
Copy link
Member

If a group was previously subscribed to the widget, we shouldn't generate content for it. We should properly update widgets when groups are removed but for now, this allows the content generator to work with widgets having orphaned group references.

Extracted from #23404

CP4AIOPS-15687

groups_by_id = MiqGroup.in_my_region.where(:id => grouped_users.keys).index_by(&:id)
users_by_userid = User.in_my_region.where(:userid => grouped_users.values.flatten.uniq).index_by(&:userid)
grouped_users.each_with_object({}) do |(k, v), h|
next unless groups_by_id.key?(k)
Copy link
Member Author

Choose a reason for hiding this comment

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

not sure if this should be moved up to grouped_users_by_id... I think it could be but don't know if that will break something else

Copy link
Member

Choose a reason for hiding this comment

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

Should we log a warning or something in this case?

Copy link
Member

Choose a reason for hiding this comment

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

I think it makes sense to change grouped_users_by_id to better meet our needs.
It is only used by this method.

It does look like it tried to handle the nil case with the 2 separate blank? tests. So I'm surprised any nil values sneaked through.
Also feel we want to eradicate the cases that did slip through.

  def grouped_users_by_id
    id_groups = Hash.new { |h, k| h[k] = [] }
    memberof.compact.each_with_object(id_groups) do |ws, h|
      h[ws.group_id] << ws.userid unless ws.userid.blank? || ws.group_id.blank?
    end
  end

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, that's what I wanted to hear. I didn't love my initial hack and spent more time in creating failing tests.

end

it 'ignores a group that no longer exists' do
@user1.update(:miq_groups => [@group2])
Copy link
Member

Choose a reason for hiding this comment

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

Is this so that when we delete group1 it doesn't fail due to being a non-empty group since user1 is in it?

Copy link
Member Author

Choose a reason for hiding this comment

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

I removed this line by not using destroy since the idea was to leave a widget's subscription to be orphaned as the group was removed. Previously, I had to workaround doing group1.destroy by making sure the existing user was moved from group 1 to 2. By using delete, I don't need to do that.


it 'ignores a group that no longer exists' do
@user1.update(:miq_groups => [@group2])
@group1.destroy
Copy link
Member

Choose a reason for hiding this comment

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

Should this be delete? I noticed we used delete in the other test (or maybe we should be using destroys in the other tests?)

Suggested change
@group1.destroy
@group1.delete

Copy link
Member Author

Choose a reason for hiding this comment

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

good point... I noticed the deletes too but still did destroy out of habit. I can update them all in another commit.

Copy link
Member Author

Choose a reason for hiding this comment

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

See above... I went with delete for the purposes of testing the widget <-> group subscription.

Copy link
Member

@Fryguy Fryguy left a comment

Choose a reason for hiding this comment

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

Overall LGTM - code-wise this is good to merge, but I had a few questions first.

@jrafanie jrafanie force-pushed the fix-grouped-subscribers branch from e70f4f2 to 7521867 Compare June 27, 2025 13:56
groups = MiqGroup.where(:id => [@group1, @group2])
expect(MiqGroup).to receive(:in_my_region).and_return(groups)
allow(groups).to receive(:where).and_return(groups)
@widget_report_vendor_and_guest_os.grouped_subscribers
Copy link
Member Author

Choose a reason for hiding this comment

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

This test was stubbing the world and not actually doing any assertions so I fixed it.

h[ws.group_id] << ws.userid unless ws.userid.blank? || ws.group_id.blank?
next if ws.userid.blank?
next if ws.group_id.blank?
next unless group_ids.include?(ws.group_id)
Copy link
Member Author

Choose a reason for hiding this comment

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

The line above is the actual fix. ☝️

jrafanie added 2 commits June 27, 2025 10:31
If a group was previously subscribed to the widget, we shouldn't
generate content for it.  We should properly update widgets
when groups are removed but for now, this allows the content generator
to work with widgets having orphaned group references.

Extracted from ManageIQ#23404

CP4AIOPS-15687
@jrafanie jrafanie force-pushed the fix-grouped-subscribers branch from ed1e4dc to ddcdccf Compare June 27, 2025 14:31
@miq-bot
Copy link
Member

miq-bot commented Jun 27, 2025

Checked commits jrafanie/manageiq@b7f1723~...ddcdccf with ruby 3.1.5, rubocop 1.56.3, haml-lint 0.62.0, and yamllint
2 files checked, 0 offenses detected
Everything looks fine. 🍪

@kbrock kbrock merged commit 107ae3f into ManageIQ:master Jun 27, 2025
8 checks passed
@jrafanie jrafanie deleted the fix-grouped-subscribers branch June 27, 2025 16:04
jrafanie added a commit to jrafanie/manageiq that referenced this pull request Jun 27, 2025
Follow up to discussion found in: ManageIQ#23491
It would be too noisy to bring this back for backports so it's a master only change.
@jrafanie jrafanie mentioned this pull request Jun 27, 2025
@Fryguy
Copy link
Member

Fryguy commented Jul 1, 2025

Backported to spassky in commit 56cbfa3.

commit 56cbfa37bca02c4761d6f1b9485e225d8fd275c5
Author: Keenan Brock <[email protected]>
Date:   Fri Jun 27 11:11:09 2025 -0400

    Merge pull request #23491 from jrafanie/fix-grouped-subscribers
    
    Don't include deleted groups in widget generation
    
    (cherry picked from commit 107ae3f460de5632f0a213ab9d479a159c1cacc5)

Fryguy pushed a commit that referenced this pull request Jul 1, 2025
Don't include deleted groups in widget generation

(cherry picked from commit 107ae3f)
@Fryguy Fryguy removed the spassky/yes label Jul 1, 2025
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