Use new service in job status graph widget

This commit is contained in:
Joe Fiorini
2015-01-21 14:46:36 -05:00
parent e0efc11ef5
commit 95ad326c29
5 changed files with 71 additions and 52 deletions

View File

@@ -400,7 +400,17 @@ angular.module('Tower', [
when('/home', { when('/home', {
templateUrl: urlPrefix + 'partials/home.html', 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', { when('/home/groups', {

View File

@@ -26,8 +26,9 @@
* *
*/ */
function Home($scope, $compile, $routeParams, $rootScope, $location, $log, Wait, DashboardCounts, HostGraph, JobStatusGraph, HostPieChart, DashboardJobs, 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'); ClearScope('home');
var buttons, html, e, waitCount, loadedCount,borderStyles, jobs_scope, schedule_scope; 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({ JobStatusGraph({
scope: $scope, scope: $scope,
target: 'dash-job-status-graph', target: 'dash-job-status-graph',
dashboard: data data: graphData.jobStatus
}); });
if ($rootScope.user_is_superuser === true) { 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', 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', HomeHosts.$inject = ['$scope', '$location', '$routeParams', 'HomeHostList', 'GenerateList', 'ProcessErrors', 'LoadBreadCrumbs', 'ReturnToCaller',
'ClearScope', 'GetBasePath', 'SearchInit', 'PaginateInit', 'FormatDate', 'SetStatus', 'ToggleHostEnabled', 'HostsEdit', 'Stream', 'ClearScope', 'GetBasePath', 'SearchInit', 'PaginateInit', 'FormatDate', 'SetStatus', 'ToggleHostEnabled', 'HostsEdit', 'Stream',
'Find', 'ShowJobSummary', 'ViewJob' 'Find', 'ShowJobSummary', 'ViewJob'
]; ];

View File

@@ -1,6 +1,6 @@
angular.module('DataServices', []) angular.module('DataServices', [])
.service('jobStatusGraphData', .service('jobStatusGraphData',
["RestServices", ["Rest",
"GetBasePath", "GetBasePath",
"ProcessErrors", "ProcessErrors",
"$rootScope", "$rootScope",
@@ -11,37 +11,42 @@ function JobStatusGraphData(Rest, getBasePath, processErrors, $rootScope, $q) {
var callbacks = {}; var callbacks = {};
var currentCallbackId = 0; 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 Rest.get();
} }
return { return {
destroyWatcher: angular.noop,
setupWatcher: function() { setupWatcher: function() {
$rootScope.$on('JobStatusChange', function() { this.destroyWatcher =
getData().then(function(result) { $rootScope.$on('JobStatusChange', function() {
$rootScope. getData().then(function(result) {
$broadcast('DataReceived:JobStatusGraph', $rootScope.
result); $broadcast('DataReceived:JobStatusGraph',
return result; result);
}).catch(function(response) { return result;
var errorMessage = 'Failed to get: ' + url + ' GET returned: ' + status; }).catch(function(response) {
var errorMessage = 'Failed to get: ' + url + ' GET returned: ' + status;
ProcessErrors(null, ProcessErrors(null,
response.data, response.data,
response.status, response.status,
null, { null, {
hdr: 'Error!', hdr: 'Error!',
msg: errorMessage msg: errorMessage
}); });
return response; return response;
}); });
}); });
}, },
get: function(period, jobType) { get: function(period, jobType) {
this.destroyWatcher();
this.setupWatcher(); this.setupWatcher();
return getData(); return getData(period, jobType);
} }
}; };

View File

@@ -11,13 +11,13 @@
'use strict'; 'use strict';
angular.module('JobStatusGraphWidget', ['RestServices', 'Utilities']) angular.module('JobStatusGraphWidget', ['RestServices', 'Utilities'])
.factory('JobStatusGraph', ['$rootScope', '$compile', '$location' , 'Rest', 'GetBasePath', 'ProcessErrors', 'Wait', .factory('JobStatusGraph', ['$rootScope', '$compile', '$location' , 'Rest', 'GetBasePath', 'ProcessErrors', 'Wait', 'jobStatusGraphData',
function ($rootScope, $compile , $location, Rest, GetBasePath, ProcessErrors) { function ($rootScope, $compile , $location, Rest, GetBasePath, ProcessErrors, jobStatusGraphData) {
return function (params) { return function (params) {
var scope = params.scope, var scope = params.scope,
target = params.target, target = params.target,
// dashboard = params.dashboard, data = params.data,
html, element, url, job_status_chart, html, element, url, job_status_chart,
period="month", period="month",
job_type="all"; job_type="all";
@@ -65,29 +65,16 @@ angular.module('JobStatusGraphWidget', ['RestServices', 'Utilities'])
// html += "</div>\n"; // html += "</div>\n";
function createGraph(){ scope.$on('DataReceived:JobStatusGraph',
function(data) {
url = GetBasePath('dashboard')+'graphs/jobs/?period='+period+'&job_type='+job_type;
Rest.setUrl(url);
Rest.get()
.success(function (data){
scope.$emit('graphDataReady', data); scope.$emit('graphDataReady', data);
return job_type, period; });
}) function createGraph(period, jobtype){
.error(function (data, status) { // console.log(jobStatusGraphData);
ProcessErrors(scope, data, status, null, { hdr: 'Error!', // jobStatusGraphData.get(period, jobtype);
msg: 'Failed to get: ' + url + ' GET returned: ' + status });
});
} }
if ($rootScope.removeReloadJobStatusGraph) {
$rootScope.removeReloadJobStatusGraph();
}
$rootScope.removeReloadJobStatusGraph = $rootScope.$on('ReloadJobStatusGraph', function() {
createGraph();
});
element = angular.element(document.getElementById(target)); element = angular.element(document.getElementById(target));
element.html(html); element.html(html);
$compile(element)(scope); $compile(element)(scope);
@@ -192,7 +179,7 @@ angular.module('JobStatusGraphWidget', ['RestServices', 'Utilities'])
period = this.getAttribute("id"); period = this.getAttribute("id");
$('#period-dropdown').replaceWith("<a id=\"period-dropdown\" role=\"button\" data-toggle=\"dropdown\" data-target=\"#\" href=\"/page.html\">"+this.text+"<span class=\"caret\"><span>\n"); $('#period-dropdown').replaceWith("<a id=\"period-dropdown\" role=\"button\" data-toggle=\"dropdown\" data-target=\"#\" href=\"/page.html\">"+this.text+"<span class=\"caret\"><span>\n");
createGraph(); createGraph(period, job_type);
}); });
//On click, update with new data //On click, update with new data
@@ -201,7 +188,8 @@ angular.module('JobStatusGraphWidget', ['RestServices', 'Utilities'])
job_type = this.getAttribute("id"); job_type = this.getAttribute("id");
$('#type-dropdown').replaceWith("<a id=\"type-dropdown\" role=\"button\" data-toggle=\"dropdown\" data-target=\"#\" href=\"/page.html\">"+this.text+"<span class=\"caret\"><span>\n"); $('#type-dropdown').replaceWith("<a id=\"type-dropdown\" role=\"button\" data-toggle=\"dropdown\" data-target=\"#\" href=\"/page.html\">"+this.text+"<span class=\"caret\"><span>\n");
createGraph(); data
createGraph(period, job_type);
}); });
scope.$emit('WidgetLoaded'); scope.$emit('WidgetLoaded');
@@ -214,6 +202,8 @@ angular.module('JobStatusGraphWidget', ['RestServices', 'Utilities'])
}); });
scope.$emit('graphDataReady', data);
}; };
} }
]); ]);

View File

@@ -8,12 +8,16 @@ describe('Job Status Graph Data Service', function() {
$on: sinon.spy(), $on: sinon.spy(),
}; };
var getBasePath = function(path) {
return '/' + path + '/';
}
function flushPromises() { function flushPromises() {
window.setTimeout(function() { window.setTimeout(function() {
inject(function($rootScope) { inject(function($rootScope) {
$rootScope.$apply(); $rootScope.$apply();
}); });
}, 100); });
} }
var restStub = { var restStub = {
@@ -42,7 +46,8 @@ describe('Job Status Graph Data Service', function() {
$provide.value("$cookieStore", { get: angular.noop }); $provide.value("$cookieStore", { get: angular.noop });
$provide.value('RestServices', restStub); $provide.value('Rest', restStub);
$provide.value('GetBasePath', getBasePath);
})); }));
afterEach(function() { afterEach(function() {
@@ -99,4 +104,12 @@ describe('Job Status Graph Data Service', function() {
return expect(result.promise).to.eventually.equal(expected); 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');
});
}); });