Skip to content

Commit 28cb1a2

Browse files
winddiesnighca
winddies
authored andcommitted
添加日志报告 (#331)
添加日志报告
1 parent 2280689 commit 28cb1a2

File tree

7 files changed

+121
-30
lines changed

7 files changed

+121
-30
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,20 @@ subscription.unsubscribe() // 上传取消
133133
}
134134
}
135135
```
136-
* next: 接收上传进度信息,res 参数是一个带有 `total` 字段的 `object`,包含`loaded``total``percent`三个属性,提供上传进度信息。
136+
* next: 接收上传进度信息,res是一个带有 `total` 字段的 `object`,包含`loaded``total``percent`三个属性,提供上传进度信息。
137137
* total.loaded: `number`,已上传大小,单位为字节。
138138
* total.total: `number`,本次上传的总量控制信息,单位为字节,注意这里的 total 跟文件大小并不一致。
139139
* total.percent: `number`,当前上传进度,范围:0100
140140

141141
* error: 上传错误后触发,当不是 xhr 请求错误时,会把当前错误产生原因直接抛出,诸如 JSON 解析异常等;当产生 xhr 请求错误时,参数 err 为一个包含 `code``message``isRequestError` 三个属性的 `object`
142142
* err.isRequestError: 用于区分是否 xhr 请求错误;当 xhr 请求出现错误并且后端通过 HTTP 状态码返回了错误信息时,该参数为 `true`;否则为 `undefined`
143+
* err.reqId: `string`,xhr请求错误的 `X-Reqid`
143144
* err.code: `number`,请求错误状态码,只有在 `err.isRequestError`true 的时候才有效,可查阅码值对应[说明](https://developer.qiniu.com/kodo/api/3928/error-responses)。
144145
* err.message: `string`,错误信息,包含错误码,当后端返回提示信息时也会有相应的错误信息。
145146

146-
* complete: 接收上传完成后的后端返回信息,res 参数为一个 `object`, 为上传成功后后端返回的信息,具体返回结构取决于后端sdk的配置,可参考[上传策略](https://developer.qiniu.com/kodo/manual/1206/put-policy)。
147+
* complete: 接收上传完成后的后端返回信息,具体返回结构取决于后端sdk的配置,可参考[上传策略](https://developer.qiniu.com/kodo/manual/1206/put-policy)。
147148

148-
* subscription: 为一个带有 `unsubscribe` 方法的类实例,通过调用 `subscription.unsubscribe()` 停止当前文件上传
149+
* subscription: 为一个带有 `unsubscribe` 方法的类实例,通过调用 `subscription.unsubscribe()` 停止当前文件上传
149150

150151
* **file**: `Blob` 对象,上传的文件
151152
* **key**: 文件资源名
@@ -159,8 +160,9 @@ subscription.unsubscribe() // 上传取消
159160
};
160161
```
161162

162-
* config.useCdnDomain: 表示是否使用 cdn 加速域名,为布尔值,`true` 表示使用,默认为 `false`
163-
* config.region: 选择上传域名区域,默认为(z0)华东
163+
* config.useCdnDomain: 表示是否使用 cdn 加速域名,为布尔值,`true` 表示使用,默认为 `false`
164+
* config.disableStatisticsReport: 是否禁用日志报告,为布尔值,默认为 `false`
165+
* config.region: 选择上传域名区域,默认为(z0)华东。
164166

165167
* **putExtra**:
166168

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ import {
99
import { UploadManager } from "./upload";
1010
import { imageMogr2, watermark, imageInfo, exif, pipeline } from "./image";
1111
import { Observable } from "./observable";
12+
import { StatisticsLogger } from './statisticsLog'
13+
14+
let statisticsLogger = new StatisticsLogger();
1215

1316
function upload(file, key, token, putExtra, config) {
17+
1418
let options = {
1519
file,
1620
key,
@@ -24,7 +28,7 @@ function upload(file, key, token, putExtra, config) {
2428
onData: e => observer.next(e),
2529
onError: e => observer.error(e),
2630
onComplete: e => observer.complete(e)
27-
});
31+
}, statisticsLogger);
2832
uploadManager.putFile();
2933
return uploadManager.stop.bind(uploadManager);
3034
});

