require 'rubygems' require 'sequel' require 'csv' require 'sparklines' require 'markaby' # require 'ruby-debug' class Trend attr_reader :name, :dates # attr_accessor :trending_dates # will be an array of Date objects def initialize(name, dates) @name = name # @trending_dates = [Date.parse(trending_date)] @dates = dates # should be an array of dates puts "initializing #{name}" end def to_s "Trend: \"#{@name}\" seen on #{ @dates.join(" ") }\n" end def also_seen_on(dates) @dates << Date.parse(dates) end def sparkline #TODO create array of occurences by week and build sparkline end def whiskerplot_to_file(filename, start_date, end_date) whiskers = Array.new( end_date - start_date, 0 ) @dates.each { |occurence_date| whiskers[ occurence_date - start_date ] = 1 } puts "#{@name}:#{whiskers}" Sparklines.plot_to_file(filename, whiskers, :type => 'whisker') end end class Trendalizer attr_reader :names_and_dates_hash, :sorted_trends def initialize(unsorted_hash_of_trend_strings) @names_and_dates_hash = Hash.new @sorted_trends = [] puts "reading trends database, hashing identical trends..." unsorted_hash_of_trend_strings.each do |row| trend_symbol = row[:trend].intern if @names_and_dates_hash[trend_symbol] @names_and_dates_hash[trend_symbol] << Date.parse( row[:trending_date] ) # push the Date onto the array else @names_and_dates_hash[trend_symbol] = [ Date.parse( row[:trending_date] ) ] # initializes array with Date object end end puts "done. hash of names and dates size: #{names_and_dates_hash.size}. sorting..." sorted_names_and_dates = names_and_dates_hash.sort { |a,b| b[1].size <=> a[1].size } # sorted_names_and_dates.each {|a,b| puts "#{a}: #{b.join(" ")}"} # prints trend: dates puts "done. initializing array of Trend objects..." sorted_names_and_dates.each { |name, dates| @sorted_trends << Trend.new(name, dates) } end def to_s @sorted_trends.to_s end def histogram_to_csv(filename) puts "writing trends to #{filename}..." csv_writer = CSV.open( filename, 'w' ) @sorted_trends.each { |trend| csv_writer << [trend.name, trend.dates.size] } csv_writer.close end end DB = Sequel.sqlite('trends.db') unless DB.table_exists?(:trends) puts "initializing new trends table in database..." DB.create_table :trends do primary_key :id varchar :trend varchar :query varchar :trending_date boolean :searched, :default => false index [:trend, :trending_date], :unique => true end end trends_db = DB[:trends] six_months = Trendalizer.new(trends_db) # puts "printing Trendalizer: #{six_months}" # six_months.histogram_to_csv('test.csv') sd = Date.parse("1 jan 2009") ed = Date.parse("5 jun 2009") # 100.times { |n| six_months.sorted_trends[n].whiskerplot_to_file("whiskerplots/#{n}_#{six_months.sorted_trends[n].name}.png", sd, ed) } mab = Markaby::Builder.new mab.html do head do title "twitter trends from 1 Jan 2009 to 5 Jun 2009" style :type => "text/css" do %[ #container{width:500; text-align:center; margin:auto;} tr{height:14px; font-size:10px;} ] end end body do div :id => 'container' do table do 100.times do |n| tr do six_months.sorted_trends[n].whiskerplot_to_file("whiskerplots/#{n}_#{six_months.sorted_trends[n].name}.png".gsub('#', ''), sd, ed) td "#{six_months.sorted_trends[n].name}" td do img :src => "whiskerplots/#{n}_#{six_months.sorted_trends[n].name}.png".gsub('#', '') end end end end end end end File.open("sparklines.html", 'w') { |f| f.puts mab }