alert_instances_controller.js 10.0 KB

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