PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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