3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
11 *Adds several properties to the 'file' object indicated upload speed, time left, upload time, etc.
12 - currentSpeed -- String indicating the upload speed, bytes per second
13 - averageSpeed -- Overall average upload speed, bytes per second
14 - movingAverageSpeed -- Speed over averaged over the last several measurements, bytes per second
15 - timeRemaining -- Estimated remaining upload time in seconds
16 - timeElapsed -- Number of seconds passed for this upload
17 - percentUploaded -- Percentage of the file uploaded (0 to 100)
18 - sizeUploaded -- Formatted size uploaded so far, bytes
20 *Adds setting 'moving_average_history_size' for defining the window size used to calculate the moving average speed.
22 *Adds several Formatting functions for formatting that values provided on the file object.
23 - SWFUpload.speed.formatBPS(bps) -- outputs string formatted in the best units (Gbps, Mbps, Kbps, bps)
24 - SWFUpload.speed.formatTime(seconds) -- outputs string formatted in the best units (x Hr y M z S)
25 - SWFUpload.speed.formatSize(bytes) -- outputs string formatted in the best units (w GB x MB y KB z B )
26 - SWFUpload.speed.formatPercent(percent) -- outputs string formatted with a percent sign (x.xx %)
27 - SWFUpload.speed.formatUnits(baseNumber, divisionArray, unitLabelArray, fractionalBoolean)
28 - Formats a number using the division array to determine how to apply the labels in the Label Array
29 - factionalBoolean indicates whether the number should be returned as a single fractional number with a unit (speed)
30 or as several numbers labeled with units (time)
34 if (typeof(SWFUpload) === "function") {
37 SWFUpload.prototype.initSettings = (function (oldInitSettings) {
39 if (typeof(oldInitSettings) === "function") {
40 oldInitSettings.call(this);
43 this.ensureDefault = function (settingName, defaultValue) {
44 this.settings[settingName] = (this.settings[settingName] == undefined) ? defaultValue : this.settings[settingName];
47 // List used to keep the speed stats for the files we are tracking
48 this.fileSpeedStats = {};
49 this.speedSettings = {};
51 this.ensureDefault("moving_average_history_size", "10");
53 this.speedSettings.user_file_queued_handler = this.settings.file_queued_handler;
54 this.speedSettings.user_file_queue_error_handler = this.settings.file_queue_error_handler;
55 this.speedSettings.user_upload_start_handler = this.settings.upload_start_handler;
56 this.speedSettings.user_upload_error_handler = this.settings.upload_error_handler;
57 this.speedSettings.user_upload_progress_handler = this.settings.upload_progress_handler;
58 this.speedSettings.user_upload_success_handler = this.settings.upload_success_handler;
59 this.speedSettings.user_upload_complete_handler = this.settings.upload_complete_handler;
61 this.settings.file_queued_handler = SWFUpload.speed.fileQueuedHandler;
62 this.settings.file_queue_error_handler = SWFUpload.speed.fileQueueErrorHandler;
63 this.settings.upload_start_handler = SWFUpload.speed.uploadStartHandler;
64 this.settings.upload_error_handler = SWFUpload.speed.uploadErrorHandler;
65 this.settings.upload_progress_handler = SWFUpload.speed.uploadProgressHandler;
66 this.settings.upload_success_handler = SWFUpload.speed.uploadSuccessHandler;
67 this.settings.upload_complete_handler = SWFUpload.speed.uploadCompleteHandler;
69 delete this.ensureDefault;
71 })(SWFUpload.prototype.initSettings);
74 SWFUpload.speed.fileQueuedHandler = function (file) {
75 if (typeof this.speedSettings.user_file_queued_handler === "function") {
76 file = SWFUpload.speed.extendFile(file);
78 return this.speedSettings.user_file_queued_handler.call(this, file);
82 SWFUpload.speed.fileQueueErrorHandler = function (file, errorCode, message) {
83 if (typeof this.speedSettings.user_file_queue_error_handler === "function") {
84 file = SWFUpload.speed.extendFile(file);
86 return this.speedSettings.user_file_queue_error_handler.call(this, file, errorCode, message);
90 SWFUpload.speed.uploadStartHandler = function (file) {
91 if (typeof this.speedSettings.user_upload_start_handler === "function") {
92 file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
93 return this.speedSettings.user_upload_start_handler.call(this, file);
97 SWFUpload.speed.uploadErrorHandler = function (file, errorCode, message) {
98 file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
99 SWFUpload.speed.removeTracking(file, this.fileSpeedStats);
101 if (typeof this.speedSettings.user_upload_error_handler === "function") {
102 return this.speedSettings.user_upload_error_handler.call(this, file, errorCode, message);
105 SWFUpload.speed.uploadProgressHandler = function (file, bytesComplete, bytesTotal) {
106 this.updateTracking(file, bytesComplete);
107 file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
109 if (typeof this.speedSettings.user_upload_progress_handler === "function") {
110 return this.speedSettings.user_upload_progress_handler.call(this, file, bytesComplete, bytesTotal);
114 SWFUpload.speed.uploadSuccessHandler = function (file, serverData) {
115 if (typeof this.speedSettings.user_upload_success_handler === "function") {
116 file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
117 return this.speedSettings.user_upload_success_handler.call(this, file, serverData);
120 SWFUpload.speed.uploadCompleteHandler = function (file) {
121 file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
122 SWFUpload.speed.removeTracking(file, this.fileSpeedStats);
124 if (typeof this.speedSettings.user_upload_complete_handler === "function") {
125 return this.speedSettings.user_upload_complete_handler.call(this, file);
129 // Private: extends the file object with the speed plugin values
130 SWFUpload.speed.extendFile = function (file, trackingList) {
134 tracking = trackingList[file.id];
138 file.currentSpeed = tracking.currentSpeed;
139 file.averageSpeed = tracking.averageSpeed;
140 file.movingAverageSpeed = tracking.movingAverageSpeed;
141 file.timeRemaining = tracking.timeRemaining;
142 file.timeElapsed = tracking.timeElapsed;
143 file.percentUploaded = tracking.percentUploaded;
144 file.sizeUploaded = tracking.bytesUploaded;
147 file.currentSpeed = 0;
148 file.averageSpeed = 0;
149 file.movingAverageSpeed = 0;
150 file.timeRemaining = 0;
151 file.timeElapsed = 0;
152 file.percentUploaded = 0;
153 file.sizeUploaded = 0;
159 // Private: Updates the speed tracking object, or creates it if necessary
160 SWFUpload.prototype.updateTracking = function (file, bytesUploaded) {
161 var tracking = this.fileSpeedStats[file.id];
163 this.fileSpeedStats[file.id] = tracking = {};
166 // Sanity check inputs
167 bytesUploaded = bytesUploaded || tracking.bytesUploaded || 0;
168 if (bytesUploaded < 0) {
171 if (bytesUploaded > file.size) {
172 bytesUploaded = file.size;
175 var tickTime = (new Date()).getTime();
176 if (!tracking.startTime) {
177 tracking.startTime = (new Date()).getTime();
178 tracking.lastTime = tracking.startTime;
179 tracking.currentSpeed = 0;
180 tracking.averageSpeed = 0;
181 tracking.movingAverageSpeed = 0;
182 tracking.movingAverageHistory = [];
183 tracking.timeRemaining = 0;
184 tracking.timeElapsed = 0;
185 tracking.percentUploaded = bytesUploaded / file.size;
186 tracking.bytesUploaded = bytesUploaded;
187 } else if (tracking.startTime > tickTime) {
188 this.debug("When backwards in time");
190 // Get time and deltas
191 var now = (new Date()).getTime();
192 var lastTime = tracking.lastTime;
193 var deltaTime = now - lastTime;
194 var deltaBytes = bytesUploaded - tracking.bytesUploaded;
196 if (deltaBytes === 0 || deltaTime === 0) {
200 // Update tracking object
201 tracking.lastTime = now;
202 tracking.bytesUploaded = bytesUploaded;
205 tracking.currentSpeed = (deltaBytes * 8 ) / (deltaTime / 1000);
206 tracking.averageSpeed = (tracking.bytesUploaded * 8) / ((now - tracking.startTime) / 1000);
208 // Calculate moving average
209 tracking.movingAverageHistory.push(tracking.currentSpeed);
210 if (tracking.movingAverageHistory.length > this.settings.moving_average_history_size) {
211 tracking.movingAverageHistory.shift();
214 tracking.movingAverageSpeed = SWFUpload.speed.calculateMovingAverage(tracking.movingAverageHistory);
217 tracking.timeRemaining = (file.size - tracking.bytesUploaded) * 8 / tracking.movingAverageSpeed;
218 tracking.timeElapsed = (now - tracking.startTime) / 1000;
221 tracking.percentUploaded = (tracking.bytesUploaded / file.size * 100);
226 SWFUpload.speed.removeTracking = function (file, trackingList) {
228 trackingList[file.id] = null;
229 delete trackingList[file.id];
234 SWFUpload.speed.formatUnits = function (baseNumber, unitDivisors, unitLabels, singleFractional) {
235 var i, unit, unitDivisor, unitLabel;
237 if (baseNumber === 0) {
238 return "0 " + unitLabels[unitLabels.length - 1];
241 if (singleFractional) {
243 unitLabel = unitLabels.length >= unitDivisors.length ? unitLabels[unitDivisors.length - 1] : "";
244 for (i = 0; i < unitDivisors.length; i++) {
245 if (baseNumber >= unitDivisors[i]) {
246 unit = (baseNumber / unitDivisors[i]).toFixed(2);
247 unitLabel = unitLabels.length >= i ? " " + unitLabels[i] : "";
252 return unit + unitLabel;
254 var formattedStrings = [];
255 var remainder = baseNumber;
257 for (i = 0; i < unitDivisors.length; i++) {
258 unitDivisor = unitDivisors[i];
259 unitLabel = unitLabels.length > i ? " " + unitLabels[i] : "";
261 unit = remainder / unitDivisor;
262 if (i < unitDivisors.length -1) {
263 unit = Math.floor(unit);
265 unit = unit.toFixed(2);
268 remainder = remainder % unitDivisor;
270 formattedStrings.push(unit + unitLabel);
274 return formattedStrings.join(" ");
278 SWFUpload.speed.formatBPS = function (baseNumber) {
279 var bpsUnits = [1073741824, 1048576, 1024, 1], bpsUnitLabels = ["Gbps", "Mbps", "Kbps", "bps"];
280 return SWFUpload.speed.formatUnits(baseNumber, bpsUnits, bpsUnitLabels, true);
283 SWFUpload.speed.formatTime = function (baseNumber) {
284 var timeUnits = [86400, 3600, 60, 1], timeUnitLabels = ["d", "h", "m", "s"];
285 return SWFUpload.speed.formatUnits(baseNumber, timeUnits, timeUnitLabels, false);
288 SWFUpload.speed.formatBytes = function (baseNumber) {
289 var sizeUnits = [1073741824, 1048576, 1024, 1], sizeUnitLabels = ["GB", "MB", "KB", "bytes"];
290 return SWFUpload.speed.formatUnits(baseNumber, sizeUnits, sizeUnitLabels, true);
293 SWFUpload.speed.formatPercent = function (baseNumber) {
294 return baseNumber.toFixed(2) + " %";
297 SWFUpload.speed.calculateMovingAverage = function (history) {
298 var vals = [], size, sum = 0.0, mean = 0.0, varianceTemp = 0.0, variance = 0.0, standardDev = 0.0;
300 var mSum = 0, mCount = 0;
302 size = history.length;
304 // Check for sufficient data
306 // Clone the array and Calculate sum of the values
307 for (i = 0; i < size; i++) {
308 vals[i] = history[i];
314 // Calculate variance for the set
315 for (i = 0; i < size; i++) {
316 varianceTemp += Math.pow((vals[i] - mean), 2);
319 variance = varianceTemp / size;
320 standardDev = Math.sqrt(variance);
322 //Standardize the Data
323 for (i = 0; i < size; i++) {
324 vals[i] = (vals[i] - mean) / standardDev;
327 // Calculate the average excluding outliers
328 var deviationRange = 2.0;
329 for (i = 0; i < size; i++) {
331 if (vals[i] <= deviationRange && vals[i] >= -deviationRange) {
338 // Calculate the average (not enough data points to remove outliers)
340 for (i = 0; i < size; i++) {
345 return mSum / mCount;