Created
October 12, 2010 08:53
-
-
Save jparbros/621872 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Script builded to ruby 1.9.1 | |
# | |
require 'CSV' | |
class ArrayIterator | |
# | |
# Class ArrayIterator | |
# Implementation of Iterator pattern | |
# | |
def initialize(array) | |
@array = array | |
@index = 0 | |
end | |
def has_next? | |
@index < @array.length | |
end | |
def has_prev? | |
@index != 0 | |
end | |
def item | |
@array[@index] | |
end | |
def next_item | |
value = @array[@index] | |
@index += 1 | |
value | |
end | |
def prev_item | |
@array[@index-1] | |
end | |
def same_event | |
value = item | |
prev_value = prev_item | |
if (has_prev? and (value['session_id'] == prev_value['session_id'])) | |
((value['when'].to_i - prev_value['when'].to_i) < 3600) | |
else | |
false | |
end | |
end | |
end | |
class Geekopardy | |
# | |
# Class to construct the statistics | |
# | |
def initialize | |
@data = {} | |
end | |
def new_user(user) | |
@data[user] = {} unless @data.keys.include? user | |
end | |
def new_visit(item) | |
@data[item['session_id']]["event_#{@data[item['session_id']].size+1}"] = [] | |
end | |
def new_view(item) | |
@data[item['session_id']]["event_#{@data[item['session_id']].size}"] << item['when'] | |
end | |
def users | |
@data.keys | |
end | |
def events(user) | |
@data[user].keys | |
end | |
def views(user,event) | |
@data[user][event] | |
end | |
end | |
file = CSV.open('events.csv','r',{:col_sep => "\t", :headers => true}).to_a | |
array = ArrayIterator.new(file) | |
geekopardy = Geekopardy.new | |
while array.has_next? | |
item = array.item | |
geekopardy.new_user item['session_id'] | |
geekopardy.new_visit item unless array.same_event | |
geekopardy.new_view item | |
array.next_item | |
end | |
# | |
# Print the results | |
# Print each timestamp of page view | |
# | |
geekopardy.users.each do |user| | |
puts "--------------------- USER #{user} ---------------------" | |
geekopardy.events(user).each do |event| | |
puts "\t------------- #{event} -------------" | |
geekopardy.views(user, event).each do |view| | |
puts "\t\t" + Time.at(view.to_i).to_s | |
end | |
end | |
end | |
# | |
# Print the results | |
# Print a sum of page view | |
# | |
puts "\n\r\n\r" | |
geekopardy.users.each do |user| | |
puts "--------------------- USER #{user} ---------------------" | |
geekopardy.events(user).each do |event| | |
puts "\t------------- #{event} -------------" | |
puts "\t Pages View: " + geekopardy.views(user, event).size.to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment