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
440f6b16
Commit
440f6b16
authored
Jul 29, 2019
by
aiduck
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
合并
parent
20ceeb35
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
94 additions
and
76 deletions
+94
-76
monitor.js
src/lib/monitor.js
+10
-19
reporter.js
src/lib/reporter.js
+54
-29
tracks.worker.js
src/lib/tracks.worker.js
+8
-4
utils.js
src/lib/utils.js
+22
-24
No files found.
src/lib/monitor.js
View file @
440f6b16
import
*
as
rrweb
from
"rrweb"
;
import
TracksWorker
from
"./tracks.worker"
;
import
utils
from
'./utils'
;
export
default
class
Monitor
{
rrwebHandler
=
null
;
tracksWorker
=
new
TracksWorker
();
constructor
()
{
this
.
tracksWorker
.
onmessage
=
({
data
:
{
type
,
payload
}
})
=>
{
switch
(
type
)
{
case
"localData"
:
window
.
localStorage
.
setItem
(
"rrevents"
,
JSON
.
stringify
(
payload
));
break
;
default
:
console
.
log
(
"monitor unknow action"
);
constructor
(
props
)
{
console
.
log
(
'初始化'
);
this
.
tracksWorker
.
postMessage
({
type
:
"init"
,
payload
:
{
system
:
props
.
system
,
config
:
props
.
config
}
};
}
)
;
}
resetRrwebHandler
()
{
this
.
rrwebHandler
&&
this
.
rrwebHandler
();
}
/**
* 初始化
* @param {*} event
* @memberof Monitor
*/
init
(
event
)
{
this
.
resetRrwebHandler
();
console
.
log
(
"初始化系统信息"
);
this
.
tracksWorker
.
postMessage
({
type
:
"init"
,
payload
:
event
});
}
/**
* 录制
* @memberof Monitor
...
...
src/lib/reporter.js
View file @
440f6b16
// 数据维护,上报
import
utils
from
'./utils'
;
import
Counter
from
"./counter"
;
import
{
dataWrapper
,
log
}
from
'./utils'
;
class
Reporter
{
system
=
''
;
//系统名
system
=
''
;
// 系统名
config
=
{
env
:
'prod'
,
log
:
false
};
// 系统配置
environmentUrl
=
''
;
// 环境
userIdentifier
=
''
;
//用户标示
path
=
''
;
// 当前路径
isUploading
=
false
;
// 是否在上传cdn
...
...
@@ -16,10 +22,24 @@ class Reporter {
setSystem
(
system
)
{
this
.
system
=
system
;
}
// 设置配置内容
setConfig
(
config
)
{
this
.
config
=
config
;
this
.
setEnvironment
(
config
.
env
);
}
// 设置系统环境变量
setEnvironment
(
env
)
{
if
(
env
===
'dev'
)
{
this
.
environmentUrl
=
'http://hunter.duibadev.com.cn'
;
}
else
if
(
env
===
'prod'
)
{
this
.
environmentUrl
=
'http://hunter.dui88.com.cn'
;
}
}
// 设置用户标示
setUserIdentifier
(
userIdentifier
)
{
this
.
userIdentifier
=
userIdentifier
;
}
// 设置当前访问路径
setPath
(
url
)
{
this
.
path
=
url
...
...
@@ -27,12 +47,12 @@ class Reporter {
// 上传CDN
toCDN
(
payload
)
{
this
.
isUploading
=
true
;
le
t
blob
=
new
Blob
([
JSON
.
stringify
(
payload
)],
{
type
:
'application/json'
});
le
t
formData
=
new
FormData
();
let
trackId
=
this
.
counter
.
next
()
;
cons
t
blob
=
new
Blob
([
JSON
.
stringify
(
payload
)],
{
type
:
'application/json'
});
cons
t
formData
=
new
FormData
();
const
trackId
=
this
.
counter
.
count
;
formData
.
append
(
'file'
,
blob
,
`
${
this
.
recordKey
+
trackId
}
.json`
);
try
{
fetch
(
`
http://hunter.duibadev.com.cn
/upload`
,
{
fetch
(
`
${
this
.
environmentUrl
}
/upload`
,
{
method
:
'POST'
,
credentials
:
'include'
,
body
:
formData
...
...
@@ -40,17 +60,28 @@ class Reporter {
return
res
.
json
();
}).
then
(
res
=>
{
// 如果cache中有数据,说明是多次提交并且数据缓存在了cache中,那么我们全量快照可能在cache[0]
let
index
=
this
.
bus
.
findIndex
(
v
=>
v
.
track
.
type
===
2
);
console
.
log
(
index
);
let
extra
=
{
const
snapArr
=
this
.
cache
&&
this
.
cache
.
length
>
0
?
this
.
cache
:
this
.
bus
;
const
snapIndex
=
snapArr
.
findIndex
((
item
,
index
)
=>
{
if
(
item
.
track
.
type
===
2
)
{
return
index
;
}
});
const
extra
=
{
system
:
this
.
system
,
userIdentifier
:
this
.
userIdentifier
,
path
:
this
.
path
,
recordKey
:
this
.
recordKey
,
trackId
:
trackId
,
isCdn
:
true
isCDN
:
true
}
log
(
this
.
config
.
log
,
'type=2定位'
,
snapIndex
);
log
(
this
.
config
.
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
.
bus
.
splice
(
index
,
1
,
utils
.
dataWrapper
(
extra
,
res
.
url
));
this
.
isUploading
=
false
;
// 如果cache里面有数据需要上传的,那么先上传
if
(
this
.
cache
&&
this
.
cache
.
length
>
0
)
{
...
...
@@ -62,47 +93,41 @@ class Reporter {
return
res
;
});
}
catch
(
e
)
{
console
.
log
(
'上传失败,原因:'
);
console
.
log
(
e
.
message
);
log
(
this
.
config
.
log
,
'上传失败,原因:'
,
e
.
message
);
}
}
toBus
(
data
)
{
le
t
extra
=
{
cons
t
extra
=
{
system
:
this
.
system
,
userIdentifier
:
this
.
userIdentifier
,
path
:
this
.
path
,
recordKey
:
this
.
recordKey
,
trackId
:
this
.
counter
.
next
(),
isC
dn
:
false
isC
DN
:
false
}
this
.
bus
.
push
(
utils
.
dataWrapper
(
extra
,
data
));
this
.
bus
.
push
(
dataWrapper
(
extra
,
data
));
}
// 上传
report
(
data
)
{
console
.
log
(
'上传数据'
,
data
);
le
t
reportData
=
data
;
log
(
this
.
config
.
log
,
'上传数据'
,
data
);
cons
t
reportData
=
data
;
this
.
bus
=
[];
if
(
this
.
isUploading
)
{
console
.
log
(
'cdn数据正在上传,先将内容存到cache'
);
log
(
this
.
config
.
log
,
'cdn数据正在上传,先将内容存到cache'
,
this
.
cache
);
this
.
cache
.
push
(
reportData
);
return
;
}
try
{
fetch
(
`
http://hunter.duibadev.com.cn
/behavior/record`
,
{
fetch
(
`
${
this
.
environmentUrl
}
/behavior/record`
,
{
method
:
'POST'
,
credentials
:
'include'
,
headers
:
{
'Content-Type'
:
'application/json; charset=utf-8'
},
body
:
JSON
.
stringify
({
tracks
:
data
})
'Content-Type'
:
'application/json;charset=utf-8'
,
body
:
JSON
.
stringify
({
tracks
:
data
})
}).
then
(
res
=>
{
console
.
log
(
res
);
log
(
this
.
config
.
log
,
'上传返回的内容'
,
res
);
});
}
catch
(
e
)
{
console
.
log
(
'上传失败,原因:'
);
console
.
log
(
e
.
message
);
log
(
this
.
config
.
log
,
'上传失败,原因:'
,
e
.
message
);
}
}
// 重置数据(分条使用)
...
...
src/lib/tracks.worker.js
View file @
440f6b16
// 入口,格式化数据,模块管理
import
md5
from
"md5"
;
import
Reporter
from
"./reporter"
;
import
{
log
}
from
'./utils'
;
const
reporter
=
new
Reporter
(
"recordKey"
);
onmessage
=
({
data
:
{
type
,
payload
}
})
=>
{
switch
(
type
)
{
case
"init"
:
reporter
.
setSystem
(
payload
);
reporter
.
setSystem
(
payload
.
system
);
reporter
.
setConfig
(
payload
.
config
);
log
(
reporter
.
config
.
log
,
'init参数'
,
payload
);
break
;
case
"record"
:
// todo : 数据本地存储
reporter
.
toBus
(
payload
);
console
.
log
(
reporter
.
bus
);
log
(
reporter
.
config
.
log
,
'bus'
,
reporter
.
bus
);
// todo : 全量快照上传cdn
log
(
reporter
.
config
.
log
,
'track type'
,
payload
.
type
);
if
(
payload
.
type
===
2
)
{
reporter
.
toCDN
(
payload
);
}
...
...
@@ -36,9 +40,9 @@ onmessage = ({ data: { type, payload } }) => {
reporter
.
setPath
(
url
);
let
recordKey
=
md5
(
url
+
email
+
Date
.
parse
(
new
Date
()));
reporter
.
reset
(
recordKey
);
console
.
log
(
url
,
"分片id"
,
reporter
.
recordKey
);
log
(
reporter
.
config
.
log
,
'分片id'
,
reporter
.
recordKey
);
break
;
default
:
console
.
log
(
"unknow action"
);
log
(
reporter
.
config
.
log
,
'unknow action'
,
type
);
}
};
src/lib/utils.js
View file @
440f6b16
const
utils
=
{
// 包装event
dataWrapper
(
extraData
,
event
)
{
return
{
...
extraData
,
track
:
event
}
},
// 判断是否有值
isNothing
(
value
)
{
return
value
===
''
||
value
===
undefined
||
value
===
null
||
(
typeof
value
===
'number'
&&
(
isNaN
(
value
)
||
!
isFinite
(
value
)));
},
// 拼接URL请求参数
serialize
(
obj
)
{
const
str
=
[];
for
(
let
p
in
obj
)
{
if
(
obj
.
hasOwnProperty
(
p
))
{
str
.
push
(
p
+
'='
+
this
.
isNothing
(
obj
[
p
])
?
''
:
obj
[
p
]);
}
}
return
str
.
join
(
'&'
);
},
}
/**
* 包装event
* @param {*} extraData
* @param {*} event
*/
export
const
dataWrapper
=
(
extraData
,
event
)
=>
{
return
{
...
extraData
,
track
:
event
}
};
export
default
utils
;
\ No newline at end of file
/**
* 是否打印日志
* @param {*} logMes
*/
export
const
log
=
(
log
,
logMes
,
logContent
)
=>
{
if
(
log
)
{
return
console
.
log
(
logMes
,
logContent
);
}
else
{
return
;
}
};
\ No newline at end of file
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