configuration_controller.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.ConfigurationController = Em.Controller.extend({
  20. name: 'configurationController',
  21. /**
  22. * get configs by tags
  23. * return Deferred object with configs as argument
  24. * @param tags {Object}
  25. * ** siteName
  26. * ** tagName (optional)
  27. * @return {object}
  28. */
  29. getConfigsByTags: function (tags) {
  30. var storedTags = [];
  31. App.db.getConfigs().forEach(function (site) {
  32. storedTags.push({
  33. siteName: site.type,
  34. tagName: site.tag
  35. })
  36. });
  37. if (this.checkTagsChanges(tags, storedTags)) {
  38. return this.loadFromServer(tags);
  39. } else {
  40. return this.loadFromDB(tags.mapProperty('siteName'));
  41. }
  42. },
  43. /**
  44. * check whether tag versions have been changed
  45. * if they are different then return true
  46. * otherwise false
  47. * @param tags
  48. * @param storedTags
  49. * @return {Boolean}
  50. */
  51. checkTagsChanges: function (tags, storedTags) {
  52. var isDifferent = false;
  53. var i = 0;
  54. while (i < tags.length && !isDifferent) {
  55. var storedTag = storedTags.findProperty('siteName', tags[i].siteName);
  56. isDifferent = (!storedTag || !tags[i].tagName || storedTag.tagName !== tags[i].tagName);
  57. i++;
  58. }
  59. return isDifferent;
  60. },
  61. loadFromDB: function (siteNames) {
  62. var dfd = $.Deferred();
  63. var configs = App.db.getConfigs().filter(function (site) {
  64. return (siteNames.contains(site.type));
  65. });
  66. dfd.resolve(configs);
  67. return dfd.promise()
  68. },
  69. /**
  70. * load configs from server
  71. * and update them in local DB
  72. * @param tags
  73. * @return {Array}
  74. */
  75. loadFromServer: function (tags) {
  76. var self = this;
  77. var dfd = $.Deferred();
  78. if (!tags.everyProperty('tagName')) {
  79. var configTags;
  80. var jqXhr = this.loadConfigTags();
  81. jqXhr.done(function (data) {
  82. configTags = data.Clusters.desired_configs;
  83. tags.forEach(function (_tag) {
  84. if (_tag.siteName && configTags[_tag.siteName] && !_tag.tagName) {
  85. _tag.tagName = configTags[_tag.siteName].tag;
  86. }
  87. }, self);
  88. self.loadConfigsByTags(tags,dfd);
  89. });
  90. } else {
  91. self.loadConfigsByTags(tags,dfd);
  92. }
  93. return dfd.promise();
  94. },
  95. /**
  96. * loadConfigsByTags: Loads properties for a config tag
  97. * @params tags
  98. * @params dfd jqXhr promise
  99. */
  100. loadConfigsByTags: function (tags,dfd) {
  101. var self = this;
  102. var loadedConfigs = [];
  103. App.config.loadConfigsByTags(tags).done(function (data) {
  104. if (data.items) {
  105. data.items.forEach(function (item) {
  106. loadedConfigs.push(item);
  107. });
  108. }
  109. }).complete(function () {
  110. self.saveToDB(loadedConfigs);
  111. dfd.resolve(loadedConfigs);
  112. });
  113. },
  114. /**
  115. * loadConfigTags: Loads all config tags applied to the cluster
  116. * @return: jqXhr promise
  117. */
  118. loadConfigTags: function () {
  119. return App.ajax.send({
  120. name: 'config.tags',
  121. sender: this
  122. });
  123. },
  124. /**
  125. * save properties obtained from server to local DB
  126. * @param loadedConfigs
  127. */
  128. saveToDB: function (loadedConfigs) {
  129. var storedConfigs = App.db.getConfigs();
  130. loadedConfigs.forEach(function (loadedSite) {
  131. var storedSite = storedConfigs.findProperty('type', loadedSite.type);
  132. if (storedSite) {
  133. storedSite.tag = loadedSite.tag;
  134. storedSite.properties = loadedSite.properties;
  135. storedSite.properties_attributes = loadedSite.properties_attributes;
  136. } else {
  137. storedConfigs.push(loadedSite);
  138. }
  139. });
  140. App.db.setConfigs(storedConfigs);
  141. }
  142. });