alert_instance.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. var dateUtils = require('utils/date');
  20. App.AlertInstance = DS.Model.extend({
  21. id: DS.attr('number'),
  22. label: DS.attr('string'),
  23. definitionName: DS.attr('string'),
  24. definitionId: DS.attr('number'),
  25. service: DS.belongsTo('App.Service'),
  26. serviceName: DS.attr('string'),
  27. componentName: DS.attr('string'),
  28. host: DS.belongsTo('App.Host'),
  29. scope: DS.attr('string'),
  30. originalTimestamp: DS.attr('number'),
  31. latestTimestamp: DS.attr('number'),
  32. maintenanceState: DS.attr('string'),
  33. instance: DS.attr('string'),
  34. state: DS.attr('string'),
  35. text: DS.attr('string'),
  36. notification: DS.hasMany('App.AlertNotification'),
  37. /**
  38. * Status icon markup
  39. * @type {string}
  40. */
  41. status: function () {
  42. var isMaintenanceStateOn = this.get('maintenanceState') === 'ON';
  43. var state = this.get('state');
  44. var stateClass = isMaintenanceStateOn ? 'PENDING' : state;
  45. var shortState = this.get('shortState')[state];
  46. var maintenanceIcon = isMaintenanceStateOn ? '<span class="icon-medkit"></span> ' : '';
  47. return '<div class="label alert-state-single-host alert-state-' + stateClass + '">' + maintenanceIcon + shortState + '</div>';
  48. }.property('state'),
  49. /**
  50. * For alerts we will have processes which are not typical
  51. * cluster services - like Ambari-Server. This method unifies
  52. * cluster services and other services into a common display-name.
  53. * @see App.AlertDefinition#serviceDisplayName()
  54. */
  55. serviceDisplayName: function () {
  56. var serviceName = this.get('service.displayName');
  57. if (!serviceName) {
  58. serviceName = this.get('serviceName');
  59. if (serviceName) {
  60. serviceName = serviceName.toCapital();
  61. }
  62. }
  63. return serviceName;
  64. }.property('serviceName', 'service.displayName'),
  65. /**
  66. * Formatted timestamp for latest instance triggering
  67. * @type {string}
  68. */
  69. lastCheckedFormatted: function () {
  70. return dateUtils.dateFormat(this.get('latestTimestamp'));
  71. }.property('latestTimestamp'),
  72. /**
  73. * Formatted timestamp for latest instance triggering
  74. * @type {string}
  75. */
  76. lastTriggeredFormatted: function () {
  77. return dateUtils.dateFormat(this.get('originalTimestamp'));
  78. }.property('originalTimestamp'),
  79. /**
  80. * Formatted timestamp with <code>$.timeago</code>
  81. * @type {string}
  82. */
  83. lastTriggeredAgoFormatted: function () {
  84. var lastTriggered = this.get('originalTimestamp');
  85. return lastTriggered ? $.timeago(new Date(lastTriggered)) : '';
  86. }.property('originalTimestamp'),
  87. lastTriggeredVerboseDisplay: function () {
  88. var originalTimestamp = this.get('originalTimestamp');
  89. var latestTimestamp = this.get('latestTimestamp');
  90. return Em.I18n.t('models.alert_instance.tiggered.verbose').format(
  91. dateUtils.dateFormat(originalTimestamp),
  92. dateUtils.dateFormat(latestTimestamp));
  93. }.property('originalTimestamp', 'latestTimestamp'),
  94. /**
  95. * Formatted timestamp with <code>$.timeago</code>
  96. * @type {string}
  97. */
  98. lastTriggeredForFormatted: function () {
  99. var lastTriggered = this.get('originalTimestamp');
  100. var previousSuffixAgo = $.timeago.settings.strings.suffixAgo;
  101. var previousPrefixAgo = $.timeago.settings.strings.prefixAgo;
  102. $.timeago.settings.strings.suffixAgo = null;
  103. $.timeago.settings.strings.prefixAgo = 'for';
  104. var triggeredFor = lastTriggered ? $.timeago(new Date(lastTriggered)) : '';
  105. $.timeago.settings.strings.suffixAgo = previousSuffixAgo;
  106. $.timeago.settings.strings.prefixAgo = previousPrefixAgo;
  107. return triggeredFor;
  108. }.property('originalTimestamp'),
  109. /**
  110. * Formatted lastChecked and lastTriggered timestamp
  111. * @returns {string}
  112. */
  113. statusChangedAndLastCheckedFormatted: function () {
  114. var lastCheckedFormatted = this.get('lastCheckedFormatted');
  115. var lastTriggeredFormatted = this.get('lastTriggeredFormatted');
  116. return Em.I18n.t('models.alert_definition.triggered.checked').format(lastTriggeredFormatted, lastCheckedFormatted);
  117. }.property('lastCheckedFormatted', 'lastTriggeredFormatted'),
  118. /**
  119. * List of css-classes for alert instance status
  120. * @type {object}
  121. */
  122. typeIcons: {
  123. 'DISABLED': 'icon-off'
  124. },
  125. /**
  126. * Define if definition serviceName is Ambari
  127. * Used in some logic in templates to distinguish definitions with Ambari serviceName
  128. * @returns {boolean}
  129. */
  130. isAmbariServiceName: function () {
  131. return this.get('serviceName') === 'AMBARI';
  132. }.property('serviceName'),
  133. shortState: {
  134. 'CRITICAL': 'CRIT',
  135. 'WARNING': 'WARN',
  136. 'OK': 'OK',
  137. 'UNKNOWN': 'UNKWN',
  138. 'PENDING': 'NONE'
  139. }
  140. });
  141. App.AlertInstance.FIXTURES = [];