widget_mixin.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.WidgetMixin = Ember.Mixin.create({
  20. /**
  21. * @type {RegExp}
  22. * @const
  23. */
  24. EXPRESSION_REGEX: /\$\{([\w\s\.\,\+\-\*\/\(\)\:\=\[\]]*)\}/g,
  25. /**
  26. * @type {RegExp}
  27. * @const
  28. */
  29. MATH_EXPRESSION_REGEX: /^[\d\s\+\-\*\/\(\)\.]+$/,
  30. /**
  31. * @type {RegExp}
  32. * @const
  33. */
  34. VALUE_NAME_REGEX: /[\w\.\:\=\[\]]+/g,
  35. /**
  36. * common metrics container
  37. * @type {Array}
  38. */
  39. metrics: [],
  40. /**
  41. * @type {boolean}
  42. */
  43. isLoaded: false,
  44. /**
  45. * @type {App.Widget}
  46. * @default null
  47. */
  48. content: null,
  49. beforeRender: function () {
  50. this.loadMetrics();
  51. },
  52. /**
  53. * load metrics
  54. */
  55. loadMetrics: function () {
  56. var requestData = this.getRequestData(this.get('content.metrics')),
  57. request,
  58. requestCounter = 0,
  59. self = this;
  60. for (var i in requestData) {
  61. request = requestData[i];
  62. requestCounter++;
  63. if (request.host_component_criteria) {
  64. this.getHostComponentMetrics(request).complete(function () {
  65. requestCounter--;
  66. if (requestCounter === 0) self.onMetricsLoaded();
  67. });
  68. } else {
  69. this.getServiceComponentMetrics(request).complete(function () {
  70. requestCounter--;
  71. if (requestCounter === 0) self.onMetricsLoaded();
  72. });
  73. }
  74. }
  75. },
  76. /**
  77. * get data formatted for request
  78. * @param {Array} metrics
  79. */
  80. getRequestData: function (metrics) {
  81. var requestsData = {};
  82. metrics.forEach(function (metric, index) {
  83. var key;
  84. if (metric.host_component_criteria) {
  85. key = metric.service_name + '_' + metric.component_name + '_' + metric.host_component_criteria;
  86. } else {
  87. key = metric.service_name + '_' + metric.component_name;
  88. }
  89. var requestMetric = $.extend({}, metric);
  90. if (requestsData[key]) {
  91. requestsData[key]["metric_paths"].push(requestMetric["metric_path"]);
  92. } else {
  93. requestMetric["metric_paths"] = [requestMetric["metric_path"]];
  94. delete requestMetric["metric_path"];
  95. requestsData[key] = requestMetric;
  96. }
  97. }, this);
  98. return requestsData;
  99. },
  100. /**
  101. * make GET call to server in order to fetch service-component metrics
  102. * @param {object} request
  103. * @returns {$.ajax}
  104. */
  105. getServiceComponentMetrics: function (request) {
  106. return App.ajax.send({
  107. name: 'widgets.serviceComponent.metrics.get',
  108. sender: this,
  109. data: {
  110. serviceName: request.service_name,
  111. componentName: request.component_name,
  112. metricPaths: request.metric_paths.join(',')
  113. },
  114. success: 'getMetricsSuccessCallback'
  115. });
  116. },
  117. /**
  118. * make GET call to server in order to fetch host-component metrics
  119. * @param {object} request
  120. * @returns {$.ajax}
  121. */
  122. getHostComponentMetrics: function (request) {
  123. return App.ajax.send({
  124. name: 'widgets.hostComponent.metrics.get',
  125. sender: this,
  126. data: {
  127. serviceName: request.service_name,
  128. componentName: request.component_name,
  129. metricPaths: request.metric_paths.join(','),
  130. hostComponentCriteria: 'host_components/HostRoles/' + request.host_component_criteria
  131. },
  132. success: 'getMetricsSuccessCallback'
  133. });
  134. },
  135. /**
  136. * callback on getting aggregated metrics and host component metrics
  137. * @param data
  138. */
  139. getMetricsSuccessCallback: function (data) {
  140. var metrics = [];
  141. this.get('content.metrics').forEach(function (_metric) {
  142. if (!Em.isNone(Em.get(data, _metric.metric_path.replace(/\//g, '.')))) {
  143. _metric.data = Em.get(data, _metric.metric_path.replace(/\//g, '.'));
  144. this.get('metrics').pushObject(_metric);
  145. }
  146. }, this);
  147. },
  148. /**
  149. * callback on metrics loaded
  150. */
  151. onMetricsLoaded: function () {
  152. var self = this;
  153. this.set('isLoaded', true);
  154. this.drawWidget();
  155. setTimeout(function() {
  156. self.loadMetrics();
  157. }, App.contentUpdateInterval);
  158. },
  159. /**
  160. * draw widget
  161. */
  162. drawWidget: function () {
  163. if (this.get('isLoaded')) {
  164. this.calculateValues();
  165. this.set('value', this.get('content.values')[0] && this.get('content.values')[0].computedValue);
  166. }
  167. },
  168. /**
  169. * calculate series datasets for graph widgets
  170. */
  171. calculateValues: function () {
  172. var metrics = this.get('metrics');
  173. var displayUnit = this.get('content.properties.display_unit');
  174. this.get('content.values').forEach(function (value) {
  175. var computeExpression = this.computeExpression(this.extractExpressions(value), metrics);
  176. value.computedValue = value.value.replace(this.get('EXPRESSION_REGEX'), function (match) {
  177. return (!Em.isNone(computeExpression[match])) ? computeExpression[match] + (displayUnit || "") : Em.I18n.t('common.na');
  178. });
  179. }, this);
  180. },
  181. /**
  182. * extract expressions
  183. * Example:
  184. * input: "${a/b} equal ${b+a}"
  185. * expressions: ['a/b', 'b+a']
  186. *
  187. * @param {object} input
  188. * @returns {Array}
  189. */
  190. extractExpressions: function (input) {
  191. var pattern = this.get('EXPRESSION_REGEX'),
  192. expressions = [],
  193. match;
  194. while (match = pattern.exec(input.value)) {
  195. expressions.push(match[1]);
  196. }
  197. return expressions;
  198. },
  199. /**
  200. * compute expression
  201. * @param expressions
  202. * @param metrics
  203. * @returns {object}
  204. */
  205. computeExpression: function (expressions, metrics) {
  206. var result = {};
  207. expressions.forEach(function (_expression) {
  208. var validExpression = true;
  209. var value = "";
  210. //replace values with metrics data
  211. var beforeCompute = _expression.replace(this.get('VALUE_NAME_REGEX'), function (match) {
  212. if (metrics.someProperty('name', match)) {
  213. return metrics.findProperty('name', match).data;
  214. } else {
  215. validExpression = false;
  216. console.warn('Metrics not found to compute expression');
  217. }
  218. });
  219. if (validExpression) {
  220. //check for correct math expression
  221. validExpression = this.get('MATH_EXPRESSION_REGEX').test(beforeCompute);
  222. !validExpression && console.warn('Value is not correct mathematical expression');
  223. }
  224. result['${' + _expression + '}'] = (validExpression) ? Number(window.eval(beforeCompute)).toString() : value;
  225. }, this);
  226. return result;
  227. },
  228. /*
  229. * make call when clicking on "remove icon" on widget
  230. */
  231. hideWidget: function () {
  232. },
  233. /*
  234. * make call when clicking on "clone icon" on widget
  235. */
  236. cloneWidget: function () {
  237. },
  238. /*
  239. * make call when clicking on "edit icon" on widget
  240. */
  241. editWidget: function () {
  242. }
  243. });
  244. App.WidgetPreviewMixin = Ember.Mixin.create({
  245. beforeRender: Em.K,
  246. isLoaded: true,
  247. metrics: [],
  248. content: Em.Object.create({
  249. widgetName: 'mock-widget',
  250. values: []
  251. }),
  252. drawWidget: function () {
  253. this.loadMetrics();
  254. this.get('content').setProperties({
  255. 'values': this.get('controller.widgetValues'),
  256. 'properties': this.get('controller.widgetProperties'),
  257. 'displayName': this.get('controller.widgetName')
  258. });
  259. this._super();
  260. }.observes('controller.widgetProperties', 'controller.widgetValues', 'controller.widgetMetrics', 'controller.widgetName'),
  261. loadMetrics: function () {
  262. var metrics = [];
  263. this.get('controller.widgetMetrics').forEach(function (metric) {
  264. metrics.push({
  265. name: metric.name,
  266. data: this.get('MOCK_VALUE')
  267. });
  268. }, this);
  269. this.set('metrics', metrics);
  270. }
  271. });