Added Longterm-stats to userdetails
authorocto <octo>
Sat, 16 Apr 2005 12:48:59 +0000 (12:48 +0000)
committerocto <octo>
Sat, 16 Apr 2005 12:48:59 +0000 (12:48 +0000)
Corrected the behavior of ident_to_name and moved it to Data::Core

lib/Onis/Data/Core.pm
lib/Onis/Plugins/Conversations.pm
lib/Onis/Plugins/Core.pm
lib/Onis/Plugins/Longterm.pm
lib/Onis/Plugins/Userdetails.pm
lib/Onis/Users.pm

index d1e3fef..76f8c8c 100644 (file)
@@ -16,7 +16,7 @@ use warnings;
 
 use Exporter;
 use Onis::Config qw(get_config);
-use Onis::Users qw(ident_to_name);
+use Onis::Users qw(chatter_to_name);
 use Onis::Data::Persistent;
 use Onis::Parser::Persistent qw(get_absolute_time);
 
@@ -43,7 +43,9 @@ our $ChannelNames = Onis::Data::Persistent->new ('ChannelNames', 'channel', 'cou
 qw(
        store unsharp calculate_nicks 
 
-       get_all_nicks get_channel get_main_nick nick_to_ident ident_to_nick nick_to_name
+       get_all_nicks get_channel get_main_nick
+       nick_to_ident ident_to_nick
+       nick_to_name ident_to_name
        get_total_lines get_most_recent_time nick_rename print_output register_plugin
 );
 @Onis::Data::Core::ISA = ('Exporter');
@@ -378,7 +380,6 @@ sub calculate_nicks
        {
                my $chatter = $_;
                my ($nick, $ident) = split (m/!/, $chatter);
-               my $name = ident_to_name ($ident);
                my ($counter) = $ChatterList->get ($chatter);
 
                $nicks->{$nick}{$ident} = 0 unless (defined ($nicks->{$nick}{$ident}));
@@ -396,7 +397,7 @@ sub calculate_nicks
                for (keys %{$nicks->{$this_nick}})
                {
                        my $ident = $_;
-                       my $name = ident_to_name ($ident);
+                       my $name = chatter_to_name ("$this_nick!$ident");
                        my $num = $nicks->{$this_nick}{$ident};
                        
                        $this_total += $num;
@@ -659,8 +660,7 @@ sub ident_to_nick
 
 =item I<$name> = B<nick_to_name> (I<$nick>)
 
-Return the name associated with I<$nick>. This function uses B<ident_to_name>
-(see L<Onis::Users>).
+Return the name associated with I<$nick>.
 
 =cut
 
@@ -671,7 +671,28 @@ sub nick_to_name
 
        if ($ident)
        {
-               return (ident_to_name ($ident));
+               return (chatter_to_name ("$nick!$ident"));
+       }
+       else
+       {
+               return ('');
+       }
+}
+
+=item I<$name> = B<ident_to_name> (I<$ident>)
+
+Returns the name associated with I<$ident>.
+
+=cut
+
+sub ident_to_name
+{
+       my $ident = shift;
+       my $nick = ident_to_nick ($ident);
+
+       if ($nick)
+       {
+               return (chatter_to_name ("$nick!$ident"));
        }
        else
        {
index 6f745d4..9c39ef1 100644 (file)
@@ -9,7 +9,6 @@ use Onis::Config qw(get_config);
 use Onis::Html qw(get_filehandle);
 use Onis::Language qw(translate);
 use Onis::Data::Core qw(register_plugin get_main_nick nick_to_ident nick_to_name);
-use Onis::Users (qw(ident_to_name));
 use Onis::Data::Persistent;
 
 =head1 NAME
index 1423fb2..fb68973 100644 (file)
@@ -20,7 +20,7 @@ complicated plugin so far.
 use Onis::Config (qw(get_config));
 use Onis::Html (qw(html_escape get_filehandle));
 use Onis::Language (qw(translate));
-use Onis::Users (qw(get_realname get_link get_image ident_to_name));
+use Onis::Users (qw(get_realname get_link get_image chatter_to_name));
 use Onis::Data::Core (qw(get_all_nicks nick_to_ident ident_to_nick get_main_nick register_plugin));
 use Onis::Data::Persistent ();
 
@@ -727,7 +727,7 @@ EOF
        {
                my $nick = $_;
                my $ident = nick_to_ident ($nick);
-               my $name  = ident_to_name ($ident);
+               my $name  = chatter_to_name ("$nick!$ident");
                my $print = $name || $nick;
 
                $linescount++;
@@ -1053,6 +1053,6 @@ sub nick_is_in_main_table
 
 =head1 AUTHOR
 
-Florian octo Forster, E<lt>octo at verplant.orgE<gt>
+Florian octo Forster E<lt>octo at verplant.orgE<gt>
 
 =cut
index 9e92c8b..f4216fa 100644 (file)
@@ -3,12 +3,23 @@ package Onis::Plugins::Longterm;
 use strict;
 use warnings;
 
+use Exporter;
+
 use Onis::Config (qw(get_config));
 use Onis::Html (qw(get_filehandle));
 use Onis::Language (qw(translate));
 use Onis::Data::Core (qw(register_plugin get_main_nick get_most_recent_time nick_to_ident nick_to_name));
 use Onis::Data::Persistent ();
 
+=head1 NAME
+
+Onis::Plugins::Longterm
+
+=cut
+
+@Onis::Plugins::Longterm::EXPORT_OK = (qw(get_longterm));
+@Onis::Plugins::Longterm::ISA = ('Exporter');
+
 register_plugin ('TEXT', \&add);
 register_plugin ('ACTION', \&add);
 register_plugin ('OUTPUT', \&output);
@@ -202,3 +213,41 @@ sub output
        }
        print $fh "  </tr>\n</table>\n\n";
 }
+
+=head1 EXPORTED FUNCTIONS
+
+=over 4
+
+=item B<get_longterm> (I<$nick>)
+
+Returns the longterm-statistics for I<$nick>. The numbers are array-counters.
+The format is as follows:
+
+  [
+    [0, 0, 0, 0], # oldest day
+    ...,
+    [0, 0, 0, 0], # yesterday
+    [0, 0, 0, 0]  # today
+  ]
+
+=cut
+
+sub get_longterm
+{
+       my $nick = shift;
+
+       if (!defined ($LongtermData->{$nick}))
+       {
+               return ([]);
+       }
+
+       return ($LongtermData->{$nick});
+}
+
+=back
+
+=head1 AUTHOR
+
+Florian octo Forster E<lt>octo at verplant.orgE<gt>
+
+=cut
index 66e9903..1d12dc8 100644 (file)
@@ -6,11 +6,12 @@ use warnings;
 use Onis::Config (qw(get_config));
 use Onis::Html (qw(html_escape get_filehandle));
 use Onis::Language (qw(translate));
-use Onis::Data::Core (qw(get_main_nick register_plugin nick_to_name));
-use Onis::Users (qw(ident_to_name get_link get_image));
+use Onis::Data::Core (qw(get_main_nick register_plugin nick_to_name get_most_recent_time));
+use Onis::Users (qw(get_link get_image));
 
 use Onis::Plugins::Core (qw(get_core_nick_counters get_sorted_nicklist));
 use Onis::Plugins::Weekdays (qw(get_weekdays));
+use Onis::Plugins::Longterm (qw(get_longterm));
 use Onis::Plugins::Conversations (qw(get_conversations));
 use Onis::Plugins::Bignumbers (qw(get_bignumbers));
 use Onis::Plugins::Interestingnumbers (qw(get_interestingnumbers));
@@ -128,6 +129,14 @@ if (get_config ('bar_width'))
        $BAR_WIDTH = $tmp if ($tmp >= 10);
 }
 
+our $LongtermDays = 7;
+if (get_config ('userdetails_longterm_days'))
+{
+       my $tmp = get_config ('userdetails_longterm_days');
+       $tmp =~ s/\D//g;
+       $LongtermDays = $tmp if ($tmp);
+}
+
 my $VERSION = '$Id: Userdetails.pm,v 1.5 2005/03/14 18:40:25 octo Exp $';
 print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);
 
@@ -144,7 +153,8 @@ sub output
 
        my $max_time = 0;
        my $max_conv = 0;
-       my $max_weekday = 0;
+       my $max_weekdays = 0;
+       my $max_longterm = 0;
 
        my @nicks = @$nicks_ref;
        my $nick_data = {};
@@ -157,6 +167,7 @@ sub output
 
                $nick_data->{$nick} = get_core_nick_counters ($nick);
                $nick_data->{$nick}{'weekdays'} = get_weekdays ($nick);
+               $nick_data->{$nick}{'longterm'} = get_longterm ($nick);
                $nick_data->{$nick}{'conversations'} = get_conversations ($nick);
                $nick_data->{$nick}{'bignumbers'} = get_bignumbers ($nick);
                $nick_data->{$nick}{'interestingnumbers'} = get_interestingnumbers ($nick);
@@ -177,29 +188,30 @@ sub output
 
                for (keys %{$nick_data->{$nick}{'weekdays'}})
                {
-                       my $day = $_;
-                       my $ptr = $nick_data->{$nick}{'weekdays'}{$day};
-
+                       my $ptr = $nick_data->{$nick}{'weekdays'}{$_};
                        for (my $i = 0; $i < 4; $i++)
                        {
-                               $max_weekday = $ptr->[$i] if ($max_weekday < $ptr->[$i]);
+                               $max_weekdays = $ptr->[$i] if ($max_weekdays < $ptr->[$i]);
                        }
                }
-       }
 
