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
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) {
33
if (typeof(oldInitSettings) === "function") {
34
oldInitSettings.call(this);
37
this.ensureDefault = function (settingName, defaultValue) {
38
this.settings[settingName] = (this.settings[settingName] == undefined) ? defaultValue : this.settings[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) {
128
tracking = trackingList[file.id];
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;
141
file.currentSpeed = 0;
142
file.averageSpeed = 0;
143
file.movingAverageSpeed = 0;
144
file.timeRemaining = 0;
145
file.timeElapsed = 0;
146
file.percentUploaded = 0;
147
file.sizeUploaded = 0;
153
// Private: Updates the speed tracking object, or creates it if necessary
154
SWFUpload.prototype.updateTracking = function (file, bytesUploaded) {
155
var tracking = this.fileSpeedStats[file.id];
157
this.fileSpeedStats[file.id] = tracking = {};
160
// Sanity check inputs
161
bytesUploaded = bytesUploaded || tracking.bytesUploaded || 0;
162
if (bytesUploaded < 0) {
165
if (bytesUploaded > file.size) {
166
bytesUploaded = file.size;
169
var tickTime = (new Date()).getTime();
170
if (!tracking.startTime) {
171
tracking.startTime = (new Date()).getTime();
172
tracking.lastTime = tracking.startTime;
173
tracking.currentSpeed = 0;
174
tracking.averageSpeed = 0;
175
tracking.movingAverageSpeed = 0;
176
tracking.movingAverageHistory = [];
177
tracking.timeRemaining = 0;
178
tracking.timeElapsed = 0;
179
tracking.percentUploaded = bytesUploaded / file.size;
180
tracking.bytesUploaded = bytesUploaded;
181
} else if (tracking.startTime > tickTime) {
182
this.debug("When backwards in time");
184
// Get time and deltas
185
var now = (new Date()).getTime();
186
var lastTime = tracking.lastTime;
187
var deltaTime = now - lastTime;
188
var deltaBytes = bytesUploaded - tracking.bytesUploaded;
190
if (deltaBytes === 0 || deltaTime === 0) {
194
// Update tracking object
195
tracking.lastTime = now;
196
tracking.bytesUploaded = bytesUploaded;
199
tracking.currentSpeed = (deltaBytes * 8 ) / (deltaTime / 1000);
200
tracking.averageSpeed = (tracking.bytesUploaded * 8) / ((now - tracking.startTime) / 1000);
202
// Calculate moving average
203
tracking.movingAverageHistory.push(tracking.currentSpeed);
204
if (tracking.movingAverageHistory.length > this.settings.moving_average_history_size) {
205
tracking.movingAverageHistory.shift();
208
tracking.movingAverageSpeed = SWFUpload.speed.calculateMovingAverage(tracking.movingAverageHistory);
211
tracking.timeRemaining = (file.size - tracking.bytesUploaded) * 8 / tracking.movingAverageSpeed;
212
tracking.timeElapsed = (now - tracking.startTime) / 1000;
215
tracking.percentUploaded = (tracking.bytesUploaded / file.size * 100);
220
SWFUpload.speed.removeTracking = function (file, trackingList) {
222
trackingList[file.id] = null;
223
delete trackingList[file.id];
228
SWFUpload.speed.formatUnits = function (baseNumber, unitDivisors, unitLabels, singleFractional) {
229
var i, unit, unitDivisor, unitLabel;
231
if (baseNumber === 0) {
232
return "0 " + unitLabels[unitLabels.length - 1];
235
if (singleFractional) {
237
unitLabel = unitLabels.length >= unitDivisors.length ? unitLabels[unitDivisors.length - 1] : "";
238
for (i = 0; i < unitDivisors.length; i++) {
239
if (baseNumber >= unitDivisors[i]) {
240
unit = (baseNumber / unitDivisors[i]).toFixed(2);
241
unitLabel = unitLabels.length >= i ? " " + unitLabels[i] : "";
246
return unit + unitLabel;
248
var formattedStrings = [];
249
var remainder = baseNumber;
251
for (i = 0; i < unitDivisors.length; i++) {
252
unitDivisor = unitDivisors[i];
253
unitLabel = unitLabels.length > i ? " " + unitLabels[i] : "";
255
unit = remainder / unitDivisor;
256
if (i < unitDivisors.length -1) {
257
unit = Math.floor(unit);
259
unit = unit.toFixed(2);
262
remainder = remainder % unitDivisor;
264
formattedStrings.push(unit + unitLabel);
268
return formattedStrings.join(" ");
272
SWFUpload.speed.formatBPS = function (baseNumber) {
273
var bpsUnits = [1073741824, 1048576, 1024, 1], bpsUnitLabels = ["Gbps", "Mbps", "Kbps", "bps"];
274
return SWFUpload.speed.formatUnits(baseNumber, bpsUnits, bpsUnitLabels, true);
277
SWFUpload.speed.formatTime = function (baseNumber) {
278
var timeUnits = [86400, 3600, 60, 1], timeUnitLabels = ["d", "h", "m", "s"];
279
return SWFUpload.speed.formatUnits(baseNumber, timeUnits, timeUnitLabels, false);
282
SWFUpload.speed.formatBytes = function (baseNumber) {
283
var sizeUnits = [1073741824, 1048576, 1024, 1], sizeUnitLabels = ["GB", "MB", "KB", "bytes"];
284
return SWFUpload.speed.formatUnits(baseNumber, sizeUnits, sizeUnitLabels, true);
287
SWFUpload.speed.formatPercent = function (baseNumber) {
288
return baseNumber.toFixed(2) + " %";
291
SWFUpload.speed.calculateMovingAverage = function (history) {
292
var vals = [], size, sum = 0.0, mean = 0.0, varianceTemp = 0.0, variance = 0.0, standardDev = 0.0;
294
var mSum = 0, mCount = 0;
296
size = history.length;
298
// Check for sufficient data
300
// Clone the array and Calculate sum of the values
301
for (i = 0; i < size; i++) {
302
vals[i] = history[i];
308
// Calculate variance for the set
309
for (i = 0; i < size; i++) {
310
varianceTemp += Math.pow((vals[i] - mean), 2);
313
variance = varianceTemp / size;
314
standardDev = Math.sqrt(variance);
316
//Standardize the Data
317
for (i = 0; i < size; i++) {
318
vals[i] = (vals[i] - mean) / standardDev;
321
// Calculate the average excluding outliers
322
var deviationRange = 2.0;
323
for (i = 0; i < size; i++) {
325
if (vals[i] <= deviationRange && vals[i] >= -deviationRange) {
332
// Calculate the average (not enough data points to remove outliers)
334
for (i = 0; i < size; i++) {
339
return mSum / mCount;
b'\\ No newline at end of file'