This class implements a mechanism that can be used to limit certain events within a certain time period. It supports an upper and a lower limit.
To create a new Limit object, the Interval interval
and the period
duration (period
in seconds) must be specified. This creates a
counter for each period within the overall interval. value
is
the value of the limit. upper
specifies whether the limit is
an upper or lower limit. The limit can also be restricted to certain a Resource specified by resource
.
# File lib/taskjuggler/Limits.rb, line 36 def initialize(name, interval, period, value, upper, resource) @name = name @interval = interval @period = period @value = value @upper = upper @resource = resource # To avoid multiple resets of untouched scoreboards we keep this dirty # flag. It's set whenever a counter is increased. @dirty = true reset end
Returns a deep copy of the class instance.
# File lib/taskjuggler/Limits.rb, line 51 def copy Limit.new(@name, @interval, @period, @value, @upper, @resource) end
Decrease the counter if the index matches the @interval. The relationship between @resource and resource is described below. @r \ r nil y nil inc inc
x - if x==y inc else -
# File lib/taskjuggler/Limits.rb, line 90 def dec(index, resource) if @interval.contains?(index) && (@resource.nil? || @resource == resource) # The condition is met, decrement the counter for the interval. @dirty = true @scoreboard[idxToSbIdx(index)] -= 1 end end
Increase the counter if the index matches the @interval. The relationship between @resource and resource is described below. @r \ r nil y nil inc inc
x - if x==y inc else -
# File lib/taskjuggler/Limits.rb, line 76 def inc(index, resource) if @interval.contains?(index) && (@resource.nil? || @resource == resource) # The condition is met, increment the counter for the interval. @dirty = true @scoreboard[idxToSbIdx(index)] += 1 end end
Returns true if the counter for the time slot specified by
index
or all counters are within the limit. If
upper
is true, only upper limits are checked. If not, only
lower limits are checked. The dependency between resource and
@resource is described in the matrix below: @r \ r nil y nil
test true
x true if x==y test else true
# File lib/taskjuggler/Limits.rb, line 107 def ok?(index, upper, resource) # if @upper does not match or the provided resource does not match, # we can ignore this limit. return true if @upper != upper || (@resource && @resource != resource) if index.nil? # No index given. We need to check all counters. @scoreboard.each do |i| return false if @upper ? i >= @value : i < @value end return true else # If the index is outside the interval we don't have to check # anything. Everything is ok. return true if !@interval.contains?(index) sbVal = @scoreboard[idxToSbIdx(index)] return @upper ? (sbVal < @value) : (sbVal >= @value) end end
This function can be used to reset the counter for a specific period
specified by index
or to reset all counters.
# File lib/taskjuggler/Limits.rb, line 57 def reset(index = nil) return unless @dirty if index.nil? @scoreboard = Scoreboard.new(@interval.startDate, @interval.endDate, @period, 0) else return unless @interval.contains?(index) # The scoreboard may be just a subset of the @interval period. @scoreboard[idxToSbIdx(index)] = 0 end @dirty = false end
The project scoreboard and the Limit scoreboard differ from each other. The Limit scoreboard may only be a subset of the project scoreboard interval. And the Limit scoreboard has a larger slot duration that depends on what kind of limit it is (daily, weekly, etc.). Therefor, we have to use this method to translate project scoreboard indexes to Limit scoreboard indexes.
# File lib/taskjuggler/Limits.rb, line 136 def idxToSbIdx(index) (index - @interval.start) * @interval.slotDuration / @period end