Commit 44d42008 authored by aiduck's avatar aiduck

去除cache添加字符内容超过3000上传cdn操作

parent c12c9040
...@@ -12,10 +12,9 @@ class Reporter { ...@@ -12,10 +12,9 @@ class Reporter {
}; };
recordKey = ""; recordKey = "";
environmentUrl = ""; // 环境配置 environmentUrl = ""; // 环境配置
isUploading = false; // 是否在上传cdn
bus = []; // 上传分片数据 bus = []; // 上传分片数据
counter = new Counter(); // 计数器和recordKey一起重置 counter = new Counter(); // 计数器和recordKey一起重置
cache = []; // cdn数据缓存 uploadingPile = new Set([]);
init({ env, ...baseInfo }) { init({ env, ...baseInfo }) {
this.baseInfo = baseInfo; this.baseInfo = baseInfo;
this.setEnvironment(env); this.setEnvironment(env);
...@@ -39,11 +38,18 @@ class Reporter { ...@@ -39,11 +38,18 @@ class Reporter {
} }
// 上传CDN // 上传CDN
toCDN(payload) { toCDN(payload) {
this.isUploading = true;
const blob = new Blob([JSON.stringify(payload)], { type: "application/json" }); const blob = new Blob([JSON.stringify(payload)], { type: "application/json" });
const formData = new FormData(); const formData = new FormData();
// 保存上下文
const trackId = this.counter.count; const trackId = this.counter.count;
uploadingPile.add(trackId);
formData.append("file", blob, `${this.recordKey + trackId}.json`); formData.append("file", blob, `${this.recordKey + trackId}.json`);
const extra = {
...this.baseInfo,
recordKey: this.recordKey,
trackId: trackId,
isCDN: true
};
try { try {
fetch(`${this.environmentUrl}/upload`, { fetch(`${this.environmentUrl}/upload`, {
// fetch(`http://172.16.47.148:3000/upload`, { // fetch(`http://172.16.47.148:3000/upload`, {
...@@ -55,35 +61,19 @@ class Reporter { ...@@ -55,35 +61,19 @@ class Reporter {
return res.json(); return res.json();
}) })
.then(res => { .then(res => {
// 如果cache中有数据,说明是多次提交并且数据缓存在了cache中,那么我们全量快照可能在cache[0] const snapIndex = this.bus && this.bus.map((item, index) => {
const snapArr = this.cache && this.cache.length > 0 ? this.cache : this.bus; if(item.trackId === trackId) {
const snapIndex = snapArr.findIndex((item, index) => {
if (item.track.type === 2) {
return index; return index;
} }
}); });
const extra = { log("url定位", snapIndex);
...this.baseInfo, log("url内容", res.data.url);
recordKey: this.recordKey, // 向bus中注入
trackId: trackId, this.bus.splice(snapIndex, 1, dataWrapper(extra, res.data.url));
isCDN: true, // 在trackId之前 包括trackId本身的数据全部上传
}; const reportData = this.bus.splice(0, snapIndex + 1);
log("type=2定位", snapIndex); this.report(reportData);
log("cnd Url", res.data.url); uploadingPile.delete(trackId);
// 向cache或者bus中注入
if (this.cache && this.cache.length > 0) {
this.cache[0].splice(snapIndex, 1, dataWrapper(extra, res.data.url));
} else {
this.bus.splice(snapIndex, 1, dataWrapper(extra, res.data.url));
}
this.isUploading = false;
// 如果cache里面有数据需要上传的,那么先上传
if (this.cache && this.cache.length > 0) {
this.cache.map(item => {
this.report(item);
});
this.cache = [];
}
return res; return res;
}); });
} catch (e) { } catch (e) {
...@@ -95,20 +85,23 @@ class Reporter { ...@@ -95,20 +85,23 @@ class Reporter {
...this.baseInfo, ...this.baseInfo,
recordKey: this.recordKey, recordKey: this.recordKey,
trackId: this.counter.next(), trackId: this.counter.next(),
isCDN: false, isCDN: false
}; };
this.bus.push(dataWrapper(extra, data)); this.bus.push(dataWrapper(extra, data));
} }
// 上传 // 尝试上传
report(data) { tryReport(data) {
log("上传数据", data); // 如果有cdn的内容在上传,那么我们不做任何的上传直接返回
const reportData = data; if (this.uploadingPile.length > 0) {
this.bus = []; log('有cdn的内容在上传');
if (this.isUploading) {
log("cdn数据正在上传,先将内容存到cache", this.cache);
this.cache.push(reportData);
return; return;
} else {
this.report(data);
} }
}
// 真正上传
report(data) {
log("上传数据", data);
try { try {
fetch(`${this.environmentUrl}/behavior/record`, { fetch(`${this.environmentUrl}/behavior/record`, {
// fetch(`http://172.16.47.148:3000/behavior/record`, { // fetch(`http://172.16.47.148:3000/behavior/record`, {
...@@ -136,7 +129,6 @@ class Reporter { ...@@ -136,7 +129,6 @@ class Reporter {
} }
// 重置数据(分条使用) // 重置数据(分条使用)
reset() { reset() {
this.cache = [];
this.bus = []; this.bus = [];
this.counter.reset(); this.counter.reset();
this.generateKey(); this.generateKey();
......
...@@ -14,19 +14,19 @@ onmessage = ({ data: { type, payload } }) => { ...@@ -14,19 +14,19 @@ onmessage = ({ data: { type, payload } }) => {
// todo : 数据本地存储 // todo : 数据本地存储
reporter.toBus(payload); reporter.toBus(payload);
log("bus", reporter.bus); log("bus", reporter.bus);
// todo : 全量快照上传cdn // todo : 全量快照上传cdn || 或者字符内容超过3000
log("track type", payload.type, payload); let strData = JSON.stringify(payload.data);
if (payload.type === 2) { if (payload.type === 2 || strData.length > 30000) {
reporter.toCDN(payload); reporter.toCDN(payload);
} }
// todo : 数据压缩 // todo : 数据压缩
// todo : 根据事件类型优先级触发上传策略(click) // todo : 根据事件类型优先级触发上传策略(click)
if (payload.data.source === 2 && (payload.data.type === 2 || payload.data.type === 3) && !reporter.isUploading) { if (payload.data.source === 2 && (payload.data.type === 2 || payload.data.type === 3) && !reporter.isUploading) {
reporter.report(reporter.bus); reporter.tryReport(reporter.bus);
} }
// todo : 数据超出100条上线,自动上传 // todo : 数据超出100条上线,自动上传
if (reporter.bus.length > 100 && !reporter.isUploading) { if (reporter.bus.length > 100 && !reporter.isUploading) {
reporter.report(reporter.bus); reporter.tryReport(reporter.bus);
} }
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