config_versions_mapper.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. App.configVersionsMapper = App.QuickDataMapper.create({
  20. model: App.ConfigVersion,
  21. propertyModel: App.ConfigProperty,
  22. config: {
  23. service_name: 'service_name',
  24. service_id: 'service_name',
  25. version: "service_config_version",
  26. create_time: 'createtime',
  27. group_id: 'group_id',
  28. group_name: 'group_name',
  29. hosts: 'hosts',
  30. author: 'user',
  31. notes: 'service_config_version_note',
  32. is_current: 'is_current',
  33. index: 'index'
  34. },
  35. map: function (json) {
  36. var configVersions = [];
  37. var itemIds = {};
  38. var serviceToHostMap = {};
  39. if (json && json.items) {
  40. json.items.forEach(function (item, index) {
  41. var parsedItem = this.parseIt(item, this.get('config'));
  42. parsedItem.id = parsedItem.service_name + '_' + parsedItem.version;
  43. parsedItem.is_requested = true;
  44. itemIds[parsedItem.id] = true;
  45. parsedItem.index = index;
  46. parsedItem.config_properties = [];
  47. if (serviceToHostMap[item.service_name]) {
  48. serviceToHostMap[item.service_name] = serviceToHostMap[item.service_name].concat(item.hosts);
  49. } else {
  50. serviceToHostMap[item.service_name] = item.hosts;
  51. }
  52. /**
  53. * parsing config properties for config version
  54. * @type {Array}
  55. */
  56. var properties = [];
  57. if (item.configurations) {
  58. item.configurations.forEach(function(config) {
  59. var type = config.type;
  60. var properties_array = Object.keys(config.properties);
  61. for (var i = 0; i < properties_array.length; i++) {
  62. var key = properties_array[i];
  63. var property = App.StackConfigProperty.find(key + '_' + type).toJSON();
  64. property.id = key + '_' + type + '_' + item.service_config_version;
  65. property.value = property.default_value = config.properties[key];
  66. property.is_final = property.default_is_final = !!item.properties_attributes && item.properties_attributes.final[key] === "true";
  67. property.config_version_id = parsedItem.id;
  68. properties.push(property);
  69. }
  70. }, this);
  71. }
  72. parsedItem.config_properties = parsedItem.config_properties.concat(properties.mapProperty('id'));
  73. App.store.loadMany(this.get('propertyModel'), properties);
  74. configVersions.push(parsedItem);
  75. }, this);
  76. this.get('model').find().forEach(function (item) {
  77. if (!itemIds[item.get('id')]) {
  78. item.set('isRequested', false);
  79. }
  80. });
  81. var itemTotal = parseInt(json.itemTotal);
  82. if (!isNaN(itemTotal)) {
  83. App.router.set('mainConfigHistoryController.filteredCount', itemTotal);
  84. }
  85. /**
  86. * this code sets hostNames for default config group
  87. * by excluding hostNames that belongs to not default groups
  88. * from list of all hosts
  89. */
  90. Object.keys(serviceToHostMap).forEach(function(sName) {
  91. var defaultHostNames = App.get('allHostNames');
  92. for (var i = 0; i < serviceToHostMap[sName].length; i++) {
  93. defaultHostNames = defaultHostNames.without(serviceToHostMap[sName][i]);
  94. }
  95. var defVer = configVersions.find(function(v) {
  96. return v.is_current && v.group_id == -1 && v.service_name == sName;
  97. });
  98. if (defVer) {
  99. defVer.hosts = defaultHostNames;
  100. }
  101. });
  102. App.store.commit();
  103. App.store.loadMany(this.get('model'), configVersions);
  104. }
  105. }
  106. });