bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/web/static/js/silence.ts (about)

     1  
     2  /// <reference path="0-bosun.ts" />
     3  interface ISilenceScope extends ng.IScope {
     4  	silences: any;
     5  	error: string;
     6  	start: string;
     7  	end: string;
     8  	duration: string;
     9  	alert: string;
    10  	hosts: string;
    11  	tags: string;
    12  	edit: string;
    13  	testSilences: any;
    14  	test: () => void;
    15  	confirm: () => void;
    16  	clear: (id: string) => void;
    17  	change: () => void;
    18  	disableConfirm: boolean;
    19  	time: (v: any) => string;
    20  	forget: string;
    21  	user: string;
    22  	message: string;
    23  	getEditSilenceLink: (silence: any, silenceId: string) => string;
    24  }
    25  
    26  bosunControllers.controller('SilenceCtrl', ['$scope', '$http', '$location', '$route', 'linkService', function($scope: ISilenceScope, $http: ng.IHttpService, $location: ng.ILocationService, $route: ng.route.IRouteService, linkService: ILinkService) {
    27  	var search = $location.search();
    28  	$scope.start = search.start;
    29  	$scope.end = search.end;
    30  	$scope.duration = search.duration;
    31  	$scope.alert = search.alert;
    32  	$scope.hosts = search.hosts;
    33  	$scope.tags = search.tags;
    34  	$scope.edit = search.edit;
    35  	$scope.forget = search.forget;
    36  	$scope.message = search.message;
    37  	if (!$scope.end && !$scope.duration) {
    38  		$scope.duration = '1h';
    39  	}
    40  	function filter(data: any[], startBefore: any, startAfter: any, endAfter: any, endBefore: any, limit: number) {
    41  		var ret = {};
    42  		var count = 0;
    43  		_.each(data, function(v,name) {
    44  			if (limit && count >= limit){
    45  				return
    46  			}
    47  			var s = moment(v.Start).utc();
    48  			var e = moment(v.End).utc();
    49  			if (startBefore && s > startBefore) {
    50  				return;
    51  			}
    52  			if (startAfter && s< startAfter) {
    53  				return;
    54  			}
    55  			if (endAfter && e < endAfter) {
    56  				return;
    57  			}
    58  			if (endBefore && e > endBefore) {
    59  				return;
    60  			}
    61  			ret[name] = v;
    62  		});
    63  		return ret;
    64  	}
    65  	function get() {
    66  		$http.get('/api/silence/get')
    67  			.success((data: any) => {
    68  				$scope.silences = [];
    69  				var now = moment.utc();
    70  				$scope.silences.push({
    71  					name: 'Active',
    72  					silences: filter(data, now, null, now, null, 0)
    73  				});
    74  				$scope.silences.push({
    75  					name: 'Upcoming',
    76  					silences: filter(data, null, now, null, null, 0)
    77  				});
    78  				$scope.silences.push({
    79  					name: 'Past',
    80  					silences: filter(data, null, null, null, now, 25)
    81  				});
    82  			})
    83  			.error((error) => {
    84  				$scope.error = error;
    85  			});
    86  	}
    87  	get();
    88  	function getData() {
    89  		var tags = ($scope.tags || '').split(',');
    90  		if ($scope.hosts) {
    91  			tags.push('host=' + $scope.hosts.split(/[ ,|]+/).join('|'));
    92  		}
    93  		tags = tags.filter((v) => { return v != ""; });
    94  		var data: any = {
    95  			start: $scope.start,
    96  			end: $scope.end,
    97  			duration: $scope.duration,
    98  			alert: $scope.alert,
    99  			tags: tags.join(','),
   100  			edit: $scope.edit,
   101  			forget: $scope.forget ? 'true' : null,
   102  			message: $scope.message,
   103  		};
   104  		return data;
   105  	}
   106  	var any = search.start || search.end || search.duration || search.alert || search.hosts || search.tags || search.forget;
   107  	var state = getData();
   108  	$scope.change = () => {
   109  		$scope.disableConfirm = true;
   110  	};
   111  	if (any) {
   112  		$scope.error = null;
   113  		$http.post('/api/silence/set', state)
   114  			.success((data) => {
   115  				if (!data) {
   116  					data = {'(none)': false};
   117  				}
   118  				$scope.testSilences = data;
   119  			})
   120  			.error((error) => {
   121  				$scope.error = error;
   122  			});
   123  	}
   124  	$scope.test = () => {
   125  		$location.search('start', $scope.start || null);
   126  		$location.search('end', $scope.end || null);
   127  		$location.search('duration', $scope.duration || null);
   128  		$location.search('alert', $scope.alert || null);
   129  		$location.search('hosts', $scope.hosts || null);
   130  		$location.search('tags', $scope.tags || null);
   131  		$location.search('forget', $scope.forget || null);
   132  		$location.search('message', $scope.message || null);
   133  		$route.reload();
   134  	};
   135  	$scope.confirm = () => {
   136  		$scope.error = null;
   137  		$scope.testSilences = null;
   138  		$scope.edit = null;
   139  		$location.search('edit', null);
   140  		state.confirm = 'true';
   141  		$http.post('/api/silence/set', state)
   142  			.error((error) => {
   143  				$scope.error = error;
   144  			})
   145  			.finally(get);
   146  	};
   147  	$scope.clear = (id: string) => {
   148  		if (!window.confirm('Clear this silence?')) {
   149  			return;
   150  		}
   151  		$scope.error = null;
   152  		$http.post('/api/silence/clear?id=' + id, {} )
   153  			.error((error) => {
   154  				$scope.error = error;
   155  			})
   156  			.finally(get);
   157  	};
   158  	$scope.time = (v: any) => {
   159  		var m = moment(v).utc();
   160  		return m.format();
   161  	};
   162  	$scope.getEditSilenceLink = (silence: any, silenceId: string) => {
   163  		return linkService.GetEditSilenceLink(silence, silenceId);
   164  	};
   165  }]);