PageRenderTime 21ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/bigdesk/_site/js/models/cluster/NodesState.js

https://gitlab.com/zouxc/elasticsearch-cn-out-of-box
JavaScript | 84 lines | 49 code | 11 blank | 24 comment | 6 complexity | db054da4c11dd0d58fd6bb8955952bc2 MD5 | raw file
  1. /*
  2. Copyright 2011-2014 Lukas Vlcek
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * REST end point: _cluster/state/nodes,master_node
  15. * @see <a href="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/cluster-state.html">cluster state</a>
  16. */
  17. var NodeState = Backbone.Model.extend({
  18. defaults: {
  19. master: false
  20. }
  21. });
  22. var NodesState = Backbone.Collection.extend({
  23. model: NodeState,
  24. url: function() {
  25. return '/_cluster/state/nodes,master_node';
  26. },
  27. // Move important keys into values.
  28. parse: function(data) {
  29. var nodes = data.nodes;
  30. var masterId = data.master_node;
  31. nodes[masterId].master = true;
  32. var nodeIds = _.keys(nodes);
  33. var nodeValues = _.values(nodes);
  34. for (var i = 0; i < nodeIds.length; i++) {
  35. nodeValues[i].id = nodeIds[i];
  36. }
  37. return nodeValues;
  38. },
  39. // Keep nodes collection up to date.
  40. add: function(models, options) {
  41. var collection = this;
  42. delete options.silent;
  43. // add new nodes
  44. _.each(models, function(model){
  45. if (collection.get(model.id) == undefined) {
  46. Backbone.Collection.prototype.add.call(collection, model, options);
  47. } else {
  48. collection.get(model.id).set(model);
  49. }
  50. });
  51. // remove gone nodes
  52. var iterator = function(nodeState) {
  53. return (_.find(models, function(m){ return m.id == nodeState.id; }));
  54. };
  55. var rejected = collection.reject(iterator);
  56. collection.remove(rejected, options);
  57. },
  58. // return master node id if available, otherwise return empty string
  59. getMasterNodeId: function() {
  60. var masterNodeId = "";
  61. var collection = this;
  62. var masterNode = collection.find(function(node){
  63. return node.get("master");
  64. });
  65. if (masterNode) {
  66. masterNodeId = masterNode.id;
  67. }
  68. return masterNodeId;
  69. }
  70. });