Class: Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/collective/checker.rb

Constant Summary collapse

DAY =
86400

Instance Method Summary collapse

Constructor Details

#initialize(activity_count, last_time, check_time = Time.now) ⇒ Checker

Returns a new instance of Checker.

Parameters:

  • activity_count

    is a daily level of activities

  • last_time

    is the last time we checked



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/collective/checker.rb', line 9

def initialize( activity_count, last_time, check_time = Time.now )
  @activity_count = activity_count.to_i                     # e.g. 192 actions
  @last_time      = Time.at last_time.to_i                  # e.g. 1 hour ago
  @check_time     = check_time                              # e.g. now

  @last_time      = [ @check_time - DAY, @last_time ].max   # reject huge intervals like since epoch
  @interval       = @check_time - @last_time                # e.g. 1 hour

  # estimations
  variation       = 2 * ( rand + rand + rand + rand ) / 4   # ~1.0 more or less
  day_est         = variation * @activity_count             # e.g. ~192
  @estimate       = ( day_est * @interval / DAY ).to_i      # e.g. 8 (per hour)
  next_interval   = @estimate > 0 ? @interval / @estimate : @interval * 2
end

Instance Method Details

#activity_countObject



39
40
41
# File 'lib/collective/checker.rb', line 39

def activity_count
  @activity_count
end

#check?Boolean

refuse to check too often

Returns:

  • (Boolean)


35
36
37
# File 'lib/collective/checker.rb', line 35

def check?
  @last_time < @check_time - 100
end

#checked(actual) ⇒ Object



28
29
30
31
32
# File 'lib/collective/checker.rb', line 28

def checked( actual )
  @activity_count = ( actual * DAY / @interval ).to_i               # e.g. 12
  next_interval   = actual > 0 ? @interval / actual : @interval * 2 # e.g. 300 seconds (3600 / 12)
  @next_time      = @check_time + next_interval                     # e.g. now + 300 seconds
end

#estimateObject



24
25
26
# File 'lib/collective/checker.rb', line 24

def estimate
  @estimate
end

#estimated_delayObject



43
44
45
# File 'lib/collective/checker.rb', line 43

def estimated_delay
  @estimate > 0 ? Math.sqrt(@estimate).to_i : 1
end

#next_timeObject



47
48
49
50
# File 'lib/collective/checker.rb', line 47

def next_time
  # If not determined yet, calculate a safety time past the Facebook api limit
  @next_time ? @next_time : @check_time + 1000
end