server_data_mapper.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. 'Hosts': {},
  25. 'previousHostStatuses': {},
  26. 'previousComponentStatuses': {},
  27. 'previousComponentPassiveStates': {},
  28. 'hostComponentsOnService': {},
  29. 'services': [],
  30. 'hostComponentRecordsMap': {}
  31. };
  32. App.ServerDataMapper = Em.Object.extend({
  33. jsonKey: false,
  34. map: function (json) {
  35. if (json) {
  36. var model = this.get('model');
  37. var jsonKey = this.get('jsonKey');
  38. if (jsonKey && json[jsonKey]) { // if data come as { hdfs: {...} }
  39. json = json[jsonKey];
  40. }
  41. $.each(json, function (field, value) {
  42. model.set(field, value);
  43. })
  44. }
  45. }
  46. });
  47. App.QuickDataMapper = App.ServerDataMapper.extend({
  48. config: {},
  49. model: null,
  50. map: function (json) {
  51. if (!this.get('model')) {
  52. return;
  53. }
  54. if (json.items) {
  55. var result = [];
  56. json.items.forEach(function (item) {
  57. result.push(this.parseIt(item, this.config));
  58. }, this);
  59. App.store.loadMany(this.get('model'), result);
  60. }
  61. },
  62. parseIt: function (data, config) {
  63. var result = {};
  64. for ( var i in config) {
  65. if (i.substr(0, 1) === '$') {
  66. i = i.substr(1, i.length);
  67. result[i] = config['$' + i];
  68. } else {
  69. var isSpecial = false;
  70. if (i.substr(-5) == '_type') {
  71. var prefix = i.substr(0, i.length - 5);
  72. isSpecial = config[prefix + '_key'] != null;
  73. } else if (i.substr(-4) == '_key') {
  74. var prefix = i.substr(0, i.length - 4);
  75. isSpecial = config[prefix + '_type'] != null;
  76. }
  77. if (!isSpecial && typeof config[i] == 'string') {
  78. result[i] = this.getJsonProperty(data, config[i]);
  79. } else if (typeof config[i] == 'object') {
  80. result[i] = [];
  81. var _data = this.getJsonProperty(data, config[i + '_key']);
  82. var _type = config[i + '_type'];
  83. var l = _data.length;
  84. for ( var index = 0; index < l; index++) {
  85. if (_type == 'array') {
  86. result[i].push(this.getJsonProperty(_data[index], config[i].item));
  87. } else {
  88. result[i].push(this.parseIt(_data[index], config[i]));
  89. }
  90. }
  91. if(_type == 'array'){
  92. result[i] = result[i].sort();
  93. }
  94. }
  95. }
  96. }
  97. return result;
  98. },
  99. getJsonProperty: function (json, path) {
  100. var pathArr = path.split('.');
  101. var current = json;
  102. pathArr = this.filterDotted(pathArr);
  103. while (pathArr.length && current) {
  104. if (pathArr[0].substr(-1) == ']') {
  105. var index = parseInt(pathArr[0].substr(-2, 1));
  106. var attr = pathArr[0].substr(0, pathArr[0].length - 3);
  107. if (attr in current) {
  108. current = current[attr][index];
  109. }
  110. } else {
  111. current = current[pathArr[0]];
  112. }
  113. pathArr.splice(0, 1);
  114. }
  115. return current;
  116. },
  117. filterDotted: function(arr) {
  118. var buffer = [];
  119. var dottedBuffer = [];
  120. var dotted = false;
  121. arr.forEach(function(item) {
  122. if(/\['|\["/.test(item)) {
  123. dottedBuffer.push(item.substr(2, item.length));
  124. dotted = true;
  125. } else if (dotted && !/\]'|"\]/.test(item)) {
  126. dottedBuffer.push(item);
  127. } else if (/']|"]/.test(item)) {
  128. dottedBuffer.push(item.substr(0, item.length - 2));
  129. buffer.push(dottedBuffer.join('.'));
  130. dotted = false;
  131. dottedBuffer = [];
  132. } else {
  133. buffer.push(item);
  134. }
  135. });
  136. return buffer;
  137. },
  138. /**
  139. * properly delete record from model
  140. * @param item
  141. */
  142. deleteRecord: function (item) {
  143. item.deleteRecord();
  144. App.store.commit();
  145. item.get('stateManager').transitionTo('loading');
  146. console.log('Record with id:' + item.get('id') + ' was deleted from model');
  147. },
  148. /**
  149. * check mutable fields whether they have been changed and if positive
  150. * return host object only with properties, that contains new value
  151. * @param current
  152. * @param previous
  153. * @param fields
  154. * @return {*}
  155. */
  156. getDiscrepancies: function (current, previous, fields) {
  157. var result = {};
  158. if (previous) {
  159. fields.forEach(function (field) {
  160. if (Array.isArray(current[field])) {
  161. if (JSON.stringify(current[field]) !== JSON.stringify(previous[field])) {
  162. result[field] = current[field];
  163. result.isLoadNeeded = true;
  164. }
  165. } else {
  166. if (current[field] != previous[field]) result[field] = current[field];
  167. }
  168. });
  169. return result;
  170. }
  171. return current;
  172. },
  173. calculateState: function (json) {
  174. // var stateEqual = (json.desired_status != json.work_status);
  175. // if (stateEqual) {
  176. // if (json.desired_status == 'STARTED' && json.work_status == 'INSTALLED') {
  177. // json.work_status = 'STARTING';
  178. // } else if (json.desired_status == 'INSTALLED' && json.work_status == 'STARTED') {
  179. // json.work_status = 'STOPPING';
  180. // }
  181. // }
  182. return json;
  183. }
  184. });
  185. App.QuickDataMapper.componentServiceMap = function () {
  186. return {
  187. 'NAMENODE': 'HDFS',
  188. 'SECONDARY_NAMENODE': 'HDFS',
  189. 'DATANODE': 'HDFS',
  190. 'HDFS_CLIENT': 'HDFS',
  191. 'JOURNALNODE': 'HDFS',
  192. 'ZKFC': 'HDFS',
  193. 'JOBTRACKER': 'MAPREDUCE',
  194. 'TASKTRACKER': 'MAPREDUCE',
  195. 'MAPREDUCE_CLIENT': 'MAPREDUCE',
  196. 'MAPREDUCE2_CLIENT': 'MAPREDUCE2',
  197. 'HISTORYSERVER': App.get('isHadoop2Stack') ? 'MAPREDUCE2' : 'MAPREDUCE',
  198. 'TEZ_CLIENT': 'TEZ',
  199. 'RESOURCEMANAGER': 'YARN',
  200. 'YARN_CLIENT': 'YARN',
  201. 'NODEMANAGER': 'YARN',
  202. 'APP_TIMELINE_SERVER': 'YARN',
  203. 'ZOOKEEPER_SERVER': 'ZOOKEEPER',
  204. 'ZOOKEEPER_CLIENT': 'ZOOKEEPER',
  205. 'HBASE_MASTER': 'HBASE',
  206. 'HBASE_REGIONSERVER': 'HBASE',
  207. 'HBASE_CLIENT': 'HBASE',
  208. 'PIG': 'PIG',
  209. 'SQOOP': 'SQOOP',
  210. 'OOZIE_SERVER': 'OOZIE',
  211. 'OOZIE_CLIENT': 'OOZIE',
  212. 'HIVE_SERVER': 'HIVE',
  213. 'HIVE_METASTORE': 'HIVE',
  214. 'HIVE_CLIENT': 'HIVE',
  215. 'MYSQL_SERVER': 'HIVE',
  216. 'HCAT': 'HCATALOG',
  217. 'WEBHCAT_SERVER': 'WEBHCAT',
  218. 'NAGIOS_SERVER': 'NAGIOS',
  219. 'GANGLIA_SERVER': 'GANGLIA',
  220. 'GANGLIA_MONITOR': 'GANGLIA',
  221. 'KERBEROS_SERVER': 'KERBEROS',
  222. 'KERBEROS_ADMIN_CLIENT': 'KERBEROS',
  223. 'KERBEROS_CLIENT': 'KERBEROS',
  224. 'HUE_SERVER': 'HUE',
  225. 'GLUSTERFS_CLIENT': 'GLUSTERFS',
  226. 'FALCON_SERVER': 'FALCON',
  227. 'FALCON_CLIENT': 'FALCON',
  228. 'NIMBUS': 'STORM',
  229. 'SUPERVISOR': 'STORM',
  230. 'STORM_UI_SERVER': 'STORM',
  231. 'DRPC_SERVER': 'STORM',
  232. 'STORM_REST_API': 'STORM'
  233. }
  234. };