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 {
...
@@ -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
();
...
...
src/lib/tracks.worker.js
View file @
44d42008
...
@@ -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
l
og
(
"track type"
,
payload
.
type
,
payload
);
l
et
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
.
r
eport
(
reporter
.
bus
);
reporter
.
tryR
eport
(
reporter
.
bus
);
}
}
// todo : 数据超出100条上线,自动上传
// todo : 数据超出100条上线,自动上传
if
(
reporter
.
bus
.
length
>
100
&&
!
reporter
.
isUploading
)
{
if
(
reporter
.
bus
.
length
>
100
&&
!
reporter
.
isUploading
)
{
reporter
.
r
eport
(
reporter
.
bus
);
reporter
.
tryR
eport
(
reporter
.
bus
);
}
}
break
;
break
;
case
"reset"
:
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