server_data_mapper.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. 'hostComponentsOnService': {}
  28. };
  29. App.ServerDataMapper = Em.Object.extend({
  30. jsonKey: false,
  31. map: function (json) {
  32. if (json) {
  33. var model = this.get('model');
  34. var jsonKey = this.get('jsonKey');
  35. if (jsonKey && json[jsonKey]) { // if data come as { hdfs: {...} }
  36. json = json[jsonKey];
  37. }
  38. $.each(json, function (field, value) {
  39. model.set(field, value);
  40. })
  41. }
  42. }
  43. });
  44. App.QuickDataMapper = App.ServerDataMapper.extend({
  45. config: {},
  46. model: null,
  47. map: function (json) {
  48. if (!this.get('model')) {
  49. return;
  50. }
  51. if (json.items) {
  52. var result = [];
  53. json.items.forEach(function (item) {
  54. result.push(this.parseIt(item, this.config));
  55. }, this);
  56. App.store.loadMany(this.get('model'), result);
  57. }
  58. },
  59. parseIt: function (data, config) {
  60. var result = {};
  61. for ( var i in config) {
  62. if (i.substr(0, 1) === '$') {
  63. i = i.substr(1, i.length);
  64. result[i] = config['$' + i];
  65. } else {
  66. var isSpecial = false;
  67. if (i.substr(-5) == '_type') {
  68. var prefix = i.substr(0, i.length - 5);
  69. isSpecial = config[prefix + '_key'] != null;
  70. } else if (i.substr(-4) == '_key') {
  71. var prefix = i.substr(0, i.length - 4);
  72. isSpecial = config[prefix + '_type'] != null;
  73. }
  74. if (!isSpecial && typeof config[i] == 'string') {
  75. result[i] = this.getJsonProperty(data, config[i]);
  76. } else if (typeof config[i] == 'object') {
  77. result[i] = [];
  78. var _data = this.getJsonProperty(data, config[i + '_key']);
  79. var _type = config[i + '_type'];
  80. var l = _data.length;
  81. for ( var index = 0; index < l; index++) {
  82. if (_type == 'array') {
  83. result[i].push(this.getJsonProperty(_data[index], config[i].item));
  84. } else {
  85. result[i].push(this.parseIt(_data[index], config[i]));
  86. }
  87. }
  88. if(_type == 'array'){
  89. result[i] = result[i].sort();
  90. }
  91. }
  92. }
  93. }
  94. return result;
  95. },
  96. getJsonProperty: function (json, path) {
  97. var pathArr = path.split('.');
  98. var current = json;
  99. while (pathArr.length && current) {
  100. if (pathArr[0].substr(-1) == ']') {
  101. var index = parseInt(pathArr[0].substr(-2, 1));
  102. var attr = pathArr[0].substr(0, pathArr[0].length - 3);
  103. if (attr in current) {
  104. current = current[attr][index];
  105. }
  106. } else {
  107. current = current[pathArr[0]];
  108. }
  109. pathArr.splice(0, 1);
  110. }
  111. return current;
  112. },
  113. calculateState: function (json) {
  114. // var stateEqual = (json.desired_status != json.work_status);
  115. // if (stateEqual) {
  116. // if (json.desired_status == 'STARTED' && json.work_status == 'INSTALLED') {
  117. // json.work_status = 'STARTING';
  118. // } else if (json.desired_status == 'INSTALLED' && json.work_status == 'STARTED') {
  119. // json.work_status = 'STOPPING';
  120. // }
  121. // }
  122. return json;
  123. }
  124. });