alert_instances_controller.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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: Em.computed.bool('controller.unhealthyAlertInstances'),
  213. isAlertEmptyList: Em.computed.empty('content'),
  214. /**
  215. * Update list of shown alert instances
  216. * @method updateAlertInstances
  217. */
  218. updateAlertInstances: function () {
  219. var self = this,
  220. displayLength = this.get('displayLength'),
  221. startIndex = this.get('startIndex');
  222. if (!displayLength) return; // wait while table-info is loaded
  223. this.get('updater').set('queryParamsForUnhealthyAlertInstances', {
  224. from: startIndex - 1,
  225. page_size: displayLength
  226. });
  227. this.set('filteringComplete', false);
  228. this.get('updater').updateUnhealthyAlertInstances(function() {
  229. self.set('filteringComplete', true);
  230. });
  231. }.observes('displayLength', 'startIndex'),
  232. /**
  233. * Show spinner when filter/sorting request is in processing
  234. * @method overlayObserver
  235. */
  236. overlayObserver: function() {
  237. var $tbody = this.$('#alert-info'),
  238. $overlay = this.$('.table-overlay'),
  239. $spinner = $($overlay).find('.spinner');
  240. if (!this.get('filteringComplete')) {
  241. if (!$tbody) return;
  242. var tbodyPos = $tbody.position();
  243. if (!tbodyPos) return;
  244. $spinner.css('display', 'block');
  245. $overlay.css({
  246. top: tbodyPos.top + 1,
  247. left: tbodyPos.left + 1,
  248. width: $tbody.width() - 1,
  249. height: $tbody.height() - 1
  250. });
  251. }
  252. },
  253. /**
  254. * No filtering for alert definitions
  255. * @method filter
  256. */
  257. filter: function() {
  258. this.set('filteredContent', this.get('content'));
  259. }.observes('content.length'),
  260. /**
  261. * Router transition to alert definition details page
  262. * @param event
  263. */
  264. gotoAlertDetails: function (event) {
  265. if (event && event.context) {
  266. this.get('parentView').hide();
  267. var definition = App.AlertDefinition.find().findProperty('id', event.context.get('definitionId'));
  268. App.router.transitionTo('main.alerts.alertDetails', definition);
  269. }
  270. },
  271. /**
  272. * Router transition to service summary page
  273. * @param event
  274. */
  275. goToService: function (event) {
  276. if (event && event.context) {
  277. this.get('parentView').hide();
  278. App.router.transitionTo('main.services.service.summary', event.context);
  279. }
  280. },
  281. /**
  282. * Router transition to host level alerts page
  283. * @param event
  284. */
  285. goToHostAlerts: function (event) {
  286. if (event && event.context) {
  287. this.get('parentView').hide();
  288. App.router.transitionTo('main.hosts.hostDetails.alerts', event.context);
  289. }
  290. },
  291. didInsertElement: function () {
  292. this.filter();
  293. this.addObserver('filteringComplete', this, this.overlayObserver);
  294. this.overlayObserver();
  295. return this._super();
  296. }
  297. })
  298. });
  299. }
  300. });