-       my $time_factor = 0;
-       my $conv_factor = 0;
+               if (@{$nick_data->{$nick}{'longterm'}})
+               {
+                       my $num = scalar (@{$nick_data->{$nick}{'longterm'}});
+                       $LongtermDays = $num if ($LongtermDays > $num);
 
-       if ($max_time)
-       {
-               $time_factor = $BAR_HEIGHT / $max_time;
-       }
+                       for (my $i = $num - $LongtermDays; $i < $num; $i++)
+                       {
+                               my $ptr = $nick_data->{$nick}{'longterm'}[$i];
 
-       if ($max_conv)
-       {
-               $conv_factor = $BAR_WIDTH / $max_conv;
+                               for (my $j = 0; $j < 4; $j++)
+                               {
+                                       $max_longterm = $ptr->[$j] if ($max_longterm < $ptr->[$j]);
+                               }
+                       }
+               }
        }
-       
+
        print $fh qq#<table class="plugin userdetails">\n#,
        qq#  <tr>\n#,
        qq#    <th colspan="#, $DISPLAY_IMAGES ? 4 : 3, qq#">$trans</th>\n#,
@@ -416,7 +428,7 @@ EOF
                }
                else
                {
-                       print '&nbsp;';
+                       print $fh "      &nbsp;\n";
                }
 
                print $fh qq#    </td>\n    <td>\n#;
