Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
bfb9e88a
Commit
bfb9e88a
authored
Mar 17, 2021
by
Rajendra Kadam
Committed by
Stan Hu
Mar 17, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a block argument in track_redis_hll_event
Add specs for the change
parent
83b2d4b1
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
15 deletions
+35
-15
app/controllers/concerns/redis_tracking.rb
app/controllers/concerns/redis_tracking.rb
+10
-5
doc/development/usage_ping/index.md
doc/development/usage_ping/index.md
+2
-1
spec/controllers/concerns/redis_tracking_spec.rb
spec/controllers/concerns/redis_tracking_spec.rb
+23
-9
No files found.
app/controllers/concerns/redis_tracking.rb
View file @
bfb9e88a
...
@@ -10,26 +10,31 @@
...
@@ -10,26 +10,31 @@
# track_redis_hll_event :index, :show, name: 'i_analytics_dev_ops_score'
# track_redis_hll_event :index, :show, name: 'i_analytics_dev_ops_score'
#
#
# You can also pass custom conditions using `if:`, using the same format as with Rails callbacks.
# You can also pass custom conditions using `if:`, using the same format as with Rails callbacks.
# You can also pass an optional block that calculates and returns a custom id to track.
module
RedisTracking
module
RedisTracking
extend
ActiveSupport
::
Concern
extend
ActiveSupport
::
Concern
class_methods
do
class_methods
do
def
track_redis_hll_event
(
*
controller_actions
,
name
:,
if:
nil
)
def
track_redis_hll_event
(
*
controller_actions
,
name
:,
if:
nil
,
&
block
)
custom_conditions
=
Array
.
wrap
(
binding
.
local_variable_get
(
'if'
))
custom_conditions
=
Array
.
wrap
(
binding
.
local_variable_get
(
'if'
))
conditions
=
[
:trackable_request?
,
*
custom_conditions
]
conditions
=
[
:trackable_request?
,
*
custom_conditions
]
after_action
only:
controller_actions
,
if:
conditions
do
after_action
only:
controller_actions
,
if:
conditions
do
track_unique_redis_hll_event
(
name
)
track_unique_redis_hll_event
(
name
,
&
block
)
end
end
end
end
end
end
private
private
def
track_unique_redis_hll_event
(
event_name
)
def
track_unique_redis_hll_event
(
event_name
,
&
block
)
return
unless
visitor_id
custom_id
=
block_given?
?
yield
(
self
)
:
nil
Gitlab
::
UsageDataCounters
::
HLLRedisCounter
.
track_event
(
event_name
,
values:
visitor_id
)
unique_id
=
custom_id
||
visitor_id
return
unless
unique_id
Gitlab
::
UsageDataCounters
::
HLLRedisCounter
.
track_event
(
event_name
,
values:
unique_id
)
end
end
def
trackable_request?
def
trackable_request?
...
...
doc/development/usage_ping/index.md
View file @
bfb9e88a
...
@@ -502,13 +502,14 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF
...
@@ -502,13 +502,14 @@ Implemented using Redis methods [PFADD](https://redis.io/commands/pfadd) and [PF
Use one of the following methods to track events:
Use one of the following methods to track events:
1.
Track event in controller using
`RedisTracking`
module with
`track_redis_hll_event(*controller_actions, name:, if: nil)`
.
1.
Track event in controller using
`RedisTracking`
module with
`track_redis_hll_event(*controller_actions, name:, if: nil
, &block
)`
.
Arguments:
Arguments:
-
`controller_actions`
: controller actions we want to track.
-
`controller_actions`
: controller actions we want to track.
-
`name`
: event name.
-
`name`
: event name.
-
`if`
: optional custom conditions, using the same format as with Rails callbacks.
-
`if`
: optional custom conditions, using the same format as with Rails callbacks.
-
`&block`
: optional block that computes and returns the
`custom_id`
that we want to track. This will override the
`visitor_id`
.
Example usage:
Example usage:
...
...
spec/controllers/concerns/redis_tracking_spec.rb
View file @
bfb9e88a
...
@@ -9,8 +9,8 @@ RSpec.describe RedisTracking do
...
@@ -9,8 +9,8 @@ RSpec.describe RedisTracking do
include
RedisTracking
include
RedisTracking
skip_before_action
:authenticate_user!
,
only: :show
skip_before_action
:authenticate_user!
,
only: :show
track_redis_hll_event
:index
,
:show
,
name:
'g_compliance_approval_rules'
,
track_redis_hll_event
(
:index
,
:show
,
name:
'g_compliance_approval_rules'
,
if:
[
:custom_condition_one?
,
:custom_condition_two?
]
if:
[
:custom_condition_one?
,
:custom_condition_two?
]
)
{
|
controller
|
controller
.
get_custom_id
}
def
index
def
index
render
html:
'index'
render
html:
'index'
...
@@ -24,6 +24,10 @@ RSpec.describe RedisTracking do
...
@@ -24,6 +24,10 @@ RSpec.describe RedisTracking do
render
html:
'show'
render
html:
'show'
end
end
def
get_custom_id
'some_custom_id'
end
private
private
def
custom_condition_one?
def
custom_condition_one?
...
@@ -92,19 +96,15 @@ RSpec.describe RedisTracking do
...
@@ -92,19 +96,15 @@ RSpec.describe RedisTracking do
end
end
end
end
context
'when user is not logged in
and there is a visitor_id
'
do
context
'when user is not logged in'
do
let
(
:visitor_id
)
{
SecureRandom
.
uuid
}
let
(
:visitor_id
)
{
SecureRandom
.
uuid
}
before
do
it
'tracks the event when there is a visitor id'
do
routes
.
draw
{
get
'show'
=>
'anonymous#show'
}
end
it
'tracks the event'
do
cookies
[
:visitor_id
]
=
{
value:
visitor_id
,
expires:
24
.
months
}
cookies
[
:visitor_id
]
=
{
value:
visitor_id
,
expires:
24
.
months
}
expect_tracking
expect_tracking
get
:show
get
:show
,
params:
{
id:
1
}
end
end
end
end
...
@@ -114,5 +114,19 @@ RSpec.describe RedisTracking do
...
@@ -114,5 +114,19 @@ RSpec.describe RedisTracking do
get
:index
get
:index
end
end
it
'tracks the event when there is custom id'
do
expect_tracking
get
:show
,
params:
{
id:
1
}
end
it
'does not track the event when there is no custom id'
do
expect
(
controller
).
to
receive
(
:get_custom_id
).
and_return
(
nil
)
expect_no_tracking
get
:show
,
params:
{
id:
2
}
end
end
end
end
end
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