Commit 4865ea31 authored by 杨贺晨吉's avatar 杨贺晨吉

Merge branch 'feature/yhcj-rip120' into 'master'

rip1.2.0版本改造

See merge request !4
parents 19e7f164 4342e267
......@@ -15,7 +15,6 @@ class Reporter {
environmentUrl = ""; // 环境配置
bus = []; // 上传分片数据
counter = new Counter(); // 计数器和recordKey一起重置
uploadingPile = new Set([]);
init({ env, ...baseInfo }) {
this.baseInfo = baseInfo;
this.setEnvironment(env);
......@@ -37,98 +36,21 @@ class Reporter {
this.environmentUrl = `${this.baseInfo.protocol}/hunter.dui88.com`;
}
}
// 上传CDN
toCDN(payload) {
// const blob = new Blob([JSON.stringify(payload)], { type: "application/json" });
// const formData = new FormData();
// 保存上下文
const trackId = this.counter.count;
const recordKey = this.recordKey;
log("trackId", trackId);
log("recordKey", recordKey);
this.uploadingPile.add(trackId);
// formData.append("file", blob, `${this.recordKey + trackId}.json`);
const extra = {
...this.baseInfo,
recordKey: this.recordKey,
trackId: trackId,
isCDN: true,
};
try {
fetch(`${this.environmentUrl}/uploadString`, {
// fetch(`http://localhost:3000/uploadString`, {
method: "POST",
credentials: "include",
// body: formData
headers: new Headers({ "content-type": "application/json" }),
body: JSON.stringify({
payload: payload,
recordKey: recordKey,
trackId: trackId
}),
})
.then(res => {
return res.json();
})
.then(res => {
/** 因为在路由跳转前可能存在上个路由里面遗留的部分数据,
* 并且销毁dom的时候数据量又大于40000,那么一定调用了上传toCDN的接口
* 而toCDN的接口会保存之前的trackId,而this.bus在路由跳转的时候会被清空
* 那么上下文的trackId在this.bus中就会找不到,找到也不是正确的*/
if (this.recordKey !== recordKey) {
log("路由跳转遗留数据");
return;
} else {
const snapIndex = this.bus && this.bus.findIndex(item => item.trackId === trackId);
log("snapIndex", snapIndex);
// 向bus中注入
this.bus.splice(snapIndex, 1, dataWrapper(extra, res.data.url));
// 在trackId之前 包括trackId本身的数据全部上传
this.uploadingPile.delete(trackId);
// 2,3都是cdn 3先回来那么就应该查看是否2还在上传
let reportData = [];
if(this.uploadingPile.size === 0) {
reportData = this.bus.splice(0, snapIndex + 1);
} else {
let min = Math.min(...Array.from(this.uploadingPile));
if(min > trackId) {
reportData = this.bus.splice(0, min);
} else {
log("等待前面cdn回传");
}
}
// const reportData = this.bus.splice(0, snapIndex + 1);
this.report(reportData);
return res;
}
});
} catch (e) {
log("上传失败,原因:", e.message);
}
}
toBus(data) {
const extra = {
...this.baseInfo,
recordKey: this.recordKey,
trackId: this.counter.next(),
isCDN: false,
trackId: this.counter.next()
};
this.bus.push(dataWrapper(extra, data));
}
// 尝试上传
tryReport(isOverHundred) {
// 如果有cdn的内容在上传,那么我们不做任何的上传直接返回
if (this.uploadingPile.size > 0) {
log("有cdn的内容在上传", this.uploadingPile);
return;
} else {
let reportData = isOverHundred ? this.bus.splice(0, 100) : this.bus.splice(0, this.bus.length);
this.report(reportData);
}
}
// 真正上传
report(data) {
log("上传数据", data);
report(isOverHundred) {
let reportData = isOverHundred ? this.bus.splice(0, 100) : this.bus.splice(0, this.bus.length);
let extraData = {
recordKey: this.recordKey,
...this.baseInfo
}
delete extraData.protocol;
log("上传数据", reportData);
log("上传后的bus", this.bus);
try {
fetch(`${this.environmentUrl}/behavior/record`, {
......@@ -136,7 +58,11 @@ class Reporter {
method: "POST",
credentials: "include",
headers: new Headers({ "content-type": "application/json" }),
body: JSON.stringify({ tracks: data }),
body: JSON.stringify({
tracks: reportData,
...extraData,
startId: reportData[0].trackId
}),
}).then(res => {
log("上传返回的内容", res);
});
......
......@@ -14,19 +14,14 @@ onmessage = ({ data: { type, payload } }) => {
// todo : 数据本地存储
reporter.toBus(payload);
log("bus", reporter.bus);
// todo : 全量快照上传cdn || 或者字符内容超过3000
let strData = JSON.stringify(payload.data);
if (payload.type === 2 || strData.length > 40000) {
reporter.toCDN(payload);
}
// todo : 数据压缩
// todo : 根据事件类型优先级触发上传策略(click)
if (payload.data.source === 2 && (payload.data.type === 2 || payload.data.type === 3)) {
reporter.tryReport(false);
reporter.report(false);
}
// todo : 数据超出100条上线,自动上传
if (reporter.bus.length > 100) {
reporter.tryReport(true);
reporter.report(true);
}
break;
case "reset":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment