RenderTaskExecInfo.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Business Analytics (C) Copyright IBM Corp. 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. /**
  9. * @class RenderTaskExecInfo
  10. * @hideconstructor
  11. * @classdesc Save the info of a render task
  12. */
  13. define([], function () {
  14. var RenderTaskExecInfo = function () {
  15. /**
  16. * @public
  17. * @description initializes a task info by setting the start time
  18. */
  19. function RenderTaskExecInfo(options) {
  20. _classCallCheck(this, RenderTaskExecInfo);
  21. this._id = options.id;
  22. this._startTime = Date.now();
  23. this._endTime = undefined;
  24. this._error = undefined;
  25. this._data = new Map([]);
  26. this._requestTimeInfo = [];
  27. }
  28. /**
  29. * @public
  30. * @function RenderTaskExecInfo#getId
  31. * @description returns the task id
  32. */
  33. RenderTaskExecInfo.prototype.getId = function getId() {
  34. return this._id;
  35. };
  36. /**
  37. * @public
  38. * @function RenderTaskExecInfo#setEndTime
  39. * @description set the end time of the task execution
  40. */
  41. RenderTaskExecInfo.prototype.setEndTime = function setEndTime() {
  42. this._endTime = Date.now();
  43. };
  44. /**
  45. * @public
  46. * @function RenderTaskExecInfo#setError
  47. * @description set the error attached to the task execution
  48. * @param {Error} - error instance from commmon see {@link Error}
  49. */
  50. RenderTaskExecInfo.prototype.setError = function setError(error) {
  51. this._error = error;
  52. };
  53. /**
  54. * @public
  55. * @function getDuration
  56. * @description get the duration calculation from the task execution
  57. * @returns {number} The duration in milliseconds
  58. */
  59. RenderTaskExecInfo.prototype.getDuration = function getDuration() {
  60. return this._endTime ? this._endTime - this._startTime : Date.now() - this._startTime;
  61. };
  62. /**
  63. * @public
  64. * @function getRequestDurations
  65. * @description gets the durations of all the requests that occured in the task
  66. * @returns {number[]} array of request durations
  67. */
  68. RenderTaskExecInfo.prototype.getRequestDurations = function getRequestDurations() {
  69. var durations = [];
  70. this._requestTimeInfo.forEach(function (timeInfo) {
  71. if (typeof timeInfo === 'string') {
  72. durations.push(parseInt(timeInfo.replace(/^.*;/, '').trim().replace(/elapsed=/, ''), 10));
  73. } else {
  74. durations.push(NaN);
  75. }
  76. });
  77. return durations;
  78. };
  79. /**
  80. * @public
  81. * @function addRequestTimeInfo
  82. * @description adds a request info; add NaN if the timeInfo is not a valid formatted string
  83. * @params {string} timeInfo - string of the following format: path;start=0;end=2;elapsed=2
  84. */
  85. RenderTaskExecInfo.prototype.addRequestTimeInfo = function addRequestTimeInfo(requestTimeInfo) {
  86. this._requestTimeInfo.push(requestTimeInfo);
  87. };
  88. /**
  89. * @public
  90. * @function RenderTaskExecInfo#toJson
  91. * @description generates the json representing the task exec info
  92. * @returns {Object} json object
  93. */
  94. RenderTaskExecInfo.prototype.toJSON = function toJSON() {
  95. var json = {
  96. 'task id': this._id,
  97. 'task start time': this._startTime,
  98. 'task end time': this._endTime,
  99. 'duration': this.getDuration(),
  100. 'error': this._error ? this._error.toJson() : undefined,
  101. 'requestTimes': this.getRequestDurations()
  102. };
  103. this._data.forEach(function (value, key) {
  104. if (value && typeof value.toJSON === 'function') {
  105. json[key] = value.toJSON();
  106. } else {
  107. json[key] = value;
  108. }
  109. });
  110. return json;
  111. };
  112. /**
  113. * @public
  114. * @function RenderTaskExecInfo#setData
  115. * @description set Data
  116. */
  117. RenderTaskExecInfo.prototype.setData = function setData(key, value) {
  118. if (typeof key !== 'string') {
  119. throw new Error('Invalid or undefined key: [' + key + ']');
  120. }
  121. this._data.set(key, value);
  122. };
  123. RenderTaskExecInfo.prototype.getData = function getData(key) {
  124. return this._data.get(key);
  125. };
  126. return RenderTaskExecInfo;
  127. }();
  128. return RenderTaskExecInfo;
  129. });
  130. //# sourceMappingURL=RenderTaskExecInfo.js.map