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