Source: api/analytics.js

  1. /**
  2. * @file Manages Salesforce Analytics API
  3. * @author Shinichi Tomita <shinichi.tomita@gmail.com>
  4. */
  5. 'use strict';
  6. var _ = require('lodash/core'),
  7. jsforce = require('../core'),
  8. Promise = require('../promise');
  9. /**
  10. * Report instance to retrieving asynchronously executed result
  11. *
  12. * @protected
  13. * @class Analytics~ReportInstance
  14. * @param {Analytics~Report} report - Report
  15. * @param {String} id - Report instance id
  16. */
  17. var ReportInstance = function(report, id) {
  18. this._report = report;
  19. this._conn = report._conn;
  20. this.id = id;
  21. };
  22. /**
  23. * Retrieve report result asynchronously executed
  24. *
  25. * @method Analytics~ReportInstance#retrieve
  26. * @param {Callback.<Analytics~ReportResult>} [callback] - Callback function
  27. * @returns {Promise.<Analytics~ReportResult>}
  28. */
  29. ReportInstance.prototype.retrieve = function(callback) {
  30. var conn = this._conn,
  31. report = this._report;
  32. var url = [ conn._baseUrl(), "analytics", "reports", report.id, "instances", this.id ].join('/');
  33. return conn.request(url).thenCall(callback);
  34. };
  35. /**
  36. * Report object in Analytics API
  37. *
  38. * @protected
  39. * @class Analytics~Report
  40. * @param {Connection} conn Connection
  41. */
  42. var Report = function(conn, id) {
  43. this._conn = conn;
  44. this.id = id;
  45. };
  46. /**
  47. * Describe report metadata
  48. *
  49. * @method Analytics~Report#describe
  50. * @param {Callback.<Analytics~ReportMetadata>} [callback] - Callback function
  51. * @returns {Promise.<Analytics~ReportMetadata>}
  52. */
  53. Report.prototype.describe = function(callback) {
  54. var url = [ this._conn._baseUrl(), "analytics", "reports", this.id, "describe" ].join('/');
  55. return this._conn.request(url).thenCall(callback);
  56. };
  57. /**
  58. * Explain plan for executing report
  59. *
  60. * @method Analytics~Report#explain
  61. * @param {Callback.<ExplainInfo>} [callback] - Callback function
  62. * @returns {Promise.<ExplainInfo>}
  63. */
  64. Report.prototype.explain = function(callback) {
  65. var url = "/query/?explain=" + this.id;
  66. return this._conn.request(url).thenCall(callback);
  67. };
  68. /**
  69. * Run report synchronously
  70. *
  71. * @method Analytics~Report#execute
  72. * @param {Object} [options] - Options
  73. * @param {Boolean} options.details - Flag if include detail in result
  74. * @param {Analytics~ReportMetadata} options.metadata - Overriding report metadata
  75. * @param {Callback.<Analytics~ReportResult>} [callback] - Callback function
  76. * @returns {Promise.<Analytics~ReportResult>}
  77. */
  78. Report.prototype.run =
  79. Report.prototype.exec =
  80. Report.prototype.execute = function(options, callback) {
  81. options = options || {};
  82. if (_.isFunction(options)) {
  83. callback = options;
  84. options = {};
  85. }
  86. var url = [ this._conn._baseUrl(), "analytics", "reports", this.id ].join('/');
  87. url += "?includeDetails=" + (options.details ? "true" : "false");
  88. var params = { method : options.metadata ? 'POST' : 'GET', url : url };
  89. if (options.metadata) {
  90. params.headers = { "Content-Type" : "application/json" };
  91. params.body = JSON.stringify(options.metadata);
  92. }
  93. return this._conn.request(params).thenCall(callback);
  94. };
  95. /**
  96. * Run report asynchronously
  97. *
  98. * @method Analytics~Report#executeAsync
  99. * @param {Object} [options] - Options
  100. * @param {Boolean} options.details - Flag if include detail in result
  101. * @param {Analytics~ReportMetadata} options.metadata - Overriding report metadata
  102. * @param {Callback.<Analytics~ReportInstanceAttrs>} [callback] - Callback function
  103. * @returns {Promise.<Analytics~ReportInstanceAttrs>}
  104. */
  105. Report.prototype.executeAsync = function(options, callback) {
  106. options = options || {};
  107. if (_.isFunction(options)) {
  108. callback = options;
  109. options = {};
  110. }
  111. var url = [ this._conn._baseUrl(), "analytics", "reports", this.id, "instances" ].join('/');
  112. if (options.details) {
  113. url += "?includeDetails=true";
  114. }
  115. var params = { method : 'POST', url : url, body: "" };
  116. if (options.metadata) {
  117. params.headers = { "Content-Type" : "application/json" };
  118. params.body = JSON.stringify(options.metadata);
  119. }
  120. return this._conn.request(params).thenCall(callback);
  121. };
  122. /**
  123. * Get report instance for specified instance ID
  124. *
  125. * @method Analytics~Report#instance
  126. * @param {String} id - Report instance ID
  127. * @returns {Analytics~ReportInstance}
  128. */
  129. Report.prototype.instance = function(id) {
  130. return new ReportInstance(this, id);
  131. };
  132. /**
  133. * List report instances which had been executed asynchronously
  134. *
  135. * @method Analytics~Report#instances
  136. * @param {Callback.<Array.<Analytics~ReportInstanceAttrs>>} [callback] - Callback function
  137. * @returns {Promise.<Array.<Analytics~ReportInstanceAttrs>>}
  138. */
  139. Report.prototype.instances = function(callback) {
  140. var url = [ this._conn._baseUrl(), "analytics", "reports", this.id, "instances" ].join('/');
  141. return this._conn.request(url).thenCall(callback);
  142. };
  143. /**
  144. * API class for Analytics API
  145. *
  146. * @class
  147. * @param {Connection} conn Connection
  148. */
  149. var Analytics = function(conn) {
  150. this._conn = conn;
  151. };
  152. /**
  153. * Get report object of Analytics API
  154. *
  155. * @param {String} id - Report Id
  156. * @returns {Analytics~Report}
  157. */
  158. Analytics.prototype.report = function(id) {
  159. return new Report(this._conn, id);
  160. };
  161. /**
  162. * Get recent report list
  163. *
  164. * @param {Callback.<Array.<Analytics~ReportInfo>>} [callback] - Callback function
  165. * @returns {Promise.<Array.<Analytics~ReportInfo>>}
  166. */
  167. Analytics.prototype.reports = function(callback) {
  168. var url = [ this._conn._baseUrl(), "analytics", "reports" ].join('/');
  169. return this._conn.request(url).thenCall(callback);
  170. };
  171. /*--------------------------------------------*/
  172. /*
  173. * Register hook in connection instantiation for dynamically adding this API module features
  174. */
  175. jsforce.on('connection:new', function(conn) {
  176. conn.analytics = new Analytics(conn);
  177. });
  178. module.exports = Analytics;