Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
interface
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
LuckySwap
interface
Commits
4ba0d8ff
Unverified
Commit
4ba0d8ff
authored
Sep 07, 2023
by
eddie
Committed by
GitHub
Sep 07, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test: unit tests for swapFlowLoggers (#7257)
parent
73064231
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
91 additions
and
11 deletions
+91
-11
SwapEventTimestampTracker.ts
src/tracing/SwapEventTimestampTracker.ts
+2
-0
swapFlowLoggers.test.ts
src/tracing/swapFlowLoggers.test.ts
+80
-0
swapFlowLoggers.ts
src/tracing/swapFlowLoggers.ts
+9
-11
No files found.
src/tracing/SwapEventTimestampTracker.ts
View file @
4ba0d8ff
...
...
@@ -57,3 +57,5 @@ export class SwapEventTimestampTracker {
return
endTime
-
startTime
}
}
export
const
timestampTracker
=
SwapEventTimestampTracker
.
getInstance
()
src/tracing/swapFlowLoggers.test.ts
0 → 100644
View file @
4ba0d8ff
import
{
SwapEventName
}
from
'
@uniswap/analytics-events
'
import
{
sendAnalyticsEvent
}
from
'
analytics
'
jest
.
mock
(
'
analytics
'
,
()
=>
({
sendAnalyticsEvent
:
jest
.
fn
(),
}))
jest
.
mock
(
'
./SwapEventTimestampTracker
'
,
()
=>
({
SwapEventType
:
{
FIRST_SWAP_ACTION
:
'
FIRST_SWAP_ACTION
'
,
FIRST_QUOTE_FETCH_STARTED
:
'
FIRST_QUOTE_FETCH_STARTED
'
,
FIRST_SWAP_SIGNATURE_REQUESTED
:
'
FIRST_SWAP_SIGNATURE_REQUESTED
'
,
FIRST_SWAP_SIGNATURE_COMPLETED
:
'
FIRST_SWAP_SIGNATURE_COMPLETED
'
,
FIRST_SWAP_SUCCESS
:
'
FIRST_SWAP_SUCCESS
'
,
},
timestampTracker
:
{
hasTimestamp
:
()
=>
false
,
setElapsedTime
:
()
=>
100
,
getElapsedTime
:
()
=>
100
,
},
}))
import
{
INTERNAL_ROUTER_PREFERENCE_PRICE
,
RouterPreference
}
from
'
state/routing/types
'
import
{
logSwapQuoteRequest
,
logSwapSuccess
,
maybeLogFirstSwapAction
}
from
'
./swapFlowLoggers
'
describe
(
'
swapFlowLoggers
'
,
()
=>
{
beforeEach
(()
=>
{
jest
.
clearAllMocks
()
})
it
(
'
logSwapSuccess calls sendAnalyticsEvent with correct parameters
'
,
()
=>
{
const
mockHash
=
'
mockHash
'
const
mockChainId
=
1
const
mockAnalyticsContext
=
{
page
:
'
mockContext
'
}
logSwapSuccess
(
mockHash
,
mockChainId
,
mockAnalyticsContext
)
expect
(
sendAnalyticsEvent
).
toHaveBeenCalledWith
(
SwapEventName
.
SWAP_TRANSACTION_COMPLETED
,
{
time_to_swap
:
100
,
time_to_swap_since_first_input
:
100
,
hash
:
mockHash
,
chainId
:
mockChainId
,
...
mockAnalyticsContext
,
})
})
it
(
'
maybeLogFirstSwapAction calls sendAnalyticsEvent with correct parameters
'
,
()
=>
{
const
mockAnalyticsContext
=
{
page
:
'
mockContext
'
}
maybeLogFirstSwapAction
(
mockAnalyticsContext
)
expect
(
sendAnalyticsEvent
).
toHaveBeenCalledWith
(
SwapEventName
.
SWAP_FIRST_ACTION
,
{
time_to_first_swap_action
:
100
,
...
mockAnalyticsContext
,
})
})
it
(
'
logSwapQuoteRequest calls sendAnalyticsEvent with correct parameters
'
,
()
=>
{
const
mockChainId
=
1
logSwapQuoteRequest
(
mockChainId
,
RouterPreference
.
X
)
expect
(
sendAnalyticsEvent
).
toHaveBeenCalledWith
(
SwapEventName
.
SWAP_QUOTE_FETCH
,
{
chainId
:
mockChainId
,
time_to_first_quote_request
:
100
,
time_to_first_quote_request_since_first_input
:
100
,
})
})
it
(
'
logSwapQuoteRequest excludes perf metrics for price quotes
'
,
()
=>
{
const
mockChainId
=
1
logSwapQuoteRequest
(
mockChainId
,
INTERNAL_ROUTER_PREFERENCE_PRICE
)
expect
(
sendAnalyticsEvent
).
toHaveBeenCalledWith
(
SwapEventName
.
SWAP_QUOTE_FETCH
,
{
chainId
:
mockChainId
,
})
})
})
src/tracing/swapFlowLoggers.ts
View file @
4ba0d8ff
...
...
@@ -3,20 +3,18 @@ import { ITraceContext } from 'analytics'
import
{
sendAnalyticsEvent
}
from
'
analytics
'
import
{
INTERNAL_ROUTER_PREFERENCE_PRICE
,
RouterPreference
}
from
'
state/routing/types
'
import
{
SwapEventTimestampTracker
,
SwapEventType
}
from
'
./SwapEventTimestampTracker
'
const
tracker
=
SwapEventTimestampTracker
.
getInstance
()
import
{
SwapEventType
,
timestampTracker
}
from
'
./SwapEventTimestampTracker
'
export
function
logSwapSuccess
(
hash
:
string
,
chainId
:
number
,
analyticsContext
:
ITraceContext
)
{
const
hasSetSwapSuccess
=
tracker
.
hasTimestamp
(
SwapEventType
.
FIRST_SWAP_SUCCESS
)
const
elapsedTime
=
tracker
.
setElapsedTime
(
SwapEventType
.
FIRST_SWAP_SUCCESS
)
const
hasSetSwapSuccess
=
t
imestampT
racker
.
hasTimestamp
(
SwapEventType
.
FIRST_SWAP_SUCCESS
)
const
elapsedTime
=
t
imestampT
racker
.
setElapsedTime
(
SwapEventType
.
FIRST_SWAP_SUCCESS
)
sendAnalyticsEvent
(
SwapEventName
.
SWAP_TRANSACTION_COMPLETED
,
{
// We only log the time-to-swap metric for the first swap of a session,
// so if it was previously set we log undefined here.
time_to_swap
:
hasSetSwapSuccess
?
undefined
:
elapsedTime
,
time_to_swap_since_first_input
:
hasSetSwapSuccess
?
undefined
:
tracker
.
getElapsedTime
(
SwapEventType
.
FIRST_SWAP_SUCCESS
,
SwapEventType
.
FIRST_SWAP_ACTION
),
:
t
imestampT
racker
.
getElapsedTime
(
SwapEventType
.
FIRST_SWAP_SUCCESS
,
SwapEventType
.
FIRST_SWAP_ACTION
),
hash
,
chainId
,
...
analyticsContext
,
...
...
@@ -25,8 +23,8 @@ export function logSwapSuccess(hash: string, chainId: number, analyticsContext:
// We only log the time-to-first-swap-input metric for the first swap input of a session.
export
function
maybeLogFirstSwapAction
(
analyticsContext
:
ITraceContext
)
{
if
(
!
tracker
.
hasTimestamp
(
SwapEventType
.
FIRST_SWAP_ACTION
))
{
const
elapsedTime
=
tracker
.
setElapsedTime
(
SwapEventType
.
FIRST_SWAP_ACTION
)
if
(
!
t
imestampT
racker
.
hasTimestamp
(
SwapEventType
.
FIRST_SWAP_ACTION
))
{
const
elapsedTime
=
t
imestampT
racker
.
setElapsedTime
(
SwapEventType
.
FIRST_SWAP_ACTION
)
sendAnalyticsEvent
(
SwapEventName
.
SWAP_FIRST_ACTION
,
{
time_to_first_swap_action
:
elapsedTime
,
...
analyticsContext
,
...
...
@@ -40,14 +38,14 @@ export function logSwapQuoteRequest(
)
{
let
performanceMetrics
=
{}
if
(
routerPreference
!==
INTERNAL_ROUTER_PREFERENCE_PRICE
)
{
const
hasSetSwapQuote
=
tracker
.
hasTimestamp
(
SwapEventType
.
FIRST_QUOTE_FETCH_STARTED
)
const
elapsedTime
=
tracker
.
setElapsedTime
(
SwapEventType
.
FIRST_QUOTE_FETCH_STARTED
)
const
hasSetSwapQuote
=
t
imestampT
racker
.
hasTimestamp
(
SwapEventType
.
FIRST_QUOTE_FETCH_STARTED
)
const
elapsedTime
=
t
imestampT
racker
.
setElapsedTime
(
SwapEventType
.
FIRST_QUOTE_FETCH_STARTED
)
performanceMetrics
=
{
// We only log the time_to_first_quote_request metric for the first quote request of a session.
time_to_first_quote_request
:
hasSetSwapQuote
?
undefined
:
elapsedTime
,
time_to_first_quote_request_since_first_input
:
hasSetSwapQuote
?
undefined
:
tracker
.
getElapsedTime
(
SwapEventType
.
FIRST_QUOTE_FETCH_STARTED
,
SwapEventType
.
FIRST_SWAP_ACTION
),
:
t
imestampT
racker
.
getElapsedTime
(
SwapEventType
.
FIRST_QUOTE_FETCH_STARTED
,
SwapEventType
.
FIRST_SWAP_ACTION
),
}
}
sendAnalyticsEvent
(
SwapEventName
.
SWAP_QUOTE_FETCH
,
{
...
...
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