uptime_text_widget.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 date = require('utils/date/date');
  20. App.UptimeTextDashboardWidgetView = App.TextDashboardWidgetView.extend({
  21. templateName: require('templates/main/dashboard/widgets/uptime'),
  22. timeUnit: null,
  23. hiddenInfoClass: "hidden-info-three-line",
  24. thresh1: 5,
  25. thresh2: 10,
  26. maxValue: 'infinity',
  27. component: null,
  28. /**
  29. * Model's field that used to calculate data and content
  30. * Should be defined in every child
  31. */
  32. modelField: null,
  33. data: null,
  34. content: null,
  35. isGreen: function () {
  36. return !Em.isNone(this.get('data'));
  37. }.property('data'),
  38. isOrange: false,
  39. isRed: false,
  40. timeConverter: function (timestamp) {
  41. var m = moment(new Date(timestamp));
  42. return [m.format('ddd MMM DD YYYY'), m.format('HH:mm:ss')];
  43. },
  44. /**
  45. * All children should have such code
  46. * <code>
  47. * didInsertElement: function() {
  48. * this._super();
  49. * this.calc();
  50. * }
  51. * </code>
  52. */
  53. didInsertElement: function () {
  54. this._super();
  55. this.addObserver('model.' + this.get('modelField'), this, this.calc);
  56. },
  57. calc: function () {
  58. // don't do this.setProperties!
  59. this.set('data', this.calcData());
  60. this.set('content', this.calcContent());
  61. },
  62. uptimeProcessing: function (uptime) {
  63. var uptimeString = this.timeConverter(uptime);
  64. var diff = App.dateTimeWithTimeZone() - uptime;
  65. if (diff < 0) {
  66. diff = 0;
  67. }
  68. var formatted = date.timingFormat(diff); //17.67 days
  69. var timeUnit = null;
  70. if (formatted) {
  71. switch (formatted.split(" ")[1]) {
  72. case 'secs':
  73. timeUnit = 's';
  74. break;
  75. case 'hours':
  76. timeUnit = 'hr';
  77. break;
  78. case 'days':
  79. timeUnit = 'd';
  80. break;
  81. case 'mins':
  82. timeUnit = 'min';
  83. break;
  84. default:
  85. timeUnit = formatted.split(" ")[1];
  86. }
  87. this.setProperties({
  88. timeUnit: timeUnit,
  89. hiddenInfo: [formatted, uptimeString[0], uptimeString[1]]
  90. });
  91. }
  92. return formatted;
  93. },
  94. calcData: function () {
  95. var field = this.get('modelField');
  96. var uptime = this.get('model').get(field);
  97. if (uptime) {
  98. var formatted = this.uptimeProcessing(App.dateTimeWithTimeZone(uptime));
  99. if (!Em.isNone(formatted)) {
  100. return parseFloat(formatted.split(" ")[0]);
  101. }
  102. }
  103. this.set('hiddenInfo', [this.get('component'), 'Not Running']);
  104. return null;
  105. },
  106. calcContent: function () {
  107. var data = this.get('data');
  108. return data ?
  109. data.toFixed(1) + ' ' + this.get('timeUnit') :
  110. Em.I18n.t('services.service.summary.notAvailable');
  111. }
  112. });