Skip to content

Commit

Permalink
showing weekly stats on the main page now
Browse files Browse the repository at this point in the history
  • Loading branch information
HamptonMakes committed Aug 24, 2009
1 parent 8b667c2 commit 8d7ff63
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 47 deletions.
10 changes: 10 additions & 0 deletions stats/models/stat.rb
Expand Up @@ -39,10 +39,20 @@ def percent_of(attribute)
end
end

def percent_change_from(stat, method = :hits)
original = stat.send(method)
new_number = self.send(method)
((new_number.to_f - original.to_f) / original) * 100
end

def requests_per_second
hits / 60.0 / 60
end

def hits_in_millions
hits / 1_000_000.0
end

def en_hits
language_hits["en"]
end
Expand Down
109 changes: 76 additions & 33 deletions stats/models/stat_merging.rb
Expand Up @@ -16,9 +16,11 @@ def merge(data = {})
end
when :hash_sum
data[key] ||= {}
attributes[key].keys.each do |hash_key|
data[key][hash_key] ||= 0
data[key][hash_key] += attributes[key][hash_key]
if attributes[key]
attributes[key].keys.each do |hash_key|
data[key][hash_key] ||= 0
data[key][hash_key] += attributes[key][hash_key]
end
end
end
end
Expand Down Expand Up @@ -55,28 +57,25 @@ def merge(list_of_stats = [])

data
end

def all_time
StatSegment.merge(StatSegment.days)
end

def days
days = ($first_day..(Date.today - 1)).to_a
days.collect do |date|
StatSegment.day(date)

def minutes(date, hour_number = nil)
if hour_number
Stat.all(:conditions => ["DATE(time) = ? AND time_length = ? AND HOUR(time) = ?", date, "minute", hour_number])
else
Stat.all(:conditions => ["DATE(time) = ? AND time_length = ?", date, "minute"])
end
end

def day(date = Date.today)
day = StatSegment.first(:conditions => ["DATE(time) = ? and time_length = ?", date, "day"])

if day.nil? || date == Date.today || date == (Date.today - 1)
day ||= StatSegment.new(:time_length => "day", :time => date)
day.attributes = merge(hours(date))
day.save
def hours(date = Date.today)
last_hour_to_track = 23

if(date == Date.today)
last_hour_to_track = Time.now.hour
end

day

((0..last_hour_to_track).to_a.collect do |hour|
StatSegment.hour(date, hour)
end).compact
end

def hour(date, hour_number)
Expand All @@ -103,24 +102,68 @@ def hour(date, hour_number)
hour
end

def minutes(date, hour_number = nil)
if hour_number
Stat.all(:conditions => ["DATE(time) = ? AND time_length = ? AND HOUR(time) = ?", date, "minute", hour_number])
else
Stat.all(:conditions => ["DATE(time) = ? AND time_length = ?", date, "minute"])
def days
days = ($first_day..(Date.today - 1)).to_a
days.collect do |date|
StatSegment.day(date)
end
end

def day(date = Date.today)
day = StatSegment.first(:conditions => ["DATE(time) = ? and time_length = ?", date, "day"])

def hours(date = Date.today)
last_hour_to_track = 23
if day.nil? || date == Date.today || date == (Date.today - 1)
day ||= StatSegment.new(:time_length => "day", :time => date)
day.attributes = merge(hours(date))
day.save
end

day
end

def week(year, week_number)
annual_offset = Date.parse("1-1-#{year}").strftime("%u").to_i
day_number_for_first_day_of_week = (week_number * 7) - annual_offset
first_day_of_week = Date.parse("#{day_number_for_first_day_of_week}-#{year}")

if(date == Date.today)
last_hour_to_track = Time.now.hour

week = Stat.first(:conditions => ["DATE(time) = ? and time_length = ?", first_day_of_week, "week"])

if week.nil?
current_day = first_day_of_week

# Start the day of the week two days after. avoids any count-from-monday or count-from-sunday problems.
week = Stat.new(:time => current_day, :time_length => "week")

# This code is ABSOLUTE SHIT... but, I am not seeing the clear answer right now
result = []
7.times do
daily_stats = day(current_day)

result << daily_stats if daily_stats
#puts daily_stats.inspect
current_day = current_day + 1
end
week.attributes = merge(result)
week.save
end

((0..last_hour_to_track).to_a.collect do |hour|
StatSegment.hour(date, hour)
end).compact
week
end

