PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/dashboard/app/scripts/collections/osd-collection.js

https://github.com/hnuzhoulin/calamari-clients
JavaScript | 68 lines | 58 code | 4 blank | 6 comment | 10 complexity | 5fb72282409df33bd54e361e71b2a004 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*jshint -W106*/
  2. /*global define*/
  3. define(['underscore', 'backbone', 'models/application-model'], function(_, Backbone, models) {
  4. 'use strict';
  5. var OSDCollection = Backbone.Collection.extend({
  6. cluster: 1,
  7. epoch: 1,
  8. cluster_update_time_unix: 0,
  9. url: function() {
  10. return '/api/v1/cluster/' + this.cluster + '/osd';
  11. },
  12. initialize: function(models, options) {
  13. if (options && options.cluster) {
  14. this.cluster = options.cluster;
  15. }
  16. },
  17. parse: function(response) {
  18. this.epoch = response.epoch;
  19. this.cluster_update_time_unix = response.cluster_update_time_unix;
  20. this.pg_state_counts = response.pg_state_counts;
  21. if (response.osds) {
  22. return response.osds;
  23. }
  24. var _new = response['new'];
  25. var changed = response.changed;
  26. var removed = response.removed;
  27. if (_new && _new.length > 0) {
  28. //console.log('adding ' + _new.length);
  29. this.add(_new);
  30. }
  31. if (changed && changed.length > 0) {
  32. //console.log('changing ' + _new.length);
  33. this.set(changed, {
  34. merge: true,
  35. remove: false,
  36. add: false
  37. });
  38. }
  39. if (removed && removed.length > 0) {
  40. //console.log('removing ' + _new.length);
  41. var list = _.pluck(removed, 'osd');
  42. var self = this;
  43. this.each(function(m) {
  44. if (_.contains(list, m.get('osd'))) {
  45. self.remove(m);
  46. }
  47. });
  48. this.remove(removed);
  49. }
  50. return {};
  51. },
  52. update: function(options) {
  53. options = options ? _.clone(options) : {};
  54. options.url = this.url() + '/epoch/' + (this.epoch + 1);
  55. // Disable normal fetch processing path
  56. options.add = false;
  57. options.remove = false;
  58. options.merge = false;
  59. return this.fetch(options);
  60. },
  61. model: models.OSDModel
  62. });
  63. return OSDCollection;
  64. });