server_data_mapper.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. /**
  20. * initialize common cache container for mappers
  21. * App.cache contains shared data, used for syncronizing incoming server data among mappers
  22. */
  23. App.cache = {
  24. 'previousHostStatuses': {},
  25. 'previousComponentStatuses': {},
  26. 'previousComponentPassiveStates': {},
  27. 'services': [],
  28. 'currentConfigVersions': {}
  29. };
  30. App.cache.clear = function () {
  31. var clear = App.cache.clear;
  32. App.cache = {
  33. 'previousHostStatuses': {},
  34. 'previousComponentStatuses': {},
  35. 'previousComponentPassiveStates': {},
  36. 'services': [],
  37. 'currentConfigVersions': {}
  38. };
  39. App.cache.clear = clear;
  40. };
  41. App.ServerDataMapper = Em.Object.extend({
  42. jsonKey: false,
  43. map: function (json) {
  44. if (json) {
  45. var model = this.get('model');
  46. var jsonKey = this.get('jsonKey');
  47. if (jsonKey && json[jsonKey]) { // if data come as { hdfs: {...} }
  48. json = json[jsonKey];
  49. }
  50. $.each(json, function (field, value) {
  51. model.set(field, value);
  52. })
  53. }
  54. }
  55. });
  56. App.QuickDataMapper = App.ServerDataMapper.extend({
  57. config: {},
  58. model: null,
  59. map: function (json) {
  60. if (!this.get('model')) {
  61. return;
  62. }
  63. if (json.items) {
  64. var result = [];
  65. json.items.forEach(function (item) {
  66. result.push(this.parseIt(item, this.config));
  67. }, this);
  68. App.store.loadMany(this.get('model'), result);
  69. }
  70. },
  71. parseIt: function (data, config) {
  72. var result = {};
  73. for ( var i in config) {
  74. if (i.substr(0, 1) === '$') {
  75. i = i.substr(1, i.length);
  76. result[i] = config['$' + i];
  77. } else {
  78. var isSpecial = false;
  79. if (i.substr(-5) == '_type') {
  80. var prefix = i.substr(0, i.length - 5);
  81. isSpecial = config[prefix + '_key'] != null;
  82. } else if (i.substr(-4) == '_key') {
  83. var prefix = i.substr(0, i.length - 4);
  84. isSpecial = config[prefix + '_type'] != null;
  85. }
  86. if (!isSpecial && typeof config[i] == 'string') {
  87. result[i] = this.getJsonProperty(data, config[i]);
  88. } else if (typeof config[i] == 'object') {
  89. result[i] = [];
  90. var _data = this.getJsonProperty(data, config[i + '_key']);
  91. var _type = config[i + '_type'];
  92. var l = _data.length;
  93. for ( var index = 0; index < l; index++) {
  94. if (_type == 'array') {
  95. result[i].push(this.getJsonProperty(_data[index], config[i].item));
  96. } else {
  97. result[i].push(this.parseIt(_data[index], config[i]));
  98. }
  99. }
  100. // As for 'widgets', just show the original order
  101. if(_type == 'array' && i != 'widgets'){
  102. result[i] = result[i].sort();
  103. }
  104. }
  105. }
  106. }
  107. return result;
  108. },
  109. getJsonProperty: function (json, path) {
  110. var pathArr = path.split('.');
  111. var current = json;
  112. while (pathArr.length && current) {
  113. if (pathArr[0].substr(-1) == ']') {
  114. var index = parseInt(pathArr[0].substr(-2, 1));
  115. var attr = pathArr[0].substr(0, pathArr[0].length - 3);
  116. if (attr in current) {
  117. current = current[attr][index];
  118. }
  119. } else {
  120. current = current[pathArr[0]];
  121. }
  122. pathArr.splice(0, 1);
  123. }
  124. return current;
  125. },
  126. /**
  127. * properly delete record from model
  128. * @param item
  129. */
  130. deleteRecord: function (item) {
  131. item.deleteRecord();
  132. App.store.commit();
  133. item.get('stateManager').transitionTo('loading');
  134. },
  135. /**
  136. * check mutable fields whether they have been changed and if positive
  137. * return host object only with properties, that contains new value
  138. * @param current
  139. * @param previous
  140. * @param fields
  141. * @return {*}
  142. */
  143. getDiscrepancies: function (current, previous, fields) {
  144. var result = {};
  145. if (previous) {
  146. fields.forEach(function (field) {
  147. if (Array.isArray(current[field])) {
  148. if (JSON.stringify(current[field]) !== JSON.stringify(previous[field])) {
  149. result[field] = current[field];
  150. result.isLoadNeeded = true;
  151. }
  152. } else {
  153. if (current[field] != previous[field]) result[field] = current[field];
  154. }
  155. });
  156. return result;
  157. }
  158. return current;
  159. },
  160. /**
  161. * Binary search <code>searchElement</code> in the array (should be sorted!)
  162. * @param {number[]|string[]} array
  163. * @param {number|string} searchElement
  164. * @returns {number} position of the needed element or negative value, if value wasn't found
  165. * @method binaryIndexOf
  166. */
  167. binaryIndexOf: function (array, searchElement) {
  168. var minIndex = 0;
  169. var maxIndex = array.length - 1;
  170. var currentIndex;
  171. var currentElement;
  172. var resultIndex;
  173. if (array[0] > searchElement || array[array.length - 1] < searchElement) {
  174. return -1;
  175. }
  176. while (minIndex <= maxIndex) {
  177. resultIndex = currentIndex = (minIndex + maxIndex) / 2 | 0;
  178. currentElement = array[currentIndex];
  179. if (currentElement < searchElement) {
  180. minIndex = currentIndex + 1;
  181. }
  182. else
  183. if (currentElement > searchElement) {
  184. maxIndex = currentIndex - 1;
  185. }
  186. else {
  187. return currentIndex;
  188. }
  189. }
  190. return ~maxIndex;
  191. }
  192. });