server_data_mapper.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. if(_type == 'array'){
  90. result[i] = result[i].sort();
  91. }
  92. }
  93. }
  94. }
  95. return result;
  96. },
  97. getJsonProperty: function (json, path) {
  98. var pathArr = path.split('.');
  99. var current = json;
  100. pathArr = this.filterDotted(pathArr);
  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. filterDotted: function(arr) {
  116. var buffer = [];
  117. var dottedBuffer = [];
  118. var dotted = false;
  119. arr.forEach(function(item) {
  120. if(/\['|\["/.test(item)) {
  121. dottedBuffer.push(item.substr(2, item.length));
  122. dotted = true;
  123. } else if (dotted && !/\]'|"\]/.test(item)) {
  124. dottedBuffer.push(item);
  125. } else if (/']|"]/.test(item)) {
  126. dottedBuffer.push(item.substr(0, item.length - 2));
  127. buffer.push(dottedBuffer.join('.'));
  128. dotted = false;
  129. dottedBuffer = [];
  130. } else {
  131. buffer.push(item);
  132. }
  133. });
  134. return buffer;
  135. },
  136. /**
  137. * properly delete record from model
  138. * @param item
  139. */
  140. deleteRecord: function (item) {
  141. item.deleteRecord();
  142. App.store.commit();
  143. item.get('stateManager').transitionTo('loading');
  144. console.log('Record with id:' + item.get('id') + ' was deleted from model');
  145. },
  146. /**
  147. * check mutable fields whether they have been changed and if positive
  148. * return host object only with properties, that contains new value
  149. * @param current
  150. * @param previous
  151. * @param fields
  152. * @return {*}
  153. */
  154. getDiscrepancies: function (current, previous, fields) {
  155. var result = {};
  156. if (previous) {
  157. fields.forEach(function (field) {
  158. if (Array.isArray(current[field])) {
  159. if (JSON.stringify(current[field]) !== JSON.stringify(previous[field])) {
  160. result[field] = current[field];
  161. result.isLoadNeeded = true;
  162. }
  163. } else {
  164. if (current[field] != previous[field]) result[field] = current[field];
  165. }
  166. });
  167. return result;
  168. }
  169. return current;
  170. },
  171. calculateState: function (json) {
  172. // var stateEqual = (json.desired_status != json.work_status);
  173. // if (stateEqual) {
  174. // if (json.desired_status == 'STARTED' && json.work_status == 'INSTALLED') {
  175. // json.work_status = 'STARTING';
  176. // } else if (json.desired_status == 'INSTALLED' && json.work_status == 'STARTED') {
  177. // json.work_status = 'STOPPING';
  178. // }
  179. // }
  180. return json;
  181. }
  182. });