PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/client/galaxy/scripts/mvc/job/job-model.js

https://bitbucket.org/galaxy/galaxy-central/
JavaScript | 202 lines | 118 code | 32 blank | 52 comment | 2 complexity | a13bc2462131a4e0a4e7b13a042486b5 MD5 | raw file
Possible License(s): CC-BY-3.0
  1. define([
  2. "mvc/history/history-contents",
  3. "mvc/dataset/states",
  4. "utils/ajax-queue",
  5. "mvc/base-mvc",
  6. "utils/localization"
  7. ], function( HISTORY_CONTENTS, STATES, AJAX_QUEUE, BASE_MVC, _l ){
  8. var logNamespace = 'jobs';
  9. //==============================================================================
  10. var searchableMixin = BASE_MVC.SearchableModelMixin;
  11. /** @class Represents a job running or ran on the server job handlers.
  12. */
  13. var Job = Backbone.Model
  14. .extend( BASE_MVC.LoggableMixin )
  15. .extend( BASE_MVC.mixin( searchableMixin, /** @lends Job.prototype */{
  16. _logNamespace : logNamespace,
  17. /** default attributes for a model */
  18. defaults : {
  19. model_class : 'Job',
  20. tool_id : null,
  21. exit_code : null,
  22. inputs : {},
  23. outputs : {},
  24. params : {},
  25. create_time : null,
  26. update_time : null,
  27. state : STATES.NEW
  28. },
  29. /** override to parse params on incomming */
  30. parse : function( response, options ){
  31. response.params = this.parseParams( response.params );
  32. return response;
  33. },
  34. /** override to treat param values as json */
  35. parseParams : function( params ){
  36. var newParams = {};
  37. _.each( params, function( value, key ){
  38. newParams[ key ] = JSON.parse( value );
  39. });
  40. return newParams;
  41. },
  42. /** instance vars and listeners */
  43. initialize : function( attributes, options ){
  44. this.debug( this + '(Job).initialize', attributes, options );
  45. this.set( 'params', this.parseParams( this.get( 'params' ) ), { silent: true });
  46. this.outputCollection = attributes.outputCollection || new HISTORY_CONTENTS.HistoryContents([]);
  47. this._setUpListeners();
  48. },
  49. /** set up any event listeners
  50. * event: state:ready fired when this DA moves into/is already in a ready state
  51. */
  52. _setUpListeners : function(){
  53. // if the state has changed and the new state is a ready state, fire an event
  54. this.on( 'change:state', function( currModel, newState ){
  55. this.log( this + ' has changed state:', currModel, newState );
  56. if( this.inReadyState() ){
  57. this.trigger( 'state:ready', currModel, newState, this.previous( 'state' ) );
  58. }
  59. });
  60. },
  61. // ........................................................................ common queries
  62. /** Is this job in a 'ready' state; where 'Ready' states are states where no
  63. * processing is left to do on the server.
  64. */
  65. inReadyState : function(){
  66. return _.contains( STATES.READY_STATES, this.get( 'state' ) );
  67. },
  68. /** Does this model already contain detailed data (as opposed to just summary level data)? */
  69. hasDetails : function(){
  70. //?? this may not be reliable
  71. return !_.isEmpty( this.get( 'outputs' ) );
  72. },
  73. // ........................................................................ ajax
  74. /** root api url */
  75. urlRoot : Galaxy.root + 'api/jobs',
  76. //url : function(){ return this.urlRoot; },
  77. // ........................................................................ searching
  78. // see base-mvc, SearchableModelMixin
  79. /** what attributes of an Job will be used in a text search */
  80. //searchAttributes : [
  81. // 'tool'
  82. //],
  83. // ........................................................................ misc
  84. /** String representation */
  85. toString : function(){
  86. return [ 'Job(', this.get( 'id' ), ':', this.get( 'tool_id' ), ')' ].join( '' );
  87. }
  88. }));
  89. //==============================================================================
  90. /** @class Backbone collection for Jobs.
  91. */
  92. var JobCollection = Backbone.Collection
  93. .extend( BASE_MVC.LoggableMixin )
  94. .extend(/** @lends JobCollection.prototype */{
  95. _logNamespace : logNamespace,
  96. model : Job,
  97. /** root api url */
  98. urlRoot : Galaxy.root + 'api/jobs',
  99. url : function(){ return this.urlRoot; },
  100. intialize : function( models, options ){
  101. console.debug( models, options );
  102. },
  103. // ........................................................................ common queries
  104. /** Get the ids of every item in this collection
  105. * @returns array of encoded ids
  106. */
  107. ids : function(){
  108. return this.map( function( item ){ return item.get( 'id' ); });
  109. },
  110. /** Get jobs that are not ready
  111. * @returns array of content models
  112. */
  113. notReady : function(){
  114. return this.filter( function( job ){
  115. return !job.inReadyState();
  116. });
  117. },
  118. /** return true if any jobs don't have details */
  119. haveDetails : function(){
  120. return this.all( function( job ){ return job.hasDetails(); });
  121. },
  122. // ........................................................................ ajax
  123. /** fetches all details for each job in the collection using a queue */
  124. queueDetailFetching : function(){
  125. var collection = this,
  126. queue = new AJAX_QUEUE.AjaxQueue( this.map( function( job ){
  127. return function(){
  128. return job.fetch({ silent: true });
  129. };
  130. }));
  131. queue.done( function(){
  132. collection.trigger( 'details-loaded' );
  133. });
  134. return queue;
  135. },
  136. //toDAG : function(){
  137. // return new JobDAG( this.toJSON() );
  138. //},
  139. // ........................................................................ sorting/filtering
  140. /** return a new collection of jobs whose attributes contain the substring matchesWhat */
  141. matches : function( matchesWhat ){
  142. return this.filter( function( job ){
  143. return job.matches( matchesWhat );
  144. });
  145. },
  146. // ........................................................................ misc
  147. /** String representation. */
  148. toString : function(){
  149. return ([ 'JobCollection(', this.length, ')' ].join( '' ));
  150. }
  151. //----------------------------------------------------------------------------- class vars
  152. }, {
  153. /** class level fn for fetching the job details for all jobs in a history */
  154. fromHistory : function( historyId ){
  155. console.debug( this );
  156. var Collection = this,
  157. collection = new Collection([]);
  158. collection.fetch({ data: { history_id: historyId }})
  159. .done( function(){
  160. window.queue = collection.queueDetailFetching();
  161. });
  162. return collection;
  163. }
  164. });
  165. //=============================================================================
  166. return {
  167. Job : Job,
  168. JobCollection : JobCollection
  169. };
  170. });