You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

361 lines
13 KiB

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