bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/web/static/js/state.ts (about) 1 interface AckGroupScope extends ng.IScope { 2 groups: CheckedStateGroup[]; 3 } 4 5 // StateGroup with an added property if checked 6 interface CheckedStateGroup extends StateGroup { 7 checked: boolean; 8 } 9 10 11 bosunApp.directive('tsAckGroup', ['$location', '$timeout', ($location: ng.ILocationService, $timeout: ng.ITimeoutService) => { 12 return { 13 scope: { 14 ack: '=', 15 groups: '=tsAckGroup', 16 schedule: '=', 17 timeanddate: '=', 18 }, 19 templateUrl: '/partials/ackgroup.html', 20 link: (scope: AckGroupScope, elem: any, attrs: any) => { 21 scope.canAckSelected = scope.ack == 'Needs Acknowledgement'; 22 scope.panelClass = scope.$parent.panelClass; 23 24 scope.btoa = scope.$parent.btoa; 25 scope.encode = scope.$parent.encode; 26 scope.shown = {}; 27 scope.collapse = (i: any) => { 28 scope.shown[i] = !scope.shown[i]; 29 30 if (scope.shown[i] && scope.groups[i].Children.length == 1) { 31 $timeout(function () { 32 scope.$broadcast("onOpen", i); 33 }, 0); 34 } 35 }; 36 scope.click = ($event: any, idx: number) => { 37 scope.collapse(idx); 38 if ($event.shiftKey && scope.schedule.checkIdx != undefined) { 39 var checked = scope.groups[scope.schedule.checkIdx].checked; 40 var start = Math.min(idx, scope.schedule.checkIdx); 41 var end = Math.max(idx, scope.schedule.checkIdx); 42 for (var i = start; i <= end; i++) { 43 if (i == idx) { 44 continue; 45 } 46 scope.groups[i].checked = checked; 47 } 48 } 49 scope.schedule.checkIdx = idx; 50 scope.update(); 51 }; 52 scope.select = (checked: boolean) => { 53 for (var i = 0; i < scope.groups.length; i++) { 54 scope.groups[i].checked = checked; 55 } 56 scope.update(); 57 }; 58 scope.update = () => { 59 scope.canCloseSelected = true; 60 scope.canForgetSelected = true; 61 scope.anySelected = false; 62 for (var i = 0; i < scope.groups.length; i++) { 63 var g = scope.groups[i]; 64 if (!g.checked) { 65 continue; 66 } 67 scope.anySelected = true; 68 if (g.Status == 'error') { 69 scope.canCloseSelected = false; 70 } 71 if (g.Status != 'unknown') { 72 scope.canForgetSelected = false; 73 } 74 } 75 }; 76 scope.multiaction = (type: string) => { 77 var keys = []; 78 var active = false; 79 angular.forEach(scope.groups, (group) => { 80 if (!group.checked) { 81 return; 82 } 83 if (group.AlertKey) { 84 if (group.State.CurrentStatus != 'normal') { 85 active = true; 86 } 87 keys.push(group.AlertKey); 88 } 89 angular.forEach(group.Children, (child) => { 90 if (child.State.CurrentStatus != 'normal') { 91 active = true; 92 } 93 keys.push(child.AlertKey); 94 }); 95 }); 96 scope.$parent.setKey("action-keys", keys); 97 $location.path("action"); 98 $location.search("type", type); 99 $location.search("active", active ? 'true' : 'false'); 100 }; 101 scope.history = () => { 102 var url = '/history?'; 103 angular.forEach(scope.groups, (group) => { 104 if (!group.checked) { 105 return; 106 } 107 if (group.AlertKey) { 108 url += '&key=' + encodeURIComponent(group.AlertKey); 109 } 110 angular.forEach(group.Children, (child) => { 111 url += '&key=' + encodeURIComponent(child.AlertKey); 112 }); 113 }); 114 return url; 115 }; 116 }, 117 }; 118 }]); 119 120 bosunApp.directive('tsState', ['$sce', '$http', function ($sce: ng.ISCEService, $http: ng.IHttpService) { 121 return { 122 templateUrl: '/partials/alertstate.html', 123 link: function (scope: any, elem: any, attrs: any) { 124 var myIdx = attrs["tsGrp"]; 125 scope.currentStatus = attrs["tsGrpstatus"] 126 scope.name = scope.child.AlertKey; 127 scope.state = scope.child.State; 128 scope.action = (type: string) => { 129 var key = encodeURIComponent(scope.name); 130 var active = scope.state.CurrentStatus != 'normal'; 131 return '/action?type=' + type + '&key=' + key + '&active=' + active; 132 }; 133 var loadedBody = false; 134 scope.toggle = () => { 135 scope.show = !scope.show; 136 if (scope.show && !loadedBody) { 137 scope.state.Body = "loading..."; 138 loadedBody = true; 139 $http.get('/api/status?ak=' + scope.child.AlertKey) 140 .success(data => { 141 var body = data[scope.child.AlertKey].Body; 142 scope.state.Body = $sce.trustAsHtml(body); 143 }) 144 .error(err => { 145 scope.state.Body = "Error loading template body: " + err; 146 }); 147 } 148 }; 149 scope.$on('onOpen', function (e, i) { 150 if (i == myIdx) { 151 scope.toggle(); 152 } 153 }); 154 scope.zws = (v: string) => { 155 if (!v) { 156 return ''; 157 } 158 return v.replace(/([,{}()])/g, '$1\u200b'); 159 }; 160 scope.state.Touched = moment(scope.state.Touched).utc(); 161 angular.forEach(scope.state.Events, (v, k) => { 162 v.Time = moment(v.Time).utc(); 163 }); 164 scope.state.last = scope.state.Events[scope.state.Events.length - 1]; 165 if (scope.state.Actions && scope.state.Actions.length > 0) { 166 scope.state.LastAction = scope.state.Actions[scope.state.Actions.length - 1]; 167 } 168 scope.state.RuleUrl = '/config?' + 169 'alert=' + encodeURIComponent(scope.state.Alert) + 170 '&fromDate=' + encodeURIComponent(scope.state.last.Time.format("YYYY-MM-DD")) + 171 '&fromTime=' + encodeURIComponent(scope.state.last.Time.format("HH:mm")); 172 var groups: string[] = []; 173 angular.forEach(scope.state.Group, (v, k) => { 174 groups.push(k + "=" + v); 175 }); 176 if (groups.length > 0) { 177 scope.state.RuleUrl += '&template_group=' + encodeURIComponent(groups.join(',')); 178 } 179 scope.state.Body = $sce.trustAsHtml(scope.state.Body); 180 }, 181 }; 182 }]); 183 184 bosunApp.directive('tsNote', () => { 185 return { 186 restrict: 'E', 187 templateUrl: '/partials/note.html', 188 }; 189 }); 190 191 192 bosunApp.directive('tsAck', () => { 193 return { 194 restrict: 'E', 195 templateUrl: '/partials/ack.html', 196 }; 197 }); 198 199 bosunApp.directive('tsClose', () => { 200 return { 201 restrict: 'E', 202 templateUrl: '/partials/close.html', 203 }; 204 }); 205 206 bosunApp.directive('tsCancelClose', () => { 207 return { 208 restrict: 'E', 209 templateUrl: '/partials/cancelClose.html', 210 }; 211 }); 212 213 bosunApp.directive('tsForget', () => { 214 return { 215 restrict: 'E', 216 templateUrl: '/partials/forget.html', 217 }; 218 }); 219 220 bosunApp.directive('tsPurge', () => { 221 return { 222 restrict: 'E', 223 templateUrl: '/partials/purge.html', 224 }; 225 }); 226 227 bosunApp.directive('tsForceClose', () => { 228 return { 229 restrict: 'E', 230 templateUrl: '/partials/forceClose.html', 231 }; 232 });