diff --git a/awx/ui/client/src/job-results/job-results.service.js b/awx/ui/client/src/job-results/job-results.service.js index 7c3d36aab9..cb51dc8864 100644 --- a/awx/ui/client/src/job-results/job-results.service.js +++ b/awx/ui/client/src/job-results/job-results.service.js @@ -41,32 +41,31 @@ function ($q, Prompt, $filter, Wait, Rest, $state, ProcessErrors, InitiatePlaybo } }); - // use the hosts data populate above to get the count - var count = { - ok : _.filter(hosts, function(o){ - return !o.failures && !o.changed && o.ok > 0; - }), - skipped : _.filter(hosts, function(o){ - return o.skipped > 0; - }), - unreachable : _.filter(hosts, function(o){ - return o.dark > 0; - }), - failures : _.filter(hosts, function(o){ - return o.failures > 0; - }), - changed : _.filter(hosts, function(o){ - return o.changed > 0; - }) + var total_hosts_by_state = { + ok: 0, + skipped: 0, + unreachable: 0, + failures: 0, + changed: 0 }; - // turn the count into an actual count, rather than a list of host - // names - Object.keys(count).forEach(key => { - count[key] = count[key].length; + // each host belongs in at most *one* of these states depending on + // the state of its tasks + _.each(hosts, function(host) { + if (host.dark > 0){ + total_hosts_by_state.unreachable++; + } else if (host.failures > 0){ + total_hosts_by_state.failures++; + } else if (host.changed > 0){ + total_hosts_by_state.changed++; + } else if (host.ok > 0){ + total_hosts_by_state.ok++; + } else if (host.skipped > 0){ + total_hosts_by_state.skipped++; + } }); - return count; + return total_hosts_by_state; }, // rest call to grab previously complete job_events getEvents: function(url) { diff --git a/awx/ui/tests/spec/job-results/job-results.service-test.js b/awx/ui/tests/spec/job-results/job-results.service-test.js new file mode 100644 index 0000000000..85cc0e526f --- /dev/null +++ b/awx/ui/tests/spec/job-results/job-results.service-test.js @@ -0,0 +1,50 @@ +'use strict'; + +describe('jobResultsService', () => { + let jobResultsService; + + beforeEach(angular.mock.module('Tower')); + + beforeEach(angular.mock.inject(( _jobResultsService_) => { + jobResultsService = _jobResultsService_; + })); + + describe('getCountsFromStatsEvent()', () => { + it('properly counts hosts based on task state', () => { + let event_data = { + "skipped": { + "skipped-host": 5 // this host skipped all 5 tasks + }, + "ok": { + "ok-host": 5, // this host was ok on all 5 tasks + "changed-host": 4 // this host had 4 ok tasks, had 1 changed task + }, + "changed": { + "changed-host": 1 + }, + "failures": { + "failed-host": 1 // this host had a failed task + }, + "dark": { + "unreachable-host": 1 // this host was unreachable + }, + "processed": { + "ok-host": 1, + "changed-host": 1, + "skipped-host": 1, + "failed-host": 1, + "unreachable-host": 1 + }, + "playbook_uuid": "c23d8872-c92a-4e96-9f78-abe6fef38f33", + "playbook": "some_playbook.yml", + }; + expect(jobResultsService.getCountsFromStatsEvent(event_data)).toEqual({ + 'ok': 1, + 'skipped': 1, + 'unreachable': 1, + 'failures': 1, + 'changed': 1 + }); + }); + }); +});