3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-26 13:59:47 +01:00

FactoidCommands: optimize histogram, top20 and count

This commit is contained in:
Pragmatic Software 2020-03-03 12:51:00 -08:00
parent 82b4e51f09
commit c5f137e853

View File

@ -913,27 +913,26 @@ sub histogram {
my $self = shift; my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->{factoids}->{factoids}; my $factoids = $self->{pbot}->{factoids}->{factoids};
my %hash; my %owners;
my $factoid_count = 0; my $factoid_count = 0;
foreach my $channel ($factoids->get_keys) { my $iter = $factoids->get_each('type = text', 'owner');
foreach my $command ($factoids->get_keys($channel)) { while (defined (my $factoid = $factoids->get_next($iter))) {
if ($factoids->get_data($channel, $command, 'type') eq 'text') { my ($owner) = $factoid->{owner} =~ m/^([^!]+)/;
$hash{$factoids->{$channel}->{$command}->{owner}}++; $owners{$owner}++;
$factoid_count++; $factoid_count++;
} }
}
}
my $top = 15;
my $text; my $text;
my $i = 0; my $i = 0;
foreach my $owner (sort { $hash{$b} <=> $hash{$a} } keys %hash) { foreach my $owner (sort { $owners{$b} <=> $owners{$a} } keys %owners) {
my $percent = int($hash{$owner} / $factoid_count * 100); my $percent = int($owners{$owner} / $factoid_count * 100);
$text .= "$owner: $hash{$owner} ($percent" . "%)\n"; $text .= "$owner: $owners{$owner} ($percent" . "%)\n";
$i++; $i++;
last if $i >= 10; last if $i >= $top;
} }
return "/say $factoid_count factoids, top 10 submitters:\n$text"; return "/say $factoid_count factoids, top $top submitters:\n$text";
} }
sub factshow { sub factshow {
@ -1155,9 +1154,7 @@ sub factinfo {
} }
sub top20 { sub top20 {
my $self = shift; my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
my ($from, $nick, $user, $host, $arguments, $stuff) = @_;
my $factoids = $self->{pbot}->{factoids}->{factoids}; my $factoids = $self->{pbot}->{factoids}->{factoids};
my %hash = (); my %hash = ();
my $text = ""; my $text = "";
@ -1168,56 +1165,51 @@ sub top20 {
if (not defined $channel) { return "Usage: top20 <channel> [nick or 'recent']"; } if (not defined $channel) { return "Usage: top20 <channel> [nick or 'recent']"; }
if (not defined $args) { if (not defined $args) {
foreach my $chan (sort $factoids->get_keys) { my $iter = $factoids->get_each('type = text', "index1 = $channel", 'index2', 'ref_count > 0', '_sort = -ref_count');
next if lc $chan ne lc $channel; while (defined (my $factoid = $factoids->get_next($iter))) {
foreach my $command (sort { $factoids->get_data($chan, $b, 'ref_count') <=> $factoids->get_data($chan, $a, 'ref_count') } $factoids->get_keys($chan)) { $text .= $factoids->get_data($factoid->{index1}, $factoid->{index2}, '_name') . " ($factoid->{ref_count}) ";
if ($factoids->get_data($chan, $command, 'ref_count') > 0 and $factoids->get_data($chan, $command, 'type') eq 'text') {
$text .= $factoids->get_data($chan, $command, '_name') . ' (' . $factoids->get_data($chan, $command, 'ref_count') . ') ';
$i++; $i++;
last if $i >= 20; last if $i >= 20;
} }
}
$channel = "the global channel" if $channel eq '.*'; $channel = "the global channel" if $channel eq '.*';
$text = "Top $i referenced factoids for $channel: $text" if $i > 0; if ($i > 0) {
return $text; return "Top $i referenced factoids for $channel: $text";
}
} else { } else {
return "No factoids referenced in $channel.";
}
}
if (lc $args eq "recent") { if (lc $args eq "recent") {
foreach my $chan (sort $factoids->get_keys) { my $iter = $factoids->get_each('type = text', "index1 = $channel", 'index2', 'created_on', 'owner', '_sort = -created_on');
next if lc $chan ne lc $channel; while (defined (my $factoid = $factoids->get_next($iter))) {
foreach my $command (sort { $factoids->get_data($chan, $b, 'created_on') <=> $factoids->get_data($chan, $a, 'created_on') } $factoids->get_keys($chan)) { my $ago = concise ago gettimeofday - $factoid->{'created_on'};
my $ago = concise ago gettimeofday - $factoids->get_data($chan, $command, 'created_on'); my ($owner) = $factoid->{'owner'} =~ /^([^!]+)/;
my $owner = $factoids->get_data($chan, $command, 'owner'); $text .= ' ' . $factoids->get_data($factoid->{index1}, $factoid->{index2}, '_name') . " [$ago by $owner]\n";
$owner =~ s/!.*$//;
$text .= ' ' . $factoids->get_data($chan, $command, '_name') . " [$ago by $owner]\n";
$i++; $i++;
last if $i >= 50; last if $i >= 50;
} }
$channel = "global channel" if $channel eq '.*'; $channel = "global channel" if $channel eq '.*';
$text = "$i most recent $channel submissions:\n\n$text" if $i > 0; $text = "$i most recent $channel submissions:\n\n$text" if $i > 0;
return $text; return $text;
} }
}
my $user = lc $args; my $iter = $factoids->get_each('type = text', "index1 = $channel", 'index2', 'ref_user', 'last_referenced_on', '_sort = -last_referenced_on');
foreach my $chan (sort $factoids->get_keys) { while (defined (my $factoid = $factoids->get_next($iter))) {
next if lc $chan ne lc $channel; my ($ref_user) = $factoid->{ref_user} =~ /^([^!]+)/;
foreach my $command (sort { ($factoids->get_data($chan, $b, 'last_referenced_on') || 0) <=> ($factoids->get_data($chan, $a, 'last_referenced_on') || 0) } if ($ref_user =~ /^\Q$args\E/i) {
$factoids->get_keys($chan)) my $ago = $factoid->{'last_referenced_on'} ? concise ago(gettimeofday - $factoid->{'last_referenced_on'}) : "unknown";
{ $text .= ' ' . $factoids->get_data($factoid->{index1}, $factoid->{index2}, '_name') . " [$ago]\n";
next if $command eq '_name';
my $ref_user = lc $factoids->get_data($chan, $command, 'ref_user');
if ($ref_user =~ /\Q$args\E/i) {
if ($user ne $ref_user && not $user =~ /$ref_user/i) { $user .= " ($ref_user)"; }
my $ago = $factoids->get_data($chan, $command, 'last_referenced_on') ? concise ago(gettimeofday - $factoids->get_data($chan, $command, 'last_referenced_on')) : "unknown";
$text .= ' ' . $factoids->get_data($chan, $command, '_name') . " [$ago]\n";
$i++; $i++;
last if $i >= 20; last if $i >= 20;
} }
} }
$text = "$i factoids last referenced by $user:\n\n$text" if $i > 0;
return $text; if ($i > 0) {
} return "$i $channel factoids last referenced by $args:\n\n$text";
} else {
return "No factoids last referenced by $args in $channel.";
} }
} }
@ -1233,18 +1225,16 @@ sub count {
$arguments = ".*" if ($arguments =~ /^factoids$/); $arguments = ".*" if ($arguments =~ /^factoids$/);
eval { eval {
foreach my $channel ($factoids->get_keys) { my $iter = $factoids->get_each('type = text', 'owner');
foreach my $command ($factoids->get_keys($channel)) { while (defined (my $factoid = $factoids->get_next($iter))) {
next if $command eq '_name';
next if $factoids->get_data($channel, $command, 'type') ne 'text';
$total++; $total++;
if ($factoids->get_data($channel, $command, 'owner') =~ /^\Q$arguments\E$/i) { $i++; } my ($owner) = $factoid->{owner} =~ /^([^!]+)/;
} if ($owner =~ /^$arguments$/i) { $i++; }
} }
}; };
return "/msg $nick $arguments: $@" if $@; return "/msg $nick Error counting $arguments: $@" if $@;
return "I have $i factoids." if $arguments eq ".*"; return "I have $i text factoids." if $arguments eq ".*";
if ($i > 0) { if ($i > 0) {
my $percent = int($i / $total * 100); my $percent = int($i / $total * 100);