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
2b910c40
Commit
2b910c40
authored
Mar 28, 2021
by
Aakriti Gupta
Committed by
Kerri Miller
Mar 28, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cache count of recently created MRs for 24 hours
- move tests from calcualtor class to count service
parent
6a0444af
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
124 additions
and
23 deletions
+124
-23
ee/app/services/groups/recent_merge_requests_count_service.rb
...pp/services/groups/recent_merge_requests_count_service.rb
+35
-0
ee/lib/analytics/group_activity_calculator.rb
ee/lib/analytics/group_activity_calculator.rb
+7
-1
ee/spec/lib/analytics/group_activity_calculator_spec.rb
ee/spec/lib/analytics/group_activity_calculator_spec.rb
+5
-22
ee/spec/services/groups/recent_merge_requests_count_service_spec.rb
...rvices/groups/recent_merge_requests_count_service_spec.rb
+77
-0
No files found.
ee/app/services/groups/recent_merge_requests_count_service.rb
0 → 100644
View file @
2b910c40
# frozen_string_literal: true
# Service class for counting and caching the number of open merge requests of
# a project.
module
Groups
class
RecentMergeRequestsCountService
<
BaseCountService
VERSION
=
1
DURATION
=
90
.
days
EXPIRES_IN
=
24
.
hours
def
initialize
(
group
,
current_user
,
params
)
@group
=
group
@current_user
=
current_user
@params
=
params
end
private
def
relation_for_count
MergeRequestsFinder
.
new
(
@current_user
,
@params
).
execute
end
def
cache_key
(
key
=
nil
)
[
'groups'
,
'recent_merge_requests_count_service'
,
VERSION
,
@group
.
id
,
@current_user
.
id
,
cache_key_name
]
end
def
cache_key_name
'recent_merge_requests_count'
end
def
cache_options
super
.
merge
(
expires_in:
EXPIRES_IN
)
end
end
end
ee/lib/analytics/group_activity_calculator.rb
View file @
2b910c40
...
...
@@ -15,8 +15,14 @@ module Analytics
end
def
merge_requests_count
# Clear db load balancing session so that primary sticking
# does not apply in case a write has happened recently.
# We want to make sure the load of the following query
# does not land on the primary db.
::
Gitlab
::
Database
::
LoadBalancing
::
Session
.
clear_session
@merge_requests_count
||=
MergeRequestsFinder
.
new
(
@current_user
,
params
).
execute
.
count
Groups
::
RecentMergeRequestsCountService
.
new
(
@group
,
@current_user
,
params
)
.
count
end
def
new_members_count
...
...
ee/spec/lib/analytics/group_activity_calculator_spec.rb
View file @
2b910c40
...
...
@@ -39,29 +39,12 @@ RSpec.describe Analytics::GroupActivityCalculator do
end
context
'with merge requests'
do
let!
(
:recent_mr
)
do
create
(
:merge_request
,
source_project:
project
,
source_branch:
"my-personal-branch-1"
)
it
'calls RecentMergeRequestsCountService#count'
do
expect_next_instance_of
(
Groups
::
RecentMergeRequestsCountService
)
do
|
count_service
|
expect
(
count_service
).
to
receive
(
:count
)
end
let!
(
:old_mr
)
do
create
(
:merge_request
,
source_project:
project
,
source_branch:
"my-personal-branch-2"
,
created_at:
100
.
days
.
ago
)
end
it
'only returns the count of recent MRs'
do
expect
(
subject
.
merge_requests_count
).
to
eq
1
end
context
'when user does not have access to some MRs'
do
let
(
:secret_mr
)
{
create
(
:merge_request
,
source_project:
secret_project
)
}
it
'does not include those MRs'
do
expect
{
secret_mr
}.
not_to
change
{
subject
.
merge_requests_count
}
end
subject
.
merge_requests_count
end
end
...
...
ee/spec/services/groups/recent_merge_requests_count_service_spec.rb
0 → 100644
View file @
2b910c40
# frozen_string_literal: true
require
'spec_helper'
RSpec
.
describe
Groups
::
RecentMergeRequestsCountService
,
:use_clean_rails_memory_store_caching
do
let_it_be
(
:group
)
{
create
(
:group
)
}
let_it_be
(
:user
)
{
create
(
:user
)
}
let_it_be
(
:project
)
{
create
(
:project
,
group:
group
)
}
let_it_be
(
:secret_subgroup
)
{
create
(
:group
,
parent:
group
)
}
let_it_be
(
:secret_project
)
{
create
(
:project
,
group:
secret_subgroup
)
}
let_it_be
(
:secret_user
)
{
create
(
:user
)
}
let_it_be
(
:params
)
do
{
group_id:
group
.
id
,
state:
'all'
,
created_after:
90
.
days
.
ago
,
include_subgroups:
true
,
attempt_group_search_optimizations:
true
,
attempt_project_search_optimizations:
true
}
end
subject
{
described_class
.
new
(
group
,
user
,
params
)
}
before_all
do
project
.
add_developer
(
user
)
project
.
add_developer
(
secret_user
)
secret_project
.
add_developer
(
secret_user
)
end
describe
'#relation_for_count'
do
before
do
allow
(
MergeRequestsFinder
).
to
receive
(
:new
).
and_call_original
end
it
'uses the MergeRequestsFinder to scope issues'
do
expect
(
MergeRequestsFinder
).
to
receive
(
:new
).
with
(
user
,
params
)
subject
.
count
end
end
describe
'#count'
do
it
'only returns the count of recent MRs'
do
create
(
:merge_request
,
source_project:
project
,
source_branch:
"my-personal-branch-1"
)
create
(
:merge_request
,
source_project:
project
,
source_branch:
"my-personal-branch-2"
,
created_at:
100
.
days
.
ago
)
expect
(
subject
.
count
).
to
eq
1
end
context
'when user does not have access to some MRs'
do
it
'caches per-user so users will see the correct value'
do
regular_user_count
=
subject
.
count
expect
do
create
(
:merge_request
,
source_project:
secret_project
)
# Delete both caches to ensure that the new count gets the updated values
described_class
.
new
(
group
,
secret_user
,
params
).
delete_cache
subject
.
delete_cache
end
.
to
change
{
described_class
.
new
(
group
,
secret_user
,
params
).
count
}
expect
(
subject
.
count
).
to
eq
(
regular_user_count
)
end
it
'does not include those MRs'
do
expect
do
create
(
:merge_request
,
source_project:
secret_project
)
subject
.
delete_cache
end
.
not_to
change
{
subject
.
count
}
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