service_config_version_mapper.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.serviceConfigVersionsMapper = App.QuickDataMapper.create({
  20. model: App.ServiceConfigVersion,
  21. config: {
  22. service_name: 'service_name',
  23. service_id: 'service_name',
  24. version: "service_config_version",
  25. create_time: 'createtime',
  26. group_id: 'group_id',
  27. group_name: 'group_name',
  28. hosts: 'hosts',
  29. author: 'user',
  30. notes: 'service_config_version_note',
  31. is_current: 'is_current',
  32. index: 'index'
  33. },
  34. map: function (json) {
  35. var result = [];
  36. var itemIds = {};
  37. var serviceToHostMap = {};
  38. if (json && json.items) {
  39. json.items.forEach(function (item, index) {
  40. var parsedItem = this.parseIt(item, this.get('config'));
  41. parsedItem.id = parsedItem.service_name + '_' + parsedItem.version;
  42. parsedItem.is_requested = true;
  43. itemIds[parsedItem.id] = true;
  44. parsedItem.index = index;
  45. if (serviceToHostMap[item.service_name]) {
  46. serviceToHostMap[item.service_name] = serviceToHostMap[item.service_name].concat(item.hosts);
  47. } else {
  48. serviceToHostMap[item.service_name] = item.hosts;
  49. }
  50. result.push(parsedItem);
  51. }, this);
  52. this.get('model').find().forEach(function (item) {
  53. if (!itemIds[item.get('id')]) {
  54. item.set('isRequested', false);
  55. }
  56. });
  57. var itemTotal = parseInt(json.itemTotal);
  58. if (!isNaN(itemTotal)) {
  59. App.router.set('mainConfigHistoryController.filteredCount', itemTotal);
  60. }
  61. /**
  62. * this code sets hostNames for default confg group
  63. * by excluding hostNames that belongs to not default groups
  64. * from list of all hosts
  65. */
  66. Object.keys(serviceToHostMap).forEach(function(sName) {
  67. var defaultHostNames = App.get('allHostNames');
  68. for (var i = 0; i < serviceToHostMap[sName].length; i++) {
  69. defaultHostNames = defaultHostNames.without(serviceToHostMap[sName][i]);
  70. }
  71. var defVer = result.find(function(v) {
  72. return v.is_current && v.group_id == -1 && v.service_name == sName;
  73. });
  74. if (defVer) {
  75. defVer.hosts = defaultHostNames;
  76. }
  77. });
  78. App.store.commit();
  79. App.store.loadMany(this.get('model'), result);
  80. }
  81. }
  82. });