--- trunk/irc-logger.pl 2006/06/24 22:57:26 34 +++ trunk/irc-logger.pl 2006/06/25 00:10:13 35 @@ -54,6 +54,7 @@ use CGI::Simple; use HTML::TagCloud; use POSIX qw/strftime/; +use HTML::CalendarMonthSimple; my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; @@ -122,8 +123,6 @@ sub get_from_log { my $args = {@_}; - $args->{limit} ||= 10; - $args->{fmt} ||= { date => '[%s] ', time => '{%s} ', @@ -156,8 +155,9 @@ $sql .= " where message ilike ? or nick ilike ? " if ($args->{search}); $sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} }); + $sql .= " where date(time) = ? " if ($args->{date}); $sql .= " order by log.time desc"; - $sql .= " limit " . $args->{limit}; + $sql .= " limit " . $args->{limit} if ($args->{limit}); my $sth = $dbh->prepare( $sql ); if (my $search = $args->{search}) { @@ -168,6 +168,9 @@ } elsif (my $tag = $args->{tag}) { $sth->execute(); warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n"; + } elsif (my $date = $args->{date}) { + $sth->execute($date); + warn "found ", $sth->rows, " messages for date $date ", $context || '', "\n"; } else { $sth->execute(); } @@ -212,19 +215,29 @@ } } + # sprintf which can take coderef as first parametar + sub cr_sprintf { + my $fmt = shift || return; + if (ref($fmt) eq 'CODE') { + $fmt->(@_); + } else { + sprintf($fmt, @_); + } + } + foreach my $row (@rows) { $row->{time} =~ s#\.\d+##; my $msg = ''; - $msg = sprintf($args->{fmt}->{date}, $row->{date}) . ' ' if ($last_row->{date} ne $row->{date}); + $msg = cr_sprintf($args->{fmt}->{date}, $row->{date}) . ' ' if ($last_row->{date} ne $row->{date}); my $t = $row->{time}; if ($last_row->{channel} ne $row->{channel}) { - $msg .= sprintf($args->{fmt}->{time_channel}, $t, $row->{channel}); + $msg .= cr_sprintf($args->{fmt}->{time_channel}, $t, $row->{channel}); } else { - $msg .= sprintf($args->{fmt}->{time}, $t); + $msg .= cr_sprintf($args->{fmt}->{time}, $t); } my $append = 1; @@ -241,19 +254,19 @@ $nick = $args->{filter}->{nick}->($nick) if (ref($args->{filter}->{nick}) eq 'CODE'); - $msg .= sprintf( $fmt, $nick ); + $msg .= cr_sprintf( $fmt, $nick ); $append = 0; } $args->{fmt}->{message} ||= '%s'; if (ref($args->{filter}->{message}) eq 'CODE') { - $msg .= sprintf($args->{fmt}->{message}, + $msg .= cr_sprintf($args->{fmt}->{message}, $args->{filter}->{message}->( $row->{message} ) ); } else { - $msg .= sprintf($args->{fmt}->{message}, $row->{message}); + $msg .= cr_sprintf($args->{fmt}->{message}, $row->{message}); } if ($append && @msgs) { @@ -544,6 +557,7 @@ .col-4 { background: #ff66ff } a:link.tag, a:visited.tag { border: 1px dashed #ccc; backgound: #ccc; text-decoration: none } a:hover.tag { border: 1px solid #eee } +hr { border: 1px dashed #ccc; height: 1px; clear: both; } _END_OF_STYLE_ my $max_color = 4; @@ -567,7 +581,7 @@ my $search = $q->param('search') || $q->param('grep') || ''; - $response->content( + my $html = qq{$NICK} . @@ -578,14 +592,44 @@ } . $cloud->html(500) . - qq{

} . - join("

", + qq{

}; + if ($request->url =~ m#/history#) { + my $sth = $dbh->prepare(qq{ + select date(time) as date,count(*) as nr + from log + group by date(time) + order by date(time) desc + }); + $sth->execute(); + my ($l_yyyy,$l_mm) = (0,0); + my $cal; + while (my $row = $sth->fetchrow_hashref) { + # this is probably PostgreSQL specific, expects ISO date + my ($yyyy,$mm,$dd) = split(/-/, $row->{date}); + if ($yyyy != $l_yyyy || $mm != $l_mm) { + $html .= $cal->as_HTML() if ($cal); + $cal = new HTML::CalendarMonthSimple('month'=>$mm,'year'=>$yyyy); + $cal->border(2); + ($l_yyyy,$l_mm) = ($yyyy,$mm); + } + $cal->setcontent($dd, qq{ + $row->{nr} + }); + } + $html .= $cal->as_HTML() if ($cal); + + } else { + $html .= join("

", get_from_log( - limit => $q->param('last') || 100, + limit => $q->param('last') || $q->param('date') ? undef : 100, search => $search || undef, tag => $q->param('tag') || undef, + date => $q->param('date') || undef, fmt => { - date => '


%s
', + date => sub { + my $date = shift || return; + qq{
$date
'}; + }, time => '%s ', time_channel => '%s %s ', nick => '%s: ', @@ -612,9 +656,15 @@ }, }, ) - ) . - qq{

} - ); + ); + } + + $html .= qq{

+
+

See history of all messages.

+ }; + + $response->content( $html ); return RC_OK; }