PageRenderTime 38ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/assets/javascripts/cartodb/models/file.js

https://gitlab.com/henrymazer/carto-db
JavaScript | 165 lines | 109 code | 29 blank | 27 comment | 21 complexity | c590c980a3dc13f10070a426f07c4d7c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. cdb.admin.File = cdb.core.Model.extend({
  2. urlRoot: '/api/v1/upload'
  3. });
  4. cdb.admin.Files = Backbone.Collection.extend({
  5. url: '/api/v1/upload'
  6. });
  7. cdb.admin.Import = cdb.core.Model.extend({
  8. idAttribute: 'item_queue_id',
  9. urlRoot: '/api/v1/imports',
  10. initialize: function() {
  11. this.bind('change', this._checkFinish, this);
  12. },
  13. setUrlRoot: function(urlRoot) {
  14. this.urlRoot = urlRoot;
  15. },
  16. /**
  17. * checks for poll to finish
  18. */
  19. pollCheck: function(i) {
  20. var self = this;
  21. var tries = 0;
  22. this.pollTimer = setInterval(function() {
  23. // cdb.log.debug("checking job for finish: " + tries);
  24. self.fetch({
  25. error: function(e) {
  26. self.trigger("change");
  27. }
  28. });
  29. ++tries;
  30. }, i || 1500);
  31. },
  32. destroyCheck: function() {
  33. clearInterval(this.pollTimer);
  34. },
  35. _checkFinish: function() {
  36. // cdb.log.info("state: " + this.get('state'), "success: " + this.get("success"));
  37. if(this.get('success') === true) {
  38. // cdb.log.debug("job finished");
  39. clearInterval(this.pollTimer);
  40. this.trigger('importComplete', this);
  41. } else if (this.get('success') === false) {
  42. // cdb.log.debug("job failure");
  43. clearInterval(this.pollTimer);
  44. this.trigger('importError', this);
  45. } else {
  46. this.trigger('importChange', this);
  47. }
  48. }
  49. });
  50. cdb.admin.Imports = cdb.core.Model.extend({
  51. url: '/api/v1/imports',
  52. initialize: function() {
  53. this.bind('change', this._checkImports, this);
  54. this.activeImports; // Active imports
  55. this.failedImports = []; // Failed imports
  56. this.lastImport; // Last import being checked
  57. },
  58. /**
  59. * checks for poll to finish
  60. */
  61. pollCheck: function(i) {
  62. var self = this;
  63. var tries = 0;
  64. this.pollTimer = setInterval(function() {
  65. // cdb.log.debug("checking imports: " + tries);
  66. self.fetch({
  67. error: function(e) {
  68. self.trigger("change");
  69. }
  70. });
  71. ++tries;
  72. }, i || 2000);
  73. },
  74. destroyCheck: function() {
  75. clearInterval(this.pollTimer);
  76. },
  77. _checkImports: function() {
  78. // Check if there will be any imports
  79. if (_.size(this.get("imports")) == 0 && !this.activeImports) {
  80. this.trigger("importsEmpty");
  81. // Stop poll
  82. this.destroyCheck();
  83. return false;
  84. }
  85. // Save imports if we don't have active imports "filled"
  86. if (!this.activeImports) {
  87. this.activeImports = this.get("imports");
  88. this.trigger("importsStart");
  89. }
  90. if (this.get("imports").length == 0 &&
  91. this.activeImports.length == 0) {
  92. // Stop poll
  93. this.destroyCheck();
  94. // Trigger error event if there was any
  95. if (this.failedImports.length > 0) {
  96. // Sending all failed imports
  97. this.trigger("importsFailed", this.failedImports);
  98. } else {
  99. // Trigger finished event
  100. this.trigger("importsFinished", this.lastImport);
  101. }
  102. } else if (this.activeImports.length != this.get("imports").length) {
  103. // Check if there is any difference between imports from back-end
  104. // and our active imports (item_queue_id)
  105. // if we are not checking any single import, check first
  106. var completed_imports = _.difference(this.activeImports, this.get("imports"));
  107. if (!this.lastImport && completed_imports.length > 0) {
  108. this._checkImport(completed_imports[0]);
  109. }
  110. }
  111. },
  112. _checkImport: function(item_queue_id) {
  113. this.lastImport = new cdb.admin.Import({ item_queue_id: item_queue_id });
  114. var self = this;
  115. this.lastImport.bind("importComplete", function(e){
  116. // Remove this import from active :)
  117. var queue_id = self.lastImport.get("item_queue_id");
  118. self._removeImport(queue_id);
  119. },this).bind("importError", function(e){
  120. // Add this import to failed imports :(
  121. self.failedImports.push(self.lastImport);
  122. // Reset import
  123. var queue_id = self.lastImport.get("item_queue_id");
  124. self._removeImport(queue_id);
  125. },this);
  126. this.lastImport.pollCheck();
  127. },
  128. _removeImport: function(queue_id) {
  129. this.lastImport.unbind();
  130. // Remove it from activeImports
  131. this.activeImports = _.filter(this.activeImports, function(import_queue){ return import_queue != queue_id; });
  132. // Check imports due to the fact that is possible
  133. // to not have any more imports in the queue
  134. this._checkImports();
  135. }
  136. });