@@ -436,8 +448,7 @@ EOF
                                for (my $i = 0; $i < 4; $i++)
                                {
                                        my $num = $nick_data->{$nick}{'weekdays'}{$day}[$i];
-                                       my $height = sprintf ("%.2f", 95 * $num / $max_weekday);
-                                       my $class = '';
+                                       my $height = sprintf ("%.2f", 95 * $num / $max_weekdays);
                                        my $img = $V_IMAGES[$i];
 
                                        print $fh qq#          <td class="bar vertical">#,
@@ -460,11 +471,62 @@ EOF
                        print $fh qq#        </tr>\n#,
                        qq#      </table>\n#;
                }
+               else
+               {
+                       print $fh "      &nbsp;\n";
+               }
 
                print $fh qq#    </td>\n    <td>\n#;
 
                #longterm
-               print $fh qq#      &nbsp;\n#;
+               if (@{$nick_data->{$nick}{'longterm'}})
+               {
+                       my $num_fields = scalar (@{$nick_data->{$nick}{'longterm'}});
+                       my $now_epoch = get_most_recent_time ();
+                       my $now_day = int ($now_epoch / 86400);
+                       my $last_day;
+
+                       my @weekdays = (qw(sun mon tue wed thu fri sat));
+
+                       $LongtermDays = $num_fields if ($LongtermDays > $num_fields);
+                       $last_day = 1 + $now_day - $LongtermDays;
+                       
+                       print $fh qq#      <table class="longterm">\n#,
+                       qq#        <tr class="bars">\n#;
+
+                       for (my $i = $num_fields - $LongtermDays; $i < $num_fields; $i++)
+                       {
+                               for (my $j = 0; $j < 4; $j++)
+                               {
+                                       my $num = $nick_data->{$nick}{'longterm'}[$i][$j];
+                                       my $height = sprintf ("%.2f", 95 * $num / $max_longterm);
+                                       my $img = $V_IMAGES[$j];
+                                       
+                                       print $fh qq#          <td class="bar vertical">#,
+                                       qq#<img src="$img" alt="" class="first last" style="height: $height\%;" />#,
+                                       qq#</td>\n#;
+                               }
+                       }
+                       
+                       print $fh qq#        </tr>\n#,
+                       qq#        <tr class="numeration">\n#;
+
+                       for (my $i = 0; $i < $LongtermDays; $i++)
+                       {
+                               my $epoch = ($last_day + $i) * 86400;
+                               my ($day, $wd) = (localtime ($epoch))[3,6];
+                               $wd = $weekdays[$wd];
+
+                               print $fh qq#          <td colspan="4" class="numeration $wd">$day.</td>\n#;
+                       }
+
+                       print $fh qq#        </tr>\n#,
+                       qq#      </table>\n#;
+               }
+               else
+               {
+                       print $fh "      &nbsp;\n";
+               }
 
                print $fh qq#    </td>\n  </tr>\n#;
        }
