Class: RequestMaster::ReqMaster

Inherits:
Object
  • Object
show all
Defined in:
lib/request_master.rb,
lib/request_master/actions.rb,
lib/request_master/ip_checks.rb,
lib/request_master/req_stats.rb,
lib/request_master/ip_details.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReqMaster

Returns a new instance of ReqMaster.



57
58
59
60
# File 'lib/request_master.rb', line 57

def initialize
  @database = RequestMaster.reqs_db
  @action_count = 0
end

Instance Attribute Details

#action_countObject

Returns the value of attribute action_count.



55
56
57
# File 'lib/request_master.rb', line 55

def action_count
  @action_count
end

#domainObject

Returns the value of attribute domain.



55
56
57
# File 'lib/request_master.rb', line 55

def domain
  @domain
end

Instance Method Details

#all_ip_stats(dom_id) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/request_master/req_stats.rb', line 58

def all_ip_stats (dom_id)

  all_ips = IpAddress.select(:public_ip).to_a
  all_ips.map!(&:public_ip)

  all_avg = Numbers([])
  all_total = Numbers([])

  output = all_ips.inject([]) { |acc, ip|
    stats = ip_stats(ip, dom_id)

    if stats.num >= 50
      all_total << stats.num
      all_avg << stats.avg
      acc << {ip => stats.avg}
    else
      acc
    end
  }

  total_stats = {
      min_total: all_total.min,
      max_total: all_total.max,
      avg_total: all_total.average.round,
      min_avg: all_avg.min,
      max_avg: all_avg.max,
      avg_avg: all_avg.average.round
  }

  output << total_stats
end

#alloc_exhaust?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/request_master/actions.rb', line 49

def alloc_exhaust?
  @allocation == 0
end

#allocate(first_run = false) ⇒ Object Also known as: reallocate



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/request_master/actions.rb', line 25

def allocate (first_run = false)
  my_ip = RequestMaster.my_ip
  checks_out = first_run ? check(my_ip) : false

  until checks_out
    script = RequestMaster.alloc_args
    my_ip = script.object.send(script.method_name, *script.args)
    checks_out = check(my_ip)

    unless checks_out
      sputs "failed because #{RequestMaster.details.to_h}"
    end
  end

  sputs "ip #{my_ip} passed checks"
  sputs ip_stats(my_ip, RequestMaster.check_args.dom_id).to_h.to_s

  comp_alloc(my_ip)
  sputs "allocation = #{@allocation}"
  @allocation
end

#check(ip, dom_id = RequestMaster.check_args.dom_id, avg_freq = RequestMaster.check_args.avg_freq) ⇒ Object



41
42
43
# File 'lib/request_master/ip_checks.rb', line 41

def check (ip, dom_id = RequestMaster.check_args.dom_id, avg_freq = RequestMaster.check_args.avg_freq)
  check_status(ip, dom_id) && check_freq(ip, dom_id, avg_freq)
end

#check_frequency(ip, dom_id, avg_freq) ⇒ Object Also known as: check_freq



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/request_master/ip_checks.rb', line 27

def check_frequency (ip, dom_id, avg_freq)
  result = ip_stats(ip, dom_id).avg
  if result.nil? || result <= avg_freq
    true
  else
    RequestMaster.details = {
        ip: ip,
        dom_id: dom_id,
        avg_freq: result
    }
    false
  end
end

#check_status(ip, dom_id) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/request_master/ip_checks.rb', line 8

def check_status (ip, dom_id)
  record = IpStatus[public_ip: ip, website: dom_id]
  if record.nil?
    true
  else
    status = record.status
    if ['blocked', 'shielded'].include?(status)
      RequestMaster.details = {
          ip: ip,
          dom_id: dom_id,
          status: status
      }
      false
    else
      true
    end
  end
end

#compute_allowance(ip, dom_id = RequestMaster.check_args.dom_id, avg_freq = RequestMaster.check_args.avg_freq) ⇒ Object Also known as: comp_alloc



8
9
10
11
12
# File 'lib/request_master/ip_details.rb', line 8

def compute_allowance (ip, dom_id = RequestMaster.check_args.dom_id, avg_freq = RequestMaster.check_args.avg_freq)

  stats = ip_stats(ip, dom_id)
  @allocation = avg_freq * (stats.days + 1) - stats.num
end

#dom_id(url) ⇒ Object



36
37
38
# File 'lib/request_master/req_stats.rb', line 36

def dom_id (url)
  Website[domain_url: url].id
end

#ip_stats(ip, dom_id) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/request_master/req_stats.rb', line 40

def ip_stats (ip, dom_id)
  requests = BrowserRequest.where(domain: dom_id, ip_address: ip)
  num = requests.count

  if num >= 2
    time_span = (requests.last.time.to_date - requests.first.time.to_date + 1).to_f
    avg = (num / time_span).to_f.round(1)
  elsif num == 1
    avg = num
    time_span = 1.0
  else
    avg = nil
    time_span = 0.0
  end

  OpenStruct.new(num: num, avg: avg, days: time_span.round(1))
end

#run_actionObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/request_master/actions.rb', line 8

def run_action
  script = RequestMaster.action_args
  result = script.object.send(script.method_name, *script.args)
  @action_count += 1

  unless script.divider.nil?

    if @action_count % script.divider == 0
      puts '------------------------'
    end

  end

  @allocation -= 1
  result
end