Class: Projects::OpenIssuesCountService

Inherits:
CountService show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/services/projects/open_issues_count_service.rb

Overview

Service class for counting and caching the number of open issues of a project.

Constant Summary collapse

PUBLIC_COUNT_KEY =

Cache keys used to store issues count

'public_open_issues_count'
TOTAL_COUNT_KEY =
'total_open_issues_count'

Constants inherited from CountService

CountService::VERSION

Instance Attribute Summary

Attributes inherited from CountService

#project

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CountService

#cache_key

Methods inherited from BaseCountService

#cache_key, #cache_options, #count, #count_stored?, #delete_cache, #raw?, #uncached_count, #update_cache_for_key

Constructor Details

#initialize(project, user = nil) ⇒ OpenIssuesCountService

Returns a new instance of OpenIssuesCountService.



13
14
15
16
17
# File 'app/services/projects/open_issues_count_service.rb', line 13

def initialize(project, user = nil)
  @user = user

  super(project)
end

Class Method Details

.query(projects, public_only: true) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord



65
66
67
68
69
70
71
72
73
# File 'app/services/projects/open_issues_count_service.rb', line 65

def self.query(projects, public_only: true)
  open_issues = Issue.opened

  if public_only
    open_issues.public_only.where(project: projects)
  else
    open_issues.where(project: projects)
  end
end

Instance Method Details

#cache_key_nameObject



19
20
21
# File 'app/services/projects/open_issues_count_service.rb', line 19

def cache_key_name
  public_only? ? PUBLIC_COUNT_KEY : TOTAL_COUNT_KEY
end

#public_count_cache_keyObject



37
38
39
# File 'app/services/projects/open_issues_count_service.rb', line 37

def public_count_cache_key
  cache_key(PUBLIC_COUNT_KEY)
end

#public_only?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'app/services/projects/open_issues_count_service.rb', line 23

def public_only?
  !user_is_at_least_reporter?
end

#refresh_cache(&block) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/projects/open_issues_count_service.rb', line 46

def refresh_cache(&block)
  count_grouped_by_confidential = self.class.query(@project, public_only: false).group(:confidential).count
  public_count = count_grouped_by_confidential[false] || 0
  total_count = public_count + (count_grouped_by_confidential[true] || 0)

  update_cache_for_key(public_count_cache_key) do
    public_count
  end

  update_cache_for_key(total_count_cache_key) do
    total_count
  end
end

#relation_for_countObject



33
34
35
# File 'app/services/projects/open_issues_count_service.rb', line 33

def relation_for_count
  self.class.query(@project, public_only: public_only?)
end

#total_count_cache_keyObject



41
42
43
# File 'app/services/projects/open_issues_count_service.rb', line 41

def total_count_cache_key
  cache_key(TOTAL_COUNT_KEY)
end

#user_is_at_least_reporter?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'app/services/projects/open_issues_count_service.rb', line 27

def user_is_at_least_reporter?
  strong_memoize(:user_is_at_least_reporter) do
    @project.member?(@user, Gitlab::Access::REPORTER)
  end
end