Class: CycleTimeConfig

Inherits:
Object
  • Object
show all
Includes:
SelfOrIssueDispatcher
Defined in:
lib/jirametrics/cycletime_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SelfOrIssueDispatcher

#method_missing, #respond_to_missing?

Constructor Details

#initialize(parent_config:, label:, block:, settings:, file_system: nil, today: Date.today) ⇒ CycleTimeConfig

Returns a new instance of CycleTimeConfig.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jirametrics/cycletime_config.rb', line 11

def initialize parent_config:, label:, block:, settings:, file_system: nil, today: Date.today

  @parent_config = parent_config
  @label = label
  @today = today
  @settings = settings
  @cache_cycletime_calculations = settings['cache_cycletime_calculations']

  # If we hit something deprecated and this is nil then we'll blow up. Although it's ugly, this
  # may make it easier to find problems in the test code ;-)
  @file_system = file_system
  instance_eval(&block) unless block.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SelfOrIssueDispatcher

Instance Attribute Details

#file_systemObject (readonly)

Returns the value of attribute file_system.



9
10
11
# File 'lib/jirametrics/cycletime_config.rb', line 9

def file_system
  @file_system
end

#labelObject (readonly)

Returns the value of attribute label.



9
10
11
# File 'lib/jirametrics/cycletime_config.rb', line 9

def label
  @label
end

#parent_configObject (readonly)

Returns the value of attribute parent_config.



9
10
11
# File 'lib/jirametrics/cycletime_config.rb', line 9

def parent_config
  @parent_config
end

#settingsObject (readonly)

Returns the value of attribute settings.



9
10
11
# File 'lib/jirametrics/cycletime_config.rb', line 9

def settings
  @settings
end

Instance Method Details

#age(issue, today: nil) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/jirametrics/cycletime_config.rb', line 122

def age issue, today: nil
  start = started_stopped_times(issue).first
  stop = today || @today || Date.today
  return nil if start.nil? || stop.nil?

  (stop.to_date - start.to_date).to_i + 1
end

#cycletime(issue) ⇒ Object



115
116
117
118
119
120
# File 'lib/jirametrics/cycletime_config.rb', line 115

def cycletime issue
  start, stop = started_stopped_times(issue)
  return nil if start.nil? || stop.nil?

  (stop.to_date - start.to_date).to_i + 1
end

#done?(issue) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/jirametrics/cycletime_config.rb', line 40

def done? issue
  started_stopped_times(issue).last
end

#fabricate_change_item(time) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/jirametrics/cycletime_config.rb', line 54

def fabricate_change_item time
  @file_system.deprecated(
    date: '2024-12-16', message: "This method should now return a ChangeItem not a #{time.class}", depth: 4
  )
  raw = {
    'field' => 'Fabricated change',
    'to' => '0',
    'toString' => '',
    'from' => '0',
    'fromString' => ''
  }
  ChangeItem.new raw: raw, time: time, artificial: true, author_raw: nil
end

#flush_cacheObject



106
107
108
# File 'lib/jirametrics/cycletime_config.rb', line 106

def flush_cache
  @cache = nil
end

#in_progress?(issue) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/jirametrics/cycletime_config.rb', line 35

def in_progress? issue
  started_time, stopped_time = started_stopped_times(issue)
  started_time && stopped_time.nil?
end

#possible_statusesObject



130
131
132
133
134
135
136
137
138
# File 'lib/jirametrics/cycletime_config.rb', line 130

def possible_statuses
  if parent_config.is_a? BoardConfig
    project_config = parent_config.project_config
  else
    # TODO: This will go away when cycletimes are no longer supported inside html_reports
    project_config = parent_config.file_config.project_config
  end
  project_config.possible_statuses
end

#start_at(block = nil) ⇒ Object



25
26
27
28
# File 'lib/jirametrics/cycletime_config.rb', line 25

def start_at block = nil
  @start_at = block unless block.nil?
  @start_at
end

#started_stopped_changes(issue) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/jirametrics/cycletime_config.rb', line 68

def started_stopped_changes issue
  cache_key = "#{issue.key}:#{issue.board.id}"
  last_result = (@cache ||= {})[cache_key]
  return *last_result if last_result && @cache_cycletime_calculations

  started = @start_at.call(issue)
  stopped = @stop_at.call(issue)

  # Obscure edge case where some of the start_at and stop_at blocks might return false in place of nil.
  # If they are false then explicitly make them nil.
  started ||= nil
  stopped ||= nil

  # These are only here for backwards compatibility. Hopefully nobody will ever need them.
  started = fabricate_change_item(started) if !started.nil? && !started.is_a?(ChangeItem)
  stopped = fabricate_change_item(stopped) if !stopped.nil? && !stopped.is_a?(ChangeItem)

  # In the case where started and stopped are exactly the same time, we pretend that
  # it just stopped and never started. This allows us to have logic like 'in or right of'
  # for the start and not have it conflict.
  started = nil if started&.time == stopped&.time

  result = [started, stopped]
  if last_result && result != last_result
    @file_system.error(
      "Calculation mismatch; this could break caching. #{issue.inspect} new=#{result.inspect}, " \
        "previous=#{last_result.inspect}"
    )
  end
  @cache[cache_key] = result
  result
end

#started_stopped_dates(issue) ⇒ Object



110
111
112
113
# File 'lib/jirametrics/cycletime_config.rb', line 110

def started_stopped_dates issue
  started_time, stopped_time = started_stopped_times(issue)
  [started_time&.to_date, stopped_time&.to_date]
end

#started_stopped_times(issue) ⇒ Object



101
102
103
104
# File 'lib/jirametrics/cycletime_config.rb', line 101

def started_stopped_times issue
  started, stopped = started_stopped_changes(issue)
  [started&.time, stopped&.time]
end

#started_time(issue) ⇒ Object



44
45
46
47
# File 'lib/jirametrics/cycletime_config.rb', line 44

def started_time issue
  @file_system.deprecated date: '2024-10-16', message: 'Use started_stopped_times() instead'
  started_stopped_times(issue).first
end

#stop_at(block = nil) ⇒ Object



30
31
32
33
# File 'lib/jirametrics/cycletime_config.rb', line 30

def stop_at block = nil
  @stop_at = block unless block.nil?
  @stop_at
end

#stopped_time(issue) ⇒ Object



49
50
51
52
# File 'lib/jirametrics/cycletime_config.rb', line 49

def stopped_time issue
  @file_system.deprecated date: '2024-10-16', message: 'Use started_stopped_times() instead'
  started_stopped_times(issue).last
end