widget_property.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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
  66. * Should be defined in child class
  67. * @type {Boolean}
  68. */
  69. isValid: function () {
  70. return true;
  71. }.property(),
  72. /**
  73. * Define whether property is required by user
  74. * @type {Boolean}
  75. */
  76. isRequired: true
  77. });
  78. App.WidgetPropertyTypes = [
  79. {
  80. name: 'display_unit',
  81. label: 'Unit',
  82. displayType: 'textField',
  83. classNames: 'widget-property-unit',
  84. isValid: function () {
  85. return this.get('isRequired') ? this.get('value') : true;
  86. }.property('value'),
  87. valueMap: {
  88. "value": "display_unit"
  89. }
  90. },
  91. {
  92. name: 'graph_type',
  93. label: 'Graph Type',
  94. displayType: 'select',
  95. options: [
  96. {
  97. label: "LINE",
  98. value: "LINE"
  99. },
  100. {
  101. label: "STACK",
  102. value: "STACK"
  103. }
  104. ],
  105. valueMap: {
  106. "value": "graph_type"
  107. }
  108. },
  109. {
  110. name: 'time_range',
  111. label: 'Time Range',
  112. displayType: 'select',
  113. options: [
  114. {
  115. label: "Last 1 hour",
  116. value: "1"
  117. },
  118. {
  119. label: "Last 2 hours",
  120. value: "2"
  121. },
  122. {
  123. label: "Last 4 hour",
  124. value: "4"
  125. },
  126. {
  127. label: "Last 12 hour",
  128. value: "12"
  129. },
  130. {
  131. label: "Last 24 hour",
  132. value: "24"
  133. },
  134. {
  135. label: "Last 1 week",
  136. value: "168"
  137. },
  138. {
  139. label: "Last 1 month",
  140. value: "720"
  141. },
  142. {
  143. label: "Last 1 year",
  144. value: "8760"
  145. }
  146. ],
  147. valueMap: {
  148. "value": "time_range"
  149. }
  150. },
  151. {
  152. name: 'threshold',
  153. label: 'Thresholds',
  154. MIN_VALUE: 0,
  155. MAX_VALUE: 1,
  156. valueMap: {
  157. "smallValue": "warning_threshold",
  158. "bigValue": "error_threshold"
  159. },
  160. /**
  161. * threshold-value
  162. * @type {string}
  163. */
  164. smallValue: '',
  165. bigValue: '',
  166. badgeOK: 'OK',
  167. badgeWarning: 'WARNING',
  168. badgeCritical: 'CRITICAL',
  169. displayType: 'threshold',
  170. classNames: 'widget-property-threshold',
  171. /**
  172. * Check if <code>smallValue</code> is valid float number
  173. * @return {boolean}
  174. */
  175. isSmallValueValid: function () {
  176. return this.validate(this.get('smallValue'));
  177. }.property('smallValue', 'bigValue'),
  178. /**
  179. * Check if <code>bigValue</code> is valid float number
  180. * @return {boolean}
  181. */
  182. isBigValueValid: function () {
  183. return this.validate(this.get('bigValue'));
  184. }.property('bigValue', 'smallValue'),
  185. /**
  186. * validate
  187. * @param {string} value
  188. * @returns {boolean}
  189. */
  190. validate: function (value) {
  191. value = Number(('' + value).trim());
  192. if (!value) {
  193. return true;
  194. }
  195. return validator.isValidFloat(value) && value > this.get('MIN_VALUE') && value <= this.get('MAX_VALUE');
  196. },
  197. isValid: Em.computed.and('isSmallValueValid', 'isBigValueValid')
  198. }
  199. ];