Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

dialing in the download graphs

  • Loading branch information...
commit eede2b6b230dc9c3da0162595286e8361e80da0c 1 parent 11c0be9
@qrush qrush authored
View
1  Gemfile
@@ -42,6 +42,7 @@ group :test do
gem "redgreen", "1.2.2"
gem "rr", "0.10.11"
gem "shoulda", "2.11.1"
+ gem "timecop", "0.3.5"
gem "treetop", "1.4.5"
gem "webrat", "0.5.3"
gem "webmock", "0.7.3"
View
2  Gemfile.lock
@@ -133,6 +133,7 @@ GEM
system_timer (1.0)
term-ansicolor (1.0.5)
thor (0.14.0)
+ timecop (0.3.5)
treetop (1.4.5)
polyglot (>= 0.3.1)
trollop (1.16.2)
@@ -180,6 +181,7 @@ DEPENDENCIES
shoulda (= 2.11.1)
sinatra (= 1.0)
system_timer (= 1.0)
+ timecop (= 0.3.5)
treetop (= 1.4.5)
validates_url_format_of (= 0.1.0)
webmock (= 0.7.3)
View
1  Rakefile
@@ -1,5 +1,4 @@
require File.expand_path('../config/application', __FILE__)
-require 'rake'
Gemcutter::Application.load_tasks
desc "Run all tests and features"
View
5 app/controllers/profiles_controller.rb
@@ -1,11 +1,12 @@
class ProfilesController < ApplicationController
- #before_filter :redirect_to_root, :unless => :signed_in?, :except => :show
+ before_filter :redirect_to_root, :unless => :signed_in?, :except => :show
def edit
end
def show
- @user = User.find_by_slug(params[:id])
+ @user = User.find_by_slug(params[:id])
+ @rubygems = @user.rubygems_downloaded
end
def update
View
13 app/models/rubygem.rb
@@ -208,6 +208,19 @@ def find_or_initialize_version_from_spec(spec)
version
end
+ def monthly_downloads
+ key_dates = self.class.monthly_dates.map(&:to_s)
+ $redis.hmget(Download.history_key(self), *key_dates).map(&:to_i)
+ end
+
+ def self.monthly_dates
+ (2..31).map { |n| n.days.ago.to_date }.reverse
+ end
+
+ def self.monthly_short_dates
+ monthly_dates.map { |date| date.strftime("%m/%d") }
+ end
+
def self.versions_key(name)
"r:#{name}"
end
View
35 app/views/profiles/show.html.erb
@@ -18,25 +18,26 @@
<%= javascript_include_tag "raphael", "g.raphael", "g.bar", :cache => true %>
<script type="text/javascript">
$(document).ready(function() {
+ var dates = <%= Rubygem.monthly_short_dates.to_json.html_safe %>;
+ <% @rubygems.each do |rubygem| %>
+ $.data(document.body, "graph-<%= rubygem.id %>", <%= rubygem.monthly_downloads.to_json.html_safe %>);
+ <% end %>
+
$(".profile-graph").each(function() {
- var r = Raphael($(this).attr("id")),
+ var id = $(this).attr("id"),
+ r = Raphael(id),
fin = function () {
- this.flag = r.g.popup(this.bar.x, this.bar.y, "9/13: " + this.bar.value || "0").insertBefore(this);
+ var currentDate = dates[Math.floor(this.bar.x / 23)]
+ this.flag = r.g.popup(this.bar.x, this.bar.y, currentDate + ": " + this.bar.value || "0").insertBefore(this);
},
fout = function () {
this.flag.animate({opacity: 0}, 300, function () {this.remove();});
};
- r.g.barchart(1, 9, 690, 125, [
- [38, 92, 37, 98, 38,
- 87, 83, 30, 19, 10,
- 38, 93, 47, 57, 55,
- 87, 83, 30, 19, 10,
- 38, 93, 47, 57, 55,
- 20, 13, 32, 28, 10]
- ],
- { type: "soft", colors : { 0 : "rgb(200, 164, 164)" } }
- ).hover(fin, fout);
+ r.g.barchart(15, 9, 655, 125,
+ [ $.data(document.body, id) ],
+ { type: "soft", colors : { 0 : "rgb(200, 164, 164)" } }
+ ).hover(fin, fout);
});
});
</script>
@@ -45,15 +46,15 @@
<div id="profile">
<div class="profile-list">
<ol>
- <% @user.rubygems.each do |rubygem| %>
+ <% @rubygems.each do |rubygem| %>
<li>
- <a href="<%=rubygem_path(rubygem)%>" class="profile-rubygem">
+ <a href="<%= rubygem_path(rubygem) %>" class="profile-rubygem">
<%= rubygem.name %>
<em><%= rubygem.versions.most_recent %></em>
</a>
- <a href="" class="profile-downloads">
- <div><span>8,990</span> downloads</div>
- <div><span>1,337</span> today</div>
+ <a href="<%= rubygem_stats_path(rubygem) %>" class="profile-downloads">
+ <div><span><%= number_with_delimiter rubygem.downloads %></span> downloads</div>
+ <div><span><%= number_with_delimiter rubygem.downloads_today %></span> today</div>
</a>
<div id="graph-<%= rubygem.id %>" class="profile-graph">
</div>
View
3  public/stylesheets/screen.css
@@ -1553,6 +1553,7 @@ h5#downloads {
text-shadow: 0 -1px 3px #000000;
vertical-align: top;
padding-top: 5px;
+ width: 178px;
}
.main .info .profile-list ol li a.profile-downloads span {
@@ -1579,7 +1580,7 @@ h5#downloads {
font-weight: 900;
color: #fff;
display: inline-block;
- width: 575px;
+ width: 500px;
overflow: hidden;
height: 21px;
line-height: 23px;
View
24 test/functional/profiles_controller_test.rb
@@ -8,8 +8,28 @@ class ProfilesControllerTest < ActionController::TestCase
sign_in_as(@user)
end
+ context "on GET to show" do
+ setup do
+ @rubygems = (0..10).map do |n|
+ Factory(:rubygem, :downloads => n * 100).tap do |rubygem|
+ Factory(:ownership, :rubygem => rubygem, :user => @user, :approved => true)
+ end
+ end.reverse
+ get :show, :id => @user.handle
+ end
+
+ should respond_with :success
+ should render_template :show
+ should assign_to(:user) { @user }
+ should "assign the last 10 most downloaded gems" do
+ assert_equal @rubygems[0..9], assigns[:rubygems]
+ end
+ end
+
context "on GET to show with handle" do
- setup {get :show, :id => @user.handle}
+ setup do
+ get :show, :id => @user.handle
+ end
should respond_with :success
should render_template :show
@@ -56,6 +76,4 @@ class ProfilesControllerTest < ActionController::TestCase
should respond_with :redirect
should redirect_to('the homepage') { root_url }
end
-
end
-
View
48 test/unit/rubygem_test.rb
@@ -535,4 +535,52 @@ class RubygemTest < ActiveSupport::TestCase
end
end
end
+
+ context "downloads" do
+ setup do
+ @rubygem = Factory(:rubygem)
+ @version = Factory(:version, :rubygem => @rubygem)
+
+ Timecop.freeze DateTime.parse("10/2/2010")
+ 1.times { Download.incr(@rubygem.name, @version.full_name) }
+ Download.rollover
+
+ Timecop.freeze DateTime.parse("10/3/2010")
+ 6.times { Download.incr(@rubygem.name, @version.full_name) }
+ Download.rollover
+
+ Timecop.freeze DateTime.parse("10/16/2010")
+ 4.times { Download.incr(@rubygem.name, @version.full_name) }
+ Download.rollover
+
+ Timecop.freeze DateTime.parse("11/1/2010")
+ 2.times { Download.incr(@rubygem.name, @version.full_name) }
+ Download.rollover
+
+ Timecop.freeze DateTime.parse("11/2/2010")
+ end
+
+ should "give counts from the past 30 days" do
+ downloads = @rubygem.monthly_downloads
+
+ assert_equal 30, downloads.size
+ assert_equal 6, downloads.first
+ (3..14).each do |n|
+ assert_equal 0, downloads[n.to_i - 2]
+ end
+ assert_equal 4, downloads[13]
+ (16..30).each do |n|
+ assert_equal 0, downloads[n.to_i - 2]
+ end
+ assert_equal 2, downloads.last
+ end
+
+ should "give the monthly dates back" do
+ assert_equal ("02".."31").map { |date| "10/#{date}" }, Rubygem.monthly_short_dates
+ end
+
+ teardown do
+ Timecop.return
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.