From: Dominik Csapak <d.csapak@proxmox.com>
To: pmg-devel@lists.proxmox.com
Subject: [pmg-devel] [PATCH pmg-api v4 12/12] statistics: refactor filter_text generation
Date: Thu, 24 Nov 2022 13:21:12 +0100 [thread overview]
Message-ID: <20221124122112.666868-13-d.csapak@proxmox.com> (raw)
In-Reply-To: <20221124122112.666868-1-d.csapak@proxmox.com>
it's basically always the same and having one place where we encode
the filter, makes it more legible
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
src/PMG/Statistic.pm | 41 ++++++++++++++++++++++++-----------------
1 file changed, 24 insertions(+), 17 deletions(-)
diff --git a/src/PMG/Statistic.pm b/src/PMG/Statistic.pm
index 96ef61d..8d63b40 100755
--- a/src/PMG/Statistic.pm
+++ b/src/PMG/Statistic.pm
@@ -562,6 +562,18 @@ sub user_stat_to_perlstring {
return $res;
}
+my sub get_filter_text {
+ my ($dbh, $field, $filter) = @_;
+
+ if (!$filter || !$field) {
+ return '';
+ }
+
+ my $pattern = $dbh->quote(encode('UTF-8', "%${filter}%"));
+
+ return "AND ${field} like ${pattern} ";
+}
+
sub user_stat_contact_details {
my ($self, $rdb, $receiver, $limit, $sorters, $filter) = @_;
@@ -571,12 +583,12 @@ sub user_stat_contact_details {
my $cond_good_mail = $self->query_cond_good_mail ($from, $to);
- my $filter_pattern = $rdb->{dbh}->quote(encode('UTF-8', "%${filter}%"));
+ my $filter_text = get_filter_text($rdb->{dbh}, 'sender', $filter);
my $query = "SELECT * FROM CStatistic, CReceivers " .
"WHERE cid = cstatistic_cid AND rid = cstatistic_rid AND $cond_good_mail " .
"AND NOT direction AND sender != '' AND receiver = ? " .
- ($filter_pattern ? "AND sender like " . $filter_pattern . ' ' : '') .
+ $filter_text .
"ORDER BY $orderby limit $limit";
my $sth = $rdb->{dbh}->prepare($query);
@@ -602,14 +614,13 @@ sub user_stat_contact {
my $cond_good_mail = $self->query_cond_good_mail($from, $to);
- my $filter_pattern;
- $filter_pattern = $rdb->{dbh}->quote(encode('UTF-8', "%${filter}%")) if $filter;
+ my $filter_text = get_filter_text($rdb->{dbh}, 'receiver', $filter);
my $query = "SELECT receiver as contact, count(*) AS count, sum (bytes) AS bytes, " .
"count (virusinfo) as viruscount " .
"FROM CStatistic, CReceivers " .
"WHERE cid = cstatistic_cid AND rid = cstatistic_rid " .
- ($filter_pattern ? "AND receiver like " . $filter_pattern . ' ' : '') .
+ $filter_text .
"AND $cond_good_mail AND NOT direction AND sender != '' ";
if ($advfilter) {
@@ -642,8 +653,7 @@ sub user_stat_sender_details {
my $cond_good_mail = $self->query_cond_good_mail($from, $to);
- my $filter_pattern;
- $filter_pattern = $rdb->{dbh}->quote(encode('UTF-8', "%${filter}%")) if $filter;
+ my $filter_text = get_filter_text($rdb->{dbh}, 'receiver', $filter);
my $sth = $rdb->{dbh}->prepare(
"SELECT " .
@@ -651,7 +661,7 @@ sub user_stat_sender_details {
"FROM CStatistic, CReceivers " .
"WHERE cid = cstatistic_cid AND rid = cstatistic_rid AND " .
"$cond_good_mail AND NOT direction AND sender = ? " .
- ($filter_pattern ? "AND receiver like " . $filter_pattern . ' ' : '') .
+ $filter_text .
"ORDER BY $orderby limit $limit");
$sth->execute(encode('UTF-8',$sender));
@@ -675,14 +685,13 @@ sub user_stat_sender {
my $cond_good_mail = $self->query_cond_good_mail ($from, $to);
- my $filter_pattern;
- $filter_pattern = $rdb->{dbh}->quote(encode('UTF-8', "%${filter}%")) if $filter;
+ my $filter_text = get_filter_text($rdb->{dbh}, 'sender', $filter);
my $query = "SELECT sender,count(*) AS count, sum (bytes) AS bytes, " .
"count (virusinfo) as viruscount, " .
"count (CASE WHEN spamlevel >= 3 THEN 1 ELSE NULL END) as spamcount " .
"FROM CStatistic WHERE $cond_good_mail AND NOT direction AND sender != '' " .
- ($filter_pattern ? "AND sender like " . $filter_pattern . ' ' : '') .
+ $filter_text .
"GROUP BY sender ORDER BY $orderby limit $limit";
my $sth = $rdb->{dbh}->prepare($query);
@@ -707,14 +716,13 @@ sub user_stat_receiver_details {
my $cond_good_mail = $self->query_cond_good_mail($from, $to);
- my $filter_pattern;
- $filter_pattern = $rdb->{dbh}->quote(encode('UTF-8', "%${filter}%")) if $filter;
+ my $filter_text = get_filter_text($rdb->{dbh}, 'sender', $filter);
my $sth = $rdb->{dbh}->prepare(
"SELECT blocked, bytes, ptime, sender, receiver, spamlevel, time, virusinfo " .
"FROM CStatistic, CReceivers " .
"WHERE cid = cstatistic_cid AND rid = cstatistic_rid AND $cond_good_mail AND receiver = ? " .
- ($filter_pattern ? "AND sender like " . $filter_pattern . ' ' : '') .
+ $filter_text .
"ORDER BY $orderby limit $limit");
$sth->execute(encode('UTF-8',$receiver));
@@ -739,8 +747,7 @@ sub user_stat_receiver {
my $cond_good_mail = $self->query_cond_good_mail ($from, $to) . " AND " .
"receiver IS NOT NULL AND receiver != ''";
- my $filter_pattern;
- $filter_pattern = $rdb->{dbh}->quote(encode('UTF-8', "%${filter}%")) if $filter;
+ my $filter_text = get_filter_text($rdb->{dbh}, 'receiver', $filter);
my $query = "SELECT receiver, " .
"count(*) AS count, " .
@@ -762,7 +769,7 @@ sub user_stat_receiver {
}
$query .= "AND $cond_good_mail and direction " .
- ($filter_pattern ? "AND receiver like " . $filter_pattern . ' ' : '') .
+ $filter_text .
"GROUP BY receiver ORDER BY $orderby LIMIT $limit";
my $sth = $rdb->{dbh}->prepare($query);
--
2.30.2
next prev parent reply other threads:[~2022-11-24 12:21 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-24 12:21 [pmg-devel] [PATCH pmg-api v4 00/12] ruledb - improve experience for non-ascii tests and mails Dominik Csapak
2022-11-24 12:21 ` [pmg-devel] [PATCH pmg-api v4 01/12] utils: return perl string from decode_rfc1522 Dominik Csapak
2022-11-24 12:21 ` [pmg-devel] [PATCH pmg-api v4 02/12] ruledb: properly substitute prox_vars in headers Dominik Csapak
2022-11-24 12:21 ` [pmg-devel] [PATCH pmg-api v4 03/12] fix #2541 ruledb: encode relevant values as utf-8 in database Dominik Csapak
2022-11-24 12:21 ` [pmg-devel] [PATCH pmg-api v4 04/12] ruledb: encode e-mail addresses for syslog Dominik Csapak
2022-11-24 12:21 ` [pmg-devel] [PATCH pmg-api v4 05/12] partially fix #2465: handle smtputf8 addresses in the rule-system Dominik Csapak
2022-11-24 12:21 ` [pmg-devel] [PATCH pmg-api v4 06/12] quarantine: handle utf8 data Dominik Csapak
2022-11-24 12:21 ` [pmg-devel] [PATCH pmg-api v4 07/12] pmgqm: handle smtputf8 data Dominik Csapak
2022-11-24 12:21 ` [pmg-devel] [PATCH pmg-api v4 08/12] statistics: handle utf8 data Dominik Csapak
2022-11-24 12:21 ` [pmg-devel] [PATCH pmg-api v4 09/12] quarantine: fix adding non-ascii senders to wl/bl Dominik Csapak
2022-11-24 12:21 ` [pmg-devel] [PATCH pmg-api v4 10/12] utils: refactor rfc1522_to_html Dominik Csapak
2022-11-24 12:21 ` [pmg-devel] [PATCH pmg-api v4 11/12] ldap: improve unicode support Dominik Csapak
2022-11-24 12:21 ` Dominik Csapak [this message]
2022-11-24 15:45 ` [pmg-devel] applied-series: [PATCH pmg-api v4 00/12] ruledb - improve experience for non-ascii tests and mails Thomas Lamprecht
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221124122112.666868-13-d.csapak@proxmox.com \
--to=d.csapak@proxmox.com \
--cc=pmg-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox