ultimix

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

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