Merge pull request #12 from Freddrickk/filters

Add a few features and some unit tests
This commit is contained in:
Frédéric Vachon 2015-02-02 16:38:06 -05:00
commit b7af587212
14 changed files with 133 additions and 26 deletions

View File

@ -57,7 +57,8 @@ module.exports = function (grunt) {
nomen: true, nomen: true,
predef: [ // Global variables predef: [ // Global variables
'angular', 'inject', 'JustGage', 'angular', 'inject', 'JustGage',
'describe', 'beforeEach', 'it', 'expect' 'describe', 'beforeEach', 'it', 'expect',
'moment'
] ]
}, },
options: { options: {

View File

@ -5,7 +5,8 @@ angular.module('adagios', [
'adagios.sidebar', 'adagios.sidebar',
'adagios.topbar', 'adagios.topbar',
'adagios.tactical', 'adagios.tactical',
'adagios.table' 'adagios.table',
'adagios.filters'
]) ])
.config(['$routeProvider', function ($routeProvider) { .config(['$routeProvider', function ($routeProvider) {

View File

@ -0,0 +1,9 @@
'use strict';
angular.module('adagios.filters', [])
.filter('timeElapsed', [function () {
return function (input) {
return moment.unix(input).fromNow();
};
}]);

View File

@ -2,12 +2,41 @@
angular.module('adagios.live') angular.module('adagios.live')
.factory('GetServices', ['$http', function ($http, columns) { .constant('filterSuffixes', { contains: '__contains',
has_fields: '__has_field',
startswith: '__startswith',
endswith: '__endswith',
exists: '__exists',
in: '__in',
regex: '__regex'
})
return function (columns) { .factory('getServices', ['$http', 'filterSuffixes',
return $http.get('/rest/status/json/services/?fields=' + columns) function ($http, filterSuffixes, columns, filters) {
.error(function (data, status, headers, config) { return function (columns, filters) {
console.error('GetServices : GET Request failed'); var filtersQuery = '';
});
}; function createFiltersQuery(filters) {
}]); var builtQuery = '';
angular.forEach(filters, function (value, key) {
var filterType = filterSuffixes[key];
angular.forEach(value, function (fieldValues, fieldName) {
var filter = fieldName + filterType;
angular.forEach(fieldValues, function (_value) {
var filterQuery = '&' + filter + '=' + _value;
builtQuery += filterQuery;
});
});
});
return builtQuery;
}
filtersQuery = createFiltersQuery(filters);
return $http.get('/rest/status/json/services/?fields=' + columns + filtersQuery)
.error(function (data, status, headers, config) {
console.error('getServices : GET Request failed');
});
};
}]);

View File

@ -0,0 +1,24 @@
'use strict';
describe('In Adagios Live', function () {
var $httpBackend;
beforeEach(module('adagios.live'));
beforeEach(inject(function (_$httpBackend_) {
$httpBackend = _$httpBackend_;
}));
describe('getServices', function () {
it('should send the proper GET request', inject(function (getServices) {
var fields = ['host_name', 'host_state', 'description'],
filters = { contains: { host_name: ['srv', 'a'], plugin_output: ['SWAP'] },
startswith: { host_name: ['srv'] } };
getServices(fields, filters);
$httpBackend.expectGET('/rest/status/json/services/?fields=host_name,host_state,description&host_name__contains=srv&host_name__contains=a&plugin_output__contains=SWAP&host_name__startswith=srv').respond('');
$httpBackend.flush();
}));
});
});

View File

@ -2,7 +2,7 @@
angular.module('adagios.live') angular.module('adagios.live')
.factory('GetProblems', ['$http', function ($http) { .factory('getProblems', ['$http', function ($http) {
var problem_number = 44; var problem_number = 44;
return problem_number; return problem_number;

View File

@ -17,12 +17,15 @@
<!-- Application --> <!-- Application -->
<script src="app.js"></script> <script src="app.js"></script>
<!-- COMPONENTS --> <!-- COMPONENTS -->
<script src="components/live/live.js"></script> <script src="components/live/live.js"></script>
<script src="components/live/notifications.js"></script> <script src="components/live/notifications.js"></script>
<script src="components/live/get_services.js"></script> <script src="components/live/get_services.js"></script>
<script src="components/ng-justgage/ng-justgage.js"></script> <script src="components/ng-justgage/ng-justgage.js"></script>
<script src="components/filters/filters.js"></script>
<!-- MODULES --> <!-- MODULES -->
<script src="sidebar/sidebar.js"></script> <script src="sidebar/sidebar.js"></script>
@ -36,6 +39,7 @@
<script src="table/table.js"></script> <script src="table/table.js"></script>
<script src="bower_components/html5-boilerplate/js/vendor/modernizr-2.6.2.min.js"></script> <script src="bower_components/html5-boilerplate/js/vendor/modernizr-2.6.2.min.js"></script>
<script src="bower_components/moment/moment.js"></script>
</head> </head>
<body class="layout color-scheme--dark"> <body class="layout color-scheme--dark">

View File

@ -3,9 +3,8 @@
angular.module('adagios.sidebar', []) angular.module('adagios.sidebar', [])
.controller('SideBarCtrl', ['$scope', '$http', function ($scope, $http) { .controller('SideBarCtrl', ['$scope', '$http', function ($scope, $http) {
return; angular.noop();
}])
}])
.directive('adgSidebar', function () { .directive('adgSidebar', function () {
return { return {

View File

@ -1 +1 @@
<div>{{entry.last_state_change}}</div> <div>{{entry.last_state_change | timeElapsed}}</div>

View File

@ -1 +1 @@
<div>{{entry.last_check}}</div> <div>{{entry.last_check * 1000 | date: medium}}</div>

View File

@ -4,27 +4,28 @@ angular.module('adagios.table', ['ngRoute',
'adagios.live' 'adagios.live'
]) ])
.controller('TableCtrl', ['$scope', 'GetServices', function ($scope, GetServices) { .controller('TableCtrl', ['$scope', 'getServices', function ($scope, getServices) {
var requestFields = []; var requestFields = [],
filters = {};
$scope.cells = ['host', 'service_check', 'duration', 'last_check'];
// The module directory name must be cell_ + key // The module directory name must be cell_ + key
$scope.cellToFieldsMap = { $scope.cellToFieldsMap = {
host: [ 'host_state', 'host_name' ], host: [ 'host_state', 'host_name' ],
service_check: ['state', 'description', 'plugin_output' ], service_check: ['state', 'description', 'plugin_output'],
duration: ['last_state_change'], duration: ['last_state_change'],
last_check: ['last_check'] last_check: ['last_check']
}; };
$scope.cells = ['host', 'service_check', 'duration', 'last_check'];
angular.forEach($scope.cells, function (key, value) { angular.forEach($scope.cells, function (key, value) {
angular.forEach($scope.cellToFieldsMap[key], function (_value) { angular.forEach($scope.cellToFieldsMap[key], function (_value) {
requestFields.push(_value); requestFields.push(_value);
}); });
}); });
new GetServices(requestFields) getServices(requestFields, filters)
.success(function (data) { .success(function (data) {
$scope.entries = data; $scope.entries = data;
}); });
@ -33,7 +34,6 @@ angular.module('adagios.table', ['ngRoute',
.directive('adgTable', function () { .directive('adgTable', function () {
return { return {
restrict: 'E', restrict: 'E',
replace: true,
templateUrl: 'table/table.html' templateUrl: 'table/table.html'
}; };
}) })

39
app/table/table_test.js Normal file
View File

@ -0,0 +1,39 @@
'use strict';
describe('In Table module', function () {
var $compile,
$rootScope,
$httpBackend;
beforeEach(module('adagios.table'));
beforeEach(inject(function (_$compile_, _$rootScope_, _$httpBackend_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$httpBackend = _$httpBackend_;
}));
describe('adgCell directive', function () {
it('should send a get request to the proper cell template', function () {
var cells = ['host', 'service_check', 'duration', 'last_check'];
angular.forEach(cells, function (cell) {
var elem = angular.element('<adg-cell type="' + cell + '"></adg-cell>');
$compile(elem)($rootScope);
$httpBackend.expectGET('table/cell_' + cell + '/cell_' + cell + '.html').respond('');
$httpBackend.flush();
});
});
});
describe('adgTable directive', function () {
it('should request table/table.html template', function () {
var elem = angular.element('<adg-table></adg-table>');
$compile(elem)($rootScope);
$httpBackend.expectGET('table/table.html').respond('');
$httpBackend.flush();
});
});
});

View File

@ -2,8 +2,8 @@
angular.module('adagios.topbar', ['adagios.live']) angular.module('adagios.topbar', ['adagios.live'])
.controller('TopBarCtrl', ['$scope', '$http', 'GetProblems', function ($scope, $http, GetProblems) { .controller('TopBarCtrl', ['$scope', '$http', 'getProblems', function ($scope, $http, getProblems) {
$scope.notifications = GetProblems; $scope.notifications = getProblems;
}]) }])
.directive('adgTopbar', function () { .directive('adgTopbar', function () {

View File

@ -13,6 +13,7 @@
"html5-boilerplate": "~4.3.0", "html5-boilerplate": "~4.3.0",
"bootstrap-sass-official": "3.3.1", "bootstrap-sass-official": "3.3.1",
"fontawesome": "4.2.0", "fontawesome": "4.2.0",
"justgage-toorshia" : "master" "justgage-toorshia": "master",
"moment": "~2.9.0"
} }
} }