Class: Merb::Threshold::Frequency

Inherits:
Object
  • Object
show all
Defined in:
lib/merb_threshold/frequency.rb

Overview

Note:
  • time is treated as relative from ‘now’

  • nowhere are events sorted within this class so they should be added in their proper order

  • Why? its easy to do this from the controller naturally since time is linear (right?)

    and its faster not having to worry about sorting whenever an occurrence is added
    

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(occ, int, unts = nil) ⇒ Frequency

Frequency.new(5, 30, :seconds) => “5 times per 30 seconds” Frequency.new(1, 3.minutes) => “1 time per 180 seconds”



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/merb_threshold/frequency.rb', line 18

def initialize(occ,int,unts = nil)
  @occurrence   = occ
  @interval     = int
    
  # All test are done with the period (reduced to seconds)
  # 50.minutes #=> 50.send :minutes => 3000 seconds        
  if unts
    @period       = interval.send unts
    @units = unts
  else #no units default :seconds
    @period       = interval
    cast_units
  end
  
  #If the period is zero, never permit?
  if period == 0
    @occurrence = 0
  end
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



14
15
16
# File 'lib/merb_threshold/frequency.rb', line 14

def events
  @events
end

#intervalObject (readonly)

Returns the value of attribute interval.



14
15
16
# File 'lib/merb_threshold/frequency.rb', line 14

def interval
  @interval
end

#occurrenceObject (readonly)

Returns the value of attribute occurrence.



14
15
16
# File 'lib/merb_threshold/frequency.rb', line 14

def occurrence
  @occurrence
end

#periodObject (readonly)

Returns the value of attribute period.



14
15
16
# File 'lib/merb_threshold/frequency.rb', line 14

def period
  @period
end

#unitsObject (readonly)

Returns the value of attribute units.



14
15
16
# File 'lib/merb_threshold/frequency.rb', line 14

def units
  @units
end

Instance Method Details

#add(evt) ⇒ Array[Fixnum]

adds a single event, this always adds and does not perform a permit? first

Parameters:

  • evt (Fixnum)

    Timestamp

Returns:

  • (Array[Fixnum])


107
108
109
110
111
# File 'lib/merb_threshold/frequency.rb', line 107

def add(evt)
  @events ||= []
  @events << evt
  @events
end

#cast_unitsObject

Casts frequency units when not specified



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/merb_threshold/frequency.rb', line 130

def cast_units
  case @interval
  when 0..120     # :seconds
    @units = (@interval != 1 ? :seconds : :second)
  when 121..3600  # :minutes
    @interval = @interval / 60.0
    @units = (@interval != 1 ? :minutes : :minute)
  else            # :hours
    @interval = @interval / 3600.0
    @units = (@interval != 1 ? :hours : :hour)
  end
end

#current_eventsArray[Fixnum]

Get list of events for current period

Returns:

  • (Array[Fixnum])


156
157
158
159
160
161
162
# File 'lib/merb_threshold/frequency.rb', line 156

def current_events
  if @events
    @events.find_all{ |evt| evt >= (Time.now.to_i - period) }
  else
    @events ||= []
  end
end

#flushObject

flushes the events array



98
99
100
# File 'lib/merb_threshold/frequency.rb', line 98

def flush
  @events = []
end

#load(evts) ⇒ Object

Note:

Should be loaded presorted (threshold should always stored sorted min=>high) an array is used rather than a set because duplicates may be needed (concurrent access times)

Loads a history of events

Parameters:

  • evts (~Array[Fixnum])

    list of timestamps



78
79
80
81
# File 'lib/merb_threshold/frequency.rb', line 78

def load(evts)
  @events ||= []
  @events += evts
end

#load!(evts) ⇒ Object

clears current events and sets

Parameters:

  • evts (~Array[Fixnum])

    list of timestamps

See Also:



91
92
93
# File 'lib/merb_threshold/frequency.rb', line 91

def load!(evts)
  @events = evts
end

#permit?Boolean

tests if the frequency would permit the additional occurence or if it

would exceed the frequency.  Histories can be loaded with frequency#load

Returns:

  • (Boolean)

See Also:



46
47
48
# File 'lib/merb_threshold/frequency.rb', line 46

def permit?
  (current_events.length < occurrence)
end

#rate~Numeric

The rate of occurences in seconds

returns the frequency for events that happened over period

Returns:

  • (~Numeric)

See Also:



123
124
125
# File 'lib/merb_threshold/frequency.rb', line 123

def rate
  current_events.length / period.to_f
end

#to_sString

Describe the frequency

Returns:

  • (String)


147
148
149
# File 'lib/merb_threshold/frequency.rb', line 147

def to_s
  @to_s ||= "#{occurrence} time#{'s' if occurrence != 1} per #{interval} #{units}"
end

#wait~Numeric

How long until the resource is freely available

Returns:

  • (~Numeric)


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

def wait
  num_evts = current_events.length
  if num_evts == 0 || num_evts < occurrence
    return 0
  else #How long until the oldest falls off?
    # originally had now - period > @mm.first but +1 all over the place was
    #   retareded:
    # Want: now - period >= @mm.first
    #   now - period + x == @mm.first
    #   => x == @mm.first + period - now
    return (current_events.first + period - Time.now.to_i)
  end
end