alert_instances_controller.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. App.MainAlertInstancesController = Em.Controller.extend({
  20. name: 'mainAlertInstancesController',
  21. content: App.AlertInstance.find(),
  22. /**
  23. * @type {App.AlertInstance[]}
  24. */
  25. unhealthyAlertInstances: [],
  26. updateUnhealthyAlertInstances: function () {
  27. Em.run.once(this, this.updateUnhealthyAlertInstancesOnce);
  28. }.observes('content.[]'),
  29. updateUnhealthyAlertInstancesOnce: function() {
  30. var alertInstances = App.AlertInstance.find().filter(function (item) {
  31. return ['CRITICAL', 'WARNING'].contains(item.get('state'));
  32. });
  33. this.set('unhealthyAlertInstances', alertInstances);
  34. },
  35. /**
  36. * Are alertInstances loaded
  37. * @type {boolean}
  38. */
  39. isLoaded: false,
  40. /**
  41. * A flag to reload alert instances table every 10 seconds
  42. * @type {boolean}
  43. */
  44. reload: false,
  45. /**
  46. * Causes automatic updates of content if set to true
  47. * @type {boolean}
  48. */
  49. isUpdating: false,
  50. /**
  51. * Times for alert instances updater
  52. * Used in <code>scheduleUpdate</code>
  53. * @type {number|null}
  54. */
  55. updateTimer: null,
  56. /**
  57. * @type {string|null} sourceName - hostName or alertDefinitionId
  58. */
  59. sourceName: null,
  60. /**
  61. * @type {string|null} sourceType - 'HOST'|'ALERT_DEFINITION'
  62. */
  63. sourceType: null,
  64. /**
  65. * Load alert instances from server (all, for selected host, for selected alert definition)
  66. * @returns {$.ajax}
  67. * @method fetchAlertInstances
  68. */
  69. fetchAlertInstances: function () {
  70. var sourceType = this.get('sourceType'),
  71. sourceName = this.get('sourceName'),
  72. ajaxData = {
  73. sender: this,
  74. success: 'getAlertInstancesSuccessCallback',
  75. error: 'getAlertInstancesErrorCallback'
  76. };
  77. switch (sourceType) {
  78. case 'HOST':
  79. $.extend(ajaxData, {
  80. name: 'alerts.instances.by_host',
  81. data: {
  82. hostName: sourceName
  83. }
  84. });
  85. break;
  86. case 'ALERT_DEFINITION':
  87. $.extend(ajaxData, {
  88. name: 'alerts.instances.by_definition',
  89. data: {
  90. definitionId: sourceName
  91. }
  92. });
  93. break;
  94. default:
  95. $.extend(ajaxData, {
  96. name: 'alerts.instances'
  97. });
  98. break;
  99. }
  100. return App.ajax.send(ajaxData);
  101. },
  102. /**
  103. * Pseudo for <code>fetchAlertInstances</code>
  104. * Used to get all alert instances
  105. * @method loadAlertInstances
  106. */
  107. loadAlertInstances: function () {
  108. this.setProperties({
  109. isLoaded: false,
  110. sourceType: null,
  111. sourceName: null
  112. });
  113. this.fetchAlertInstances();
  114. },
  115. /**
  116. * Pseudo for <code>fetchAlertInstances</code>
  117. * Used to get alert instances for some host
  118. * @param {string} hostName
  119. * @method loadAlertInstancesByHost
  120. */
  121. loadAlertInstancesByHost: function (hostName) {
  122. this.setProperties({
  123. isLoaded: false,
  124. sourceType: 'HOST',
  125. sourceName: hostName
  126. });
  127. this.fetchAlertInstances();
  128. },
  129. /**
  130. * Pseudo for <code>fetchAlertInstances</code>
  131. * Used to get alert instances for some alert definition
  132. * @param {string} definitionId
  133. * @method loadAlertInstancesByAlertDefinition
  134. */
  135. loadAlertInstancesByAlertDefinition: function (definitionId) {
  136. this.setProperties({
  137. isLoaded: false,
  138. sourceType: 'ALERT_DEFINITION',
  139. sourceName: definitionId
  140. });
  141. this.fetchAlertInstances();
  142. },
  143. scheduleUpdate: function () {
  144. var self = this;
  145. if (this.get('isUpdating')) {
  146. this.set('updateTimer', setTimeout(function () {
  147. self.fetchAlertInstances().complete(function() {
  148. self.scheduleUpdate();
  149. });
  150. }, App.get('alertInstancesUpdateInterval')));
  151. }
  152. else {
  153. clearTimeout(this.get('updateTimer'));
  154. }
  155. }.observes('isUpdating'),
  156. /**
  157. * Success-callback for alert instances request
  158. * @param {object} json
  159. * @method getAlertInstancesSuccessCallback
  160. */
  161. getAlertInstancesSuccessCallback: function (json) {
  162. App.alertInstanceMapper.mapLocal(json);
  163. this.set('isLoaded', true);
  164. this.toggleProperty('reload');
  165. },
  166. /**
  167. * Error-callback for alert instances request
  168. * @method getAlertInstancesErrorCallback
  169. */
  170. getAlertInstancesErrorCallback: function () {
  171. this.set('isLoaded', true);
  172. },
  173. /**
  174. * Onclick handler for alerts number located right to bg ops number (see application.hbs)
  175. * @method showPopup
  176. * @return {App.ModalPopup}
  177. */
  178. showPopup: function () {
  179. var self = this;
  180. return App.ModalPopup.show({
  181. alertsNumberBinding: 'App.router.mainAlertDefinitionsController.unhealthyAlertInstancesCount',
  182. header: function () {
  183. return Em.I18n.t('alerts.fastAccess.popup.header').format(this.get('alertsNumber'));
  184. }.property('alertsNumber'),
  185. classNames: ['sixty-percent-width-modal', 'alerts-popup'],
  186. secondary: Em.I18n.t('alerts.fastAccess.popup.body.showmore'),
  187. autoHeight: false,
  188. isHideBodyScroll: true,
  189. onSecondary: function () {
  190. this._super();
  191. App.router.transitionTo('main.alerts.index');
  192. },
  193. bodyClass: App.TableView.extend(App.TableServerViewMixin, {
  194. updaterBinding: 'App.router.updateController',
  195. templateName: require('templates/common/modal_popups/alerts_popup'),
  196. controller: self,
  197. isPaginate: true,
  198. willInsertElement: function () {
  199. this._super();
  200. this.updateAlertInstances();
  201. },
  202. /**
  203. * Number of all critical and warning alert instances
  204. * @type {Boolean}
  205. */
  206. filteredCount: function () {
  207. return App.router.get('mainAlertDefinitionsController.unhealthyAlertInstancesCount');
  208. }.property('alertsNumber'),
  209. content: function () {
  210. return this.get('controller.unhealthyAlertInstances');
  211. }.property('controller.unhealthyAlertInstances.@each.state'),
  212. isLoaded: function () {
  213. return !!this.get('controller.unhealthyAlertInstances');
  214. }.property('controller.unhealthyAlertInstances'),
  215. isAlertEmptyList: function () {
  216. return !this.get('content.length');
  217. }.property('content.length'),
  218. /**
  219. * Update list of shown alert instances
  220. * @method updateAlertInstances
  221. */
  222. updateAlertInstances: function () {
  223. var self = this,
  224. displayLength = this.get('displayLength'),
  225. startIndex = this.get('startIndex');
  226. if (!displayLength) return; // wait while table-info is loaded
  227. this.get('updater').set('queryParamsForUnhealthyAlertInstances', {
  228. from: startIndex - 1,
  229. page_size: displayLength
  230. });
  231. this.set('filteringComplete', false);
  232. this.get('updater').updateUnhealthyAlertInstances(function() {
  233. self.set('filteringComplete', true);
  234. });
  235. }.observes('displayLength', 'startIndex'),
  236. /**
  237. * Show spinner when filter/sorting request is in processing
  238. * @method overlayObserver
  239. */
  240. overlayObserver: function() {
  241. var $tbody = this.$('#alert-info'),
  242. $overlay = this.$('.table-overlay'),
  243. $spinner = $($overlay).find('.spinner');
  244. if (!this.get('filteringComplete')) {
  245. if (!$tbody) return;
  246. var tbodyPos = $tbody.position();
  247. if (!tbodyPos) return;
  248. $spinner.css('display', 'block');
  249. $overlay.css({
  250. top: tbodyPos.top + 1,
  251. left: tbodyPos.left + 1,
  252. width: $tbody.width() - 1,
  253. height: $tbody.height() - 1
  254. });
  255. }
  256. },
  257. /**
  258. * No filtering for alert definitions
  259. * @method filter
  260. */
  261. filter: function() {
  262. this.set('filteredContent', this.get('content'));
  263. }.observes('content.length'),
  264. /**
  265. * Router transition to alert definition details page
  266. * @param event
  267. */
  268. gotoAlertDetails: function (event) {
  269. if (event && event.context) {
  270. this.get('parentView').hide();
  271. var definition = App.AlertDefinition.find().findProperty('id', event.context.get('definitionId'));
  272. App.router.transitionTo('main.alerts.alertDetails', definition);
  273. }
  274. },
  275. /**
  276. * Router transition to service summary page
  277. * @param event
  278. */
  279. goToService: function (event) {
  280. if (event && event.context) {
  281. this.get('parentView').hide();
  282. App.router.transitionTo('main.services.service.summary', event.context);
  283. }
  284. },
  285. /**
  286. * Router transition to host level alerts page
  287. * @param event
  288. */
  289. goToHostAlerts: function (event) {
  290. if (event && event.context) {
  291. this.get('parentView').hide();
  292. App.router.transitionTo('main.hosts.hostDetails.alerts', event.context);
  293. }
  294. },
  295. didInsertElement: function () {
  296. this.filter();
  297. return this._super();
  298. }
  299. })
  300. });
  301. }
  302. });