Skip to content

Commit

Permalink
Updated to start using the tasks that fetch the statistics from googl…
Browse files Browse the repository at this point in the history
…e analytics and populate the table 'statistic'.
  • Loading branch information
daronco committed Dec 13, 2011
1 parent b9f8c50 commit 3acdde8
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 172 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ tmp/*
db/data.yml
pids/*
doc/app/*
config/analytics_conf.yml
3 changes: 3 additions & 0 deletions config/analytics_conf.yml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
user: [email protected]
passwd: my-password
agent: UA-12345678-0
3 changes: 2 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
:password => config["admin_password"],
:password_confirmation => config["admin_password"],
:_full_name => config["admin_fullname"]
u.update_attribute(:superuser,true)
u.update_attribute(:superuser, true)
u.update_attribute(:created_at, DateTime.now)
u.activate
u.profile!.update_attribute(:full_name, config["admin_fullname"])
55 changes: 55 additions & 0 deletions lib/tasks/statistics.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'tasks/support/statistics_helper'
require 'garb'
require 'yaml'

namespace :statistics do

# matched urls will be grouped using the first capture (the content
# matched inside the parenthesis)
RULES = [ '(^/bigbluebutton/servers/[^/]+)',
'(^/bigbluebutton/servers/[^/]+/rooms/[^/]+)',
'(^/feedback)',
'(^/invitations)',
'(^/invite)',
'(^/spaces/[^/]+)',
'(^/users/[^/]+)' ]

desc "Resets the Statistics table with data from google analytics"
task :init => :environment do
Statistic.destroy_all
stats = StatisticsHelper.get_statistics(Date.parse("10/01/2009"), Date.today, RULES)
StatisticsHelper.update_statistics_table(stats)
end

desc "Updates the Statistics table with data from google analytics"
task :update => :environment do
stats = StatisticsHelper.get_statistics(Date.yesterday, Date.today, RULES)
StatisticsHelper.update_statistics_table(stats)
end

desc "Updates the Statistics table with data from google analytics"
task :print => :environment do
puts "Current statistics (page views):"
puts " Total (page views): " + Statistic.sum("unique_pageviews").to_s
puts " Average (page views): " + Statistic.average("unique_pageviews").to_s
puts " Minimum (page views): " + Statistic.minimum("unique_pageviews").to_s
puts " Maximum (page views): " + Statistic.maximum("unique_pageviews").to_s
puts " URLs: " + Statistic.count.to_s
puts
puts "Top spaces:"
Statistic.where(['url LIKE ?', '/spaces/%']).order('unique_pageviews desc').first(5).each do |rec|
puts " " + rec.url + " : " + rec.unique_pageviews.to_s
end
puts
puts "Top users:"
Statistic.where(['url LIKE ?', '/users/%']).order('unique_pageviews desc').first(5).each do |rec|
puts " " + rec.url + " : " + rec.unique_pageviews.to_s
end
puts
puts "Top webconf rooms:"
Statistic.where(['url LIKE ?', '%/rooms/%']).order('unique_pageviews desc').first(5).each do |rec|
puts " " + rec.url + " : " + rec.unique_pageviews.to_s
end
end

end
63 changes: 63 additions & 0 deletions lib/tasks/support/statistics_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class StatisticsHelper

# Gets the data from google analytics and returns a hash with { 'url' => views }
def self.get_statistics(start_date, end_date, rules)
profile = garb_login

# we ask for 1.000.000 last visits, if we have more we should use a higher number in limit
report = Garb::Report.new(profile, :start_date => start_date, :end_date => end_date, :limit => 100000)
report.metrics :unique_pageviews
report.dimensions :page_path
filter_results(report.results, rules)
end

# Updates the db table with the incremental results in 'final_results'
def self.update_statistics_table(final_results)
final_results.each do |key,value|
sta = Statistic.find_by_url(key)
if sta
sta.unique_pageviews = sta.unique_pageviews + value
else
sta = Statistic.new
sta.url = key
sta.unique_pageviews = value
end
sta.save
end
end

private

# method to add the different urls to sum up the visits
def self.filter_results(results, rules)
final_hash = Hash.new

# try to match each result with each rule
# the key in the hash is the first capture in the regex match
# this will group several urls into a single key
results.each do |res|
path = res.page_path
views = res.unique_pageviews.to_i

rules.each do |rule|
if path.match(rule)
key = path.match(rule)[1]
final_hash[key] = final_hash[key] ? (final_hash[key] + views) : views
end
end
end

final_hash
end

# Creates the garb profile used to access google analytics
def self.garb_login
myhash = YAML.load_file("#{Rails.root.to_s}/config/analytics_conf.yml")
user = myhash["user"]
pass = myhash["passwd"]
agent = myhash["agent"]
Garb::Session.login(user, pass)
profile = Garb::Profile.first(agent)
end

end
91 changes: 0 additions & 91 deletions lib/tasks/vcc/analytics.rake

This file was deleted.

70 changes: 0 additions & 70 deletions lib/tasks/vcc/print_project_stats.rake

This file was deleted.

18 changes: 8 additions & 10 deletions lib/tasks/vcc/stats.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace :vcc do

def print_stats(klass)
puts "#{ I18n.t(klass.to_s.underscore, :count => :other) }:"
puts "\tTotal: #{ klass.count }"

# Create a Hash like stats_hash[year][month] = 0
stats_hash = Hash.new{ |h, year| h[year] = Hash.new{ |year, month| year[month] = 0 } }
Expand All @@ -20,21 +21,18 @@ namespace :vcc do
puts "\t#{ year} #{ format("%2d", month) }: #{ months[month] }"
end
end

puts
puts "\tTotal: #{ klass.count }"
end

def print_video_stats
puts "Videos:"

# Create a Hash like stats_hash[year][month] = 0
stats_hash = Hash.new{ |h, year| h[year] = Hash.new{ |year, month| year[month] = 0 } }

AgendaEntry.with_recording.each do |obj|
stats_hash[obj.created_at.year][obj.created_at.month] += 1
stats_hash[obj.created_at.year][obj.created_at.month] += 1
end

stats_hash.each_pair do |year, months|
(1..12).each do |month|
puts "\t#{ year} #{ format("%2d", month) }: #{ months[month] }"
Expand All @@ -43,7 +41,7 @@ namespace :vcc do

puts
puts "\tTotal: #{ AgendaEntry.with_recording.count }"

end

end

0 comments on commit 3acdde8

Please sign in to comment.