Permalink
Browse files

Trending indicator for phrases and cities.

  • Loading branch information...
1 parent dd15d01 commit 7056eb0b0dce8e92ff286311ff38346e26e0c118 @paulmwatson committed Apr 24, 2009
@@ -8,7 +8,8 @@ def index
@topten_cities = Mention.find :all, :group => 'city_id', :select => 'count(id) as city_mentions, city_id', :order => 'city_mentions DESC', :limit => 20
@location = get_location(params[:country])
- @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
+ @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|
@@ -6,7 +6,6 @@ def index
@topten_cities = Mention.find :all, :group => 'city_id', :select => 'count(id) as city_mentions, city_id', :order => 'city_mentions DESC', :limit => 20
@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()
@@ -5,7 +5,8 @@ def index
@phrases = Phrase.find :all, :order => 'title'
@location = get_location(@city.name, @city.country)
- @mentions = Mention.find :all, :conditions => {:city_id => @city.id, :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
+ @mentions = Mention.find :all, :conditions => {:city_id => @city.id, :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()
@phrases.each do |phrase|
View
@@ -26,6 +26,13 @@ def sick_mention_count(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
+
def last_sickness_quotient
self.sickness_quotient(Date.today)
@@ -35,9 +42,17 @@ 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)
+ 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
+
+ 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)
+ end
end
@@ -4,7 +4,14 @@
</div>
<ul class="list">
<% @cities.each do |city|%>
- <li><a href="/<%= city.country %>/<%= city.name.gsub('.','%2E') %>"><span class="city"><%= 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">
+ <%- 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>
<% end %>
</ul>
<a id="map" href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=<%= @location.latitude %>,<%= @location.longitude %>"><img src="http://maps.google.com/staticmap?center=<%= @location.latitude %>,<%= @location.longitude %>&zoom=3&size=500x213&maptype=mobile&key=<%= GOOGLE_API_KEY %>&sensor=false" alt="Map" /></a>
@@ -15,7 +15,12 @@
<% i = i + 1%>
<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>
+ <%- 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>
<% end %>
@@ -5,7 +5,15 @@
</div>
<ul class="list">
<% @phrases.each do |phrase|%>
- <li><a href="/<%= params[:country] %>/<%= params[:city] %>/<%= phrase.title %>"><span class="city"><%= phrase.title %></span> <span id="phrase_graph_<%= phrase.id %>" class="tiny_graph"><% @trend[phrase.id].each do |hour| %><%= hour %>,<% end %></span></a></li>
+ <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>
+ <%= phrase.title %></span>
+ <span id="phrase_graph_<%= phrase.id %>" class="tiny_graph"><% @trend[phrase.id].each do |hour| %><%= hour %>,<% end %></span></a></li>
<% end %>
</ul>
<a id="map" href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=<%= @location.latitude %>,<%= @location.longitude %>"><img src="http://maps.google.com/staticmap?center=<%= @location.latitude %>,<%= @location.longitude %>&zoom=11&size=500x213&maptype=mobile&key=<%= GOOGLE_API_KEY %>&sensor=false" alt="Map" /></a>

0 comments on commit 7056eb0

Please sign in to comment.