Permalink
Browse files

City page updated to include 31 day trends aggregated from phrases.

  • Loading branch information...
1 parent b5b03fa commit 60741c6466ec08513eb9bd61e68123a2ae65a5a0 @paulmwatson committed May 1, 2009
@@ -5,18 +5,39 @@ def index
@phrases = Phrase.find :all, :order => 'title'
@location = get_location(@city.name, @city.country)
- @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
+ # 30 day range centered on the current date
+ @range = 30
+ @range_center = params[:date] ? Date.parse(params[:date]) : Date.today
+ @range_start = @range_center - (@range / 2)
+ @range_end = @range_center + (@range / 2)
+ @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 => @city.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")}
+
+ @mentions = Mention.find :all, :select => "id, message, DATE(mentioned_at) as mentioned_at_date, mentioned_at, substring_index(mentioner, ' (', 1) as mentioner_stripped, mentioner, link, exact_location, phrase_id", :group => 'mentioned_at_date, mentioner_stripped', :conditions => {:city_id => @city.id, :mentioned_at => ("#{@range_start.year}-#{@range_start.month.to_s.rjust(2, '0')}-#{@range_start.day.to_s.rjust(2, '0')} 00:00".."#{@range_end.year}-#{@range_end.month.to_s.rjust(2, '0')}-#{@range_end.day.to_s.rjust(2, '0')} 23:59")}, :order => :mentioned_at
+ @graph_max = Mention.find(:first, :conditions => {:city_id => @city.id}, :group => 'mentioned_at_date, mentioner_stripped', :select => "substring_index(mentioner, ' (', 1) as mentioner_stripped, DATE(mentioned_at) as mentioned_at_date, count(id) as graph_max", :order => 'graph_max DESC').graph_max
+
@trend = Hash.new()
@phrases.each do |phrase|
- @trend[phrase.id] = Array.new(24,0)
+ @trend[phrase.id] = Array.new((@range +1),0)
end
+ @thirty_day_trend = Array.new((@range + 1),0)
+ @hour_trend = Array.new(24,0)
+
@mentions.each do |mention|
- begin
- @trend[mention.phrase_id][mention.mentioned_at.hour] = @trend[mention.phrase_id][mention.mentioned_at.hour] + 1
- rescue
+ #@trend[mention.phrase_id][mention.mentioned_at.hour] = @trend[mention.phrase_id][mention.mentioned_at.hour] + 1
+
+ published_date = mention.mentioned_at
+ # Work out day difference of mention and update array
+ index = ((published_date.to_date - @range_end) + @range).to_i
+ @thirty_day_trend[index] = @thirty_day_trend[index] + 1
+
+ if (mention.phrase_id != -1)
+ @trend[mention.phrase_id][index] = @trend[mention.phrase_id][index] + 1
+ end
+
+ if (published_date.to_date == @range_center)
+ @hour_trend[published_date.hour] = @hour_trend[published_date.hour] + 1
end
end
end
@@ -1,4 +1,9 @@
<div class="intro info">
+ <div class="note">
+ <p><strong>Our system has been overwhelmed!</strong></p>
+ <p>SickCity measures conversation about disease in your city, under the presumption that conversations about sickness track closely to actual instances of sickness. And they do, we think -- except occasionally when it does not! And right now, with swine flu on everybody's minds and in their tweets, they clearly do not. The system is awash this week in information that does not accurately correspond to the state of health of the areas it purports to be monitoring.</p>
+ <p>This is by no means a failure of our system - it's actually a chance for improvement. We're recalibrating things and should have the kinks worked out soon. Once that happens, SickCity should be stronger and better than ever.</p>
+ </div>
<p><a href="http://diycity.org"><img src="/images/diycity.png" alt="DIYCity" /></a></p>
<p>&nbsp;</p>
<p>Realtime disease detection for your city from messages on <em>Twitter</em> (and soon <em>Facebook</em>)</p>
@@ -5,12 +5,10 @@
<div class="center">&nbsp;</div>
<div id="thirty_day_trend" class="trend"></div>
<div class="navbar second">
- <% if ((@range_center - 1) >= (Date.today - 60)) %>
- <a class="back" href="/<%= params[:country] %>/<%= params[:city] %>/<%= params[:phrase] %>/<%= @range_center - 1 %>"><%= (@range_center - 1).strftime('%d %b') %><img src="/images/bkr.png" /></a>
- <% end %>
-
- <a class="forward" href="/<%= params[:country] %>/<%= params[:city] %>/<%= params[:phrase] %>/<%= @range_center + 1 %>"><img src="/images/bk2l.png" /><%= (@range_center + 1).strftime('%d %b') %></a>
-
+<% if ((@range_center - 1) >= (Date.today - 60)) %>
+ <a class="back" href="/<%= params[:country] %>/<%= params[:city] %>/phrase/<%= params[:phrase] %>/<%= @range_center - 1 %>"><%= (@range_center - 1).strftime('%d %b') %><img src="/images/bkr.png" /></a>
+<% end %>
+ <a class="forward" href="/<%= params[:country] %>/<%= params[:city] %>/phrase/<%= params[:phrase] %>/<%= @range_center + 1 %>"><img src="/images/bk2l.png" /><%= (@range_center + 1).strftime('%d %b') %></a>
<h1 style="font-size: 15px"><%= @twenty_four_hour_count.length %> mentions of "<%= @phrase.title %>" on <%= @range_center.strftime('%B %d') %></h1>
</div>
<div id="twenty_four_hour_trend" class="trend"></div>
@@ -1,22 +1,37 @@
-<div class="intro info">
+<div class="intro alt">
<h2><%= @city.name %></h2>
- <p>Realtime disease detection <a href="http://diycity.org/sickcity-realtime-disease-detection-your-city">by DIYCity</a></p>
- <%= "<p class=\"notice\">#{flash[:notice]}</p>" if flash[:notice] %>
+ <p><%= @mentions.length %> sick tweets over <%= @range + 1 %> days:</p>
</div>
+<div class="center">&nbsp;</div>
+<div id="thirty_day_trend" class="trend"></div>
+<div class="navbar second">
+<% if ((@range_center - 1) >= (Date.today - 60)) %>
+ <a class="back" href="/<%= params[:country] %>/<%= params[:city] %>/<%= @range_center - 1 %>"><%= (@range_center - 1).strftime('%d %b') %><img src="/images/bkr.png" /></a>
+<% end %>
+ <a class="forward" href="/<%= params[:country] %>/<%= params[:city] %>/<%= @range_center + 1 %>"><img src="/images/bk2l.png" /><%= (@range_center + 1).strftime('%d %b') %></a>
+ <h1 style="font-size: 15px"><%= @twenty_four_hour_count.length %> sick tweets on <%= @range_center.strftime('%B %d') %></h1>
+</div>
+<div id="twenty_four_hour_trend" class="trend"></div>
<ul class="list">
<% @phrases.each do |phrase|%>
- <li><a href="/<%= params[:country] %>/<%= params[:city] %>/<%= phrase.title %>"><span class="city">
+ <li><a href="/<%= params[:country] %>/<%= params[:city] %>/phrase/<%= phrase.title %>"><span class="city">
<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>
+ <span id="phrase_graph_<%= phrase.id %>" class="tiny_graph phrase_trend"><% @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>
+
<div class="navbar">
<a class="back home" href="/<%= params[:country] %>/">Cities<img src="/images/bkr.png" /></a>
<a class="forward button home" href="/"><img src="/images/bk2l.png" /><img class="buttonimage" src="/images/logosmall.png" /></a>
<h1><%= params[:city] %></h1>
</div>
<script type="text/javascript">
- $('.tiny_graph').sparkline();
-</script>
+ $('.phrase_trend').sparkline('html', {type:'bar', barColor: '#900'});
+ $('#thirty_day_trend').sparkline([<% @thirty_day_trend.each do |day| %><%= day %>,<% end %>], {type: 'bar',height: '50px', barSpacing: 2, barWidth: 8, barColor: '#000000', chartRangeMax: <%= @graph_max %>, chartRangeMin: 0});
+ var trend_width = 490;
+ $('#twenty_four_hour_trend').sparkline([<% @hour_trend.each do |hour| %><%= hour %>,<% end %>], {height: '50px', width: trend_width + 'px', fillColor: '#c6dddc', lineColor: '#333'});
+</script>
+<p class="twitterpower"><a href="http://twitter.com"><img src="/images/powered-by-twitter-sig.gif" /></a><p>
View
@@ -12,9 +12,10 @@
map.connet '/about', :controller => 'site', :action => 'about'
map.connect '/:country', :controller => 'cities'
map.connect '/:country/:city', :controller => 'phrases'
+ map.connect '/:country/:city/:date', :controller => 'phrases'
- map.connect '/:country/:city/:phrase', :controller => 'mentions'
- map.connect '/:country/:city/:phrase/:date', :controller => 'mentions'
+ map.connect '/:country/:city/phrase/:phrase', :controller => 'mentions'
+ map.connect '/:country/:city/phrase/:phrase/:date', :controller => 'mentions'
map.resources :cities
@@ -388,3 +388,18 @@ p.notice
padding: 5px;
-moz-border-radius: 3px;
}
+
+.note
+{
+ text-align: left;
+ background-color: #fff;
+ -moz-border-radius: 5px;
+ color: #c00;
+ padding: 10px;
+ margin-bottom: 20px;
+}
+
+.note p
+{
+ margin: 10px 0;
+}

0 comments on commit 60741c6

Please sign in to comment.