Permalink
Browse files

Merge branch 'master' of git@github.com:paulmwatson/sickcity

  • Loading branch information...
2 parents f336f37 + 40dc143 commit f3fc7e2965a27b8683a6e1f4f0355336b1c56e76 @aditya aditya committed Apr 29, 2009
@@ -2,22 +2,37 @@ class CountriesController < ApplicationController
layout 'base'
def index
@countries = City.find :all, :group => 'country', :order => 'country'
- @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
+ @cities = City.find :all, :order => 'country, name', :conditions => {:hidden => false}, :limit => 10
+
+ sql = 'select city_id from mentions group by city_id having count(id) > 100'
+ cities_with_more_than_1000_tweets = ActiveRecord::Base.connection.execute(sql)
+ city_ids = ''
+ cities_with_more_than_1000_tweets.each do |result|
+ city_ids = city_ids + "#{result},"
+ end
+ city_ids = city_ids[0..city_ids.length-2]
+
+ top_ten_stats = Stat.find(:all, :conditions => 'city_id in (' + city_ids + ')', :order => 'quotient desc', :limit => 10)
+
+ @top_ten_cities = []
+ top_ten_stats.each do |stat|
+ @top_ten_cities << City.find(stat.city_id)
+ end
@working_date = Date.today
@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|
- @trend[city.id] = Array.new(24,0)
+ @trend_today = Hash.new()
+ @top_ten_cities.each do |city|
+ @trend_today[city.id] = Array.new(24,0)
end
@mentions.each do |mention|
begin
- @trend[mention.city_id][mention.mentioned_at.hour] = @trend[mention.city_id][mention.mentioned_at.hour] + 1
+ @trend_today[mention.city_id][mention.mentioned_at.hour] = @trend_today[mention.city_id][mention.mentioned_at.hour] + 1
rescue
end
end
end
-end
+
+end
@@ -14,7 +14,10 @@ def index
end
@mentions.each do |mention|
- @trend[mention.phrase_id][mention.mentioned_at.hour] = @trend[mention.phrase_id][mention.mentioned_at.hour] + 1
+ begin
+ @trend[mention.phrase_id][mention.mentioned_at.hour] = @trend[mention.phrase_id][mention.mentioned_at.hour] + 1
+ rescue
+ end
end
end
View
@@ -0,0 +1,2 @@
+class AllTweet < ActiveRecord::Base
+end
View
@@ -3,56 +3,25 @@ class City < ActiveRecord::Base
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 sick_mention_count_by_phrase(date, phrase)
- @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 => {:phrase_id => phrase.id, :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
-
+ # Last 6 hours today compared to same 6 hours yesterday
+ def trending_up
+ today_from = Time.now - ((60 * 60) * 6)
+ today_to = Time.now
- 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
-
- def sickness_quotient_by_phrase(date, phrase)
- ((Float(self.sick_mention_count_by_phrase(date, phrase)) / Float(self.mention_count(date))) * 100)
+ yesterday_from = (Time.now - (60 * 60 * 24)) - ((60 * 60) * 6)
+ yesterday_to = (Time.now - (60 * 60 * 24))
+
+ today_mentions = Mention.find(:all, :conditions => {:city_id => self.id, :mentioned_at => (time_string_builder(today_from)..time_string_builder(today_to))})
+ yesterday_mentions = Mention.find(:all, :conditions => {:city_id => self.id, :mentioned_at => (time_string_builder(yesterday_from)..time_string_builder(yesterday_to))})
+
+ today_mentions.length > yesterday_mentions.length
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)
+ def time_string_builder(time)
+ "#{time.year}-#{time.month.to_s.rjust(2, '0')}-#{time.day.to_s.rjust(2, '0')} #{time.hour.to_s.rjust(2, '0')}:#{time.min.to_s.rjust(2, '0')}"
end
- def trending_up_by_phrase(date, phrase)
- self.sickness_quotient_by_phrase(date, phrase).round(2) > self.sickness_quotient_by_phrase(date-1, phrase).round(2)
+ def stat
+ Stat.find_by_city_id(self.id)
end
end
View
@@ -1,4 +1,22 @@
class Phrase < ActiveRecord::Base
has_many :mentions
has_many :histories
+
+ def trending_up_by_city(city_id)
+ today_from = Time.now - ((60 * 60) * 6)
+ today_to = Time.now
+
+ yesterday_from = (Time.now - (60 * 60 * 24)) - ((60 * 60) * 6)
+ yesterday_to = (Time.now - (60 * 60 * 24))
+
+ today_mentions = Mention.find(:all, :conditions => {:phrase_id => self.id, :city_id => city_id, :mentioned_at => (time_string_builder(today_from)..time_string_builder(today_to))})
+ yesterday_mentions = Mention.find(:all, :conditions => {:phrase_id => self.id, :city_id => city_id, :mentioned_at => (time_string_builder(yesterday_from)..time_string_builder(yesterday_to))})
+
+ today_mentions.length > yesterday_mentions.length
+ end
+
+ def time_string_builder(time)
+ "#{time.year}-#{time.month.to_s.rjust(2, '0')}-#{time.day.to_s.rjust(2, '0')} #{time.hour.to_s.rjust(2, '0')}:#{time.min.to_s.rjust(2, '0')}"
+ end
+
end
View
@@ -0,0 +1,3 @@
+class Stat < ActiveRecord::Base
+ belongs_to :city
+end
@@ -5,12 +5,7 @@
<ul class="list">
<% @cities.each do |city|%>
<li><a href="/<%= city.country %>/<%= city.name.gsub('.','%2E') %>"><span class="city">
- <%- begin
- trending_up = city.trending_up(@working_date-1)
- rescue
- trending_up = false
- end -%>
- <span class="trending_<%= trending_up ? 'up' : 'down' %>"><%= trending_up ? 'Up' : 'Down' %></span>
+ <span class="trending_<%= city.trending_up ? 'up' : 'down' %>"><%= city.trending_up ? '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 %>
</ul>
@@ -5,26 +5,16 @@
<p>&nbsp;</p>
<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">* Up/down trends are based on past two days data</p>
+<h1>SickCity's Ten Sickest Cities</h1>
<ol class="list">
- <% i = 0 %>
- <% @topten_cities.each do |mention|%>
- <%- city = mention.city -%>
+ <% @top_ten_cities.each do |city|%>
<%- if !city.hidden %>
- <% i = i + 1%>
<li><a href="/<%= city.country %>/<%= city.name.gsub('.','%2E') %>">
<span class="city">
- <%- begin
- trending_up = city.trending_up(@working_date-1)
- rescue
- trending_up = false
- end -%>
- <span class="trending_<%= trending_up ? 'up' : 'down' %>"><%= trending_up ? '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>
+ <span class="trending_<%= city.trending_up ? 'up' : 'down' %>"><%= city.trending_up ? 'Up' : 'Down' %></span>
+ &nbsp;<%= city.name %></span>
+ <span id="city_graph_<%= city.id %>_today" class="tiny_graph today"><% @trend_today[city.id].each do |hour| %><%= hour %>,<% end %></span></a></li>
<% end %>
- <% break if i > 9 %>
<% end %>
</ol>
<h1>All Countries</h1>
@@ -40,5 +30,5 @@
<a class="forward button" href="/about/"><img src="/images/bk2l.png" />About</a>
</div>
<script type="text/javascript">
- $('.tiny_graph').sparkline();
+ $('.tiny_graph.today').sparkline('html', {defaultPixelsPerValue:5});
</script>
@@ -6,12 +6,7 @@
<ul class="list">
<% @phrases.each do |phrase|%>
<li><a href="/<%= params[:country] %>/<%= params[:city] %>/<%= phrase.title %>"><span class="city">
- <%- begin
- trending_up = @city.trending_up_by_phrase(@working_date-1, phrase)
- rescue
- trending_up = false
- end -%>
- <span class="trending_<%= trending_up ? 'up' : 'down' %>"><%= trending_up ? 'Up' : 'Down' %></span>
+ <span class="trending_<%= phrase.trending_up_by_city(@city.id) ? 'up' : 'down' %>"><%= phrase.trending_up_by_city(@city.id) ? 'Up' : 'Down' %></span>
<%= phrase.title %></span>
<span id="phrase_graph_<%= phrase.id %>" class="tiny_graph"><% @trend[phrase.id].each do |hour| %><%= hour %>,<% end %></span></a></li>
<% end %>
@@ -0,0 +1,12 @@
+class CreateAllTweets < ActiveRecord::Migration
+ def self.up
+ create_table :all_tweets do |t|
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :all_tweets
+ end
+end
@@ -0,0 +1,12 @@
+class CreateStats < ActiveRecord::Migration
+ def self.up
+ create_table :stats do |t|
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :stats
+ end
+end
@@ -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,7 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+# one:
+# column: value
+#
+# two:
+# column: value
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class AllTweetTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
View
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class StatTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end

0 comments on commit f3fc7e2

Please sign in to comment.