alert_instances_controller.js 4.3 KB

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