index 20c2e3f..b34cff5 100644 (file)
@@ -8,7 +8,8 @@ use Onis::Data::Persistent ();
 
 @Onis::Users::EXPORT_OK =
 (qw(
-       ident_to_name chatter_to_name name_to_ident
+       chatter_to_name 
+       name_to_chatter name_to_ident name_to_nick
        get_realname get_link get_image
 ));
 @Onis::Users::ISA = ('Exporter');
@@ -41,9 +42,8 @@ Set $::DEBUG to ``0x1000'' to get extra debug messages.
 =cut
 
 our $Users = {};
-our $IdentToName = {};
-our $NameToIdent = {};
-
+our $ChatterToName = {};
+our $NameToChatter = {};
 
 my $VERSION = '$Id$';
 print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);
@@ -196,68 +196,71 @@ sub read_config
 
 =over 4
 
-=item B<ident_to_name> (I<$ident>)
+=item B<chatter_to_name> (I<$chatter>)
 
-Matches the ident against the configured hostmasks. Uses caching to
-speed up execution. Returns the name or an empty string if not found.
+Passes the ident-part of I<$chatter> to B<ident_to_name>.
 
 =cut
 
-sub ident_to_name
+sub chatter_to_name
 {
-       my $ident = shift;
-       my $name = '';
+       my $chatter = shift;
+       my $retval = '';
 
-       if (defined ($IdentToName->{$ident}))
+       if (defined ($ChatterToName->{$chatter}))
        {
-               $name = $IdentToName->{$ident};
+               return ($ChatterToName->{$chatter});
        }
-       else
+
+       USER: for (keys %$Users)
        {
-               USER: for (keys (%$Users))
+               my $name = $_;
+               for (@{$Users->{$name}{'host'}})
                {
-                       my $this_name = $_;
-                       for (@{$Users->{$this_name}{'host'}})
-                       {
-                               my $host_re = $_;
+                       my $re = $_;
 
-                               if ($ident =~ $host_re)
-                               {
-                                       $name = $this_name;
-                                       last (USER);
-                               }
+                       if ($chatter =~ $re)
+                       {
+                               $retval = $_;
+                               last USER;
                        }
                }
+       }
 
-               if (($::DEBUG & 0x1000) and $name)
-               {
-                       print STDERR $/, __FILE__, ": Host ``$ident'' belongs to ``$name''";
-               }
+       if (($::DEBUG & 0x1000) and $retval)
+       {
+               print STDERR $/, __FILE__, ": ``$chatter'' identified as ``$retval''";
        }
-       
-       $IdentToName->{$ident} = $name;
-       $NameToIdent->{$name} = $ident if ($name);
-       return ($name);
+
+       $ChatterToName->{$chatter} = $retval;
+       $NameToChatter->{$retval} = $chatter if ($retval);
+
+       return ($retval);
 }
 
-=item B<chatter_to_name> (I<$chatter>)
+=item B<name_to_chatter> (I<$name>)
 
-Passes the ident-part of I<$chatter> to B<ident_to_name>.
+Returns the most recent chatter for I<$name>.
 
 =cut
 
-sub chatter_to_name
+sub name_to_chatter
 {
-       my $chatter = shift;
-       my ($nick, $ident) = split (m/!/, $chatter);
+       my $name = shift;
 
-       return (ident_to_name ($ident));
+       if (defined ($NameToChatter->{$name}))
+       {
+               return ($NameToChatter->{$name});
+       }
+       else
+       {
+               return ('');
+       }
 }
 
 =item B<name_to_ident> (I<$name>)
 
-Does the reverse of B<ident_to_name>: Returns the most recent association of
-I<$name> to an ident. This function should rarely be needed..
+Returns the most recent ident for I<$name>.
 
 =cut
 
@@ -265,9 +268,35 @@ sub name_to_ident
 {
        my $name = shift;
 
-       if (defined ($NameToIdent->{$name}))
+       if (defined ($NameToChatter->{$name}))
+       {
+               my $chatter = $NameToChatter->{$name};
+               my ($nick, $ident) = split (m/!/, $chatter);
+
+               return ($ident);
+       }
+       else
+       {
+               return ('');
+       }
+}
+
+=item B<name_to_nick> (I<$name>)
+
+Returns the most recent nick for I<$name>.
+
+=cut
+
+sub name_to_nick
+{
+       my $name = shift;
+
+       if (defined ($NameToChatter->{$name}))
        {
-               return ($NameToIdent->{$name});
+               my $chatter = $NameToChatter->{$name};
+               my ($nick, $ident) = split (m/!/, $chatter);
+
+               return ($nick);
        }
        else
        {