From 95ad326c2973327452a6048a69b6f08bb9b01ffa Mon Sep 17 00:00:00 2001 From: Joe Fiorini Date: Wed, 21 Jan 2015 14:46:36 -0500 Subject: [PATCH] Use new service in job status graph widget --- awx/ui/static/js/app.js | 12 ++++- awx/ui/static/js/controllers/Home.js | 9 ++-- .../js/services/job-status-graph-data.js | 45 ++++++++++--------- awx/ui/static/js/widgets/JobStatusGraph.js | 40 +++++++---------- .../services/job-status-graph-data-test.js | 17 ++++++- 5 files changed, 71 insertions(+), 52 deletions(-) diff --git a/awx/ui/static/js/app.js b/awx/ui/static/js/app.js index cab1d896e0..08bec05b44 100644 --- a/awx/ui/static/js/app.js +++ b/awx/ui/static/js/app.js @@ -400,7 +400,17 @@ angular.module('Tower', [ when('/home', { templateUrl: urlPrefix + 'partials/home.html', - controller: 'Home' + controller: 'Home', + resolve: { + graphData: function($q, jobStatusGraphData) { + return $q.all({ + jobStatus: jobStatusGraphData.get("month", "all").then(function(data) { + console.log('got data: ', data); + return data.data; + }) + }); + } + } }). when('/home/groups', { diff --git a/awx/ui/static/js/controllers/Home.js b/awx/ui/static/js/controllers/Home.js index 820ef907c9..b753e9d442 100644 --- a/awx/ui/static/js/controllers/Home.js +++ b/awx/ui/static/js/controllers/Home.js @@ -26,8 +26,9 @@ * */ function Home($scope, $compile, $routeParams, $rootScope, $location, $log, Wait, DashboardCounts, HostGraph, JobStatusGraph, HostPieChart, DashboardJobs, - ClearScope, Stream, Rest, GetBasePath, ProcessErrors, Button){ + ClearScope, Stream, Rest, GetBasePath, ProcessErrors, Button, graphData){ + console.log('graphData:', graphData); ClearScope('home'); var buttons, html, e, waitCount, loadedCount,borderStyles, jobs_scope, schedule_scope; @@ -120,7 +121,7 @@ function Home($scope, $compile, $routeParams, $rootScope, $location, $log, Wait, JobStatusGraph({ scope: $scope, target: 'dash-job-status-graph', - dashboard: data + data: graphData.jobStatus }); if ($rootScope.user_is_superuser === true) { @@ -188,7 +189,7 @@ function Home($scope, $compile, $routeParams, $rootScope, $location, $log, Wait, } Home.$inject = ['$scope', '$compile', '$routeParams', '$rootScope', '$location', '$log','Wait', 'DashboardCounts', 'HostGraph','JobStatusGraph', 'HostPieChart', 'DashboardJobs', - 'ClearScope', 'Stream', 'Rest', 'GetBasePath', 'ProcessErrors', 'Button' + 'ClearScope', 'Stream', 'Rest', 'GetBasePath', 'ProcessErrors', 'Button', 'graphData' ]; @@ -757,4 +758,4 @@ function HomeHosts($scope, $location, $routeParams, HomeHostList, GenerateList, HomeHosts.$inject = ['$scope', '$location', '$routeParams', 'HomeHostList', 'GenerateList', 'ProcessErrors', 'LoadBreadCrumbs', 'ReturnToCaller', 'ClearScope', 'GetBasePath', 'SearchInit', 'PaginateInit', 'FormatDate', 'SetStatus', 'ToggleHostEnabled', 'HostsEdit', 'Stream', 'Find', 'ShowJobSummary', 'ViewJob' -]; \ No newline at end of file +]; diff --git a/awx/ui/static/js/services/job-status-graph-data.js b/awx/ui/static/js/services/job-status-graph-data.js index 037747c496..9bcdd33a25 100644 --- a/awx/ui/static/js/services/job-status-graph-data.js +++ b/awx/ui/static/js/services/job-status-graph-data.js @@ -1,6 +1,6 @@ angular.module('DataServices', []) .service('jobStatusGraphData', - ["RestServices", + ["Rest", "GetBasePath", "ProcessErrors", "$rootScope", @@ -11,37 +11,42 @@ function JobStatusGraphData(Rest, getBasePath, processErrors, $rootScope, $q) { var callbacks = {}; var currentCallbackId = 0; - function getData() { + function getData(period, jobType) { + var url = getBasePath('dashboard')+'graphs/jobs/?period='+period+'&job_type='+jobType; + Rest.setUrl(url); return Rest.get(); } return { + destroyWatcher: angular.noop, setupWatcher: function() { - $rootScope.$on('JobStatusChange', function() { - getData().then(function(result) { - $rootScope. - $broadcast('DataReceived:JobStatusGraph', - result); - return result; - }).catch(function(response) { - var errorMessage = 'Failed to get: ' + url + ' GET returned: ' + status; + this.destroyWatcher = + $rootScope.$on('JobStatusChange', function() { + getData().then(function(result) { + $rootScope. + $broadcast('DataReceived:JobStatusGraph', + result); + return result; + }).catch(function(response) { + var errorMessage = 'Failed to get: ' + url + ' GET returned: ' + status; - ProcessErrors(null, - response.data, - response.status, - null, { - hdr: 'Error!', - msg: errorMessage - }); - return response; - }); + ProcessErrors(null, + response.data, + response.status, + null, { + hdr: 'Error!', + msg: errorMessage + }); + return response; + }); }); }, get: function(period, jobType) { + this.destroyWatcher(); this.setupWatcher(); - return getData(); + return getData(period, jobType); } }; diff --git a/awx/ui/static/js/widgets/JobStatusGraph.js b/awx/ui/static/js/widgets/JobStatusGraph.js index 6477ca7b6f..f5bbbbddd6 100644 --- a/awx/ui/static/js/widgets/JobStatusGraph.js +++ b/awx/ui/static/js/widgets/JobStatusGraph.js @@ -11,13 +11,13 @@ 'use strict'; angular.module('JobStatusGraphWidget', ['RestServices', 'Utilities']) - .factory('JobStatusGraph', ['$rootScope', '$compile', '$location' , 'Rest', 'GetBasePath', 'ProcessErrors', 'Wait', - function ($rootScope, $compile , $location, Rest, GetBasePath, ProcessErrors) { + .factory('JobStatusGraph', ['$rootScope', '$compile', '$location' , 'Rest', 'GetBasePath', 'ProcessErrors', 'Wait', 'jobStatusGraphData', + function ($rootScope, $compile , $location, Rest, GetBasePath, ProcessErrors, jobStatusGraphData) { return function (params) { var scope = params.scope, target = params.target, - // dashboard = params.dashboard, + data = params.data, html, element, url, job_status_chart, period="month", job_type="all"; @@ -65,29 +65,16 @@ angular.module('JobStatusGraphWidget', ['RestServices', 'Utilities']) // html += "\n"; - function createGraph(){ - - url = GetBasePath('dashboard')+'graphs/jobs/?period='+period+'&job_type='+job_type; - Rest.setUrl(url); - Rest.get() - .success(function (data){ + scope.$on('DataReceived:JobStatusGraph', + function(data) { scope.$emit('graphDataReady', data); - return job_type, period; + }); - }) - .error(function (data, status) { - ProcessErrors(scope, data, status, null, { hdr: 'Error!', - msg: 'Failed to get: ' + url + ' GET returned: ' + status }); - }); + function createGraph(period, jobtype){ + // console.log(jobStatusGraphData); + // jobStatusGraphData.get(period, jobtype); } - if ($rootScope.removeReloadJobStatusGraph) { - $rootScope.removeReloadJobStatusGraph(); - } - $rootScope.removeReloadJobStatusGraph = $rootScope.$on('ReloadJobStatusGraph', function() { - createGraph(); - }); - element = angular.element(document.getElementById(target)); element.html(html); $compile(element)(scope); @@ -192,7 +179,7 @@ angular.module('JobStatusGraphWidget', ['RestServices', 'Utilities']) period = this.getAttribute("id"); $('#period-dropdown').replaceWith(""+this.text+"\n"); - createGraph(); + createGraph(period, job_type); }); //On click, update with new data @@ -201,7 +188,8 @@ angular.module('JobStatusGraphWidget', ['RestServices', 'Utilities']) job_type = this.getAttribute("id"); $('#type-dropdown').replaceWith(""+this.text+"\n"); - createGraph(); + data + createGraph(period, job_type); }); scope.$emit('WidgetLoaded'); @@ -214,6 +202,8 @@ angular.module('JobStatusGraphWidget', ['RestServices', 'Utilities']) }); + scope.$emit('graphDataReady', data); + }; } - ]); \ No newline at end of file + ]); diff --git a/awx/ui/tests/unit/services/job-status-graph-data-test.js b/awx/ui/tests/unit/services/job-status-graph-data-test.js index 4cb80df2a4..56dec232ba 100644 --- a/awx/ui/tests/unit/services/job-status-graph-data-test.js +++ b/awx/ui/tests/unit/services/job-status-graph-data-test.js @@ -8,12 +8,16 @@ describe('Job Status Graph Data Service', function() { $on: sinon.spy(), }; + var getBasePath = function(path) { + return '/' + path + '/'; + } + function flushPromises() { window.setTimeout(function() { inject(function($rootScope) { $rootScope.$apply(); }); - }, 100); + }); } var restStub = { @@ -42,7 +46,8 @@ describe('Job Status Graph Data Service', function() { $provide.value("$cookieStore", { get: angular.noop }); - $provide.value('RestServices', restStub); + $provide.value('Rest', restStub); + $provide.value('GetBasePath', getBasePath); })); afterEach(function() { @@ -99,4 +104,12 @@ describe('Job Status Graph Data Service', function() { return expect(result.promise).to.eventually.equal(expected); }); + it('requests data with given period and jobType', function() { + restStub.setUrl = sinon.spy(); + + jobStatusGraphData.get('1', '2'); + + expect(restStub.setUrl).to.have.been.calledWith('/dashboard/graphs/jobs/?period=1&job_type=2'); + }); + });