implement traffic counter that adjusts redness of lobsters logo

This commit is contained in:
joshua stein 2012-08-16 16:11:30 -05:00
parent 342ede4d4c
commit 43444f179b
5 changed files with 32 additions and 9 deletions

View file

@ -239,12 +239,6 @@ div#inside {
margin-left: 4px;
margin-right: 11px;
}
#l_holder.dead {
background-color: black;
}
#l_holder.boring {
background-color: darkred;
}
#header h1 {
font-size: 11pt;

View file

@ -1,6 +1,9 @@
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :authenticate_user
before_filter :increase_traffic_counter
TRAFFIC_DECREMENTER = 0.1
def authenticate_user
if session[:u]
@ -10,6 +13,23 @@ class ApplicationController < ActionController::Base
true
end
def increase_traffic_counter
@traffic = 1.0
Keystore.transaction do
date = (Keystore.value_for("traffic:date") || Time.now.to_i)
traffic = (Keystore.incremented_value_for("traffic:hits", 0).
to_f / 100.0) + 1.0
# every second, decrement traffic by some amount
@traffic = [ 1.0, traffic.to_f -
((Time.now.to_i - date) * TRAFFIC_DECREMENTER) ].max
Keystore.put("traffic:date", Time.now.to_i)
Keystore.put("traffic:hits", (@traffic * 100.0).to_i)
end
end
def require_logged_in_user
if @user
true

View file

@ -1,6 +1,7 @@
<div id="header">
<div id="headerleft">
<a id="l_holder" class="boring" href="/"></a>
<a id="l_holder" style="background-color: #<%= sprintf("%02x%02x%02x",
[ 255, (@traffic * 5).floor + 50.0 ].min, 0, 0) %>;" href="/"></a>
<% links = { "/" => "Home", "/newest" => "Newest Stories" } %>
<% if @user %>

View file

@ -0,0 +1,8 @@
class KeystoreBigint < ActiveRecord::Migration
def up
execute("ALTER TABLE keystores CHANGE value value BIGINT")
end
def down
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120712174445) do
ActiveRecord::Schema.define(:version => 20120816203248) do
create_table "comments", :force => true do |t|
t.datetime "created_at", :null => false
@ -46,7 +46,7 @@ ActiveRecord::Schema.define(:version => 20120712174445) do
create_table "keystores", :id => false, :force => true do |t|
t.string "key", :limit => 50, :default => "", :null => false
t.integer "value", :null => false
t.integer "value", :limit => 8
end
add_index "keystores", ["key"], :name => "key", :unique => true