+ my $max_entry = int(($to - $from) / $span);
+ while (my $ref = $sth->fetchrow_hashref()) {
+ my $i = $ref->{index};
+ $res->[$i] = $ref;
+ $max_entry = $i if $i > $max_entry;
+ }
+
+ for my $i (0..$max_entry) {
+ $res->[$i] //= { index => $i, rbl_rejects => 0, pregreet_rejects => 0};
+
+ my $d = $res->[$i];
+ $d->{time} = $from + $i*$span - $timezone;
+ }
+ $sth->finish();
+
+ 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 AND time < $to ".
+ "GROUP BY index ORDER BY index";
+
+ my $sth = $rdb->{dbh}->prepare($cmd);
+
+ $sth->execute ();
+