Module: Wwwtf::Cache::King

Defined in:
lib/wwwtf/cache/king.rb

Class Method Summary collapse

Class Method Details

.cache_control_percent(cache_control) ⇒ Object



54
55
56
# File 'lib/wwwtf/cache/king.rb', line 54

def self.cache_control_percent(cache_control)
  (cache_control.compact.size * 100) / cache_control.size
end

.expire_in_day_percent(cache_control) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/wwwtf/cache/king.rb', line 58

def self.expire_in_day_percent(cache_control)
  max_age_in_days = cache_control.compact.collect{|max_age|
                      max_age.to_i / 86400 # i.e. 60 * 60 * 24
                    }
  max_age_in_days.delete(0)
  (max_age_in_days.size * 100 ) / cache_control.size
end

.filter_cache_headers(headers_batch) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/wwwtf/cache/king.rb', line 39

def self.filter_cache_headers(headers_batch)
  filtered_headers = {}
  reqd_headers     = ['Cache-Control', 'Last-Modified']
	headers_batch.each do |url, headers|
    filtered = {}
    reqd_headers.each {|hdr|
      filtered = filtered.merge(
                   {hdr => headers[hdr]}
                 )
    }
    filtered_headers = filtered_headers.merge( {url => filtered} )
  end
  filtered_headers
end

.headers(page_urls) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/wwwtf/cache/king.rb', line 31

def self.headers(page_urls)
  headers     = {}
  page_urls.each do |src|
    headers = headers.merge({src => Wwwtf::HTTP.headers(src)})
  end
  filter_cache_headers(headers)
end

.last_modified_median(last_modified) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/wwwtf/cache/king.rb', line 70

def self.last_modified_median(last_modified)
  require 'time'
  last_modified   = last_modified.compact
  return nil if last_modified.empty?
  last_mod_sorted = last_modified.each {|mod_time|
                      Time.parse(mod_time)
                    }.sort
  half_size = last_mod_sorted.size / 2
  if ( last_mod_sorted.size % 2 ) == 0
    median_mod_time = Wwwtf::UtilsTime.hours_until_now(
                        last_mod_sorted[half_size]
                      )
    median_mod_time += Wwwtf::UtilsTime.hours_until_now(
                         last_mod_sorted[half_size - 1]
                       )
    median_mod_time = median_mod_time / 2
  else
    median_mod_time = Wwwtf::UtilsTime.hours_until_now(
                        last_mod_sorted[half_size]
                      )
  end
  median_mod_time
end

.last_modified_percent(last_modified) ⇒ Object



66
67
68
# File 'lib/wwwtf/cache/king.rb', line 66

def self.last_modified_percent(last_modified)
  (last_modified.compact.size * 100) / last_modified.size
end

.stat(url) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/wwwtf/cache/king.rb', line 7

def self.stat(url)
  begin
    page_urls = Wwwtf::HTTP.page_dependency(url)
    all_headers = headers(page_urls)
    cache_control, last_modified = [], []
    all_headers.each do |url, headr|
      max_age = Wwwtf::Cache.cache_control_max_age(headr['Cache-Control'])
      cache_control.push max_age
      last_modified.push headr['Last-Modified']
    end
    {
      'page_urls'                  => page_urls,
      'total_count'                => page_urls.size,
      'cache_control%'             => cache_control_percent(cache_control),
      'expire_in_day%'             => expire_in_day_percent(cache_control),
      'last_modified%'             => last_modified_percent(last_modified),
      'last_modified_hours_median' => last_modified_median(last_modified)
    }
  rescue
    puts "WWWTF Cache King failed in collecting statistic for #{url}."
    exit 1
  end
end