alert_instances_controller.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /**
  22. * Are alertInstances loaded
  23. * @type {boolean}
  24. */
  25. isLoaded: false,
  26. /**
  27. * A flag to reload alert instances table every 10 seconds
  28. * @type {boolean}
  29. */
  30. reload: false,
  31. /**
  32. * Causes automatic updates of content if set to true
  33. * @type {boolean}
  34. */
  35. isUpdating: false,
  36. /**
  37. * Times for alert instances updater
  38. * Used in <code>scheduleUpdate</code>
  39. * @type {number|null}
  40. */
  41. updateTimer: null,
  42. /**
  43. * @type {string|null} sourceName - hostName or alertDefinitionId
  44. */
  45. sourceName: null,
  46. /**
  47. * @type {string|null} sourceType - 'HOST'|'ALERT_DEFINITION'
  48. */
  49. sourceType: null,
  50. /**
  51. * Load alert instances from server (all, for selected host, for selected alert definition)
  52. * @returns {$.ajax}
  53. * @method fetchAlertInstances
  54. */
  55. fetchAlertInstances: function () {
  56. var sourceType = this.get('sourceType'),
  57. sourceName = this.get('sourceName'),
  58. ajaxData = {
  59. sender: this,
  60. success: 'getAlertInstancesSuccessCallback',
  61. error: 'getAlertInstancesErrorCallback'
  62. };
  63. switch (sourceType) {
  64. case 'HOST':
  65. $.extend(ajaxData, {
  66. name: 'alerts.instances.by_host',
  67. data: {
  68. hostName: sourceName
  69. }
  70. });
  71. break;
  72. case 'ALERT_DEFINITION':
  73. $.extend(ajaxData, {
  74. name: 'alerts.instances.by_definition',
  75. data: {
  76. definitionId: sourceName
  77. }
  78. });
  79. break;
  80. default:
  81. $.extend(ajaxData, {
  82. name: 'alerts.instances'
  83. });
  84. break;
  85. }
  86. return App.ajax.send(ajaxData);
  87. },
  88. /**
  89. * Pseudo for <code>fetchAlertInstances</code>
  90. * Used to get all alert instances
  91. * @method loadAlertInstances
  92. */
  93. loadAlertInstances: function () {
  94. this.set('isLoaded', false);
  95. this.set('sourceType', null);
  96. this.set('sourceName', null);
  97. this.fetchAlertInstances();
  98. },
  99. /**
  100. * Pseudo for <code>fetchAlertInstances</code>
  101. * Used to get alert instances for some host
  102. * @param {string} hostName
  103. * @method loadAlertInstancesByHost
  104. */
  105. loadAlertInstancesByHost: function (hostName) {
  106. this.set('isLoaded', false);
  107. this.set('sourceType', 'HOST');
  108. this.set('sourceName', hostName);
  109. this.fetchAlertInstances();
  110. },
  111. /**
  112. * Pseudo for <code>fetchAlertInstances</code>
  113. * Used to get alert instances for some alert definition
  114. * @param {string} definitionId
  115. * @method loadAlertInstancesByAlertDefinition
  116. */
  117. loadAlertInstancesByAlertDefinition: function (definitionId) {
  118. this.set('isLoaded', false);
  119. this.set('sourceType', 'ALERT_DEFINITION');
  120. this.set('sourceName', definitionId);
  121. this.fetchAlertInstances();
  122. },
  123. scheduleUpdate: function () {
  124. var self = this;
  125. if (this.get('isUpdating')) {
  126. this.set('updateTimer', setTimeout(function () {
  127. self.fetchAlertInstances();
  128. self.scheduleUpdate();
  129. }, App.get('alertInstancesUpdateInterval')));
  130. }
  131. else {
  132. clearTimeout(this.get('updateTimer'));
  133. }
  134. }.observes('isUpdating'),
  135. /**
  136. * Success-callback for alert instances request
  137. * @param {object} json
  138. * @method getAlertInstancesSuccessCallback
  139. */
  140. getAlertInstancesSuccessCallback: function (json) {
  141. App.alertInstanceMapper.map(json);
  142. this.set('isLoaded', true);
  143. this.set('reload', !this.get('reload'));
  144. },
  145. /**
  146. * Error-callback for alert instances request
  147. * @method getAlertInstancesErrorCallback
  148. */
  149. getAlertInstancesErrorCallback: function () {
  150. this.set('isLoaded', true);
  151. }
  152. });