swfupload.speed.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. Speed Plug-in
  3. Features:
  4. *Adds several properties to the 'file' object indicated upload speed, time left, upload time, etc.
  5. - currentSpeed -- String indicating the upload speed, bits per second
  6. - averageSpeed -- Overall average upload speed, bits per second
  7. - movingAverageSpeed -- Speed over averaged over the last several measurements, bits per second
  8. - timeRemaining -- Estimated remaining upload time in seconds
  9. - timeElapsed -- Number of seconds passed for this upload
  10. - percentUploaded -- Percentage of the file uploaded (0 to 100)
  11. - sizeUploaded -- Formatted size uploaded so far, bytes
  12. *Adds setting 'moving_average_history_size' for defining the window size used to calculate the moving average speed.
  13. *Adds several Formatting functions for formatting that values provided on the file object.
  14. - SWFUpload.speed.formatBPS(bps) -- outputs string formatted in the best units (Gbps, Mbps, Kbps, bps)
  15. - SWFUpload.speed.formatTime(seconds) -- outputs string formatted in the best units (x Hr y M z S)
  16. - SWFUpload.speed.formatSize(bytes) -- outputs string formatted in the best units (w GB x MB y KB z B )
  17. - SWFUpload.speed.formatPercent(percent) -- outputs string formatted with a percent sign (x.xx %)
  18. - SWFUpload.speed.formatUnits(baseNumber, divisionArray, unitLabelArray, fractionalBoolean)
  19. - Formats a number using the division array to determine how to apply the labels in the Label Array
  20. - factionalBoolean indicates whether the number should be returned as a single fractional number with a unit (speed)
  21. or as several numbers labeled with units (time)
  22. */
  23. var SWFUpload;
  24. if (typeof(SWFUpload) === "function") {
  25. SWFUpload.speed = {};
  26. SWFUpload.prototype.initSettings = (function (oldInitSettings) {
  27. return function (userSettings) {
  28. if (typeof(oldInitSettings) === "function") {
  29. oldInitSettings.call(this, userSettings);
  30. }
  31. this.ensureDefault = function (settingName, defaultValue) {
  32. this.settings[settingName] = (userSettings[settingName] == undefined) ? defaultValue : userSettings[settingName];
  33. };
  34. // List used to keep the speed stats for the files we are tracking
  35. this.fileSpeedStats = {};
  36. this.speedSettings = {};
  37. this.ensureDefault("moving_average_history_size", "10");
  38. this.speedSettings.user_file_queued_handler = this.settings.file_queued_handler;
  39. this.speedSettings.user_file_queue_error_handler = this.settings.file_queue_error_handler;
  40. this.speedSettings.user_upload_start_handler = this.settings.upload_start_handler;
  41. this.speedSettings.user_upload_error_handler = this.settings.upload_error_handler;
  42. this.speedSettings.user_upload_progress_handler = this.settings.upload_progress_handler;
  43. this.speedSettings.user_upload_success_handler = this.settings.upload_success_handler;
  44. this.speedSettings.user_upload_complete_handler = this.settings.upload_complete_handler;
  45. this.settings.file_queued_handler = SWFUpload.speed.fileQueuedHandler;
  46. this.settings.file_queue_error_handler = SWFUpload.speed.fileQueueErrorHandler;
  47. this.settings.upload_start_handler = SWFUpload.speed.uploadStartHandler;
  48. this.settings.upload_error_handler = SWFUpload.speed.uploadErrorHandler;
  49. this.settings.upload_progress_handler = SWFUpload.speed.uploadProgressHandler;
  50. this.settings.upload_success_handler = SWFUpload.speed.uploadSuccessHandler;
  51. this.settings.upload_complete_handler = SWFUpload.speed.uploadCompleteHandler;
  52. delete this.ensureDefault;
  53. };
  54. })(SWFUpload.prototype.initSettings);
  55. SWFUpload.speed.fileQueuedHandler = function (file) {
  56. if (typeof this.speedSettings.user_file_queued_handler === "function") {
  57. file = SWFUpload.speed.extendFile(file);
  58. return this.speedSettings.user_file_queued_handler.call(this, file);
  59. }
  60. };
  61. SWFUpload.speed.fileQueueErrorHandler = function (file, errorCode, message) {
  62. if (typeof this.speedSettings.user_file_queue_error_handler === "function") {
  63. file = SWFUpload.speed.extendFile(file);
  64. return this.speedSettings.user_file_queue_error_handler.call(this, file, errorCode, message);
  65. }
  66. };
  67. SWFUpload.speed.uploadStartHandler = function (file) {
  68. if (typeof this.speedSettings.user_upload_start_handler === "function") {
  69. file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
  70. return this.speedSettings.user_upload_start_handler.call(this, file);
  71. }
  72. };
  73. SWFUpload.speed.uploadErrorHandler = function (file, errorCode, message) {
  74. file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
  75. SWFUpload.speed.removeTracking(file, this.fileSpeedStats);
  76. if (typeof this.speedSettings.user_upload_error_handler === "function") {
  77. return this.speedSettings.user_upload_error_handler.call(this, file, errorCode, message);
  78. }
  79. };
  80. SWFUpload.speed.uploadProgressHandler = function (file, bytesComplete, bytesTotal) {
  81. this.updateTracking(file, bytesComplete);
  82. file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
  83. if (typeof this.speedSettings.user_upload_progress_handler === "function") {
  84. return this.speedSettings.user_upload_progress_handler.call(this, file, bytesComplete, bytesTotal);
  85. }
  86. };
  87. SWFUpload.speed.uploadSuccessHandler = function (file, serverData) {
  88. if (typeof this.speedSettings.user_upload_success_handler === "function") {
  89. file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
  90. return this.speedSettings.user_upload_success_handler.call(this, file, serverData);
  91. }
  92. };
  93. SWFUpload.speed.uploadCompleteHandler = function (file) {
  94. file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
  95. SWFUpload.speed.removeTracking(file, this.fileSpeedStats);
  96. if (typeof this.speedSettings.user_upload_complete_handler === "function") {
  97. return this.speedSettings.user_upload_complete_handler.call(this, file);
  98. }
  99. };
  100. // Private: extends the file object with the speed plugin values
  101. SWFUpload.speed.extendFile = function (file, trackingList) {
  102. var tracking;
  103. if (!file) {
  104. return file;
  105. }
  106. if (trackingList) {
  107. tracking = trackingList[file.id];
  108. }
  109. if (tracking) {
  110. file.currentSpeed = tracking.currentSpeed;
  111. file.averageSpeed = tracking.averageSpeed;
  112. file.movingAverageSpeed = tracking.movingAverageSpeed;
  113. file.timeRemaining = tracking.timeRemaining;
  114. file.timeElapsed = tracking.timeElapsed;
  115. file.percentUploaded = tracking.percentUploaded;
  116. file.sizeUploaded = tracking.bytesUploaded;
  117. } else {
  118. file.currentSpeed = 0;
  119. file.averageSpeed = 0;
  120. file.movingAverageSpeed = 0;
  121. file.timeRemaining = 0;
  122. file.timeElapsed = 0;
  123. file.percentUploaded = 0;
  124. file.sizeUploaded = 0;
  125. }
  126. return file;
  127. };
  128. // Private: Updates the speed tracking object, or creates it if necessary
  129. SWFUpload.prototype.updateTracking = function (file, bytesUploaded) {
  130. var tracking = this.fileSpeedStats[file.id];
  131. if (!tracking) {
  132. this.fileSpeedStats[file.id] = tracking = {};
  133. }
  134. // Sanity check inputs
  135. bytesUploaded = bytesUploaded || tracking.bytesUploaded || 0;
  136. if (bytesUploaded < 0) {
  137. bytesUploaded = 0;
  138. }
  139. if (bytesUploaded > file.size) {
  140. bytesUploaded = file.size;
  141. }
  142. var tickTime = (new Date()).getTime();
  143. if (!tracking.startTime) {
  144. tracking.startTime = (new Date()).getTime();
  145. tracking.lastTime = tracking.startTime;
  146. tracking.currentSpeed = 0;
  147. tracking.averageSpeed = 0;
  148. tracking.movingAverageSpeed = 0;
  149. tracking.movingAverageHistory = [];
  150. tracking.timeRemaining = 0;
  151. tracking.timeElapsed = 0;
  152. tracking.percentUploaded = bytesUploaded / file.size;
  153. tracking.bytesUploaded = bytesUploaded;
  154. } else if (tracking.startTime > tickTime) {
  155. this.debug("When backwards in time");
  156. } else {
  157. // Get time and deltas
  158. var now = (new Date()).getTime();
  159. var lastTime = tracking.lastTime;
  160. var deltaTime = now - lastTime;
  161. var deltaBytes = bytesUploaded - tracking.bytesUploaded;
  162. if (deltaBytes === 0 || deltaTime === 0) {
  163. return tracking;
  164. }
  165. // Update tracking object
  166. tracking.lastTime = now;
  167. tracking.bytesUploaded = bytesUploaded;
  168. // Calculate speeds
  169. tracking.currentSpeed = (deltaBytes * 8 ) / (deltaTime / 1000);
  170. tracking.averageSpeed = (tracking.bytesUploaded * 8) / ((now - tracking.startTime) / 1000);
  171. // Calculate moving average
  172. tracking.movingAverageHistory.push(tracking.currentSpeed);
  173. if (tracking.movingAverageHistory.length > this.settings.moving_average_history_size) {
  174. tracking.movingAverageHistory.shift();
  175. }
  176. tracking.movingAverageSpeed = SWFUpload.speed.calculateMovingAverage(tracking.movingAverageHistory);
  177. // Update times
  178. tracking.timeRemaining = (file.size - tracking.bytesUploaded) * 8 / tracking.movingAverageSpeed;
  179. tracking.timeElapsed = (now - tracking.startTime) / 1000;
  180. // Update percent
  181. tracking.percentUploaded = (tracking.bytesUploaded / file.size * 100);
  182. }
  183. return tracking;
  184. };
  185. SWFUpload.speed.removeTracking = function (file, trackingList) {
  186. try {
  187. trackingList[file.id] = null;
  188. delete trackingList[file.id];
  189. } catch (ex) {
  190. }
  191. };
  192. SWFUpload.speed.formatUnits = function (baseNumber, unitDivisors, unitLabels, singleFractional) {
  193. var i, unit, unitDivisor, unitLabel;
  194. if (baseNumber === 0) {
  195. return "0 " + unitLabels[unitLabels.length - 1];
  196. }
  197. if (singleFractional) {
  198. unit = baseNumber;
  199. unitLabel = unitLabels.length >= unitDivisors.length ? unitLabels[unitDivisors.length - 1] : "";
  200. for (i = 0; i < unitDivisors.length; i++) {
  201. if (baseNumber >= unitDivisors[i]) {
  202. unit = (baseNumber / unitDivisors[i]).toFixed(2);
  203. unitLabel = unitLabels.length >= i ? " " + unitLabels[i] : "";
  204. break;
  205. }
  206. }
  207. return unit + unitLabel;
  208. } else {
  209. var formattedStrings = [];
  210. var remainder = baseNumber;
  211. for (i = 0; i < unitDivisors.length; i++) {
  212. unitDivisor = unitDivisors[i];
  213. unitLabel = unitLabels.length > i ? " " + unitLabels[i] : "";
  214. unit = remainder / unitDivisor;
  215. if (i < unitDivisors.length -1) {
  216. unit = Math.floor(unit);
  217. } else {
  218. unit = unit.toFixed(2);
  219. }
  220. if (unit > 0) {
  221. remainder = remainder % unitDivisor;
  222. formattedStrings.push(unit + unitLabel);
  223. }
  224. }
  225. return formattedStrings.join(" ");
  226. }
  227. };
  228. SWFUpload.speed.formatBPS = function (baseNumber) {
  229. var bpsUnits = [1073741824, 1048576, 1024, 1], bpsUnitLabels = ["Gbps", "Mbps", "Kbps", "bps"];
  230. return SWFUpload.speed.formatUnits(baseNumber, bpsUnits, bpsUnitLabels, true);
  231. };
  232. SWFUpload.speed.formatTime = function (baseNumber) {
  233. var timeUnits = [86400, 3600, 60, 1], timeUnitLabels = ["d", "h", "m", "s"];
  234. return SWFUpload.speed.formatUnits(baseNumber, timeUnits, timeUnitLabels, false);
  235. };
  236. SWFUpload.speed.formatBytes = function (baseNumber) {
  237. var sizeUnits = [1073741824, 1048576, 1024, 1], sizeUnitLabels = ["GB", "MB", "KB", "bytes"];
  238. return SWFUpload.speed.formatUnits(baseNumber, sizeUnits, sizeUnitLabels, true);
  239. };
  240. SWFUpload.speed.formatPercent = function (baseNumber) {
  241. return baseNumber.toFixed(2) + " %";
  242. };
  243. SWFUpload.speed.calculateMovingAverage = function (history) {
  244. var vals = [], size, sum = 0.0, mean = 0.0, varianceTemp = 0.0, variance = 0.0, standardDev = 0.0;
  245. var i;
  246. var mSum = 0, mCount = 0;
  247. size = history.length;
  248. // Check for sufficient data
  249. if (size >= 8) {
  250. // Clone the array and Calculate sum of the values
  251. for (i = 0; i < size; i++) {
  252. vals[i] = history[i];
  253. sum += vals[i];
  254. }
  255. mean = sum / size;
  256. // Calculate variance for the set
  257. for (i = 0; i < size; i++) {
  258. varianceTemp += Math.pow((vals[i] - mean), 2);
  259. }
  260. variance = varianceTemp / size;
  261. standardDev = Math.sqrt(variance);
  262. //Standardize the Data
  263. for (i = 0; i < size; i++) {
  264. vals[i] = (vals[i] - mean) / standardDev;
  265. }
  266. // Calculate the average excluding outliers
  267. var deviationRange = 2.0;
  268. for (i = 0; i < size; i++) {
  269. if (vals[i] <= deviationRange && vals[i] >= -deviationRange) {
  270. mCount++;
  271. mSum += history[i];
  272. }
  273. }
  274. } else {
  275. // Calculate the average (not enough data points to remove outliers)
  276. mCount = size;
  277. for (i = 0; i < size; i++) {
  278. mSum += history[i];
  279. }
  280. }
  281. return mSum / mCount;
  282. };
  283. }