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

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

https://gitlab.com/zouxc/elasticsearch-cn-out-of-box
JavaScript | 95 lines | 44 code | 12 blank | 39 comment | 5 complexity | f3a3bac9e2c743f406558f35194b20be 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: _nodes/stats?human=true
  15. * @see <a href="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/cluster-nodes-stats.html">nodes statistics<a/>
  16. */
  17. var NodeStats = Backbone.Model;
  18. var NodeStatsCollection = Backbone.Collection.extend({
  19. model: NodeStats
  20. });
  21. var NodeStatsTimestamp = Backbone.Model.extend({
  22. initialize: function(attributes){
  23. var nodes = attributes.nodes;
  24. var nodeIds = _.keys(nodes);
  25. var nodeValues = _.values(nodes);
  26. for (var i = 0; i < nodeIds.length; i++) {
  27. nodeValues[i].id = nodeIds[i];
  28. }
  29. this.set({nodes: new NodeStatsCollection(nodeValues)});
  30. this.set("id", new Date().getTime());
  31. }
  32. });
  33. var NodesStats = Backbone.Collection.extend({
  34. model: NodeStatsTimestamp,
  35. url: function() {
  36. return '/_nodes/stats?human=true';
  37. },
  38. parse: function(response) {
  39. delete response.cluster_name;
  40. return response;
  41. },
  42. // Here comes the backbone hack:
  43. //
  44. // Cluster health status is fetched using {add:true} option, which means that it will always
  45. // add incoming data, but we also need to remove data that are older then storeSize threshold.
  46. //
  47. // So we call fetch with {silent:true}, then we override add method in which we remove {silent}
  48. // from the options and "delegate to parent" to trigger the 'add' event. When we remove old data,
  49. // we call remove method which triggers 'remove' event.
  50. // As a result we can subscribe to NodesStats collection on 'add' and 'remove' events which will be
  51. // fired correctly only for those items that are added or removed.
  52. //
  53. // Also note that when we call the fetch we inject some additional metadata into options: now, storeSize.
  54. // Probably that is the way how to go about this in backbone.
  55. //
  56. // Theoretically we would not need to use {silent:true} and omit "delegation to parent". But this
  57. // way we have finer control over the flow and we can theoretically decide not to add incoming data
  58. // in the future which will allow us to control triggering 'add' event correctly.
  59. //
  60. add: function(models, options) {
  61. delete options.silent;
  62. if (options && options.now && options.storeSize) {
  63. var iterator = function(nodeStatsTimestamp) {
  64. return !(nodeStatsTimestamp.id < (options.now - options.storeSize));
  65. };
  66. var rejected = this.reject(iterator);
  67. if (rejected.length > 0) {
  68. this.remove(rejected, options);
  69. }
  70. }
  71. var parentCall = Backbone.Collection.prototype.add.call(this, models, options);
  72. // custom trigger: collection has been updated
  73. this.trigger("nodesStatsUpdated", {});
  74. return parentCall;
  75. },
  76. // make sure models are ordered by time (in case AJAX responses are returned in wrong order)
  77. comparator: function(model) {
  78. return model.id;
  79. }
  80. });