Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
rip
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fanxuehui
rip
Commits
44d42008
Commit
44d42008
authored
Aug 28, 2019
by
aiduck
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
去除cache添加字符内容超过3000上传cdn操作
parent
c12c9040
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
44 deletions
+36
-44
reporter.js
src/lib/reporter.js
+31
-39
tracks.worker.js
src/lib/tracks.worker.js
+5
-5
No files found.
src/lib/reporter.js
View file @
44d42008
...
...
@@ -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
();
...
...
src/lib/tracks.worker.js
View file @
44d42008
...
...
@@ -14,19 +14,19 @@ onmessage = ({ data: { type, payload } }) => {
// todo : 数据本地存储
reporter
.
toBus
(
payload
);
log
(
"bus"
,
reporter
.
bus
);
// todo : 全量快照上传cdn
l
og
(
"track type"
,
payload
.
type
,
payload
);
if
(
payload
.
type
===
2
)
{
// todo : 全量快照上传cdn
|| 或者字符内容超过3000
l
et
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
.
r
eport
(
reporter
.
bus
);
reporter
.
tryR
eport
(
reporter
.
bus
);
}
// todo : 数据超出100条上线,自动上传
if
(
reporter
.
bus
.
length
>
100
&&
!
reporter
.
isUploading
)
{
reporter
.
r
eport
(
reporter
.
bus
);
reporter
.
tryR
eport
(
reporter
.
bus
);
}
break
;
case
"reset"
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment