]> git.proxmox.com Git - pmg-api.git/blobdiff - PMG/Statistic.pm
fix #1974: postscreen_stat_graph: go through all entries
[pmg-api.git] / PMG / Statistic.pm
index 8a511bb12f40d06a7347aee20db80179f2a0796c..c2b103fbdfdae70e57096cd845af55bf49e07b26 100755 (executable)
@@ -393,11 +393,11 @@ sub total_mail_stat {
     my $cmds = "SELECT sum(CountIn) + $glcount AS count_in, sum(CountOut) AS count_out, " .
        "sum (VirusIn) AS viruscount_in, sum (VirusOut) AS viruscount_out, " .
        "sum (SpamIn) AS spamcount_in, sum (SpamOut) AS spamcount_out, " .
-       "sum (BytesIn) AS bytes_in, sum (BytesOut) AS bytes_out, " .
+       "sum (BytesIn)*1024*1024 AS bytes_in, sum (BytesOut)*1024*1024 AS bytes_out, " .
        "sum (BouncesIn) AS bounces_in, sum (BouncesOut) AS bounces_out, " .
        "sum (GreylistCount) + $glcount as glcount, " .
        "sum (SPFCount) as spfcount, " .
-       "sum (RBLCount) as rblcount, " .
+       "sum (RBLCount) as rbl_rejects, " .
        "sum(PTimeSum)/(sum(CountIn) + $glcount + sum(CountOut)) AS avptime " .
        "FROM DailyStat where time >= $from and time < $to";
 
@@ -411,14 +411,14 @@ sub total_mail_stat {
     if (!$ref->{avptime}) {
        $ref->{count_in} = $ref->{count_out} = $ref->{viruscount_in} = $ref->{viruscount_out} =
            $ref->{spamcount_in} = $ref->{spamcount_out} = $ref->{glcount} = $ref->{spfcount} =
-           $ref->{rblcount} = $ref->{bounces_in} = $ref->{bounces_out} = $ref->{bytes_in} =
+           $ref->{rbl_rejects} = $ref->{bounces_in} = $ref->{bounces_out} = $ref->{bytes_in} =
            $ref->{bytes_out} = $ref->{avptime} = 0;
     }
 
     $ref->{count} = $ref->{count_in} + $ref->{count_out};
 
     $ref->{junk_in} = $ref->{viruscount_in} + $ref->{spamcount_in} + $ref->{glcount} +
-       $ref->{spfcount} + $ref->{rblcount};
+       $ref->{spfcount} + $ref->{rbl_rejects};
 
     $ref->{junk_out} = $ref->{viruscount_out} + $ref->{spamcount_out};
 
@@ -484,7 +484,7 @@ sub total_domain_stat {
     my ($from, $to) = $self->localdayspan();
 
     my $query = "SELECT domain, SUM (CountIn) AS count_in, SUM (CountOut) AS count_out," .
-       "SUM (BytesIn) AS mbytes_in, SUM (BytesOut) AS mbytes_out, " .
+       "SUM (BytesIn)*1024*1024 AS bytes_in, SUM (BytesOut)*1024*1024 AS bytes_out, " .
        "SUM (VirusIn) AS viruscount_in, SUM (VirusOut) AS viruscount_out," .
        "SUM (SpamIn) as spamcount_in, SUM (SpamOut) as spamcount_out " .
        "FROM DomainStat where time >= $from AND time < $to " .
@@ -744,7 +744,26 @@ sub user_stat_receiver {
     return $res;
 }
 
-sub rbl_count_stats {
+sub postscreen_stat {
+    my ($self, $rdb) = @_;
+
+    my ($from, $to) = $self->localhourspan();
+    my $timezone = tz_local_offset();;
+
+    my $cmd = "SELECT " .
+       "sum(rblcount) as rbl_rejects, " .
+       "sum(PregreetCount) as pregreet_rejects " .
+       "FROM LocalStat WHERE time >= $from AND time < $to";
+
+    my $sth = $rdb->{dbh}->prepare($cmd);
+    $sth->execute();
+    my $res = $sth->fetchrow_hashref();
+    $sth->finish();
+
+    return $res;
+}
+
+sub postscreen_stat_graph {
     my ($self, $rdb, $span) = @_;
     my $res;
 
@@ -753,23 +772,25 @@ sub rbl_count_stats {
 
     my $cmd = "SELECT " .
        "(time - $from) / $span AS index, " .
-       "sum(rblcount) as count " .
+       "sum(rblcount) as rbl_rejects, " .
+       "sum(PregreetCount) as pregreet_rejects " .
        "FROM LocalStat WHERE time >= $from AND time < $to " .
        "GROUP BY index ORDER BY index";
 
     my $sth = $rdb->{dbh}->prepare($cmd);
     $sth->execute ();
 
+    my $max_entry = int(($to - $from) / $span);
     while (my $ref = $sth->fetchrow_hashref()) {
-       @$res[$ref->{index}] = $ref;
+       my $i = $ref->{index};
+       $res->[$i] = $ref;
+       $max_entry = $i if $i > $max_entry;
     }
 
-    my $c = int (($to - $from) / $span);
-
-    for (my $i = 0; $i < $c; $i++) {
-       @$res[$i] //= { index => $i, count => 0, };
+    for my $i (0..$max_entry) {
+       $res->[$i] //= { index => $i, rbl_rejects => 0, pregreet_rejects => 0};
 
-       my $d = @$res[$i];
+       my $d = $res->[$i];
        $d->{time} = $from + $i*$span - $timezone;
     }
     $sth->finish();
@@ -795,7 +816,7 @@ sub recent_mailcount {
        "COUNT (CASE WHEN virusinfo IS NULL AND direction AND spamlevel >= 3 THEN 1 ELSE NULL END) as spam_in, ".
        "COUNT (CASE WHEN virusinfo IS NULL AND NOT direction AND spamlevel >= 3 THEN 1 ELSE NULL END) as spam_out ".
        "FROM cstatistic ".
-       "WHERE time >= $from ".
+       "WHERE time >= $from AND time < $to ".
        "GROUP BY index ORDER BY index";
 
     my $sth =  $rdb->{dbh}->prepare($cmd);
@@ -833,6 +854,34 @@ sub recent_mailcount {
     return $res;
 }
 
+sub recent_receivers {
+    my ($self, $rdb, $limit) = @_;
+    my $res = [];
+
+    my ($from, $to) = $self->timespan();
+
+    my $cmd = "SELECT ".
+       "COUNT(receiver) as count, receiver ".
+       "FROM CStatistic, CReceivers ".
+       "WHERE time >= ? ".
+       "AND cid = cstatistic_cid ".
+       "AND rid = cstatistic_rid ".
+       "AND blocked = false ".
+       "AND direction = true ".
+       "GROUP BY receiver ORDER BY count DESC LIMIT ?;";
+
+    my $sth =  $rdb->{dbh}->prepare($cmd);
+
+    $sth->execute ($from, $limit);
+
+    while (my $ref = $sth->fetchrow_hashref()) {
+       push @$res, $ref;
+    }
+    $sth->finish();
+
+    return $res;
+}
+
 sub traffic_stat_graph {
     my ($self, $rdb, $span) = @_;
     my $res;