~vcs-imports/clansuite/trunk

« back to all changes in this revision

Viewing changes to themes/core/javascript/swfupload/swfupload.speed.js

  • Committer: paulbr
  • Date: 2011-01-13 19:44:03 UTC
  • Revision ID: paulbr-20110113194403-gwsy005jgfrqyzca
Ajax Test Modul

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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, 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
 
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 (userSettings) {
 
33
                        if (typeof(oldInitSettings) === "function") {
 
34
                                oldInitSettings.call(this, userSettings);
 
35
                        }
 
36
                        
 
37
                        this.ensureDefault = function (settingName, defaultValue) {
 
38
                                this.settings[settingName] = (userSettings[settingName] == undefined) ? defaultValue : userSettings[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 (!file) {
 
128
                        return file;
 
129
                }
 
130
                
 
131
                if (trackingList) {
 
132
                        tracking = trackingList[file.id];
 
133
                }
 
134
                
 
135
                if (tracking) {
 
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;
 
143
 
 
144
                } else {
 
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;
 
152
                }
 
153
                
 
154
                return file;
 
155
        };
 
156
        
 
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];
 
160
                if (!tracking) {
 
161
                        this.fileSpeedStats[file.id] = tracking = {};
 
162
                }
 
163
                
 
164
                // Sanity check inputs
 
165
                bytesUploaded = bytesUploaded || tracking.bytesUploaded || 0;
 
166
                if (bytesUploaded < 0) {
 
167
                        bytesUploaded = 0;
 
168
                }
 
169
                if (bytesUploaded > file.size) {
 
170
                        bytesUploaded = file.size;
 
171
                }
 
172
                
 
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");
 
187
                } else {
 
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;
 
193
                        
 
194
                        if (deltaBytes === 0 || deltaTime === 0) {
 
195
                                return tracking;
 
196
                        }
 
197
                        
 
198
                        // Update tracking object
 
199
                        tracking.lastTime = now;
 
200
                        tracking.bytesUploaded = bytesUploaded;
 
201
                        
 
202
                        // Calculate speeds
 
203
                        tracking.currentSpeed = (deltaBytes * 8 ) / (deltaTime / 1000);
 
204
                        tracking.averageSpeed = (tracking.bytesUploaded * 8) / ((now - tracking.startTime) / 1000);
 
205
 
 
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();
 
210
                        }
 
211
                        
 
212
                        tracking.movingAverageSpeed = SWFUpload.speed.calculateMovingAverage(tracking.movingAverageHistory);
 
213
                        
 
214
                        // Update times
 
215
                        tracking.timeRemaining = (file.size - tracking.bytesUploaded) * 8 / tracking.movingAverageSpeed;
 
216
                        tracking.timeElapsed = (now - tracking.startTime) / 1000;
 
217
                        
 
218
                        // Update percent
 
219
                        tracking.percentUploaded = (tracking.bytesUploaded / file.size * 100);
 
220
                }
 
221
                
 
222
                return tracking;
 
223
        };
 
224
        SWFUpload.speed.removeTracking = function (file, trackingList) {
 
225
                try {
 
226
                        trackingList[file.id] = null;
 
227
                        delete trackingList[file.id];
 
228
                } catch (ex) {
 
229
                }
 
230
        };
 
231
        
 
232
        SWFUpload.speed.formatUnits = function (baseNumber, unitDivisors, unitLabels, singleFractional) {
 
233
                var i, unit, unitDivisor, unitLabel;
 
234
 
 
235
                if (baseNumber === 0) {
 
236
                        return "0 " + unitLabels[unitLabels.length - 1];
 
237
                }
 
238
                
 
239
                if (singleFractional) {
 
240
                        unit = baseNumber;
 
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] : "";
 
246
                                        break;
 
247
                                }
 
248
                        }
 
249
                        
 
250
                        return unit + unitLabel;
 
251
                } else {
 
252
                        var formattedStrings = [];
 
253
                        var remainder = baseNumber;
 
254
                        
 
255
                        for (i = 0; i < unitDivisors.length; i++) {
 
256
                                unitDivisor = unitDivisors[i];
 
257
                                unitLabel = unitLabels.length > i ? " " + unitLabels[i] : "";
 
258
                                
 
259
                                unit = remainder / unitDivisor;
 
260
                                if (i < unitDivisors.length -1) {
 
261
                                        unit = Math.floor(unit);
 
262
                                } else {
 
263
                                        unit = unit.toFixed(2);
 
264
                                }
 
265
                                if (unit > 0) {
 
266
                                        remainder = remainder % unitDivisor;
 
267
                                        
 
268
                                        formattedStrings.push(unit + unitLabel);
 
269
                                }
 
270
                        }
 
271
                        
 
272
                        return formattedStrings.join(" ");
 
273
                }
 
274
        };
 
275
        
 
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);
 
279
        
 
280
        };
 
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);
 
284
        
 
285
        };
 
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);
 
289
        
 
290
        };
 
291
        SWFUpload.speed.formatPercent = function (baseNumber) {
 
292
                return baseNumber.toFixed(2) + " %";
 
293
        };
 
294
        
 
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;
 
297
                var i;
 
298
                var mSum = 0, mCount = 0;
 
299
                
 
300
                size = history.length;
 
301
                
 
302
                // Check for sufficient data
 
303
                if (size >= 8) {
 
304
                        // Clone the array and Calculate sum of the values 
 
305
                        for (i = 0; i < size; i++) {
 
306
                                vals[i] = history[i];
 
307
                                sum += vals[i];
 
308
                        }
 
309
 
 
310
                        mean = sum / size;
 
311
 
 
312
                        // Calculate variance for the set
 
313
                        for (i = 0; i < size; i++) {
 
314
                                varianceTemp += Math.pow((vals[i] - mean), 2);
 
315
                        }
 
316
 
 
317
                        variance = varianceTemp / size;
 
318
                        standardDev = Math.sqrt(variance);
 
319
                        
 
320
                        //Standardize the Data
 
321
                        for (i = 0; i < size; i++) {
 
322
                                vals[i] = (vals[i] - mean) / standardDev;
 
323
                        }
 
324
 
 
325
                        // Calculate the average excluding outliers
 
326
                        var deviationRange = 2.0;
 
327
                        for (i = 0; i < size; i++) {
 
328
                                
 
329
                                if (vals[i] <= deviationRange && vals[i] >= -deviationRange) {
 
330
                                        mCount++;
 
331
                                        mSum += history[i];
 
332
                                }
 
333
                        }
 
334
                        
 
335
                } else {
 
336
                        // Calculate the average (not enough data points to remove outliers)
 
337
                        mCount = size;
 
338
                        for (i = 0; i < size; i++) {
 
339
                                mSum += history[i];
 
340
                        }
 
341
                }
 
342
 
 
343
                return mSum / mCount;
 
344
        };
 
345
        
 
346
}
 
 
b'\\ No newline at end of file'