alerts_controller.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.MainAlertsController = Em.Controller.extend({
  20. name: 'mainAlertsController',
  21. alerts: [],
  22. isLoaded: false,
  23. isUpdating: false,
  24. // name of resource(hostName or ServiceName)
  25. resourceName: null,
  26. //"HOST" or "SERVICE"
  27. resourceType: null,
  28. updateTimer: null,
  29. /**
  30. * load alerts for service or host
  31. * @param name
  32. * @param type
  33. */
  34. loadAlerts: function (name, type) {
  35. this.set('isLoaded', false);
  36. this.set('resourceName', name);
  37. this.set('resourceType', type);
  38. this.getFromServer();
  39. },
  40. /**
  41. * update alerts
  42. */
  43. update: function () {
  44. var self = this;
  45. if (this.get('isUpdating')) {
  46. this.set('updateTimer', setTimeout(function () {
  47. self.getFromServer();
  48. self.update();
  49. }, App.componentsUpdateInterval));
  50. } else {
  51. clearTimeout(this.get('updateTimer'));
  52. }
  53. }.observes('isUpdating'),
  54. /**
  55. * ask alerts from server by type
  56. */
  57. getFromServer: function () {
  58. if (App.router.get('clusterController.isNagiosInstalled')) {
  59. if (this.get('resourceType') === "SERVICE") {
  60. this.getAlertsByService();
  61. } else if (this.get('resourceType') === "HOST") {
  62. this.getAlertsByHost();
  63. } else {
  64. console.warn("GET Alerts error: unknown resourceType");
  65. }
  66. } else {
  67. this.set('isLoaded', true);
  68. }
  69. },
  70. getAlertsByHost: function () {
  71. if (this.get('resourceName')) {
  72. App.ajax.send({
  73. name: 'alerts.get_by_host',
  74. sender: this,
  75. data: {
  76. hostName: this.get('resourceName')
  77. },
  78. success: 'getAlertsSuccessCallback',
  79. error: 'getAlertsErrorCallback'
  80. })
  81. } else {
  82. console.warn('GET Alerts error: hostName parameter is missing');
  83. }
  84. },
  85. getAlertsByService: function () {
  86. if (this.get('resourceName')) {
  87. App.ajax.send({
  88. name: 'alerts.get_by_service',
  89. sender: this,
  90. data: {
  91. serviceName: this.get('resourceName')
  92. },
  93. success: 'getAlertsSuccessCallback',
  94. error: 'getAlertsErrorCallback'
  95. })
  96. } else {
  97. console.warn('GET Alerts error: serviceName parameter is missing');
  98. }
  99. },
  100. /**
  101. * map to associate old status format with and maintain sorting
  102. */
  103. statusNumberMap: {
  104. "OK" : "0",
  105. "WARNING": "1",
  106. "CRITICAL": "2"
  107. },
  108. getAlertsSuccessCallback: function (json) {
  109. var alerts = [];
  110. if (json && json.alerts && json.alerts.detail) {
  111. json.alerts.detail.forEach(function (_alert) {
  112. alerts.pushObject(App.Alert.create({
  113. title: _alert.description,
  114. serviceType: _alert.service_name,
  115. lastTime: _alert.status_time,
  116. status: this.get('statusNumberMap')[_alert.status] || "3",
  117. message: _alert.output,
  118. hostName: _alert.host_name,
  119. lastCheck: _alert.last_status_time
  120. }));
  121. }, this);
  122. }
  123. this.set('alerts', this.sortAlerts(alerts));
  124. this.set('isLoaded', true);
  125. },
  126. /**
  127. * alerts sorting
  128. * firstly by status
  129. * 1. UNKNOWN
  130. * 2. CRITICAL
  131. * 3. WARNING
  132. * 4. OK
  133. * secondly by date
  134. * @param array
  135. * @return {*}
  136. */
  137. sortAlerts: function (array) {
  138. return array.sort(function (left, right) {
  139. var statusDiff = right.get('status') - left.get('status');
  140. if (statusDiff == 0) { // same error severity - sort by time
  141. var rightTime = right.get('date');
  142. var leftTime = left.get('date');
  143. rightTime = rightTime ? rightTime.getTime() : 0;
  144. leftTime = leftTime ? leftTime.getTime() : 0;
  145. statusDiff = rightTime - leftTime;
  146. }
  147. return statusDiff;
  148. });
  149. },
  150. getAlertsErrorCallback: function(){
  151. this.set('isLoaded', true);
  152. }
  153. });