Skip to content

Commit d06a88b

Browse files
committed
refactor(ccp): 优化ccp收集
1 parent e58690c commit d06a88b

File tree

5 files changed

+58
-26
lines changed

5 files changed

+58
-26
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"@zyf2e/monitor-types": "^2.1.29",
8787
"@zyf2e/monitor-utils": "^2.1.29",
8888
"@zyf2e/monitor-vue": "^2.1.29",
89-
"core-js": "^3.19.1"
89+
"core-js": "^3.19.1",
90+
"path-to-regexp": "^6.2.0"
9091
}
9192
}

packages/web-performance/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dist"
1212
],
1313
"dependencies": {
14-
"core-js": "^3.19.1"
14+
"core-js": "^3.19.1",
15+
"path-to-regexp": "^6.2.0"
1516
}
1617
}

packages/web-performance/src/metrics/getCCP.ts

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { proxyFetch, proxyXhr } from '../lib/proxyHandler'
88
import getFirstVisitedState from '../lib/getFirstVisitedState'
99
import metricsStore from '../lib/store'
1010
import { IReportHandler } from '../types'
11-
import { getApiPath, isIncludeArr, isEqualArr } from '../utils'
11+
import { getApiPath, isIncludeArr, isEqualArr, isExistPath } from '../utils'
1212
import getPath from '../utils/getPath'
1313
import { isPerformanceSupported } from '../utils/isSupported'
1414
import { metricsName } from '../constants'
@@ -65,7 +65,7 @@ const beforeHandler = (url, apiConfig, hashHistory, excludeRemotePath) => {
6565
const firstVisitedState = getFirstVisitedState().state
6666
if (firstVisitedState) {
6767
const remotePath = getApiPath(url)
68-
if (!excludeRemotePath?.includes(remotePath)) {
68+
if (!isExistPath(excludeRemotePath, remotePath)) {
6969
if (apiConfig && apiConfig[path]) {
7070
if (apiConfig[path].some((o) => remotePath === o)) {
7171
remoteQueue.queue.push(remotePath)
@@ -82,32 +82,33 @@ const beforeHandler = (url, apiConfig, hashHistory, excludeRemotePath) => {
8282
}
8383
}
8484

85-
const afterHandler = (url, apiConfig, store, hashHistory) => {
85+
const afterHandler = (url, apiConfig, store, hashHistory, excludeRemotePath) => {
8686
if (isPerformanceSupported()) {
8787
const path = getPath(location, hashHistory)
8888
const firstVisitedState = getFirstVisitedState().state
8989
if (firstVisitedState) {
9090
const remotePath = getApiPath(url)
91-
completeQueue.push(remotePath)
92-
if (apiConfig && apiConfig[path]) {
93-
if (isIncludeArr(remoteQueue.queue, completeQueue) && !remoteQueue.hasStoreMetrics) {
94-
console.log('api list = ', remoteQueue.queue)
95-
remoteQueue.hasStoreMetrics = true
96-
const now = performance.now()
97-
if (now < getFirstHiddenTime().timeStamp) {
98-
storeMetrics(metricsName.ACT, now, store)
99-
computeCCPAndRL(store)
91+
if (!isExistPath(excludeRemotePath, remotePath)) {
92+
completeQueue.push(remotePath)
93+
if (apiConfig && apiConfig[path]) {
94+
if (isIncludeArr(remoteQueue.queue, completeQueue) && !remoteQueue.hasStoreMetrics) {
95+
console.log('api list = ', remoteQueue.queue)
96+
remoteQueue.hasStoreMetrics = true
97+
const now = performance.now()
98+
if (now < getFirstHiddenTime().timeStamp) {
99+
storeMetrics(metricsName.ACT, now, store)
100+
computeCCPAndRL(store)
101+
}
100102
}
101-
}
102-
} else {
103-
console.log(remoteQueue.queue, completeQueue)
104-
if (isIncludeArr(remoteQueue.queue, completeQueue) && !remoteQueue.hasStoreMetrics && isDone) {
105-
console.log('api list = ', remoteQueue.queue)
106-
remoteQueue.hasStoreMetrics = true
107-
const now = performance.now()
108-
if (now < getFirstHiddenTime().timeStamp) {
109-
storeMetrics(metricsName.ACT, now, store)
110-
computeCCPAndRL(store)
103+
} else {
104+
if (isIncludeArr(remoteQueue.queue, completeQueue) && !remoteQueue.hasStoreMetrics && isDone) {
105+
console.log('api list = ', remoteQueue.queue)
106+
remoteQueue.hasStoreMetrics = true
107+
const now = performance.now()
108+
if (now < getFirstHiddenTime().timeStamp) {
109+
storeMetrics(metricsName.ACT, now, store)
110+
computeCCPAndRL(store)
111+
}
111112
}
112113
}
113114
}
@@ -152,6 +153,16 @@ const maxWaitTime4Report = (cb: () => void, maxWaitCCPDuration) => {
152153
setTimeout(cb, maxWaitCCPDuration)
153154
}
154155

156+
/**
157+
* @param {metricsStore} store
158+
* @param {Function} report
159+
* @param {boolean} isCustomEvent
160+
* @param { { [prop: string]: Array<string> } } apiConfig
161+
* @param { boolean} hashHistory
162+
* @param { Array<string>} excludeRemotePath
163+
* @param { number } maxWaitCCPDuration
164+
* @param { boolean } immediately
165+
* */
155166
export const initCCP = (
156167
store: metricsStore,
157168
report: IReportHandler,
@@ -193,10 +204,10 @@ export const initCCP = (
193204

194205
proxyXhr(
195206
(url) => beforeHandler(url, apiConfig, hashHistory, excludeRemotePath),
196-
(url) => afterHandler(url, apiConfig, store, hashHistory)
207+
(url) => afterHandler(url, apiConfig, store, hashHistory, excludeRemotePath)
197208
)
198209
proxyFetch(
199210
(url) => beforeHandler(url, apiConfig, hashHistory, excludeRemotePath),
200-
(url) => afterHandler(url, apiConfig, store, hashHistory)
211+
(url) => afterHandler(url, apiConfig, store, hashHistory, excludeRemotePath)
201212
)
202213
}

packages/web-performance/src/utils/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { pathToRegexp } from 'path-to-regexp'
2+
13
export const roundByFour = (num: number, digits = 4) => {
24
try {
35
return parseFloat(num.toFixed(digits))
@@ -86,3 +88,15 @@ export const getApiPath = (url: string): string => {
8688
}
8789
return ''
8890
}
91+
92+
export const isExistPath = (paths: Array<string>, target: string) => {
93+
const regArr = paths.map((path) => pathToRegexp(path))
94+
95+
for (let i = 0; i < regArr.length; i++) {
96+
if (regArr[i].exec(target)) {
97+
return true
98+
}
99+
}
100+
101+
return false
102+
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4885,6 +4885,11 @@ [email protected]:
48854885
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c"
48864886
integrity sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=
48874887

4888+
path-to-regexp@^6.2.0:
4889+
version "6.2.0"
4890+
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-6.2.0.tgz#f7b3803336104c346889adece614669230645f38"
4891+
integrity sha512-f66KywYG6+43afgE/8j/GoiNyygk/bnoCbps++3ErRKsIYkGGupyv07R2Ok5m9i67Iqc+T2g1eAUGUPzWhYTyg==
4892+
48884893
path-type@^4.0.0:
48894894
version "4.0.0"
48904895
resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b"

0 commit comments

Comments
 (0)