server_data_mapper.js 3.9 KB

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