]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Modules/Factoids.pl
show modified by in factinfo
[infobot.git] / src / Modules / Factoids.pl
index 46cbf8d0370e24b14650e2c4c1f6f77e99e900b1..45f85548a9286f07cac6e56a87029dd172dfa9be 100644 (file)
@@ -19,16 +19,10 @@ sub CmdFactInfo {
        return;
     }
 
-    my $i = 0;
-    my %factinfo;
-    my @factinfo = &getFactInfo($faqtoid,"*");
-    foreach ( &dbGetRowInfo("factoids") ) {
-       $factinfo{$_} = $factinfo[$i] || '';
-       $i++;
-    }
+    my %factinfo = &dbGetColNiceHash("factoids", "*", "factoid_key=".&dbQuote($faqtoid));
 
     # factoid does not exist.
-    if (scalar @factinfo <= 1) {
+    if (scalar (keys %factinfo) <= 1) {
        &performReply("there's no such factoid as \002$faqtoid\002");
        return;
     }
@@ -56,7 +50,7 @@ sub CmdFactInfo {
            if (time() - $time > 60*60*24*7) {
                my $days = int( (time() - $time)/60/60/24 );
                $string .= " at \037". scalar(localtime $time). "\037" .
-                               " ($days days) ";
+                               " ($days days)";
            } else {
                $string .= " ".&Time2String(time() - $time)." ago";
            }
@@ -66,28 +60,28 @@ sub CmdFactInfo {
     }
 
     # modified:
-#    if ($factinfo{'modified_by'}) {
-#      $string = "last modified";
-#
-#      my $time = $factinfo{'modified_time'};
-#      if ($time) {
-#          if (time() - $time > 60*60*24*7) {
-#              $string .= " at \037". scalar(localtime $time). "\037";
-#          } else {
-#              $string .= " ".&Time2String(time() - $time)." ago";
-#          }
-#      }
-#
-#      my @x;
-#      foreach (split ",", $factinfo{'modified_by'}) {
-#          /\!/;
-#          push(@x, $`);
-#      }
-#      $string .= "by ".&IJoin(@x);
-#
-#      $i++;
-#      push(@array,$string);
-#    }
+    if ($factinfo{'modified_by'}) {
+       $string = "last modified";
+
+       my $time = $factinfo{'modified_time'};
+       if ($time) {
+           if (time() - $time > 60*60*24*7) {
+               $string .= " at \037". scalar(localtime $time). "\037";
+           } else {
+               $string .= " ".&Time2String(time() - $time)." ago ";
+           }
+       }
+
+       my @x;
+       foreach (split ",", $factinfo{'modified_by'}) {
+           /\!/;
+           push(@x, $`);
+       }
+       $string .= "by ".&IJoin(@x);
+
+       $i++;
+       push(@array,$string);
+    }
 
     # requested:
     if ($factinfo{'requested_by'}) {
@@ -143,7 +137,7 @@ sub CmdFactStats {
     my ($type) = @_;
 
     if ($type =~ /^author$/i) {
-       my %hash = &dbGetCol("factoids", "factoid_key","created_by");
+       my %hash = &dbGetCol("factoids", "factoid_key,created_by", "created_by IS NOT NULL");
        my %author;
 
        foreach (keys %hash) {
@@ -167,21 +161,21 @@ sub CmdFactStats {
        my $count;
        my @list;
        foreach $count (sort { $b <=> $a } keys %count) {
-           my $author = join(", ", sort keys %{$count{$count}});
+           my $author = join(", ", sort keys %{ $count{$count} });
            push(@list, "$count by $author");
        }
 
        my $prefix = "factoid statistics by author: ";
        return &formListReply(0, $prefix, @list);
 
-    } elsif ($type =~ /^broken$/i) {
-        &status("factstats(broken): starting...");
+    } elsif ($type =~ /^vandalism$/i) {
+        &status("factstats(vandalism): starting...");
        my $start_time  = &timeget();
-       my %data        = &dbGetCol("factoids", "factoid_key","factoid_value");
+       my %data        = &dbGetCol("factoids", "factoid_key,factoid_value", "factoid_value IS NOT NULL");
        my @list;
 
        my $delta_time  = &timedelta($start_time);
-        &status(sprintf("factstats(broken): %.02f sec to retreive all factoids.", $delta_time)) if ($delta_time > 0);
+        &status(sprintf("factstats(vandalismbroken): %.02f sec to retreive all factoids.", $delta_time)) if ($delta_time > 0);
        $start_time     = &timeget();
 
        # parse the factoids.
@@ -193,7 +187,71 @@ sub CmdFactStats {
        }
 
        $delta_time     = &timedelta($start_time);
-        &status(sprintf("factstats(broken): %.02f sec to complete.", $delta_time)) if ($delta_time > 0);
+        &status(sprintf("factstats(vandalism): %.02f sec to complete.", $delta_time)) if ($delta_time > 0);
+
+       # bail out on no results.
+       if (scalar @list == 0) {
+           return 'no vandalised factoids... wooohoo.';
+       }
+
+       # parse the results.
+       my $prefix = "Vandalised factoid ";
+       return &formListReply(1, $prefix, @list);
+
+    } elsif ($type =~ /^total$/i) {
+        &status("factstats(total): starting...");
+       my $start_time  = &timeget();
+       my @list;
+       my $str;
+       my($i,$j);
+       my %hash;
+
+       ### lets do it.
+       # total factoids requests.
+       $i = &sumKey("factoids", "requested_count");
+       push(@list, "total requests - $i");
+
+       # total factoids modified.
+       $str = &countKeys("factoids", "modified_by");
+       push(@list, "total modified - $str");
+
+       # total factoids modified.
+       $j      = &countKeys("factoids", "requested_count");
+       $str    = &countKeys("factoids", "factoid_key");
+       push(@list, "total non-requested - ".($str - $i));
+
+       # average request/factoid.
+       # i/j == total(requested_count)/count(requested_count)
+       $str = sprintf("%.01f", $i/$j);
+       push(@list, "average requested per factoid - $str");
+
+       # total prepared for deletion.
+       $str    = scalar( &searchTable("factoids", "factoid_key", "factoid_value", " #DEL") );
+       push(@list, "total prepared for deletion - $str");
+
+       # total unique authors.
+       foreach ( &dbRawReturn("SELECT created_by FROM factoids WHERE created_by IS NOT NULL") ) {
+           /^(\S+)!/;
+           my $nick = lc $1;
+           $hash{$nick}++;
+       }
+       push(@list, "total unique authors - ".(scalar keys %hash) );
+       undef %hash;
+
+       # total unique requesters.
+       foreach ( &dbRawReturn("SELECT requested_by FROM factoids WHERE requested_by IS NOT NULL") ) {
+           /^(\S+)!/;
+           my $nick = lc $1;
+           $hash{$nick}++;
+       }
+       push(@list, "total unique requesters - ".(scalar keys %hash) );
+       undef %hash;
+
+       ### end of "job".
+
+       my $delta_time  = &timedelta($start_time);
+        &status(sprintf("factstats(broken): %.02f sec to retreive all factoids.", $delta_time)) if ($delta_time > 0);
+       $start_time     = &timeget();
 
        # bail out on no results.
        if (scalar @list == 0) {
@@ -201,7 +259,7 @@ sub CmdFactStats {
        }
 
        # parse the results.
-       my $prefix = "broken factoid ";
+       my $prefix = "General factoid stiatistics ";
        return &formListReply(1, $prefix, @list);
 
     } elsif ($type =~ /^deadredir$/i) {
@@ -213,7 +271,7 @@ sub CmdFactStats {
        for (@list) {
            my $factoid = $_;
            my $val = &getFactInfo($factoid, "factoid_value");
-           if ($val =~ /^<REPLY> see( also)? (.*?)\.?$/i) {
+           if ($val =~ /^<REPLY> ?see( also)? (.*?)\.?$/i) {
                my $redirf = lc $2;
                my $redir = &getFactInfo($redirf, "factoid_value");
                next if (defined $redir);
@@ -240,17 +298,17 @@ sub CmdFactStats {
     } elsif ($type =~ /^dup(licate|e)$/i) {
         &status("factstats(dupe): starting...");
        my $start_time  = &timeget();
-       my %hash        = &dbGetCol("factoids", "factoid_key", "factoid_value", 1);
+       my %hash        = &dbGetCol("factoids", "factoid_key,factoid_value", "factoid_value IS NOT NULL", 1);
        my $refs        = 0;
        my @list;
        my $v;
 
        foreach $v (keys %hash) {
-           my $count = scalar(keys %{$hash{$v}});
+           my $count = scalar(keys %{ $hash{$v} });
            next if ($count == 1);
 
            my @sublist;
-           foreach (keys %{$hash{$v}}) {
+           foreach (keys %{ $hash{$v} }) {
                if ($v =~ /^<REPLY> see /i) {
                    $refs++;
                    next;
@@ -356,7 +414,7 @@ sub CmdFactStats {
        return &formListReply(1, $prefix, @list);
 
     } elsif ($type =~ /^locked$/i) {
-       my %hash = &dbGetCol("factoids", "factoid_key","locked_by");
+       my %hash = &dbGetCol("factoids", "factoid_key,locked_by", "locked_by IS NOT NULL");
        my @list = keys %hash;
 
        for (@list) {
@@ -367,7 +425,7 @@ sub CmdFactStats {
        return &formListReply(0, $prefix, @list);
 
     } elsif ($type =~ /^new$/i) {
-       my %hash = &dbGetCol("factoids", "factoid_key","created_time");
+       my %hash = &dbGetCol("factoids", "factoid_key,created_time", "created_time IS NOT NULL");
        my %age;
 
        foreach (keys %hash) {
@@ -384,7 +442,7 @@ sub CmdFactStats {
 
        my @list;
        foreach (sort {$a <=> $b} keys %age) {
-           push(@list, join(",", keys %{$age{$_}}));
+           push(@list, join(",", keys %{ $age{$_} }));
        }
 
        my $prefix = "new factoids in the last 24hours ";
@@ -421,7 +479,7 @@ sub CmdFactStats {
            my @sublist;
            my $length;
            foreach $length (@length) {
-               foreach (keys %{$length{$length}}) {
+               foreach (keys %{ $length{$length} }) {
                    if ($key{$_} =~ /^$val/i) {
                        s/([\,\;]+)/\037$1\037/g;
                        s/( and|and )/\037$1\037/g;
@@ -445,7 +503,7 @@ sub CmdFactStats {
        return &formListReply(1, $prefix, @list);
 
     } elsif ($type =~ /^profanity$/i) {
-       my %data = &dbGetCol("factoids", "factoid_key","factoid_value");
+       my %data = &dbGetCol("factoids", "factoid_key,factoid_value", "factoid_value IS NOT NULL");
        my @list;
 
        foreach (keys %data) {
@@ -478,7 +536,7 @@ sub CmdFactStats {
 
        my @newlist;
        foreach $f (keys %redir) {
-           my @sublist = keys %{$redir{$f}};
+           my @sublist = keys %{ $redir{$f} };
            for (@sublist) {
                s/([\,\;]+)/\037$1\037/g;
            }
@@ -491,7 +549,7 @@ sub CmdFactStats {
        return &formListReply(1, $prefix, @newlist);
 
     } elsif ($type =~ /^request(ed)?$/i) {
-       my %hash = &dbGetCol("factoids", "factoid_key", "requested_count",1);
+       my %hash = &dbGetCol("factoids", "factoid_key,requested_count", "requested_count IS NOT NULL", 1);
 
        if (!scalar keys %hash) {
            return 'sorry, no factoids have been questioned.';
@@ -515,8 +573,27 @@ sub CmdFactStats {
        my $prefix = "factoid statistics on $type ";
        return &formListReply(0, $prefix, @list);
 
+    } elsif ($type =~ /^reqrate$/i) {
+       my %hash = &dbGetCol("factoids",
+               "factoid_key,(unix_timestamp() - created_time)/requested_count as rate",
+               "requested_by IS NOT NULL and created_time IS NOT NULL ORDER BY rate LIMIT 15", 1);
+
+       my $rate;
+       my @list;
+       my $total       = 0;
+       my $users       = 0;
+       foreach $rate (sort { $b <=> $a } keys %hash) {
+           my $f       = join(", ", sort keys %{ $hash{$rate} });
+           my $str     = "$f - ".&Time2String($rate);
+           $str        =~ s/\002//g;
+           push(@list, $str);
+       }
+
+       my $prefix = "Rank of top factoid rate (time/req): ";
+       return &formListReply(0, $prefix, @list);
+
     } elsif ($type =~ /^requesters?$/i) {
-       my %hash = &dbGetCol("factoids", "factoid_key","requested_by");
+       my %hash = &dbGetCol("factoids", "factoid_key,requested_by", "requested_by IS NOT NULL");
        my %requester;
 
        foreach (keys %hash) {
@@ -533,7 +610,7 @@ sub CmdFactStats {
        # work-around.
        my %count;
        foreach (keys %requester) {
-           $count{$requester{$_}}{$_} = 1;
+           $count{ $requester{$_} }{$_} = 1;
        }
        undef %requester;