5
*Adds several properties to the 'file' object indicated upload speed, time left, upload time, etc.
6
- currentSpeed -- String indicating the upload speed, bits per second
7
- averageSpeed -- Overall average upload speed, bits per second
8
- movingAverageSpeed -- Speed over averaged over the last several measurements, bits 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
14
*Adds setting 'moving_average_history_size' for defining the window size used to calculate the moving average speed.
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)
28
if (typeof(SWFUpload) === "function") {
31
SWFUpload.prototype.initSettings = (function (oldInitSettings) {
32
return function (userSettings) {
33
if (typeof(oldInitSettings) === "function") {
34
oldInitSettings.call(this, userSettings);
37
this.ensureDefault = function (settingName, defaultValue) {
38
this.settings[settingName] = (userSettings[settingName] == undefined) ? defaultValue : userSettings[settingName];
41
// List used to keep the speed stats for the files we are tracking
42
this.fileSpeedStats = {};
43
this.speedSettings = {};
45
this.ensureDefault("moving_average_history_size", "10");
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;
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;
63
delete this.ensureDefault;
65
})(SWFUpload.prototype.initSettings);
68
SWFUpload.speed.fileQueuedHandler = function (file) {
69
if (typeof this.speedSettings.user_file_queued_handler === "function") {
70
file = SWFUpload.speed.extendFile(file);
72
return this.speedSettings.user_file_queued_handler.call(this, file);
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);
80
return this.speedSettings.user_file_queue_error_handler.call(this, file, errorCode, message);
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);
91
SWFUpload.speed.uploadErrorHandler = function (file, errorCode, message) {
92
file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
93
SWFUpload.speed.removeTracking(file, this.fileSpeedStats);
95
if (typeof this.speedSettings.user_upload_error_handler === "function") {
96
return this.speedSettings.user_upload_error_handler.call(this, file, errorCode, message);
99
SWFUpload.speed.uploadProgressHandler = function (file, bytesComplete, bytesTotal) {
100
this.updateTracking(file, bytesComplete);
101
file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
103
if (typeof this.speedSettings.user_upload_progress_handler === "function") {
104
return this.speedSettings.user_upload_progress_handler.call(this, file, bytesComplete, bytesTotal);
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);
114
SWFUpload.speed.uploadCompleteHandler = function (file) {
115
file = SWFUpload.speed.extendFile(file, this.fileSpeedStats);
116
SWFUpload.speed.removeTracking(file, this.fileSpeedStats);
118
if (typeof this.speedSettings.user_upload_complete_handler === "function") {
119
return this.speedSettings.user_upload_complete_handler.call(this, file);
123
// Private: extends the file object with the speed plugin values
124
SWFUpload.speed.extendFile = function (file, trackingList) {
132
tracking = trackingList[file.id];
136
file.currentSpeed = tracking.currentSpeed;
137
file.averageSpeed = tracking.averageSpeed;
138
file.movingAverageSpeed = tracking.movingAverageSpeed;
139
file.timeRemaining = tracking.timeRemaining;
140
file.timeElapsed = tracking.timeElapsed;
141
file.percentUploaded = tracking.percentUploaded;
142
file.sizeUploaded = tracking.bytesUploaded;
145
file.currentSpeed = 0;
146
file.averageSpeed = 0;
147
file.movingAverageSpeed = 0;
148
file.timeRemaining = 0;
149
file.timeElapsed = 0;
150
file.percentUploaded = 0;
151
file.sizeUploaded = 0;
157
// Private: Updates the speed tracking object, or creates it if necessary
158
SWFUpload.prototype.updateTracking = function (file, bytesUploaded) {
159
var tracking = this.fileSpeedStats[file.id];
161
this.fileSpeedStats[file.id] = tracking = {};
164
// Sanity check inputs
165
bytesUploaded = bytesUploaded || tracking.bytesUploaded || 0;
166
if (bytesUploaded < 0) {
169
if (bytesUploaded > file.size) {
170
bytesUploaded = file.size;
173
var tickTime = (new Date()).getTime();
174
if (!tracking.startTime) {
175
tracking.startTime = (new Date()).getTime();
176
tracking.lastTime = tracking.startTime;
177
tracking.currentSpeed = 0;
178
tracking.averageSpeed = 0;
179
tracking.movingAverageSpeed = 0;
180
tracking.movingAverageHistory = [];
181
tracking.timeRemaining = 0;
182
tracking.timeElapsed = 0;
183
tracking.percentUploaded = bytesUploaded / file.size;
184
tracking.bytesUploaded = bytesUploaded;
185
} else if (tracking.startTime > tickTime) {
186
this.debug("When backwards in time");
188
// Get time and deltas
189
var now = (new Date()).getTime();
190
var lastTime = tracking.lastTime;
191
var deltaTime = now - lastTime;
192
var deltaBytes = bytesUploaded - tracking.bytesUploaded;
194
if (deltaBytes === 0 || deltaTime === 0) {
198
// Update tracking object
199
tracking.lastTime = now;
200
tracking.bytesUploaded = bytesUploaded;
203
tracking.currentSpeed = (deltaBytes * 8 ) / (deltaTime / 1000);
204
tracking.averageSpeed = (tracking.bytesUploaded * 8) / ((now - tracking.startTime) / 1000);
206
// Calculate moving average
207
tracking.movingAverageHistory.push(tracking.currentSpeed);
208
if (tracking.movingAverageHistory.length > this.settings.moving_average_history_size) {
209
tracking.movingAverageHistory.shift();
212
tracking.movingAverageSpeed = SWFUpload.speed.calculateMovingAverage(tracking.movingAverageHistory);
215
tracking.timeRemaining = (file.size - tracking.bytesUploaded) * 8 / tracking.movingAverageSpeed;
216
tracking.timeElapsed = (now - tracking.startTime) / 1000;
219
tracking.percentUploaded = (tracking.bytesUploaded / file.size * 100);
224
SWFUpload.speed.removeTracking = function (file, trackingList) {
226
trackingList[file.id] = null;
227
delete trackingList[file.id];
232
SWFUpload.speed.formatUnits = function (baseNumber, unitDivisors, unitLabels, singleFractional) {
233
var i, unit, unitDivisor, unitLabel;
235
if (baseNumber === 0) {
236
return "0 " + unitLabels[unitLabels.length - 1];
239
if (singleFractional) {
241
unitLabel = unitLabels.length >= unitDivisors.length ? unitLabels[unitDivisors.length - 1] : "";
242
for (i = 0; i < unitDivisors.length; i++) {
243
if (baseNumber >= unitDivisors[i]) {
244
unit = (baseNumber / unitDivisors[i]).toFixed(2);
245
unitLabel = unitLabels.length >= i ? " " + unitLabels[i] : "";
250
return unit + unitLabel;
252
var formattedStrings = [];
253
var remainder = baseNumber;
255
for (i = 0; i < unitDivisors.length; i++) {
256
unitDivisor = unitDivisors[i];
257
unitLabel = unitLabels.length > i ? " " + unitLabels[i] : "";
259
unit = remainder / unitDivisor;
260
if (i < unitDivisors.length -1) {
261
unit = Math.floor(unit);
263
unit = unit.toFixed(2);
266
remainder = remainder % unitDivisor;
268
formattedStrings.push(unit + unitLabel);
272
return formattedStrings.join(" ");
276
SWFUpload.speed.formatBPS = function (baseNumber) {
277
var bpsUnits = [1073741824, 1048576, 1024, 1], bpsUnitLabels = ["Gbps", "Mbps", "Kbps", "bps"];
278
return SWFUpload.speed.formatUnits(baseNumber, bpsUnits, bpsUnitLabels, true);
281
SWFUpload.speed.formatTime = function (baseNumber) {
282
var timeUnits = [86400, 3600, 60, 1], timeUnitLabels = ["d", "h", "m", "s"];
283
return SWFUpload.speed.formatUnits(baseNumber, timeUnits, timeUnitLabels, false);
286
SWFUpload.speed.formatBytes = function (baseNumber) {
287
var sizeUnits = [1073741824, 1048576, 1024, 1], sizeUnitLabels = ["GB", "MB", "KB", "bytes"];
288
return SWFUpload.speed.formatUnits(baseNumber, sizeUnits, sizeUnitLabels, true);
291
SWFUpload.speed.formatPercent = function (baseNumber) {
292
return baseNumber.toFixed(2) + " %";
295
SWFUpload.speed.calculateMovingAverage = function (history) {
296
var vals = [], size, sum = 0.0, mean = 0.0, varianceTemp = 0.0, variance = 0.0, standardDev = 0.0;
298
var mSum = 0, mCount = 0;
300
size = history.length;
302
// Check for sufficient data
304
// Clone the array and Calculate sum of the values
305
for (i = 0; i < size; i++) {
306
vals[i] = history[i];
312
// Calculate variance for the set
313
for (i = 0; i < size; i++) {
314
varianceTemp += Math.pow((vals[i] - mean), 2);
317
variance = varianceTemp / size;
318
standardDev = Math.sqrt(variance);
320
//Standardize the Data
321
for (i = 0; i < size; i++) {
322
vals[i] = (vals[i] - mean) / standardDev;
325
// Calculate the average excluding outliers
326
var deviationRange = 2.0;
327
for (i = 0; i < size; i++) {
329
if (vals[i] <= deviationRange && vals[i] >= -deviationRange) {
336
// Calculate the average (not enough data points to remove outliers)
338
for (i = 0; i < size; i++) {
343
return mSum / mCount;
b'\\ No newline at end of file'