src/statisticsLog.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {createXHR} from './utils'
2+
3+
export class StatisticsLogger{
4+
5+
log(info, token) {
6+
let logString = "";
7+
Object.keys(info).forEach(k => logString += info[k] + ",");
8+
this.send(logString, token);
9+
}
10+
11+
send(logString, token){
12+
let xhr = createXHR();
13+
let count = 0;
14+
xhr.open('POST', "https://uplog.qbox.me/log/3");
15+
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
16+
xhr.setRequestHeader('Authorization', 'UpToken ' + token);
17+
xhr.onreadystatechange = function () {
18+
if (xhr.readyState === 4) {
19+
if (xhr.status !== 200) {
20+
count++;
21+
count <=3 ? xhr.send(logString) : "";
22+
}
23+
}
24+
};
25+
xhr.send(logString);
26+
}
27+
28+
}

src/upload.js

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
removeLocalFileInfo,
99
isContainFileMimeType,
1010
sum,
11+
getDomainFromUrl,
12+
getPortFromUrl,
1113
getHeadersForChunkUpload,
1214
getHeadersForMkFile,
1315
request,
@@ -19,10 +21,11 @@ import {
1921
let BLOCK_SIZE = 4 * 1024 * 1024;
2022

2123
export class UploadManager {
22-
constructor(options, handlers) {
24+
constructor(options, handlers, statisticsLogger) {
2325
this.config = Object.assign(
2426
{
2527
useCdnDomain: true,
28+
disableStatisticsReport: false,
2629
region: null
2730
},
2831
options.config
@@ -35,6 +38,8 @@ export class UploadManager {
3538
},
3639
options.putExtra
3740
);
41+
this.statisticsLogger = statisticsLogger;
42+
this.progress = null;
3843
this.xhrList = [];
3944
this.xhrHandler = xhr => this.xhrList.push(xhr);
4045
this.file = options.file;
@@ -60,14 +65,25 @@ export class UploadManager {
6065
}
6166

6267
this.uploadUrl = getUploadUrl(this.config);
68+
this.uploadAt = new Date().getTime();
6369

64-
let upload =
65-
this.file.size > BLOCK_SIZE ? this.resumeUpload() : this.directUpload();
66-
upload.then(res => this.onComplete(res), err => {
70+
let upload = this.file.size > BLOCK_SIZE ? this.resumeUpload() : this.directUpload();
71+
upload.then(res => {
72+
this.onComplete(res.data);
73+
if(!this.config.disableStatisticsReport){
74+
this.sendLog(res.reqId, 200);
75+
}
76+
}, err => {
6777
this.onError(err);
78+
if(err.isRequestError && !this.config.disableStatisticsReport){
79+
if(err.code !== 0){
80+
this.sendLog(err.reqId, err.code);
81+
}else{
82+
this.sendLog("", -2);
83+
}
84+
}
6885
this.stop();
6986
})
70-
7187
return upload;
7288
}
7389

@@ -76,6 +92,21 @@ export class UploadManager {
7692
this.xhrList = [];
7793
}
7894

95+
sendLog(reqId, code){
96+
this.statisticsLogger.log({
97+
code: code,
98+
reqId: reqId,
99+
host: getDomainFromUrl(this.uploadUrl),
100+
remoteIp: "",
101+
port: getPortFromUrl(this.uploadUrl),
102+
duration: (new Date().getTime() - this.uploadAt)/1000,
103+
time: Math.floor(this.uploadAt/1000),
104+
bytesSent: this.progress ? this.progress.total.loaded : 0,
105+
upType: "jssdk-h5",
106+
size: this.file.size
107+
}, this.token)
108+
}
109+
79110
// 直传
80111
directUpload() {
81112
let formData = new FormData();
@@ -185,8 +216,7 @@ export class UploadManager {
185216
let headers = getHeadersForMkFile(this.token);
186217
let onCreate = this.xhrHandler;
187218
let method = "POST";
188-
189-
return request(requestUrL, { method, body, headers, onCreate }).then(
219+
return request(requestUrL, { method, body, headers, onCreate}).then(
190220
res => {
191221
this.updateMkFileProgress(1);
192222
return Promise.resolve(res);
@@ -195,9 +225,8 @@ export class UploadManager {
195225
}
196226

197227
updateDirectProgress(loaded, total) {
198-
this.onData(
199-
{total: this.getProgressInfoItem(loaded, total)}
200-
)
228+
this.progress = {total: this.getProgressInfoItem(loaded, total)}
229+
this.onData(this.progress)
201230
}
202231

203232
initChunksProgress() {
@@ -216,17 +245,16 @@ export class UploadManager {
216245
}
217246

218247
notifyResumeProgress() {
219-
this.onData(
220-
{
221-
total: this.getProgressInfoItem(
222-
sum(this.loaded.chunks) + this.loaded.mkFileProgress,
223-
this.file.size + 1
224-
),
225-
chunks: this.chunks.map((chunk, index) => {
226-
return this.getProgressInfoItem(this.loaded.chunks[index], chunk.size);
227-
})
228-
}
229-
);
248+
this.progress = {
249+
total: this.getProgressInfoItem(
250+
sum(this.loaded.chunks) + this.loaded.mkFileProgress,
251+
this.file.size + 1
252+
),
253+
chunks: this.chunks.map((chunk, index) => {
254+
return this.getProgressInfoItem(this.loaded.chunks[index], chunk.size);
255+
})
256+
};
257+
this.onData(this.progress);
230258
}
231259

232260
getProgressInfoItem(loaded, size) {

src/utils.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,17 @@ export function request(url, options) {
170170
if (xhr.readyState !== 4) {
171171
return;
172172
}
173+
let reqId = xhr.getResponseHeader("x-reqId")|| "";
173174
if (xhr.status !== 200) {
174175
let message = `xhr request failed, code: ${xhr.status};`;
175176
if (responseText) {
176177
message = message + ` response: ${responseText}`;
177178
}
178-
reject({code: xhr.status, message: message, isRequestError: true});
179+
reject({code: xhr.status, message: message,reqId: reqId, isRequestError: true});
179180
return;
180181
}
181182
try {
182-
resolve(JSON.parse(responseText))
183+
resolve({data: JSON.parse(responseText), reqId: reqId})
183184
} catch (err) {
184185
reject(err)
185186
}
@@ -189,6 +190,33 @@ export function request(url, options) {
189190
});
190191
}
191192

193+
export function getPortFromUrl(url) {
194+
if (url && url.match) {
195+
let groups = url.match(/(^https?)/);
196+
if (!groups) {
197+
return "";
198+
}
199+
let type = groups[1];
200+
groups = url.match(/^https?:\/\/([^:^/]*):(\d*)/);
201+
if (groups) {
202+
return groups[2];
203+
}
204+
if (type === "http") {
205+
return "80";
206+
}
207+
return "443";
208+
}
209+
return "";
210+
}
211+
212+
export function getDomainFromUrl (url) {
213+
if (url && url.match) {
214+
let groups = url.match(/^https?:\/\/([^:^/]*)/);
215+
return groups ? groups[1] : "";
216+
}
217+
return "";
218+
}
219+
192220
// 构造区域上传url
193221
export function getUploadUrl(config) {
194222
let upHosts = regionUphostMap[config.region] || regionUphostMap[region.z0];

test/config.json.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
'AccessKey': '<Your Access Key>', // https://portal.qiniu.com/user/key
33
'SecretKey': '<Your Secret Key>',
44
'Bucket': '<Your Bucket Name>',
5-
'Port': 19110,
5+
'Port': 9000,
66
'UptokenUrl': 'uptoken',
77
'Domain': '<Your Bucket Domain>' // bucket domain eg:http://qiniu-plupload.qiniudn.com/
88
};

test/demo1/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var domain = res.domain;
44
var config = {
55
useCdnDomain: true,
6+
disableStatisticsReport: false,
67
region: qiniu.region.z2
78
};
89
var putExtra = {

0 commit comments

Comments
 (0)