Commit 44d42008 authored by aiduck's avatar aiduck

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

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