Module: RailsRequests

Defined in:
lib/rails_requests.rb,
lib/rails_requests/engine.rb,
lib/rails_requests/record.rb,
lib/rails_requests/version.rb,
lib/rails_requests/collection.rb,
lib/rails_requests/middleware.rb,
lib/rails_requests/base_report.rb,
lib/rails_requests/full_report.rb,
lib/rails_requests/global_report.rb

Defined Under Namespace

Classes: BaseReport, Collection, Engine, FullReport, GlobalReport, Middleware, Record

Constant Summary collapse

VERSION =
'0.1.1.1'
@@redis =
Redis.new
@@duration =
24.hours
@@debug =
false

Class Method Summary collapse

Class Method Details

.cache_key(now = Date.current) ⇒ Object

date key in redis store



25
26
27
# File 'lib/rails_requests.rb', line 25

def RailsRequests.cache_key(now = Date.current)
  "date-#{now}"
end

.daysObject



34
35
36
# File 'lib/rails_requests.rb', line 34

def RailsRequests.days
  (RailsRequests.duration % 24.days).parts[:days] + 1
end

.dumpObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rails_requests.rb', line 52

def RailsRequests.dump
@data = []

  all = {}
  RailsRequests.days.times do |e|
    date    = e.days.ago.to_date
    all[date] = RailsRequests.redis.hgetall(RailsRequests.cache_key(date))
  end

  stop    = Time.at(60 * (Time.now.to_i / 60))
  current = stop - RailsRequests.duration

  while current <= stop
    views = all.dig(current.to_date, current.strftime("%H:%M")) || 0

    @data << [current.to_i * 1000, views.to_i]

    current += 1.minute
  end

  @data.sort!
end

.field_key(now = Time.now) ⇒ Object

write to current slot time - date -minute



30
31
32
# File 'lib/rails_requests.rb', line 30

def RailsRequests.field_key(now = Time.now)
  now.strftime("%H:%M")
end

.median(array) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rails_requests.rb', line 38

def RailsRequests.median(array)
  sorted = array.sort
  size   = sorted.size
  center = size / 2

  if size == 0
    nil
  elsif size.even?
    (sorted[center - 1] + sorted[center]) / 2.0
  else
    sorted[center]
  end
end

.populate_test_data(seed = 20, limit = 10000, days = 7) ⇒ Object

populate test data run in rails c



77
78
79
80
81
82
# File 'lib/rails_requests.rb', line 77

def RailsRequests.populate_test_data(seed = 20, limit = 10000, days = 7)
  limit.times do
    t = rand(86400*days).seconds.ago # within last 7 days
    RailsRequests.redis.hincrby(RailsRequests.cache_key(t.to_date), RailsRequests.field_key(t), rand(seed))
  end
end

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (RailsRequests)

    the object that the method was called on



20
21
22
# File 'lib/rails_requests.rb', line 20

def self.setup
  yield(self)
end