class Croque::Monsieur

Attributes

date[RW]
hour[RW]
id[RW]
line[RW]
time[RW]

Public Class Methods

get_list(date, page, per) click to toggle source
# File lib/croque/monsieur.rb, line 46
def get_list(date, page, per)
  # maybe String
  page_num = (page || 1).to_i
  per_num = (per || total_count(date)).to_i
  # get csv data
  csv_data = File.open(ranking_path(date), "r").read.gsub(/\r/, "")
  csv = CSV.new(csv_data)
  # Sorted lines as ranking
  start = ((page_num-1)*per_num)
  # csv to Array
  lines = csv.to_a
  # start..end
  lines = lines.slice(start, per_num) || []
  # generate this class instance
  lines.map do |line|
    # line = [date, hour, uuid, processing_time (ms)]
    self.new(*line)
  end
end
new(date, hour, id, time) click to toggle source
# File lib/croque/monsieur.rb, line 6
def initialize(date, hour, id, time)
  self.date = Date.parse(date)
  self.hour = hour.to_i
  self.id = id
  self.time = time
end
total_count(date) click to toggle source
# File lib/croque/monsieur.rb, line 66
def total_count(date)
  wc_result = `wc -l #{ranking_path(date)}`
  wc_result.match(/\d+/)[0].try(:to_i)
end

Private Class Methods

ranking_path(date) click to toggle source
# File lib/croque/monsieur.rb, line 72
def ranking_path(date)
  Croque.config.store_path.join("#{date}", "ranking.csv")
end

Public Instance Methods

active_record_time() click to toggle source
# File lib/croque/monsieur.rb, line 25
def active_record_time
  get_line[3].to_f
end
body() click to toggle source
# File lib/croque/monsieur.rb, line 13
def body
  # return Array
  lines = get_line[7].split("\t")
  lines.map do |line|
    line.strip.gsub(/\e\[\d+m/, '')
  end
end
full_path() click to toggle source
# File lib/croque/monsieur.rb, line 33
def full_path
  URI.unescape(get_line[4])
end
path_info() click to toggle source
# File lib/croque/monsieur.rb, line 37
def path_info
  get_line[5]
end
processing_time() click to toggle source
# File lib/croque/monsieur.rb, line 29
def processing_time
  self.time.to_f
end
query() click to toggle source
# File lib/croque/monsieur.rb, line 41
def query
  URI.unescape(get_line[6])
end
views_time() click to toggle source
# File lib/croque/monsieur.rb, line 21
def views_time
  get_line[2].to_f
end

Private Instance Methods

csv_path() click to toggle source
# File lib/croque/monsieur.rb, line 88
def csv_path
  Croque.config.store_path.join("#{self.date}", "#{self.hour}.csv")
end
get_line() click to toggle source
# File lib/croque/monsieur.rb, line 78
def get_line
  self.line ||= if File.exist?(csv_path)
    csv_data = File.open(csv_path, "r").read.gsub(/\r/, "")
    csv = CSV.new(csv_data)
    csv.to_a.find{ |line| line[0] == self.id }
  else
    nil
  end
end