server_data_mapper.js 5.7 KB

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