PageRenderTime 62ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/static/scripts/mvc/dataset/hda-model.js

https://bitbucket.org/galaxy/galaxy-central
JavaScript | 350 lines | 166 code | 38 blank | 146 comment | 28 complexity | c2ed7fbc196b14fc1df2f8342f8fa9cf MD5 | raw file
Possible License(s): CC-BY-3.0
  1. //define([
  2. // "../mvc/base-mvc"
  3. //], function(){
  4. //==============================================================================
  5. /** @class (HDA) model for a Galaxy dataset
  6. * related to a history.
  7. * @name HistoryDatasetAssociation
  8. *
  9. * @augments Backbone.Model
  10. * @borrows LoggableMixin#logger as #logger
  11. * @borrows LoggableMixin#log as #log
  12. * @constructs
  13. */
  14. var HistoryDatasetAssociation = Backbone.Model.extend( LoggableMixin ).extend(
  15. /** @lends HistoryDatasetAssociation.prototype */{
  16. ///** logger used to record this.log messages, commonly set to console */
  17. //// comment this out to suppress log output
  18. //logger : console,
  19. /** default attributes for a model */
  20. defaults : {
  21. // ---these are part of an HDA proper:
  22. // parent (containing) history
  23. history_id : null,
  24. // often used with tagging
  25. model_class : 'HistoryDatasetAssociation',
  26. hid : 0,
  27. // ---whereas these are Dataset related/inherited
  28. id : null,
  29. name : '(unnamed dataset)',
  30. // one of HistoryDatasetAssociation.STATES
  31. state : 'new',
  32. // sniffed datatype (sam, tabular, bed, etc.)
  33. data_type : null,
  34. // size in bytes
  35. file_size : 0,
  36. file_ext : '',
  37. // array of associated file types (eg. [ 'bam_index', ... ])
  38. meta_files : [],
  39. misc_blurb : '',
  40. misc_info : '',
  41. deleted : false,
  42. purged : false,
  43. visible : true,
  44. accessible : true
  45. },
  46. /** fetch location of this history in the api */
  47. urlRoot: 'api/histories/',
  48. url : function(){
  49. //TODO: get this via url router
  50. return this.urlRoot + this.get( 'history_id' ) + '/contents/' + this.get( 'id' );
  51. //TODO: this breaks on save()
  52. },
  53. /** Set up the model, determine if accessible, bind listeners
  54. * @see Backbone.Model#initialize
  55. */
  56. //TODO:? use initialize (or validate) to check purged AND deleted -> purged XOR deleted
  57. initialize : function(){
  58. this.log( this + '.initialize', this.attributes );
  59. this.log( '\tparent history_id: ' + this.get( 'history_id' ) );
  60. // (curr) only handles changing state of non-accessible hdas to STATES.NOT_VIEWABLE
  61. //!! this state is not in trans.app.model.Dataset.states - set it here -
  62. //TODO: change to server side.
  63. if( !this.get( 'accessible' ) ){
  64. this.set( 'state', HistoryDatasetAssociation.STATES.NOT_VIEWABLE );
  65. }
  66. // if the state has changed and the new state is a ready state, fire an event
  67. this.on( 'change:state', function( currModel, newState ){
  68. this.log( this + ' has changed state:', currModel, newState );
  69. if( this.inReadyState() ){
  70. this.trigger( 'state:ready', currModel, newState, this.previous( 'state' ) );
  71. }
  72. });
  73. // debug on change events
  74. //this.on( 'change', function( currModel, changedList ){
  75. // this.log( this + ' has changed:', currModel, changedList );
  76. //});
  77. //this.bind( 'all', function( event ){
  78. // this.log( this + '', arguments );
  79. //});
  80. },
  81. /** Is this hda deleted or purged?
  82. */
  83. isDeletedOrPurged : function(){
  84. return ( this.get( 'deleted' ) || this.get( 'purged' ) );
  85. },
  86. /** based on show_deleted, show_hidden (gen. from the container control),
  87. * would this ds show in the list of ds's?
  88. * @param {Boolean} show_deleted are we showing deleted hdas?
  89. * @param {Boolean} show_hidden are we showing hidden hdas?
  90. */
  91. //TODO: too many 'visible's
  92. isVisible : function( show_deleted, show_hidden ){
  93. var isVisible = true;
  94. if( ( !show_deleted )
  95. && ( this.get( 'deleted' ) || this.get( 'purged' ) ) ){
  96. isVisible = false;
  97. }
  98. if( ( !show_hidden )
  99. && ( !this.get( 'visible' ) ) ){
  100. isVisible = false;
  101. }
  102. return isVisible;
  103. },
  104. /** Is this HDA in a 'ready' state; where 'Ready' states are states where no
  105. * processing (for the ds) is left to do on the server.
  106. * Currently: NEW, OK, EMPTY, FAILED_METADATA, NOT_VIEWABLE, DISCARDED,
  107. * and ERROR
  108. */
  109. inReadyState : function(){
  110. var state = this.get( 'state' );
  111. //TODO: to list inclusion test
  112. //TODO: class level readyStates list
  113. return (
  114. this.isDeletedOrPurged()
  115. || ( state === HistoryDatasetAssociation.STATES.OK )
  116. || ( state === HistoryDatasetAssociation.STATES.EMPTY )
  117. || ( state === HistoryDatasetAssociation.STATES.FAILED_METADATA )
  118. || ( state === HistoryDatasetAssociation.STATES.NOT_VIEWABLE )
  119. || ( state === HistoryDatasetAssociation.STATES.DISCARDED )
  120. || ( state === HistoryDatasetAssociation.STATES.ERROR )
  121. );
  122. },
  123. /** Convenience function to match hda.has_data.
  124. */
  125. hasData : function(){
  126. //TODO:?? is this equivalent to all possible hda.has_data calls?
  127. return ( this.get( 'file_size' ) > 0 );
  128. },
  129. /** String representation
  130. */
  131. toString : function(){
  132. var nameAndId = this.get( 'id' ) || '';
  133. if( this.get( 'name' ) ){
  134. nameAndId = this.get( 'hid' ) + ' :"' + this.get( 'name' ) + '",' + nameAndId;
  135. }
  136. return 'HDA(' + nameAndId + ')';
  137. }
  138. });
  139. //------------------------------------------------------------------------------
  140. /** Class level map of possible HDA states to their string equivalents.
  141. * A port of galaxy.model.Dataset.states.
  142. */
  143. HistoryDatasetAssociation.STATES = {
  144. // NOT ready states
  145. /** is uploading and not ready */
  146. UPLOAD : 'upload',
  147. /** the job that will produce the dataset queued in the runner */
  148. QUEUED : 'queued',
  149. /** the job that will produce the dataset paused */
  150. PAUSED : 'paused',
  151. /** the job that will produce the dataset is running */
  152. RUNNING : 'running',
  153. /** metadata for the dataset is being discovered/set */
  154. SETTING_METADATA : 'setting_metadata',
  155. // ready states
  156. /** was created without a tool */
  157. NEW : 'new',
  158. /** has no data */
  159. EMPTY : 'empty',
  160. /** has successfully completed running */
  161. OK : 'ok',
  162. /** metadata discovery/setting failed or errored (but otherwise ok) */
  163. FAILED_METADATA : 'failed_metadata',
  164. /** not accessible to the current user (i.e. due to permissions) */
  165. NOT_VIEWABLE : 'noPermission', // not in trans.app.model.Dataset.states
  166. /** deleted while uploading */
  167. DISCARDED : 'discarded',
  168. /** the tool producing this dataset failed */
  169. ERROR : 'error'
  170. };
  171. //==============================================================================
  172. /** @class Backbone collection of (HDA) models
  173. *
  174. * @borrows LoggableMixin#logger as #logger
  175. * @borrows LoggableMixin#log as #log
  176. * @constructs
  177. */
  178. var HDACollection = Backbone.Collection.extend( LoggableMixin ).extend(
  179. /** @lends HDACollection.prototype */{
  180. model : HistoryDatasetAssociation,
  181. ///** logger used to record this.log messages, commonly set to console */
  182. //// comment this out to suppress log output
  183. //logger : console,
  184. /** Set up.
  185. * @see Backbone.Collection#initialize
  186. */
  187. initialize : function(){
  188. //this.bind( 'all', function( event ){
  189. // this.log( this + '', arguments );
  190. //});
  191. },
  192. /** Get the ids of every hda in this collection
  193. * @returns array of encoded ids
  194. */
  195. ids : function(){
  196. return this.map( function( hda ){ return hda.id; });
  197. },
  198. /** Get the hda with the given hid
  199. * @param {Int} hid the hid to search for
  200. * @returns {HistoryDatasetAssociation} the hda with the given hid or undefined if not found
  201. */
  202. getByHid : function( hid ){
  203. return _.first( this.filter( function( hda ){ return hda.get( 'hid' ) === hid; }) );
  204. },
  205. /** If the given hid is in the collection, return it's index. If not, return the insertion point it would need.
  206. * NOTE: assumes hids are unique and valid
  207. * @param {Int} hid the hid to find or create. If hid is 0, null, undefined: return the last hid + 1
  208. * @returns the collection index of the existing hda or an insertion point if it doesn't exist
  209. */
  210. hidToCollectionIndex : function( hid ){
  211. // if the hid is 0, null, undefined: assume a request for a new hid (return the last index)
  212. if( !hid ){
  213. return this.models.length;
  214. }
  215. var endingIndex = this.models.length - 1;
  216. //TODO: prob. more efficient to cycle backwards through these (assuming ordered by hid)
  217. for( var i=endingIndex; i>=0; i-- ){
  218. var hdaHid = this.at( i ).get( 'hid' );
  219. //this.log( i, 'hdaHid:', hdaHid );
  220. if( hdaHid == hid ){
  221. //this.log( '\t match:', hdaHid, hid, ' returning:', i );
  222. return i;
  223. }
  224. if( hdaHid < hid ){
  225. //this.log( '\t past it, returning:', ( i + 1 ) );
  226. return i + 1;
  227. }
  228. }
  229. return null;
  230. },
  231. /** Get every 'shown' hda in this collection based on show_deleted/hidden
  232. * @param {Boolean} show_deleted are we showing deleted hdas?
  233. * @param {Boolean} show_hidden are we showing hidden hdas?
  234. * @returns array of hda models
  235. * @see HistoryDatasetAssociation#isVisible
  236. */
  237. getVisible : function( show_deleted, show_hidden ){
  238. return this.filter( function( item ){ return item.isVisible( show_deleted, show_hidden ); });
  239. },
  240. /** For each possible hda state, get an array of all hda ids in that state
  241. * @returns a map of states -> hda ids
  242. * @see HistoryDatasetAssociation#STATES
  243. */
  244. getStateLists : function(){
  245. var stateLists = {};
  246. _.each( _.values( HistoryDatasetAssociation.STATES ), function( state ){
  247. stateLists[ state ] = [];
  248. });
  249. //NOTE: will err on unknown state
  250. this.each( function( item ){
  251. stateLists[ item.get( 'state' ) ].push( item.get( 'id' ) );
  252. });
  253. return stateLists;
  254. },
  255. /** Get the id of every hda in this collection not in a 'ready' state (running).
  256. * @returns an array of hda ids
  257. * @see HistoryDatasetAssociation#inReadyState
  258. */
  259. running : function(){
  260. var idList = [];
  261. this.each( function( item ){
  262. if( !item.inReadyState() ){
  263. idList.push( item.get( 'id' ) );
  264. }
  265. });
  266. return idList;
  267. },
  268. /** Set ea
  269. * Precondition: each data_array object must have an id
  270. * @param {Object[]} data_array an array of attribute objects to update the models with
  271. * @see HistoryDatasetAssociation#set
  272. */
  273. set : function( data_array ){
  274. var collection = this;
  275. if( !data_array || !_.isArray( data_array ) ){ return; }
  276. //TODO: remove when updated backbone >= 1.0
  277. data_array.forEach( function( hda_data ){
  278. var model = collection.get( hda_data.id );
  279. if( model ){
  280. model.set( hda_data );
  281. }
  282. });
  283. },
  284. /** Update (fetch) the data of the hdas with the given ids.
  285. * @param {String[]} ids an array of hda ids to update
  286. * @returns {HistoryDatasetAssociation[]} hda models that were updated
  287. * @see HistoryDatasetAssociation#fetch
  288. */
  289. update : function( ids ){
  290. this.log( this + 'update:', ids );
  291. if( !( ids && ids.length ) ){ return []; }
  292. var collection = this,
  293. updatedHdas = null;
  294. _.each( ids, function( id, index ){
  295. var hda = collection.get( id );
  296. if( hda ){
  297. hda.fetch();
  298. updatedHdas.push( hda );
  299. }
  300. });
  301. return updatedHdas;
  302. },
  303. /** String representation. */
  304. toString : function(){
  305. return ( 'HDACollection()' );
  306. }
  307. });
  308. //==============================================================================
  309. //return {
  310. // HistoryDatasetAssociation : HistoryDatasetAssociation,
  311. // HDACollection : HDACollection,
  312. //};});