alerts_controller.js 4.3 KB

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