#!/usr/bin/perl # barchart.pl - create a histogram illustrating number of tweets in a given time interval # Eric Lease Morgan # February 17, 2011 - first cut; needs command-line arguments # configure use constant STARTDATE => '2011-02-07 06:20:00'; use constant ENDDATE => '2011-02-13 17:24:34'; use constant DSN => 'dbi:mysql:c4l11'; use constant USERNAME => '---'; use constant PASSWORD => '---'; use constant INTERVAL => 'minutes'; use constant LENGTH => 20; # require use DateTime; use DateTime::Format::MySQL; use DBI; use strict; use Text::BarGraph; # initalize my $dbh = DBI->connect( DSN, USERNAME, PASSWORD ); my $startdate = DateTime::Format::MySQL->parse_datetime( STARTDATE ); my $enddate = DateTime::Format::MySQL->parse_datetime( ENDDATE ); my $interval = INTERVAL; # search & retrieve my %datetimes = (); while ( DateTime::Format::MySQL->format_datetime( $startdate ) le DateTime::Format::MySQL->format_datetime( $enddate ) ) { my $start = DateTime::Format::MySQL->format_datetime( $startdate ); my $end = DateTime::Format::MySQL->format_datetime( $startdate->add( $interval => LENGTH ) ); my $sql = "SELECT COUNT( tweet_id ) FROM tweets WHERE datetime >= '$start' AND datetime <= '$end'"; $datetimes{ $start } = ( $dbh->selectrow_array( $sql ) )[ 0 ]; $startdate = DateTime::Format::MySQL->parse_datetime( $end ); } # clean up $dbh->disconnect; # graph my $graph = Text::BarGraph->new(); $graph->enable_color( 0 ); print $graph->graph( \%datetimes ); # done exit;