config_groups_mapper.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. /**
  18. * THIS IS NOT USED FOR NOW
  19. * FOR CONFIG GROUPS WE ARE USING OLD MODELS AND LOGIC
  20. */
  21. var App = require('app');
  22. App.configGroupsMapper = App.QuickDataMapper.create({
  23. model: App.ServiceConfigGroup,
  24. config: {
  25. id: 'id',
  26. config_group_id: 'ConfigGroup.id',
  27. name: 'ConfigGroup.group_name',
  28. service_name: 'ConfigGroup.tag',
  29. description: 'ConfigGroup.description',
  30. hosts: 'hosts',
  31. service_id: 'ConfigGroup.tag',
  32. desired_configs: 'ConfigGroup.desired_configs'
  33. },
  34. /**
  35. * using this config when saving group from config_version api
  36. */
  37. config2: {
  38. id: 'id',
  39. config_group_id: 'group_id',
  40. name: 'group_name',
  41. service_name: 'service_name',
  42. hosts: 'hosts',
  43. service_id: 'service_name'
  44. },
  45. map: function (json, mapFromVersions, serviceNames) {
  46. console.time('App.configGroupsMapper');
  47. if (serviceNames && serviceNames.length > 0) {
  48. var configGroups = [];
  49. /**
  50. * ex: { "HDFS": ["host1", "host2"], "YARN": ["host1"] }
  51. * this property is used to store host names for default config group.
  52. * While parsing data for not default groups host names will be excluded from this list.
  53. * In case there is no not default config groups for some service <code>hostNamesForService<code>
  54. * will not contain property for this service which mean all host belongs to default group
  55. */
  56. var hostNamesForService = {};
  57. var configGroupsForService = {};
  58. if (json && json.items) {
  59. json.items.forEach(function (configGroup) {
  60. if (configGroup.group_name != 'default') {
  61. if (mapFromVersions) {
  62. configGroup.id = App.ServiceConfigGroup.groupId(configGroup.service_name, configGroup.group_name);
  63. } else {
  64. configGroup.id = App.ServiceConfigGroup.groupId(configGroup.ConfigGroup.tag, configGroup.ConfigGroup.group_name);
  65. configGroup.hosts = configGroup.ConfigGroup.hosts.mapProperty('host_name');
  66. configGroup.service_name = configGroup.ConfigGroup.tag;
  67. }
  68. /**
  69. * creating (if not exists) field in <code>hostNamesForService<code> with host names for default group
  70. */
  71. if (!hostNamesForService[configGroup.service_name]) {
  72. hostNamesForService[configGroup.service_name] = App.get('allHostNames').slice(0);
  73. }
  74. if (!configGroupsForService[configGroup.service_name]) {
  75. configGroupsForService[configGroup.service_name] = [configGroup.id];
  76. }
  77. configGroupsForService[configGroup.service_name].push(configGroup.id);
  78. /**
  79. * excluding host names that belongs for current config group from default group
  80. */
  81. configGroup.hosts.forEach(function (host) {
  82. hostNamesForService[configGroup.service_name].splice(hostNamesForService[configGroup.service_name].indexOf(host), 1);
  83. });
  84. configGroup = this.parseIt(configGroup, (mapFromVersions ? this.get('config2') : this.get('config')));
  85. configGroup.parent_config_group_id = App.ServiceConfigGroup.getParentConfigGroupId(configGroup.service_name);
  86. configGroups.push(configGroup);
  87. }
  88. }, this);
  89. }
  90. /**
  91. * generating default config groups
  92. */
  93. serviceNames.forEach(function (serviceName) {
  94. configGroups.push(this.generateDefaultGroup(serviceName, hostNamesForService[serviceName], configGroupsForService[serviceName]));
  95. }, this);
  96. configGroups.sort(function (configGroupA, configGroupB) {
  97. return configGroupA.config_group_id == -1 || (configGroupA.name > configGroupB.name);
  98. });
  99. App.store.loadMany(this.get('model'), configGroups);
  100. App.store.commit();
  101. }
  102. console.timeEnd('App.configGroupsMapper');
  103. },
  104. /**
  105. * generate mock object for default config group
  106. * @param {string} serviceName
  107. * @param {string[]} [hostNames=null]
  108. * @param {Array} childConfigGroups
  109. * @returns {{id: string, config_group_id: string, name: string, service_name: string, description: string, host_names: [string], service_id: string}}
  110. */
  111. generateDefaultGroup: function (serviceName, hostNames, childConfigGroups) {
  112. return {
  113. id: App.ServiceConfigGroup.getParentConfigGroupId(serviceName),
  114. config_group_id: -1,
  115. name: 'Default',
  116. service_name: serviceName,
  117. description: 'Default cluster level ' + App.format.role(serviceName, true) + ' configuration',
  118. hosts: hostNames ? hostNames.slice() : App.get('allHostNames').slice(),
  119. child_config_groups: childConfigGroups ? childConfigGroups.uniq() : [],
  120. service_id: serviceName,
  121. desired_configs: [],
  122. properties: []
  123. }
  124. }
  125. });