Skip to content

Commit 6864551

Browse files
committed
extract statistics logger out of uploader
1 parent a019f47 commit 6864551

File tree

1 file changed

+91
-90
lines changed

1 file changed

+91
-90
lines changed

src/qiniu.js

Lines changed: 91 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,94 @@
173173

174174
var changeUrlTimes = 0;
175175

176+
function StatisticsLogger() {
177+
// api to collect upload logs
178+
var qiniuCollectUploadLogUrl = "https://uplog.qbox.me/log/3";
179+
180+
/**
181+
* { log: string, status: number }[] status: 0 待处理, 1 正在发送, 2 发送完毕
182+
*/
183+
var queue = [];
184+
var TaskStatus = {
185+
waiting: 0,
186+
processing: 1,
187+
finished: 2
188+
}
189+
190+
/**
191+
* send logs to statistics server
192+
*
193+
* @param {number} code status code
194+
* @param {string} req_id request id
195+
* @param {string} host
196+
* @param {string} remote_ip
197+
* @param {string} port
198+
* @param {string} duration
199+
* @param {string} up_time
200+
* @param {number} bytes_sent uploaded size (bytes)
201+
* @param {string} up_type js sdk runtime: html5, html4, flash
202+
* @param {number} file_size file total size (bytes)
203+
*/
204+
this.log = function (code, req_id, host, remote_ip, port, duration, up_time, bytes_sent, up_type, file_size) {
205+
var log = Array.prototype.join.call(arguments, ',');
206+
queue.push({
207+
log: log,
208+
status: TaskStatus.waiting
209+
});
210+
logger.debug("[STATISTICS] send log to statistics server", log);
211+
}
212+
213+
function tick() {
214+
var unFinishedTasks = [];
215+
for (var i = 0; i < queue.length; i++) {
216+
if (queue[i].status !== TaskStatus.finished) {
217+
unFinishedTasks.push(queue[i]);
218+
}
219+
if (queue[i].status === TaskStatus.waiting) {
220+
send(queue[i]);
221+
}
222+
}
223+
queue = unFinishedTasks;
224+
}
225+
226+
function send(task) {
227+
task.status = TaskStatus.processing;
228+
var ajax = that.createAjax();
229+
ajax.open('POST', qiniuCollectUploadLogUrl, true);
230+
ajax.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
231+
ajax.setRequestHeader('Authorization', 'UpToken ' + that.token);
232+
ajax.onreadystatechange = function () {
233+
if (ajax.readyState === 4) {
234+
if (ajax.status === 200) {
235+
logger.debug("[STATISTICS] successfully report log to server");
236+
task.status = TaskStatus.finished;
237+
} else {
238+
logger.debug("[STATISTICS] report log to server failed");
239+
task.status = TaskStatus.waiting;
240+
}
241+
}
242+
};
243+
ajax.send(task.log);
244+
}
245+
246+
// start a timer to report
247+
setInterval(tick, 1000);
248+
}
249+
var statisticsLogger = new StatisticsLogger();
250+
var ExtraErrors = {
251+
ZeroSizeFile: -6,
252+
InvalidToken: -5,
253+
InvalidArgument: -4,
254+
InvalidFile: -3,
255+
Cancelled: -2,
256+
NetworkError: -1,
257+
UnknownError: 0,
258+
TimedOut: -1001,
259+
UnknownHost: -1003,
260+
CannotConnectToHost: -1004,
261+
NetworkConnectionLost: -1005
262+
}
263+
176264
/**
177265
* reset upload url
178266
* if current page protocal is https
@@ -793,94 +881,6 @@
793881
return "";
794882
}
795883

796-
function StatisticsLogger() {
797-
// api to collect upload logs
798-
var qiniuCollectUploadLogUrl = "https://uplog.qbox.me/log/3";
799-
800-
/**
801-
* { log: string, status: number }[] status: 0 待处理, 1 正在发送, 2 发送完毕
802-
*/
803-
var queue = [];
804-
var TaskStatus = {
805-
waiting: 0,
806-
processing: 1,
807-
finished: 2
808-
}
809-
810-
/**
811-
* send logs to statistics server
812-
*
813-
* @param {number} code status code
814-
* @param {string} req_id request id
815-
* @param {string} host
816-
* @param {string} remote_ip
817-
* @param {string} port
818-
* @param {string} duration
819-
* @param {string} up_time
820-
* @param {number} bytes_sent uploaded size (bytes)
821-
* @param {string} up_type js sdk runtime: html5, html4, flash
822-
* @param {number} file_size file total size (bytes)
823-
*/
824-
this.log = function (code, req_id, host, remote_ip, port, duration, up_time, bytes_sent, up_type, file_size) {
825-
var log = Array.prototype.join.call(arguments, ',');
826-
queue.push({
827-
log: log,
828-
status: TaskStatus.waiting
829-
});
830-
logger.debug("[STATISTICS] send log to statistics server", log);
831-
}
832-
833-
function tick() {
834-
var unFinishedTasks = [];
835-
for (var i = 0; i < queue.length; i++) {
836-
if (queue[i].status !== TaskStatus.finished) {
837-
unFinishedTasks.push(queue[i]);
838-
}
839-
if (queue[i].status === TaskStatus.waiting) {
840-
send(queue[i]);
841-
}
842-
}
843-
queue = unFinishedTasks;
844-
}
845-
846-
function send(task) {
847-
task.status = TaskStatus.processing;
848-
var ajax = that.createAjax();
849-
ajax.open('POST', qiniuCollectUploadLogUrl, true);
850-
ajax.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
851-
ajax.setRequestHeader('Authorization', 'UpToken ' + that.token);
852-
ajax.onreadystatechange = function () {
853-
if (ajax.readyState === 4) {
854-
if (ajax.status === 200) {
855-
logger.debug("[STATISTICS] successfully report log to server");
856-
task.status = TaskStatus.finished;
857-
} else {
858-
logger.debug("[STATISTICS] report log to server failed");
859-
task.status = TaskStatus.waiting;
860-
}
861-
}
862-
};
863-
ajax.send(task.log);
864-
}
865-
866-
// start a timer to report
867-
setInterval(tick, 1000);
868-
}
869-
var statisticsLogger = new StatisticsLogger();
870-
var ExtraErrors = {
871-
ZeroSizeFile: -6,
872-
InvalidToken: -5,
873-
InvalidArgument: -4,
874-
InvalidFile: -3,
875-
Cancelled: -2,
876-
NetworkError: -1,
877-
UnknownError: 0,
878-
TimedOut: -1001,
879-
UnknownHost: -1003,
880-
CannotConnectToHost: -1004,
881-
NetworkConnectionLost: -1005
882-
}
883-
884884
/********** inner function define end **********/
885885

886886
if (op.log_level) {
@@ -1351,9 +1351,10 @@
13511351
// add send log for upload error
13521352
var matchedGroups = (err && err.responseHeaders && err.responseHeaders.match) ? err.responseHeaders.match(/(X-Reqid\:\ )([^,]*)/) : []
13531353
var req_id = matchedGroups[2]
1354-
var errcode = plupload.HTTP_ERROR ? err.status : err.code, req_id
1354+
var errcode = plupload.HTTP_ERROR ? err.status : err.code,
1355+
req_id
13551356
statisticsLogger.log(
1356-
errcode == 0 ? ExtraErrors.NetworkError: errcode,
1357+
errcode == 0 ? ExtraErrors.NetworkError : errcode,
13571358
req_id,
13581359
getDomainFromUrl(up.settings.url),
13591360
undefined,

0 commit comments

Comments
 (0)