diff --git a/app/controllers/api/v2/job_invocations_controller.rb b/app/controllers/api/v2/job_invocations_controller.rb index 449da9b91..5484440bf 100644 --- a/app/controllers/api/v2/job_invocations_controller.rb +++ b/app/controllers/api/v2/job_invocations_controller.rb @@ -115,10 +115,10 @@ def output param :id, :identifier, :required => true def hosts set_hosts_and_template_invocations - set_statuses_and_smart_proxies @total = @hosts.size @hosts = @hosts.search_for(params[:search], :order => params[:order]).paginate(:page => params[:page], :per_page => params[:per_page]) @subtotal = @hosts.total_entries + set_statuses_and_smart_proxies if params[:awaiting] @hosts = @hosts.select { |host| @host_statuses[host.id] == 'N/A' } end @@ -313,13 +313,14 @@ def set_hosts_and_template_invocations def set_statuses_and_smart_proxies template_invocations = @template_invocations.includes(:run_host_job_task).to_a hosts = @hosts.to_a - @host_statuses = Hash[hosts.map do |host| - template_invocation = template_invocations.find { |ti| ti.host_id == host.id } + template_invocations_by_host_id = template_invocations.index_by(&:host_id) + @host_statuses = hosts.to_h do |host| + template_invocation = template_invocations_by_host_id[host.id] task = template_invocation.try(:run_host_job_task) [host.id, template_invocation_status(task, @job_invocation.task)] - end] - @smart_proxy_id = Hash[template_invocations.map { |ti| [ti.host_id, ti.smart_proxy_id] }] - @smart_proxy_name = Hash[template_invocations.map { |ti| [ti.host_id, ti.smart_proxy_name] }] + end + @smart_proxy_id = template_invocations.to_h { |ti| [ti.host_id, ti.smart_proxy_id] } + @smart_proxy_name = template_invocations.to_h { |ti| [ti.host_id, ti.smart_proxy_name] } end end end diff --git a/test/functional/api/v2/job_invocations_controller_test.rb b/test/functional/api/v2/job_invocations_controller_test.rb index 5c4835e28..6c5fea2ee 100644 --- a/test/functional/api/v2/job_invocations_controller_test.rb +++ b/test/functional/api/v2/job_invocations_controller_test.rb @@ -235,6 +235,23 @@ class JobInvocationsControllerTest < ActionController::TestCase end end + describe '#hosts' do + test 'should compute job_status for the paginated subset' do + invocation = FactoryBot.create(:job_invocation, :with_template, :with_task) + 2.times { invocation.template_invocations << FactoryBot.create(:template_invocation, :with_task, :with_host, :job_invocation => invocation) } + invocation.job_category = invocation.pattern_template_invocations.first.template.job_category + invocation.targeting.hosts = invocation.template_invocations.map(&:host) + invocation.save! + + get :hosts, params: { :id => invocation.id, :page => 2, :per_page => 1 } + assert_response :success + result = ActiveSupport::JSON.decode(@response.body) + assert_equal 3, result['total'] + assert_equal 1, result['results'].size + assert_equal(['success'], result['results'].map { |r| r['job_status'] }) + end + end + describe 'raw output' do let(:fake_output) do (1..5).map do |i| diff --git a/webpack/JobInvocationDetail/__tests__/MainInformation.test.js b/webpack/JobInvocationDetail/__tests__/MainInformation.test.js index e3e62c0dc..90f04640a 100644 --- a/webpack/JobInvocationDetail/__tests__/MainInformation.test.js +++ b/webpack/JobInvocationDetail/__tests__/MainInformation.test.js @@ -35,8 +35,11 @@ jest.spyOn(api, 'get'); const originalToLocaleString = Date.prototype.toLocaleString; beforeAll(() => { // eslint-disable-next-line no-extend-native - Date.prototype.toLocaleString = function (locale, options) { - return originalToLocaleString.call(this, locale, { ...options, timeZone: 'UTC' }); + Date.prototype.toLocaleString = function(locale, options) { + return originalToLocaleString.call(this, locale, { + ...options, + timeZone: 'UTC', + }); }; }); afterAll(() => { @@ -232,7 +235,7 @@ describe('JobInvocationDetailPage', () => { { key: GET_REPORT_TEMPLATES, url: '/api/report_templates' }, { key: JOB_INVOCATION_KEY, - url: `/api/job_invocations/${jobId}?host_status=true`, + url: `/api/job_invocations/${jobId}`, }, { key: GET_REPORT_TEMPLATE_INPUTS, diff --git a/webpack/JobInvocationDetail/index.js b/webpack/JobInvocationDetail/index.js index 6b266241b..34fc19443 100644 --- a/webpack/JobInvocationDetail/index.js +++ b/webpack/JobInvocationDetail/index.js @@ -72,7 +72,7 @@ const JobInvocationDetailPage = ({ } useEffect(() => { - dispatch(getJobInvocation(`/api/job_invocations/${id}?host_status=true`)); + dispatch(getJobInvocation(`/api/job_invocations/${id}`)); if (finished && !autoRefresh) { dispatch(stopInterval(JOB_INVOCATION_KEY)); }