/config/plugins/visualizations/charts/static/library/datasets.js

https://bitbucket.org/cistrome/cistrome-harvard/ · JavaScript · 149 lines · 93 code · 24 blank · 32 comment · 17 complexity · a7ab127a83de0c2583c67b1f709ffecf MD5 · raw file

  1. // dependencies
  2. define(['utils/utils'], function(Utils) {
  3. // widget
  4. return Backbone.Collection.extend(
  5. {
  6. // list of datasets
  7. list: {},
  8. // initialize
  9. initialize: function(app, options)
  10. {
  11. // link app
  12. this.app = app;
  13. // configure options
  14. this.options = Utils.merge(options, this.optionsDefault);
  15. },
  16. // wait
  17. request: function(options, success, error) {
  18. // link this
  19. var self = this;
  20. // check if column data is requested
  21. if (options.groups) {
  22. this._fetch(options, success);
  23. } else {
  24. // check if dataset is available from cache
  25. var dataset = this.list[options.id];
  26. if (dataset) {
  27. success(dataset);
  28. return;
  29. }
  30. // request dataset
  31. Utils.request('GET', config.root + 'api/datasets/' + options.id, {}, function(dataset) {
  32. switch (dataset.state) {
  33. case 'error':
  34. if (error) {
  35. error(dataset);
  36. }
  37. break;
  38. default:
  39. self.list[options.id] = dataset;
  40. success(dataset);
  41. }
  42. });
  43. }
  44. },
  45. // fetch data columns into dataset object
  46. _fetch: function(options, callback) {
  47. // set offset
  48. var offset = options.start ? options.start : 0;
  49. // set limit
  50. var limit = Math.abs(options.end - options.start);
  51. var query_limit = this.app.config.get('query_limit');
  52. if (!limit || limit > query_limit) {
  53. limit = query_limit;
  54. }
  55. // get column indices
  56. var index_string = '';
  57. var index_map = {};
  58. var index_count = 0;
  59. for (var i in options.groups) {
  60. var group = options.groups[i];
  61. for (var key in group.columns) {
  62. var column = group.columns[key];
  63. // add to index string
  64. index_string += column + ',';
  65. // add to dictionary
  66. index_map[column] = index_count;
  67. // increase counter
  68. index_count++;
  69. }
  70. }
  71. if (index_count == 0) {
  72. callback({});
  73. return;
  74. }
  75. index_string = index_string.substring(0, index_string.length - 1);
  76. // initialize result dictionary
  77. var result = options.groups.slice(0);
  78. for (var i in result) {
  79. result[i].values = [];
  80. }
  81. // make request
  82. var self = this;
  83. Utils.request('GET', config.root + 'api/datasets/' + options.id, {
  84. data_type : 'raw_data',
  85. provider : 'dataset-column',
  86. limit : limit,
  87. offset : offset,
  88. indeces : index_string
  89. }, function(response) {
  90. // loop through rows
  91. for (var i in response.data) {
  92. // get row
  93. var row = response.data[i];
  94. // collect all data into the defined groups
  95. for (var j in options.groups) {
  96. // get group
  97. var group = options.groups[j];
  98. // initialize value
  99. var value = {
  100. x : parseInt(i) + offset
  101. };
  102. // fill value
  103. for (var key in group.columns) {
  104. // get column
  105. var column = group.columns[key];
  106. // identify column
  107. var index = index_map[column];
  108. // read value from row
  109. var v = row[index];
  110. if(isNaN(v) || !v) {
  111. v = 0;
  112. }
  113. // add value to dictionary
  114. value[key] = v;
  115. }
  116. // add to result
  117. result[j].values.push(value);
  118. }
  119. }
  120. // callback
  121. callback(result);
  122. });
  123. }
  124. });
  125. });