]> git.proxmox.com Git - pmg-api.git/commitdiff
add 'recent' statistics api call
authorDominik Csapak <d.csapak@proxmox.com>
Tue, 10 Oct 2017 13:08:58 +0000 (15:08 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 11 Oct 2017 04:31:23 +0000 (06:31 +0200)
this api call returns a time grouped series of mail type counts

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
PMG/API2/Statistics.pm
PMG/Statistic.pm

index 3953aea1b2eff9b5c2981e7ef1571c82e643e606..a72c05c212926c74ed02f4af55450d03f30298bb 100644 (file)
@@ -47,6 +47,7 @@ __PACKAGE__->register_method ({
            { name => "domains" },
            { name => "mail" },
            { name => "mailcount" },
+           { name => "recent" },
            { name => "maildistribution" },
            { name => "spamscores" },
            { name => "sender" },
@@ -793,6 +794,112 @@ __PACKAGE__->register_method ({
        return $res;
     }});
 
+__PACKAGE__->register_method ({
+    name => 'recent',
+    path => 'recent',
+    method => 'GET',
+    description => "Mail Count Statistics.",
+    permissions => { check => [ 'admin', 'qmanager', 'audit'] },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           hours => {
+               description => "How many hours you want to get",
+               type => 'integer',
+               minimum => 1,
+               maximum => 24,
+               optional => 1,
+               default => 12,
+           },
+           timespan => {
+               description => "The Timespan for one datapoint (in seconds)",
+               type => 'integer',
+               minimum => 1,
+               maximum => 1800,
+               optional => 1,
+               default => 1800,
+           },
+       },
+    },
+    returns => {
+       type => 'array',
+       items => {
+           type => "object",
+           properties => {
+               index => {
+                   description => "Time index.",
+                   type => 'integer',
+               },
+               time => {
+                   description => "Time (Unix epoch).",
+                   type => 'integer',
+               },
+               count => {
+                   description => "Overall mail count (in and out).",
+                   type => 'number',
+               },
+               count_in => {
+                   description => "Incoming mail count.",
+                   type => 'number',
+               },
+               count_out => {
+                   description => "Outgoing mail count.",
+                   type => 'number',
+               },
+               spam => {
+                   description => "Overall spam mail count (in and out).",
+                   type => 'number',
+               },
+               spam_in => {
+                   description => "Incoming spam mails (spamcount_in + glcount + spfcount).",
+                   type => 'number',
+               },
+               spam_out => {
+                   description => "Outgoing spam mails.",
+                   type => 'number',
+               },
+               bytes_in => {
+                   description => "Number of incoming bytes mails.",
+                   type => 'number',
+               },
+               bytes_out => {
+                   description => "Number of outgoing bytes mails.",
+                   type => 'number',
+               },
+               virus_in => {
+                   description => "Number of incoming virus mails.",
+                   type => 'number',
+               },
+               virus_out => {
+                   description => "Number of outgoing virus mails.",
+                   type => 'number',
+               },
+               timespan => {
+                   description => "Timespan in seconds for one data point",
+                   type => 'number',
+               }
+           },
+       },
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $restenv = PMG::RESTEnvironment->get();
+
+       my $hours = $param->{hours} // 12;
+       my $span = $param->{timespan} // 1800;
+
+       my $end = time();
+       my $start = $end - 3600*$hours;
+
+       my $stat = PMG::Statistic->new($start, $end);
+       my $rdb = PMG::RuleDB->new();
+
+       my $res = $stat->recent_mailcount($rdb, $span);
+
+       return $res;
+    }});
+
 __PACKAGE__->register_method ({
     name => 'mailcount',
     path => 'mailcount',
index 3682ffe8b11272b15f69bd3187da4db01d78d51a..8a511bb12f40d06a7347aee20db80179f2a0796c 100755 (executable)
@@ -777,6 +777,62 @@ sub rbl_count_stats {
     return $res;
 }
 
+sub recent_mailcount {
+    my ($self, $rdb, $span) = @_;
+    my $res;
+
+    my ($from, $to) = $self->timespan();
+
+    my $cmd = "SELECT".
+       "(time - $from) / $span AS index, ".
+       "COUNT (CASE WHEN direction THEN 1 ELSE NULL END) as count_in, ".
+       "COUNT (CASE WHEN NOT direction THEN 1 ELSE NULL END) as count_out, ".
+       "SUM (CASE WHEN direction THEN bytes ELSE 0 END) as bytes_in, ".
+       "SUM (CASE WHEN NOT direction THEN bytes ELSE 0 END) as bytes_out, ".
+       "SUM (ptime) / 1000.0 as ptimesum, ".
+       "COUNT (CASE WHEN virusinfo IS NOT NULL AND direction THEN 1 ELSE NULL END) as virus_in, ".
+       "COUNT (CASE WHEN virusinfo IS NOT NULL AND NOT direction THEN 1 ELSE NULL END) as virus_out, ".
+       "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 ".
+       "GROUP BY index ORDER BY index";
+
+    my $sth =  $rdb->{dbh}->prepare($cmd);
+
+    $sth->execute ();
+
+    while (my $ref = $sth->fetchrow_hashref()) {
+       @$res[$ref->{index}] = $ref;
+    }
+
+    $sth->finish();
+
+    my $c = int(($to - $from) / $span);
+
+    for (my $i = 0; $i < $c; $i++) {
+       @$res[$i] //= {
+           index => $i,
+           count => 0, count_in => 0, count_out => 0,
+           spam => 0, spam_in => 0, spam_out => 0,
+           virus => 0, virus_in => 0, virus_out => 0,
+           bytes => 0, bytes_in => 0, bytes_out => 0,
+           };
+
+       my $d = @$res[$i];
+
+       $d->{time} = $from + $i*$span;
+       $d->{count} = $d->{count_in} + $d->{count_out};
+       $d->{spam} = $d->{spam_in} + $d->{spam_out};
+       $d->{virus} = $d->{virus_in} + $d->{virus_out};
+       $d->{bytes} = $d->{bytes_in} + $d->{bytes_out};
+       $d->{timespan} = $span+0;
+       $d->{ptimesum} += 0;
+    }
+
+    return $res;
+}
+
 sub traffic_stat_graph {
     my ($self, $rdb, $span) = @_;
     my $res;