alert.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. /**
  20. * Defines structure for App.Alert class. Keys mentioned here are for JSON data
  21. * which comes back from NAGIOS server.
  22. */
  23. App.Alert = Em.Object.extend({
  24. title: null,//service_description in ajax response
  25. serviceType: null,
  26. status: null,//current_state in ajax response
  27. message: null,//plugin_output in ajax response
  28. hostName: null,
  29. lastCheck: null,
  30. lastTime: null,
  31. date: function () {
  32. return DS.attr.transforms.date.from(this.get('lastTime'));
  33. }.property('lastTime'),
  34. /**
  35. * Used to show correct icon in UI
  36. */
  37. isOk: function () {
  38. return this.get('status') == "0";
  39. }.property('status'),
  40. /**
  41. * Used to show correct icon in UI
  42. */
  43. isWarning: function () {
  44. return this.get('status') == "1";
  45. }.property('status'),
  46. /**
  47. * Used to show correct icon in UI
  48. */
  49. isCritical: function() {
  50. return this.get('status') == '2';
  51. }.property('status'),
  52. /**
  53. * Used to show correct icon in UI
  54. */
  55. isPassive: function() {
  56. return this.get('status') == '3';
  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', 'Supervisors 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 timeFormat = this.t('services.alerts.OK.timePrefix');
  79. switch (this.get('status')) {
  80. case "1":
  81. timeFormat = this.t('services.alerts.WARN.timePrefix');
  82. break;
  83. case "2":
  84. timeFormat = this.t('services.alerts.CRIT.timePrefix');
  85. break;
  86. case "3":
  87. timeFormat = this.t('services.alerts.MAINT.timePrefix');
  88. break;
  89. case "4":
  90. timeFormat = this.t('services.alerts.UNKNOWN.timePrefix');
  91. break;
  92. }
  93. var prevSuffix = $.timeago.settings.strings.suffixAgo;
  94. $.timeago.settings.strings.suffixAgo = '';
  95. var since = timeFormat.format($.timeago(this.makeTimeAtleastMinuteAgo(d)));
  96. $.timeago.settings.strings.suffixAgo = prevSuffix;
  97. return since;
  98. } else if (d == 0) {
  99. var timeFormat = this.t('services.alerts.OK.timePrefixShort');
  100. switch (this.get('status')) {
  101. case "1":
  102. timeFormat = this.t('services.alerts.WARN.timePrefixShort');
  103. break;
  104. case "2":
  105. timeFormat = this.t('services.alerts.CRIT.timePrefixShort');
  106. break;
  107. case "3":
  108. timeFormat = this.t('services.alerts.MAINT.timePrefixShort');
  109. break;
  110. case "4":
  111. timeFormat = this.t('services.alerts.UNKNOWN.timePrefixShort');
  112. break;
  113. }
  114. return timeFormat;
  115. } else {
  116. return "";
  117. }
  118. }.property('date', 'status'),
  119. makeTimeAtleastMinuteAgo: function(d){
  120. var time = d.getTime();
  121. var diff = App.dateTime() - time;
  122. if (diff < 60000) {
  123. diff = 60000 - diff;
  124. return new Date(time - diff);
  125. }
  126. return d;
  127. },
  128. /**
  129. * Provides more details about when this alert happened.
  130. *
  131. * @type {String}
  132. */
  133. timeSinceAlertDetails: function () {
  134. var details = "";
  135. var date = this.get('date');
  136. if (date) {
  137. var dateString = date.toDateString();
  138. dateString = dateString.substr(dateString.indexOf(" ") + 1);
  139. dateString = Em.I18n.t('services.alerts.occurredOn').format(dateString, date.toLocaleTimeString());
  140. details += dateString;
  141. }
  142. var lastCheck = this.get('lastCheck');
  143. if (lastCheck) {
  144. lastCheck = new Date(lastCheck * 1000);
  145. details = details ? details + Em.I18n.t('services.alerts.brLastCheck').format($.timeago(lastCheck)) : Em.I18n.t('services.alerts.lastCheck').format($.timeago(lastCheck));
  146. }
  147. return details;
  148. }.property('lastCheck', 'date'),
  149. /**
  150. * Used to show appropriate service label in UI
  151. */
  152. serviceName: function () {
  153. if (this.get('serviceType')) {
  154. var type = this.get('serviceType').toLowerCase();
  155. switch (type) {
  156. case 'mapreduce':
  157. return 'MapReduce';
  158. case 'hdfs':
  159. return 'HDFS';
  160. case 'hbase':
  161. return "HBase";
  162. case 'zookeeper':
  163. return "Zookeeper";
  164. case 'oozie':
  165. return "Oozie";
  166. case 'hive':
  167. return 'Hive';
  168. }
  169. }
  170. return null;
  171. }.property('serviceType'),
  172. /**
  173. * Used to provide appropriate service link in UI
  174. */
  175. serviceLink: function () {
  176. if (this.get('serviceType')) {
  177. var type = this.get('serviceType').toLowerCase();
  178. switch (type) {
  179. case 'mapreduce':
  180. return '#/main/services/MAPREDUCE/summary';
  181. case 'hdfs':
  182. return '#/main/services/HDFS/summary';
  183. case 'hbase':
  184. return '#/main/services/HBASE/summary';
  185. case 'zookeeper':
  186. return '#/main/services/ZOOKEEPER/summary';
  187. case 'oozie':
  188. return '#/main/services/OOZIE/summary';
  189. case 'hive':
  190. return '#/main/services/HIVE/summary';
  191. }
  192. }
  193. return null;
  194. }.property('serviceType')
  195. });