Upgrade to ExtJS 3.2.2 - Released 06/02/2010
[extjs.git] / examples / image-organizer / SWFUpload / plugins / swfupload.speed.js
1 /*!
2  * Ext JS Library 3.2.2
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /*
8         Speed Plug-in
9         
10         Features:
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
19                 
20                 *Adds setting 'moving_average_history_size' for defining the window size used to calculate the moving average speed.
21                 
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)
31         */
32
33 var SWFUpload;
34 if (typeof(SWFUpload) === "function") {
35         SWFUpload.speed = {};
36         
37         SWFUpload.prototype.initSettings = (function (oldInitSettings) {
38                 return function () {
39                         if (typeof(oldInitSettings) === "function") {
40                                 oldInitSettings.call(this);
41                         }
42                         
43                         this.ensureDefault = function (settingName, defaultValue) {
44                                 this.settings[settingName] = (this.settings[settingName] == undefined) ? defaultValue : this.settings[settingName];
45                         };
46
47                         // List used to keep the speed stats for the files we are tracking
48                         this.fileSpeedStats = {};
49                         this.speedSettings = {};
50
51                         this.ensureDefault("moving_average_history_size", "10");
52                         
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;
60                         
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;
68                         
69                         delete this.ensureDefault;
70                 };
71         })(SWFUpload.prototype.initSettings);
72
73         
74         SWFUpload.speed.fileQueuedHandler = function (file) {
75                 if (typeof this.speedSettings.user_file_queued_handler === "function") {
76                         file = SWFUpload.speed.extendFile(file);
77                         
78                         return this.speedSettings.user_file_queued_handler.call(this, file);
79                 }
80         };
81         
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);
85                         
86                         return this.speedSettings.user_file_queue_error_handler.call(this, file, errorCode, message);
87                 }
88         };
89
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);
94                 }
95         };
96         
97         SWFUpload.speed.uploadErrorHandler = function (file, errorCode, message) {
98                 file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
99                 SWFUpload.speed.removeTracking(file, this.fileSpeedStats);
100
101                 if (typeof this.speedSettings.user_upload_error_handler === "function") {
102                         return this.speedSettings.user_upload_error_handler.call(this, file, errorCode, message);
103                 }
104         };
105         SWFUpload.speed.uploadProgressHandler = function (file, bytesComplete, bytesTotal) {
106                 this.updateTracking(file, bytesComplete);
107                 file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
108
109                 if (typeof this.speedSettings.user_upload_progress_handler === "function") {
110                         return this.speedSettings.user_upload_progress_handler.call(this, file, bytesComplete, bytesTotal);
111                 }
112         };
113         
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);
118                 }
119         };
120         SWFUpload.speed.uploadCompleteHandler = function (file) {
121                 file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
122                 SWFUpload.speed.removeTracking(file, this.fileSpeedStats);
123
124                 if (typeof this.speedSettings.user_upload_complete_handler === "function") {
125                         return this.speedSettings.user_upload_complete_handler.call(this, file);
126                 }
127         };
128         
129         // Private: extends the file object with the speed plugin values
130         SWFUpload.speed.extendFile = function (file, trackingList) {
131                 var tracking;
132                 
133                 if (trackingList) {
134                         tracking = trackingList[file.id];
135                 }
136                 
137                 if (tracking) {
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;
145
146                 } else {
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;
154                 }
155                 
156                 return file;
157         };
158         
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];
162                 if (!tracking) {
163                         this.fileSpeedStats[file.id] = tracking = {};
164                 }
165                 
166                 // Sanity check inputs
167                 bytesUploaded = bytesUploaded || tracking.bytesUploaded || 0;
168                 if (bytesUploaded < 0) {
169                         bytesUploaded = 0;
170                 }
171                 if (bytesUploaded > file.size) {
172                         bytesUploaded = file.size;
173                 }
174                 
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");
189                 } else {
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;
195                         
196                         if (deltaBytes === 0 || deltaTime === 0) {
197                                 return tracking;
198                         }
199                         
200                         // Update tracking object
201                         tracking.lastTime = now;
202                         tracking.bytesUploaded = bytesUploaded;
203                         
204                         // Calculate speeds
205                         tracking.currentSpeed = (deltaBytes * 8 ) / (deltaTime / 1000);
206                         tracking.averageSpeed = (tracking.bytesUploaded * 8) / ((now - tracking.startTime) / 1000);
207
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();
212                         }
213                         
214                         tracking.movingAverageSpeed = SWFUpload.speed.calculateMovingAverage(tracking.movingAverageHistory);
215                         
216                         // Update times
217                         tracking.timeRemaining = (file.size - tracking.bytesUploaded) * 8 / tracking.movingAverageSpeed;
218                         tracking.timeElapsed = (now - tracking.startTime) / 1000;
219                         
220                         // Update percent
221                         tracking.percentUploaded = (tracking.bytesUploaded / file.size * 100);
222                 }
223                 
224                 return tracking;
225         };
226         SWFUpload.speed.removeTracking = function (file, trackingList) {
227                 try {
228                         trackingList[file.id] = null;
229                         delete trackingList[file.id];
230                 } catch (ex) {
231                 }
232         };
233         
234         SWFUpload.speed.formatUnits = function (baseNumber, unitDivisors, unitLabels, singleFractional) {
235                 var i, unit, unitDivisor, unitLabel;
236
237                 if (baseNumber === 0) {
238                         return "0 " + unitLabels[unitLabels.length - 1];
239                 }
240                 
241                 if (singleFractional) {
242                         unit = baseNumber;
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] : "";
248                                         break;
249                                 }
250                         }
251                         
252                         return unit + unitLabel;
253                 } else {
254                         var formattedStrings = [];
255                         var remainder = baseNumber;
256                         
257                         for (i = 0; i < unitDivisors.length; i++) {
258                                 unitDivisor = unitDivisors[i];
259                                 unitLabel = unitLabels.length > i ? " " + unitLabels[i] : "";
260                                 
261                                 unit = remainder / unitDivisor;
262                                 if (i < unitDivisors.length -1) {
263                                         unit = Math.floor(unit);
264                                 } else {
265                                         unit = unit.toFixed(2);
266                                 }
267                                 if (unit > 0) {
268                                         remainder = remainder % unitDivisor;
269                                         
270                                         formattedStrings.push(unit + unitLabel);
271                                 }
272                         }
273                         
274                         return formattedStrings.join(" ");
275                 }
276         };
277         
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);
281         
282         };
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);
286         
287         };
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);
291         
292         };
293         SWFUpload.speed.formatPercent = function (baseNumber) {
294                 return baseNumber.toFixed(2) + " %";
295         };
296         
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;
299                 var i;
300                 var mSum = 0, mCount = 0;
301                 
302                 size = history.length;
303                 
304                 // Check for sufficient data
305                 if (size >= 8) {
306                         // Clone the array and Calculate sum of the values 
307                         for (i = 0; i < size; i++) {
308                                 vals[i] = history[i];
309                                 sum += vals[i];
310                         }
311
312                         mean = sum / size;
313
314                         // Calculate variance for the set
315                         for (i = 0; i < size; i++) {
316                                 varianceTemp += Math.pow((vals[i] - mean), 2);
317                         }
318
319                         variance = varianceTemp / size;
320                         standardDev = Math.sqrt(variance);
321                         
322                         //Standardize the Data
323                         for (i = 0; i < size; i++) {
324                                 vals[i] = (vals[i] - mean) / standardDev;
325                         }
326
327                         // Calculate the average excluding outliers
328                         var deviationRange = 2.0;
329                         for (i = 0; i < size; i++) {
330                                 
331                                 if (vals[i] <= deviationRange && vals[i] >= -deviationRange) {
332                                         mCount++;
333                                         mSum += history[i];
334                                 }
335                         }
336                         
337                 } else {
338                         // Calculate the average (not enough data points to remove outliers)
339                         mCount = size;
340                         for (i = 0; i < size; i++) {
341                                 mSum += history[i];
342                         }
343                 }
344
345                 return mSum / mCount;
346         };
347         
348 }