Skip to content

Fixes #38891 - Refactor @subtotal assignment for API hosts action#1015

Merged
adamruzicka merged 1 commit intotheforeman:masterfrom
ofedoren:bug-38891-sizes-typo
Nov 25, 2025
Merged

Fixes #38891 - Refactor @subtotal assignment for API hosts action#1015
adamruzicka merged 1 commit intotheforeman:masterfrom
ofedoren:bug-38891-sizes-typo

Conversation

@ofedoren
Copy link
Member

@ofedoren ofedoren commented Nov 5, 2025

I think there was a typo in size method, but looking at the condition and used methods, I think it can be unified a bit.

@Lukshio, could you please check if I didn't messed up your patch?

@ofedoren ofedoren marked this pull request as draft November 5, 2025 15:32
@ofedoren ofedoren force-pushed the bug-38891-sizes-typo branch from 03131c5 to e882bdb Compare November 5, 2025 15:53
@ofedoren ofedoren marked this pull request as ready for review November 5, 2025 15:53
Comment on lines 121 to 122
@hosts = @hosts.select { |host| @host_statuses[host.id] == 'N/A' } if params[:awaiting]
@subtotal = @hosts.size
Copy link
Contributor

Choose a reason for hiding this comment

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

Sadly this breaks pagination

> ::Host.search_for('name ~ test-00*').paginate(page: 1, per_page: 5).size
5

> ::Host.search_for('name ~ test-00*').paginate(page: 1, per_page: 5).total_entries
10

Copy link
Member Author

Choose a reason for hiding this comment

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

That example makes me think that the variable names are... misleading?

#size is more logical for @subtotal whereas #total_entries is more logical for @total...

Copy link
Contributor

Choose a reason for hiding this comment

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

That example makes me think that the variable names are... misleading?

Pretty much, yes. Iirc the foreman logic for this is that total is the total number of the records without searching and subtotal is the total number of records matched by the search without pagination.

Copy link
Contributor

Choose a reason for hiding this comment

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

That example makes me think that the variable names are... misleading?

Pretty much, yes. Iirc the foreman logic for this is that total is the total number of the records without searching and subtotal is the total number of records matched by the search without pagination.

Yes, subtotal is sum of filtered results

def hosts
set_hosts_and_template_invocations
set_statuses_and_smart_proxies
@total = @job_invocation.targeting.hosts.size
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 is removed since it's essentially this: https://github.com/theforeman/foreman_remote_execution/blob/master/app/controllers/api/v2/job_invocations_controller.rb#L304, although not sure 'cause of .where later when params[:search] is present.

@subtotal = @hosts.respond_to?(:total_entries) ? @hosts.total_entries : @hosts.sizes
end
@hosts = @hosts.select { |host| @host_statuses[host.id] == 'N/A' } if params[:awaiting]
@subtotal = @hosts.try(:count).to_i
Copy link
Member Author

Choose a reason for hiding this comment

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

@ofedoren
Copy link
Member Author

Thanks, @adamruzicka, updated. I thought about making it use https://github.com/theforeman/foreman/blob/develop/app/controllers/api/v2/base_controller.rb#L75-L85, so we're compliant with the pattern, but it would require even nastier changes :/

@ofedoren
Copy link
Member Author

Thanks, @adamruzicka, updated.

Welp, I've never thought I'll open a can of worms by this patch :/ The page sometimes feels even more bugged.

Before this patch:
Peek 2025-11-12 12-02.webm
After:
Peek 2025-11-12 12-04.webm

@ofedoren ofedoren force-pushed the bug-38891-sizes-typo branch from 96fbedc to 38a2241 Compare November 12, 2025 11:10
Copy link
Contributor

@adamruzicka adamruzicka left a comment

Choose a reason for hiding this comment

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

Let's get this in

@adamruzicka adamruzicka merged commit 1772fd0 into theforeman:master Nov 25, 2025
17 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