Class: Iudex::Worker::Prioritizer

Inherits:
Filter::FilterBase
  • Object
show all
Includes:
Math
Defined in:
lib/iudex-worker/prioritizer.rb

Constant Summary collapse

WWW_BEGINS =

WWW begins

Time.utc( 1991, "aug", 6, 20,0,0 )
MINUTE =
60.0
HOUR =
60.0 * 60.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) {|_self| ... } ⇒ Prioritizer

Returns a new instance of Prioritizer.

Yields:

  • (_self)

Yield Parameters:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/iudex-worker/prioritizer.rb', line 35

def initialize( name, opts = {} )
  @name = name

  @constant            = 0.0
  @impedance           = 2.0
  @min_next_unmodified =  5 * MINUTE
  @min_next            = 10 * MINUTE
  @visiting_now        = false

  @factors = [ [ 30.0, :ref_change_rate ],
               [ -1.0, :log_pub_age ] ]

  @log = RJack::SLF4J[ self.class ]

  opts.each { |k,v| send( k.to_s + '=', v ) }
  yield self if block_given?

  @min_next_unmodified = [ @min_next_unmodified, @min_next ].min
  @constant = @constant.to_f
end

Instance Attribute Details

#constantObject

Returns the value of attribute constant.



24
25
26
# File 'lib/iudex-worker/prioritizer.rb', line 24

def constant
  @constant
end

#factorsObject

Returns the value of attribute factors.



28
29
30
# File 'lib/iudex-worker/prioritizer.rb', line 28

def factors
  @factors
end

#impedanceObject

Returns the value of attribute impedance.



25
26
27
# File 'lib/iudex-worker/prioritizer.rb', line 25

def impedance
  @impedance
end

#min_nextObject

Returns the value of attribute min_next.



26
27
28
# File 'lib/iudex-worker/prioritizer.rb', line 26

def min_next
  @min_next
end

#min_next_unmodifiedObject

Returns the value of attribute min_next_unmodified.



27
28
29
# File 'lib/iudex-worker/prioritizer.rb', line 27

def min_next_unmodified
  @min_next_unmodified
end

#visiting_nowObject

Returns the value of attribute visiting_now.



29
30
31
# File 'lib/iudex-worker/prioritizer.rb', line 29

def visiting_now
  @visiting_now
end

Instance Method Details

#adjust(map, priority, delta = 0.0) ⇒ Object



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/iudex-worker/prioritizer.rb', line 69

def adjust( map, priority, delta = 0.0 )

  old_priority = priority
  memo = ( ( ( @constant != 0.0 ) ? [ @constant ] : [] ) if @log.debug? )

  new_priority = @factors.inject( @constant ) do | p, (w,func)|
    comp = ( w * send( func, map ) )
    ( memo << "%.1f:%s" % [ comp.to_f, func ] ) if memo && comp != 0.0
    p + comp
  end

  #FIXME: new_priority = [ 0.0, new_priority ].max

  priority = ( ( ( priority || 0.0 ) * @impedance + new_priority ) /
               ( @impedance + 1 ) )

  if map.last_visit || visiting_now
    delta = ( map.status == 304 ) ? @min_next_unmodified : @min_next
  else
    delta = 0.0
  end

  @log.debug do
    memo.join( ' + ' ) +
      ( " :: %.1f -> %.1f = %.1f in %.1fs" %
        ( [ old_priority, new_priority,
            priority, delta ].map { |f| f.to_f } ) )
  end

  [ priority, delta ]
end

#as_time(torj) ⇒ Object

FIXME: Generalize?



140
141
142
143
144
145
146
147
# File 'lib/iudex-worker/prioritizer.rb', line 140

def as_time( torj )
  if torj.is_a?( Time )
    torj
  else
    ms = torj.time
    Time.at( ms / 1_000, ( ms % 1_000 ) * 1_000 ) # s, µs
  end
end

#describeObject



56
57
58
# File 'lib/iudex-worker/prioritizer.rb', line 56

def describe
  [ @name, @constant, @min_next ]
end

#filter(map) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/iudex-worker/prioritizer.rb', line 60

def filter( map )

  map.priority, delta = adjust( map, map.priority )

  map.next_visit_after = ( as_time( map.visit_start ) + delta if delta )

  true
end

#log_pub_age(map) ⇒ Object



101
102
103
104
105
# File 'lib/iudex-worker/prioritizer.rb', line 101

def log_pub_age( map )
  diff = sdiff( ( map.pub_date || WWW_BEGINS ), map.visit_start ) / MINUTE
  diff = 1.0 / MINUTE if diff < 1.0 / MINUTE
  ( log( diff ) - log( 1.0 / MINUTE ) )
end

#oldest(refs) ⇒ Object



130
131
132
# File 'lib/iudex-worker/prioritizer.rb', line 130

def oldest( refs )
  ( refs.map { |r| r.pub_date }.compact.min ) if refs
end

#ref_change_rate(map) ⇒ Object

References per hour, with updates rated at 1/4 a new reference.



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/iudex-worker/prioritizer.rb', line 113

def ref_change_rate( map )
  s = since( map )
  if s.nil? || s == 0.0
    0.0
  else
    ( ( ( map.new_references || 0.0 ) +
        ( map.updated_references || 0.0 ) / 4.0 ) /
      s *
      HOUR )
  end
end

#sdiff(prev, now) ⇒ Object



134
135
136
137
# File 'lib/iudex-worker/prioritizer.rb', line 134

def sdiff( prev, now )
  diff = as_time( now ) - as_time( prev || WWW_BEGINS )
  ( diff < 0.0 ) ? 0.0 : diff
end

#since(map) ⇒ Object



125
126
127
128
# File 'lib/iudex-worker/prioritizer.rb', line 125

def since( map )
  sdiff( map.last_visit || oldest( map.references ),
         map.visit_start )
end