widget_property.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 validator = require('utils/validator');
  20. App.WidgetProperty = Ember.Object.extend({
  21. /**
  22. * label to be shown for property
  23. * @type {String}
  24. */
  25. label: '',
  26. /**
  27. * PORT|METRIC|AGGREGATE
  28. * @type {String}
  29. */
  30. type: '',
  31. /**
  32. * config property value
  33. * @type {*}
  34. */
  35. value: null,
  36. /**
  37. * input displayType
  38. * one of 'textFields', 'textArea', 'select' or 'threshold'
  39. * @type {String}
  40. */
  41. displayType: '',
  42. /**
  43. * space separated list of css class names to use
  44. * @type {String}
  45. */
  46. classNames: '',
  47. /**
  48. * view class according to <code>displayType</code>
  49. * @type {Em.View}
  50. */
  51. viewClass: function () {
  52. var displayType = this.get('displayType');
  53. switch (displayType) {
  54. case 'textField':
  55. return App.WidgetPropertyTextFieldView;
  56. case 'threshold':
  57. return App.WidgetPropertyThresholdView;
  58. case 'select':
  59. return App.WidgetPropertySelectView;
  60. default:
  61. }
  62. }.property('displayType'),
  63. /**
  64. * Define whether property is valid
  65. * Computed property should be defined in child class
  66. * @type {Boolean}
  67. */
  68. isValid: true,
  69. /**
  70. * Define whether property is required by user
  71. * @type {Boolean}
  72. */
  73. isRequired: true
  74. });
  75. App.WidgetPropertyTypes = [
  76. {
  77. name: 'display_unit',
  78. label: 'Unit',
  79. displayType: 'textField',
  80. classNames: 'widget-property-unit',
  81. isValid: function () {
  82. return this.get('isRequired') ? Boolean(this.get('value')) : true;
  83. }.property('value'),
  84. valueMap: {
  85. "value": "display_unit"
  86. }
  87. },
  88. {
  89. name: 'graph_type',
  90. label: 'Graph Type',
  91. displayType: 'select',
  92. options: [
  93. {
  94. label: "LINE",
  95. value: "LINE"
  96. },
  97. {
  98. label: "STACK",
  99. value: "STACK"
  100. }
  101. ],
  102. valueMap: {
  103. "value": "graph_type"
  104. }
  105. },
  106. {
  107. name: 'time_range',
  108. label: 'Time Range',
  109. displayType: 'select',
  110. options: [
  111. {
  112. label: "Last 1 hour",
  113. value: "1"
  114. },
  115. {
  116. label: "Last 2 hours",
  117. value: "2"
  118. },
  119. {
  120. label: "Last 4 hour",
  121. value: "4"
  122. },
  123. {
  124. label: "Last 12 hour",
  125. value: "12"
  126. },
  127. {
  128. label: "Last 24 hour",
  129. value: "24"
  130. },
  131. {
  132. label: "Last 1 week",
  133. value: "168"
  134. },
  135. {
  136. label: "Last 1 month",
  137. value: "720"
  138. },
  139. {
  140. label: "Last 1 year",
  141. value: "8760"
  142. }
  143. ],
  144. valueMap: {
  145. "value": "time_range"
  146. }
  147. },
  148. {
  149. name: 'threshold',
  150. label: 'Thresholds',
  151. MIN_VALUE: 0,
  152. MAX_VALUE: 1,
  153. valueMap: {
  154. "smallValue": "warning_threshold",
  155. "bigValue": "error_threshold"
  156. },
  157. /**
  158. * threshold-value
  159. * @type {string}
  160. */
  161. smallValue: '',
  162. bigValue: '',
  163. badgeOK: 'OK',
  164. badgeWarning: 'WARNING',
  165. badgeCritical: 'CRITICAL',
  166. displayType: 'threshold',
  167. classNames: 'widget-property-threshold',
  168. /**
  169. * Check if <code>smallValue</code> is valid float number
  170. * @return {boolean}
  171. */
  172. isSmallValueValid: function () {
  173. return this.validate(this.get('smallValue'));
  174. }.property('smallValue', 'bigValue'),
  175. /**
  176. * Check if <code>bigValue</code> is valid float number
  177. * @return {boolean}
  178. */
  179. isBigValueValid: function () {
  180. return this.validate(this.get('bigValue'));
  181. }.property('bigValue', 'smallValue'),
  182. /**
  183. * validate
  184. * @param {string} value
  185. * @returns {boolean}
  186. */
  187. validate: function (value) {
  188. value = Number(('' + value).trim());
  189. if (!value && !isNaN(value)) {
  190. return true;
  191. }
  192. return validator.isValidFloat(value) && value > this.get('MIN_VALUE') && value <= this.get('MAX_VALUE');
  193. },
  194. isValid: Em.computed.and('isSmallValueValid', 'isBigValueValid')
  195. }
  196. ];