implement traffic counter that adjusts redness of lobsters logo
This commit is contained in:
parent
342ede4d4c
commit
43444f179b
|
@ -239,12 +239,6 @@ div#inside {
|
||||||
margin-left: 4px;
|
margin-left: 4px;
|
||||||
margin-right: 11px;
|
margin-right: 11px;
|
||||||
}
|
}
|
||||||
#l_holder.dead {
|
|
||||||
background-color: black;
|
|
||||||
}
|
|
||||||
#l_holder.boring {
|
|
||||||
background-color: darkred;
|
|
||||||
}
|
|
||||||
|
|
||||||
#header h1 {
|
#header h1 {
|
||||||
font-size: 11pt;
|
font-size: 11pt;
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
class ApplicationController < ActionController::Base
|
class ApplicationController < ActionController::Base
|
||||||
protect_from_forgery
|
protect_from_forgery
|
||||||
before_filter :authenticate_user
|
before_filter :authenticate_user
|
||||||
|
before_filter :increase_traffic_counter
|
||||||
|
|
||||||
|
TRAFFIC_DECREMENTER = 0.1
|
||||||
|
|
||||||
def authenticate_user
|
def authenticate_user
|
||||||
if session[:u]
|
if session[:u]
|
||||||
|
@ -10,6 +13,23 @@ class ApplicationController < ActionController::Base
|
||||||
true
|
true
|
||||||
end
|
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
|
def require_logged_in_user
|
||||||
if @user
|
if @user
|
||||||
true
|
true
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
<div id="header">
|
<div id="header">
|
||||||
<div id="headerleft">
|
<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" } %>
|
<% links = { "/" => "Home", "/newest" => "Newest Stories" } %>
|
||||||
<% if @user %>
|
<% if @user %>
|
||||||
|
|
8
db/migrate/20120816203248_keystore_bigint.rb
Normal file
8
db/migrate/20120816203248_keystore_bigint.rb
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
class KeystoreBigint < ActiveRecord::Migration
|
||||||
|
def up
|
||||||
|
execute("ALTER TABLE keystores CHANGE value value BIGINT")
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
end
|
||||||
|
end
|
|
@ -11,7 +11,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended to check this file into your version control system.
|
# 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|
|
create_table "comments", :force => true do |t|
|
||||||
t.datetime "created_at", :null => false
|
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|
|
create_table "keystores", :id => false, :force => true do |t|
|
||||||
t.string "key", :limit => 50, :default => "", :null => false
|
t.string "key", :limit => 50, :default => "", :null => false
|
||||||
t.integer "value", :null => false
|
t.integer "value", :limit => 8
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index "keystores", ["key"], :name => "key", :unique => true
|
add_index "keystores", ["key"], :name => "key", :unique => true
|
||||||
|
|
Loading…
Reference in a new issue