alert.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.AlertStatus = {
  20. negative: 'corrupt',
  21. positive: 'ok'
  22. };
  23. /**
  24. * Defines structure for App.Alert class. Keys mentioned here are for JSON data
  25. * which comes back from NAGIOS server.
  26. */
  27. App.Alert = DS.Model.extend({
  28. title: DS.attr('string'),//service_description in ajax response
  29. serviceType: DS.attr('string'),
  30. status: DS.attr('string'),//current_state in ajax response
  31. message: DS.attr('string'),//plugin_output in ajax response
  32. hostName: DS.attr('string'),
  33. currentAttempt: DS.attr('string'),
  34. isFlapping: DS.attr('number'),
  35. lastCheck: DS.attr('number'),
  36. lastTime: DS.attr('number'),
  37. date: function () {
  38. return DS.attr.transforms.date.from(this.get('lastTime'));
  39. }.property('lastTime'),
  40. /**
  41. * Used to show correct icon in UI
  42. */
  43. isOk: function () {
  44. return this.get('status') == "0";
  45. }.property('status'),
  46. /**
  47. * Used to show correct icon in UI
  48. */
  49. isWarning: function () {
  50. return this.get('status') == "1";
  51. }.property('status'),
  52. /**
  53. * Used to show correct icon in UI
  54. */
  55. isCritical: function() {
  56. return this.get('status') == '2';
  57. }.property('status'),
  58. /**
  59. * Used to show only required alerts at the service level
  60. */
  61. ignoredForServices: function() {
  62. return ['NodeManager health', 'NodeManager process', 'TaskTracker process', 'RegionServer process', 'DataNode process', 'DataNode space', 'ZooKeeper Server process'].contains(this.get('title'));
  63. }.property('title'),
  64. /**
  65. * Used to show only required alerts at the host level
  66. */
  67. ignoredForHosts: function() {
  68. return this.get('title').indexOf('Percent') != -1;
  69. }.property('title'),
  70. /**
  71. * Provides how long ago this alert happened.
  72. *
  73. * @type {String}
  74. */
  75. timeSinceAlert: function () {
  76. var d = this.get('date');
  77. if (d) {
  78. var prefix = this.t('services.alerts.OK.timePrefix');
  79. switch (this.get('status')) {
  80. case "1":
  81. prefix = this.t('services.alerts.WARN.timePrefix');
  82. break;
  83. case "2":
  84. prefix = this.t('services.alerts.CRIT.timePrefix');
  85. break;
  86. case "3":
  87. prefix = this.t('services.alerts.UNKNOWN.timePrefix');
  88. break;
  89. }
  90. var prevSuffix = $.timeago.settings.strings.suffixAgo;
  91. $.timeago.settings.strings.suffixAgo = '';
  92. var since = prefix + $.timeago(this.makeTimeAtleastMinuteAgo(d));
  93. $.timeago.settings.strings.suffixAgo = prevSuffix;
  94. return since;
  95. }
  96. return "";
  97. }.property('date', 'status'),
  98. makeTimeAtleastMinuteAgo: function(d){
  99. var diff = new Date().getTime() - d.getTime();
  100. if (diff < 60000) {
  101. diff = 60000 - diff;
  102. var newD = new Date(d.getTime() - diff );
  103. //console.log("Making time more than 1 minute. New time=",newD,", Old time=",d);
  104. return newD;
  105. }
  106. return d;
  107. },
  108. /**
  109. * Provides more details about when this alert happened.
  110. *
  111. * @type {String}
  112. */
  113. timeSinceAlertDetails: function () {
  114. var details = "";
  115. var date = this.get('date');
  116. if (date) {
  117. var dateString = date.toDateString();
  118. dateString = dateString.substr(dateString.indexOf(" ") + 1);
  119. dateString = "Occurred on " + dateString + ", " + date.toLocaleTimeString();
  120. details += dateString;
  121. }
  122. var lastCheck = this.get('lastCheck');
  123. if (lastCheck) {
  124. lastCheck = new Date(lastCheck * 1000);
  125. details = details + "<br>Last checked " + $.timeago(lastCheck);
  126. }
  127. return details;
  128. }.property('lastCheck', 'date'),
  129. /**
  130. * Used to show appropriate service label in UI
  131. */
  132. serviceName: function () {
  133. if (this.get('serviceType')) {
  134. var type = this.get('serviceType').toLowerCase();
  135. switch (type) {
  136. case 'mapreduce':
  137. return 'MapReduce';
  138. case 'hdfs':
  139. return 'HDFS';
  140. case 'hbase':
  141. return "HBase";
  142. case 'zookeeper':
  143. return "Zookeeper";
  144. case 'oozie':
  145. return "Oozie";
  146. case 'hive':
  147. return 'Hive';
  148. }
  149. }
  150. return null;
  151. }.property('serviceType'),
  152. /**
  153. * Used to provide appropriate service link in UI
  154. */
  155. serviceLink: function () {
  156. if (this.get('serviceType')) {
  157. var type = this.get('serviceType').toLowerCase();
  158. switch (type) {
  159. case 'mapreduce':
  160. return '#/main/services/MAPREDUCE/summary';
  161. case 'hdfs':
  162. return '#/main/services/HDFS/summary';
  163. case 'hbase':
  164. return '#/main/services/HBASE/summary';
  165. case 'zookeeper':
  166. return '#/main/services/ZOOKEEPER/summary';
  167. case 'oozie':
  168. return '#/main/services/OOZIE/summary';
  169. case 'hive':
  170. return '#/main/services/HIVE/summary';
  171. }
  172. }
  173. return null;
  174. }.property('serviceType')
  175. });
  176. App.Alert.FIXTURES = [
  177. ];