Permalink
Browse files

Up/down trends with sickness quotients and counts

  • Loading branch information...
1 parent 40d9d35 commit dd15d01affca8bbfcc4cac806a39ac290d4523d6 @paulmwatson committed Apr 23, 2009
@@ -5,7 +5,9 @@ def index
@cities = City.find :all, :order => 'country, name', :conditions => {:hidden => false}
@topten_cities = Mention.find :all, :group => 'city_id', :select => 'count(id) as city_mentions, city_id', :order => 'city_mentions DESC', :limit => 20
- @mentions = Mention.find :all, :conditions => {:mentioned_at => ("#{Date.today.year}-#{Date.today.month.to_s.rjust(2, '0')}-#{Date.today.day.to_s.rjust(2, '0')} 00:00".."#{Date.today.year}-#{Date.today.month.to_s.rjust(2, '0')}-#{Date.today.day.to_s.rjust(2, '0')} 23:59")}, :order => :mentioned_at
+ @working_date = Date.today
+ puts @working_date
+ @mentions = Mention.find :all, :conditions => {:mentioned_at => ("#{@working_date.year}-#{@working_date.month.to_s.rjust(2, '0')}-#{@working_date.day.to_s.rjust(2, '0')} 00:00".."#{@working_date.year}-#{@working_date.month.to_s.rjust(2, '0')}-#{@working_date.day.to_s.rjust(2, '0')} 23:59")}, :order => :mentioned_at
@trend = Hash.new()
@cities.each do |city|
View
@@ -1,4 +1,43 @@
class City < ActiveRecord::Base
has_many :mentions
has_many :histories
+ has_many :counts
+
+ # Count of total mentions for the city for a day
+ def mention_count(date)
+ Count.find(:first, :conditions => {:city_id => self.id, :date => date}).count
+ end
+
+ # Latest count of total mentions for the city for a day
+ def last_mention_count
+ Count.find(:first, :conditions => {:city_id => self.id}, :order => 'date DESC').count
+ end
+
+ # Latest count of SICK mentions for the city for the day
+ def last_sick_mention_count
+ @range_center = Date.today
+ twenty_four_hour_count = Mention.find(:all, :select => "DATE(mentioned_at) as mentioned_at_date, mentioned_at, substring_index(mentioner, ' (', 1) as mentioner_stripped, mentioner, link, exact_location", :group => 'mentioner_stripped, mentioned_at_date', :conditions => {:city_id => self.id, :mentioned_at => ("#{@range_center.year}-#{@range_center.month.to_s.rjust(2, '0')}-#{@range_center.day.to_s.rjust(2, '0')} 00:00".."#{@range_center.year}-#{@range_center.month.to_s.rjust(2, '0')}-#{@range_center.day.to_s.rjust(2, '0')} 23:59")})
+ twenty_four_hour_count.length
+ end
+
+ # Count of SICK mentions for the city for today
+ def sick_mention_count(date)
+ @range_center = date
+ twenty_four_hour_count = Mention.find(:all, :select => "DATE(mentioned_at) as mentioned_at_date, mentioned_at, substring_index(mentioner, ' (', 1) as mentioner_stripped, mentioner, link, exact_location", :group => 'mentioner_stripped, mentioned_at_date', :conditions => {:city_id => self.id, :mentioned_at => ("#{@range_center.year}-#{@range_center.month.to_s.rjust(2, '0')}-#{@range_center.day.to_s.rjust(2, '0')} 00:00".."#{@range_center.year}-#{@range_center.month.to_s.rjust(2, '0')}-#{@range_center.day.to_s.rjust(2, '0')} 23:59")})
+ twenty_four_hour_count.length
+ end
+
+ def last_sickness_quotient
+ self.sickness_quotient(Date.today)
+ end
+
+ def sickness_quotient(date)
+ ((Float(self.sick_mention_count(date)) / Float(self.mention_count(date))) * 100)
+ end
+
+ # Must not compute on today as data won't all be there
+ # So only use dates that are yesterday or older
+ def trending_up(date)
+ self.sickness_quotient(date).round(2) > self.sickness_quotient(date-1).round(2)
+ end
end
View
@@ -0,0 +1,3 @@
+class Count < ActiveRecord::Base
+ belongs_to :city
+end
@@ -6,14 +6,18 @@
<p<span class="phrase">flu</span>, <span class="phrase">food poisoning</span>, <span class="phrase">headcolds</span>, <span class="phrase">chicken pox</span> &amp; <span class="phrase">sore throats</span></p>
</div>
<h1>SickCity's Ten Sickest Cities*</h1>
-<p style="font-size: 11px; text-align: center">*Some cities are still coming online</p>
+<p style="font-size: 11px; text-align: center">* Up/down trends are based on past two days data</p>
<ol class="list">
<% i = 0 %>
<% @topten_cities.each do |mention|%>
<%- city = mention.city -%>
<%- if !city.hidden %>
<% i = i + 1%>
- <li><a href="/<%= city.country %>/<%= city.name.gsub('.','%2E') %>"><span class="city"><span class="toptencount"><%= "%02d" % i %></span> <%= city.name %></span> <span id="city_graph_<%= city.id %>" class="tiny_graph"><% @trend[city.id].each do |hour| %><%= hour %>,<% end %></span></a></li>
+ <li><a href="/<%= city.country %>/<%= city.name.gsub('.','%2E') %>">
+ <span class="city">
+ <span class="trending_<%= city.trending_up(@working_date-1) ? 'up' : 'down' %>"><%= city.trending_up(@working_date-1) ? 'Up' : 'Down' %></span>
+ &nbsp;<%= city.name %></span>
+ <span id="city_graph_<%= city.id %>" class="tiny_graph"><% @trend[city.id].each do |hour| %><%= hour %>,<% end %></span></a></li>
<% end %>
<% break if i > 9 %>
<% end %>
@@ -0,0 +1,12 @@
+class CreateCounts < ActiveRecord::Migration
+ def self.up
+ create_table :counts do |t|
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :counts
+ end
+end
@@ -298,7 +298,35 @@ ol.list li, ul.list li
.city
{
display: block;
- width: 200px;
+}
+
+.percent_sick
+{
+ font-size: smaller;
+}
+
+.trending_down
+{
+ background-color: #6c3;
+ -moz-border-radius: 5px;
+ font-size: 11px;
+ padding: 3px 5px 2px 5px;
+ text-transform: uppercase;
+ color: #fff;
+ width: 5em;
+ vertical-align: 2px;
+}
+
+.trending_up
+{
+ vertical-align: 2px;
+ background-color: #c00;
+ -moz-border-radius: 5px;
+ font-size: 11px;
+ padding: 3px 15px 2px 15px;
+ text-transform: uppercase;
+ color: #fff;
+ width: 5em;
}
.wrapper
View
@@ -0,0 +1,7 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+# one:
+# column: value
+#
+# two:
+# column: value
View
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class CountTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end

0 comments on commit dd15d01

Please sign in to comment.