Commit 4342e267 authored by aiduck's avatar aiduck

rip1.2.0版本改造

parent 19e7f164
...@@ -15,7 +15,6 @@ class Reporter { ...@@ -15,7 +15,6 @@ class Reporter {
environmentUrl = ""; // 环境配置 environmentUrl = ""; // 环境配置
bus = []; // 上传分片数据 bus = []; // 上传分片数据
counter = new Counter(); // 计数器和recordKey一起重置 counter = new Counter(); // 计数器和recordKey一起重置
uploadingPile = new Set([]);
init({ env, ...baseInfo }) { init({ env, ...baseInfo }) {
this.baseInfo = baseInfo; this.baseInfo = baseInfo;
this.setEnvironment(env); this.setEnvironment(env);
...@@ -37,98 +36,21 @@ class Reporter { ...@@ -37,98 +36,21 @@ class Reporter {
this.environmentUrl = `${this.baseInfo.protocol}/hunter.dui88.com`; 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) { toBus(data) {
const extra = { const extra = {
...this.baseInfo, trackId: this.counter.next()
recordKey: this.recordKey,
trackId: this.counter.next(),
isCDN: false,
}; };
this.bus.push(dataWrapper(extra, data)); 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) { report(isOverHundred) {
log("上传数据", data); 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); log("上传后的bus", this.bus);
try { try {
fetch(`${this.environmentUrl}/behavior/record`, { fetch(`${this.environmentUrl}/behavior/record`, {
...@@ -136,7 +58,11 @@ class Reporter { ...@@ -136,7 +58,11 @@ class Reporter {
method: "POST", method: "POST",
credentials: "include", credentials: "include",
headers: new Headers({ "content-type": "application/json" }), headers: new Headers({ "content-type": "application/json" }),
body: JSON.stringify({ tracks: data }), body: JSON.stringify({
tracks: reportData,
...extraData,
startId: reportData[0].trackId
}),
}).then(res => { }).then(res => {
log("上传返回的内容", res); log("上传返回的内容", res);
}); });
......
...@@ -14,19 +14,14 @@ onmessage = ({ data: { type, payload } }) => { ...@@ -14,19 +14,14 @@ onmessage = ({ data: { type, payload } }) => {
// todo : 数据本地存储 // todo : 数据本地存储
reporter.toBus(payload); reporter.toBus(payload);
log("bus", reporter.bus); 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 : 数据压缩
// todo : 根据事件类型优先级触发上传策略(click) // todo : 根据事件类型优先级触发上传策略(click)
if (payload.data.source === 2 && (payload.data.type === 2 || payload.data.type === 3)) { if (payload.data.source === 2 && (payload.data.type === 2 || payload.data.type === 3)) {
reporter.tryReport(false); reporter.report(false);
} }
// todo : 数据超出100条上线,自动上传 // todo : 数据超出100条上线,自动上传
if (reporter.bus.length > 100) { if (reporter.bus.length > 100) {
reporter.tryReport(true); reporter.report(true);
} }
break; break;
case "reset": 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