number_widget_view.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.NumberWidgetView = Em.View.extend(App.WidgetMixin, {
  20. templateName: require('templates/common/widget/number_widget'),
  21. /**
  22. * @type {string}
  23. */
  24. value: '',
  25. /**
  26. * @type {string}
  27. */
  28. displayValue: function () {
  29. var value = parseFloat(this.get('value'));
  30. if (isNaN(value)) return Em.I18n.t('common.na');
  31. return value + (this.get('content.properties.display_unit') || '');
  32. }.property('value', 'content.properties.display_unit'),
  33. /**
  34. * common metrics container
  35. * @type {Array}
  36. */
  37. metrics: [],
  38. /**
  39. * color of content calculated by thresholds
  40. * @type {string}
  41. */
  42. contentColor: function () {
  43. var value = parseFloat(this.get('value'));
  44. var threshold1 = parseFloat(this.get('content.properties.warning_threshold'));
  45. var threshold2 = parseFloat(this.get('content.properties.error_threshold'));
  46. if (isNaN(value)) {
  47. return 'grey';
  48. } else if (isNaN(threshold1) || (isNaN(threshold2) && value <= threshold1) || (!isNaN(threshold2) && (threshold1 > threshold2) && (value > threshold1)) || (!isNaN(threshold2) && (threshold1 < threshold2) && (value <= threshold1))) {
  49. return 'green';
  50. } else if (!isNaN(threshold2) && value.isInRange(threshold1, threshold2)) {
  51. return 'orange';
  52. } else {
  53. return 'red';
  54. }
  55. }.property('value', 'content.properties.warning_threshold', 'content.properties.error_threshold')
  56. });