alertmanager/ui/app/js/app.js

403 lines
8.5 KiB
JavaScript
Raw Normal View History

2015-10-12 15:08:07 +00:00
'use strict';
angular.module('am.directives', []);
angular.module('am.directives').directive('route',
function(RecursionHelper) {
return {
restrict: 'E',
scope: {
route: '='
},
templateUrl: '/app/partials/route.html',
compile: function(element) {
// Use the compile function from the RecursionHelper,
// And return the linking function(s) which it returns
return RecursionHelper.compile(element);
}
};
}
);
angular.module('am.directives').directive('alert',
function() {
return {
restrict: 'E',
scope: {
2015-11-09 10:09:32 +00:00
alert: '=',
2015-11-09 11:03:36 +00:00
group: '='
},
templateUrl: '/app/partials/alert.html'
};
}
);
angular.module('am.directives').directive('silence',
function() {
return {
restrict: 'E',
scope: {
sil: '='
},
templateUrl: '/app/partials/silence.html'
};
}
);
angular.module('am.directives').directive('silenceForm',
function() {
return {
restrict: 'E',
scope: {
silence: '='
},
templateUrl: '/app/partials/silence-form.html'
};
}
);
2015-10-12 15:08:07 +00:00
angular.module('am.services', ['ngResource']);
angular.module('am.services').factory('Silence',
function($resource) {
return $resource('', {
id: '@id'
}, {
'query': {
method: 'GET',
url: '/api/v1/silences'
},
'create': {
method: 'POST',
url: '/api/v1/silences'
},
'get': {
method: 'GET',
url: '/api/v1/silence/:id'
},
'delete': {
method: 'DELETE',
url: '/api/v1/silence/:id'
}
2015-10-12 15:08:07 +00:00
});
}
);
2015-10-15 14:24:27 +00:00
angular.module('am.services').factory('Alert',
function($resource) {
2015-10-15 14:24:27 +00:00
return $resource('', {}, {
'query': {
method: 'GET',
url: '/api/v1/alerts'
}
2015-10-15 14:24:27 +00:00
});
}
);
angular.module('am.services').factory('AlertGroups',
function($resource) {
return $resource('', {}, {
'query': {
method: 'GET',
url: '/api/v1/alerts/groups'
}
});
}
);
angular.module('am.services').factory('Alert',
function($resource) {
return $resource('', {}, {
'query': {
method: 'GET',
url: '/api/v1/alerts'
}
});
}
);
2015-10-12 15:08:07 +00:00
angular.module('am.controllers', []);
2015-10-15 10:01:19 +00:00
angular.module('am.controllers').controller('NavCtrl',
function($scope, $location) {
$scope.items = [{
name: 'Silences',
url: '/silences'
}, {
name: 'Alerts',
url: '/alerts'
}, {
name: 'Status',
url: '/status'
}];
2015-10-15 10:01:19 +00:00
$scope.selected = function(item) {
return item.url == $location.path()
}
2015-10-15 10:01:19 +00:00
}
);
2015-10-22 08:46:52 +00:00
angular.module('am.controllers').controller('AlertCtrl',
function($scope) {
$scope.showDetails = false;
$scope.toggleDetails = function() {
$scope.showDetails = !$scope.showDetails
}
$scope.showSilenceForm = false;
$scope.toggleSilenceForm = function() {
$scope.showSilenceForm = !$scope.showSilenceForm
}
$scope.silence = {
matchers: []
}
2015-11-09 10:09:32 +00:00
angular.forEach($scope.alert.labels, function(value, key) {
this.push({
name: key,
value: value,
2015-11-09 10:09:32 +00:00
isRegex: false
});
}, $scope.silence.matchers);
2015-11-06 16:35:37 +00:00
$scope.$on('silence-created', function(evt) {
$scope.toggleSilenceForm();
});
}
2015-10-22 08:46:52 +00:00
);
2015-10-15 14:24:27 +00:00
angular.module('am.controllers').controller('AlertsCtrl',
function($scope, AlertGroups) {
$scope.groups = null;
2015-11-10 12:47:04 +00:00
$scope.allReceivers = [];
2015-10-15 14:24:27 +00:00
$scope.notEmpty = function(group) {
var l = 0;
angular.forEach(group.blocks, function(blk) {
if ($scope.receivers.indexOf(blk.routeOpts.receiver) >= 0) {
l += blk.alerts.length || 0;
}
});
return l > 0;
};
2015-10-15 14:24:27 +00:00
$scope.refresh = function() {
AlertGroups.query({},
2015-10-15 14:24:27 +00:00
function(data) {
$scope.groups = data.data;
2015-11-09 10:09:32 +00:00
2015-11-10 12:47:04 +00:00
$scope.allReceivers = [];
2015-11-09 10:09:32 +00:00
angular.forEach($scope.groups, function(group) {
angular.forEach(group.blocks, function(blk) {
if ($scope.allReceivers.indexOf(blk.routeOpts.receiver) < 0) {
$scope.allReceivers.push(blk.routeOpts.receiver);
2015-11-09 10:09:32 +00:00
}
})
});
2015-11-10 12:47:04 +00:00
if (!$scope.receivers) {
$scope.receivers = angular.copy($scope.allReceivers);
2015-11-09 10:09:32 +00:00
}
2015-10-15 14:24:27 +00:00
},
function(data) {
2015-11-06 13:14:46 +00:00
$scope.error = data.data;
2015-10-15 14:24:27 +00:00
}
);
}
$scope.refresh();
}
);
2015-11-06 16:07:55 +00:00
angular.module('am.controllers').controller('SilenceCtrl',
2015-11-09 14:30:02 +00:00
function($scope, $location, Silence) {
2015-11-06 16:07:55 +00:00
2015-11-09 14:30:02 +00:00
$scope.highlight = $location.search()['hl'] == $scope.sil.id;
2015-11-16 20:45:28 +00:00
$scope.showDetails = false;
2015-11-16 20:45:28 +00:00
$scope.showSilenceForm = false;
2015-11-16 20:45:28 +00:00
$scope.toggleSilenceForm = function() {
$scope.showSilenceForm = !$scope.showSilenceForm
}
$scope.toggleDetails = function() {
$scope.showDetails = !$scope.showDetails
}
2015-11-16 20:45:28 +00:00
var silCopy = angular.copy($scope.sil);
$scope.delete = function(id) {
Silence.delete({id: id},
2015-11-06 16:07:55 +00:00
function(data) {
2015-11-06 16:35:37 +00:00
$scope.$emit('silence-deleted');
2015-11-06 16:07:55 +00:00
},
function(data) {
$scope.error = data.data;
});
};
2015-11-16 20:45:28 +00:00
$scope.$on('silence-created', function(evt) {
$scope.delete(silCopy.id);
});
2015-11-06 16:07:55 +00:00
}
);
2015-10-12 15:08:07 +00:00
angular.module('am.controllers').controller('SilencesCtrl',
function($scope, Silence) {
$scope.silences = [];
2015-11-06 16:07:55 +00:00
$scope.order = "endsAt";
2015-10-12 20:22:21 +00:00
2015-11-09 14:19:22 +00:00
$scope.showForm = false;
$scope.toggleForm = function() {
$scope.showForm = !$scope.showForm
}
2015-10-12 20:22:21 +00:00
$scope.refresh = function() {
Silence.query({},
2015-11-06 16:07:55 +00:00
function(data) {
2015-10-12 20:22:21 +00:00
$scope.silences = data.data || [];
2015-11-06 16:07:55 +00:00
angular.forEach($scope.silences, function(value) {
value.endsAt = new Date(value.endsAt);
value.startsAt = new Date(value.startsAt);
value.createdAt = new Date(value.createdAt);
});
},
function(data) {
2015-11-02 17:51:54 +00:00
$scope.error = data.data;
2015-10-12 20:22:21 +00:00
}
);
2015-11-02 17:51:54 +00:00
};
2015-10-12 20:22:21 +00:00
2015-11-06 13:14:46 +00:00
$scope.$on('silence-created', function(evt) {
$scope.refresh();
});
2015-11-06 16:07:55 +00:00
$scope.$on('silence-deleted', function(evt) {
$scope.refresh();
});
2015-11-06 13:14:46 +00:00
2015-11-10 10:19:04 +00:00
$scope.elapsed = function(elapsed) {
return function(sil) {
if (elapsed) {
return sil.endsAt <= new Date;
}
return sil.endsAt > new Date;
}
};
2015-10-12 20:22:21 +00:00
$scope.refresh();
}
);
angular.module('am.controllers').controller('SilenceCreateCtrl',
2015-11-06 16:35:37 +00:00
function($scope, Silence) {
2015-10-12 15:08:07 +00:00
$scope.error = null;
$scope.silence = $scope.silence || {};
if (!$scope.silence.matchers) {
2015-11-16 13:47:03 +00:00
$scope.silence.matchers = [{}];
}
2015-11-06 13:14:46 +00:00
var origSilence = angular.copy($scope.silence);
2015-11-06 13:14:46 +00:00
2015-10-15 14:24:27 +00:00
$scope.reset = function() {
var now = new Date();
var end = new Date();
2015-10-15 14:24:27 +00:00
now.setMilliseconds(0);
end.setMilliseconds(0);
now.setSeconds(0);
end.setSeconds(0);
2015-11-06 13:14:46 +00:00
end.setHours(end.getHours() + 4)
$scope.silence = angular.copy(origSilence);
$scope.silence.startsAt = now;
$scope.silence.endsAt = end;
2015-11-02 17:51:54 +00:00
};
2015-10-15 14:24:27 +00:00
$scope.reset();
$scope.addMatcher = function() {
$scope.silence.matchers.push({});
};
$scope.delMatcher = function(i) {
$scope.silence.matchers.splice(i, 1);
};
$scope.create = function() {
Silence.create($scope.silence,
function(data) {
2015-11-06 16:35:37 +00:00
$scope.$emit('silence-created');
$scope.reset();
},
function(data) {
$scope.error = data.data;
}
);
};
2015-10-12 15:08:07 +00:00
}
);
angular.module('am.services').factory('Status',
function($resource) {
return $resource('', {}, {
'get': {
method: 'GET',
url: '/api/v1/status'
}
});
}
);
angular.module('am.controllers').controller('StatusCtrl',
function($scope, Status) {
2015-11-02 18:47:12 +00:00
Status.get({},
function(data) {
$scope.config = data.data.config;
$scope.versionInfo = data.data.versionInfo;
2015-11-02 19:04:37 +00:00
$scope.uptime = data.data.uptime;
2015-11-02 18:47:12 +00:00
},
function(data) {
console.log(data.data);
})
}
);
2015-10-12 15:08:07 +00:00
angular.module('am', [
'ngRoute',
2015-11-09 12:32:41 +00:00
'ngSanitize',
2015-11-06 16:07:55 +00:00
'angularMoment',
2015-10-12 15:08:07 +00:00
'am.controllers',
'am.services',
'am.directives'
2015-10-12 15:08:07 +00:00
]);
angular.module('am').config(
function($routeProvider) {
$routeProvider.
when('/alerts', {
templateUrl: '/app/partials/alerts.html',
controller: 'AlertsCtrl'
}).
when('/silences', {
templateUrl: '/app/partials/silences.html',
controller: 'SilencesCtrl'
}).
2015-11-02 18:47:12 +00:00
when('/status', {
templateUrl: '/app/partials/status.html',
controller: 'StatusCtrl'
}).
otherwise({
redirectTo: '/alerts'
});
2015-10-12 15:08:07 +00:00
}
);