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', {
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', {

View File

@ -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'
];
];

View File

@ -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);
}
};

View File

@ -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 += "</div>\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("<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
@ -201,7 +188,8 @@ angular.module('JobStatusGraphWidget', ['RestServices', 'Utilities'])
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");
createGraph();
data
createGraph(period, job_type);
});
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(),
};
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');
});
});