def weeks
first_week = $first_day.strftime("%V").to_i
last_week = Date.today.strftime("%V").to_i - 1

(first_week..last_week).to_a.collect do |week_number|
self.week(2009, week_number)
end
end

def all_time_hits
Stat.sum(:hits, :conditions => {:time_length => "hour"})
end


end
end
14 changes: 12 additions & 2 deletions stats/server.rb
Expand Up @@ -16,9 +16,19 @@
end

get("/") do
@time = "Weeks"
@today_path = "/hourly/" + Time.now.strftime("%Y/%m/%d")
@days = StatSegment.days
@allTime = StatSegment.all_time
@allTime = StatSegment.all_time_hits
@stats = Stat.weeks

haml :index
end

get "/daily" do
@time = "Days"
@today_path = "/hourly/" + Time.now.strftime("%Y/%m/%d")
@allTime = StatSegment.all_time_hits
@stats = Stat.days

haml :index
end
Expand Down
12 changes: 6 additions & 6 deletions stats/views/hourly.haml
Expand Up @@ -19,13 +19,13 @@
%p

%img{:src => line_chart(@minutes, ['average_action_time_in_ms'], "Average (Mean) Request Time (ms)")}/
%img{:src => line_chart(@hours, ['median_action_time_in_ms'], "Median Request Time (ms)")}/
%img{:src => line_chart(@minutes, ['median_action_time_in_ms'], "Median Request Time (ms)")}/
%img{:src => line_chart((@minutes.any? ? @minutes : @hours), ['hits_per_second'], "Traffic per Second")}/
%img{:src => line_chart(@hours, ["format_hits['android']", "format_hits['native_iphone']", "format_hits['palm_pre']", "format_hits['webkit']"], "Traffic by Other Formats")}/
%img{:src => line_chart(@hours, ["language_hits['de']", "language_hits['fr']", "language_hits['ja']"], "Traffic by Other Languages")}/
%img{:src => line_chart(@hours, [:cache_hit_ratio], "Local Cache Hit Ratio") { |c| c.axis :y, :range => [0.0, 1.0]}}/
%img{:src => line_chart(@hours, [:spider_cache_hit_ratio], "Wikipedia Cache Hit Ratio")}/
%img{:src => line_chart(@hours, [:load_average], "Server Load Average") }
%img{:src => line_chart(@minutes, ["format_hits['android']", "format_hits['native_iphone']", "format_hits['palm_pre']", "format_hits['webkit']"], "Traffic by Other Formats")}/
%img{:src => line_chart(@minutes, ["language_hits['de']", "language_hits['fr']", "language_hits['ja']"], "Traffic by Other Languages")}/
%img{:src => line_chart(@minutes, [:cache_hit_ratio], "Local Cache Hit Ratio") { |c| c.axis :y, :range => [0.0, 1.0]}}/
%img{:src => line_chart(@minutes, [:spider_cache_hit_ratio], "Wikipedia Cache Hit Ratio")}/
%img{:src => line_chart(@minutes, [:load_average], "Server Load Average") }

.notice
%em All times in UTC
Expand Down
12 changes: 6 additions & 6 deletions stats/views/index.haml
@@ -1,28 +1,28 @@
%h2 Mobile Wikipedia Stats

%h3
= number(StatSegment.sum(:hits, :conditions => {:time_length => "hour"}))
= number(@allTime)
Happy Pages Served*

%img{:src => line_chart(@days, ["hits"], "Total Traffic by Day")}
%img{:src => line_chart(@days, ["average_action_time_in_ms"], "Average Page Serving Speed (ms)")}
%img{:src => line_chart(@stats, ["hits_in_millions"], "Total Traffic in Millions")}
%img{:src => line_chart(@stats, ["average_action_time_in_ms"], "Average Page Serving Speed (ms)")}

%h3
= to_date_url("See today's incomplete stats", Date.today)


%h2 All Complete Days So Far
%h2= @time

%table
%tr
%td Date
%td= @time
%td Page Views
%td
Average of Hourly
%br/
Median Request Time
%td Links
- @days.reverse.each do |stats|
- @stats.reverse.each do |stats|
%tr
%td= stats.time.strftime("%Y/%m/%d")
%td{:style => "text-align: right"}= number stats.hits
Expand Down

0 comments on commit 8d7ff63

Please sign in to comment.