Class: Gitlab::IssuablesCountForState

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/issuables_count_for_state.rb

Overview

Class for counting and caching the number of issuables per state.

Constant Summary collapse

CACHE_KEY =

The name of the Gitlab::SafeRequestStore cache key.

:issuables_count_for_state
CACHE_EXPIRES_IN =

The expiration time for the Rails cache.

1.hour
THRESHOLD =
1000
STATES =

The state values that can be safely casted to a Symbol.

%w[opened closed merged all].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(finder, project = nil, fast_fail: false, store_in_redis_cache: false) ⇒ IssuablesCountForState

finder - The finder class to use for retrieving the issuables. fast_fail - restrict counting to a shorter period, degrading gracefully on failure



24
25
26
27
28
29
30
# File 'lib/gitlab/issuables_count_for_state.rb', line 24

def initialize(finder, project = nil, fast_fail: false, store_in_redis_cache: false)
  @finder = finder
  @project = project
  @fast_fail = fast_fail
  @cache = Gitlab::SafeRequestStore[CACHE_KEY] ||= initialize_cache
  @store_in_redis_cache = store_in_redis_cache
end

Instance Attribute Details

#finderObject (readonly)

Returns the value of attribute finder.



15
16
17
# File 'lib/gitlab/issuables_count_for_state.rb', line 15

def finder
  @finder
end

#projectObject (readonly)

Returns the value of attribute project.



15
16
17
# File 'lib/gitlab/issuables_count_for_state.rb', line 15

def project
  @project
end

Class Method Details

.declarative_policy_classObject



17
18
19
# File 'lib/gitlab/issuables_count_for_state.rb', line 17

def self.declarative_policy_class
  'IssuablePolicy'
end

Instance Method Details

#[](state) ⇒ Object

Returns the count for the given state.

state - The name of the state as either a String or a Symbol.

Returns an Integer.



50
51
52
53
54
# File 'lib/gitlab/issuables_count_for_state.rb', line 50

def [](state)
  state = state.to_sym if cast_state_to_symbol?(state)

  cache_for_finder[state] || 0
end

#fast_fail?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/gitlab/issuables_count_for_state.rb', line 36

def fast_fail?
  !!@fast_fail
end

#for_state_or_opened(state = nil) ⇒ Object



32
33
34
# File 'lib/gitlab/issuables_count_for_state.rb', line 32

def for_state_or_opened(state = nil)
  self[state || :opened]
end