PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

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