From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <s.ivanov@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id A410C6CBC3
 for <pmg-devel@lists.proxmox.com>; Tue,  2 Feb 2021 14:03:46 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 9E5F3F0E7
 for <pmg-devel@lists.proxmox.com>; Tue,  2 Feb 2021 14:03:46 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id E2401F0D7
 for <pmg-devel@lists.proxmox.com>; Tue,  2 Feb 2021 14:03:45 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id A97C44616D
 for <pmg-devel@lists.proxmox.com>; Tue,  2 Feb 2021 14:03:45 +0100 (CET)
From: Stoiko Ivanov <s.ivanov@proxmox.com>
To: pmg-devel@lists.proxmox.com
Date: Tue,  2 Feb 2021 14:03:15 +0100
Message-Id: <20210202130317.31873-2-s.ivanov@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20210202130317.31873-1-s.ivanov@proxmox.com>
References: <20210202130317.31873-1-s.ivanov@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.065 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [statistics.pm]
Subject: [pmg-devel] [PATCH pmg-api v3 1/2] api: statistics: add common
 method for details
X-BeenThere: pmg-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Mail Gateway development discussion
 <pmg-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pmg-devel>, 
 <mailto:pmg-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pmg-devel/>
List-Post: <mailto:pmg-devel@lists.proxmox.com>
List-Help: <mailto:pmg-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel>, 
 <mailto:pmg-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 02 Feb 2021 13:03:46 -0000

This patch changes adds a new method, which yields the detail statistics
for a particular address.

Depending on the provided type argument it returns the same information as
the contact/sender/receiver detail calls.

This allows the statistics to be displayed for addresses containing
'/' in their localpart, once this is permitted in our api schema.

the idea follows a similar change for the user blocklists in
e8d909c11faeb5a4f84f39ef50e0eaf8ea65046d

By adding a new API method we can eventually drop the old methods with 7.0

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
---
 src/PMG/API2/Statistics.pm | 63 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/src/PMG/API2/Statistics.pm b/src/PMG/API2/Statistics.pm
index 72bfeb5..7de6d78 100644
--- a/src/PMG/API2/Statistics.pm
+++ b/src/PMG/API2/Statistics.pm
@@ -44,6 +44,7 @@ __PACKAGE__->register_method ({
 
 	return [
 	    { name => "contact" },
+	    { name => "detail" },
 	    { name => "domains" },
 	    { name => "mail" },
 	    { name => "mailcount" },
@@ -322,6 +323,62 @@ sub get_detail_statistics {
     return $res;
 }
 
+__PACKAGE__->register_method ({
+    name => 'detailstats',
+    path => 'detail',
+    method => 'GET',
+    description => "Detailed Statistics.",
+    permissions => { check => [ 'admin', 'qmanager', 'audit'] },
+    parameters => {
+	additionalProperties => 0,
+	properties => $default_properties->({
+	    type => {
+		description => "Type of statistics",
+		type => 'string',
+		enum => [ 'contact', 'sender', 'receiver' ],
+	    },
+	    address => get_standard_option('pmg-email-address', {
+		description => "Email address.",
+	    }),
+	    filter => {
+		description => "Address filter.",
+		type => 'string',
+		maxLength => 512,
+		optional => 1,
+	    },
+	    orderby => $api_properties->{orderby},
+	}),
+    },
+    returns => {
+	type => 'array',
+	items => {
+	    type => "object",
+	    properties => $detail_return_properties->({
+		sender => {
+		    description => "Sender email. (for contact and receiver statistics)",
+		    type => 'string',
+		    optional => 1,
+		},
+		receiver => {
+		    description => "Receiver email. (for sender statistics)",
+		    type => 'string',
+		    optional => 1,
+		},
+	    }),
+	},
+    },
+    code => sub {
+	my ($param) = @_;
+
+	my $type = $param->{type};
+	$param->{$type} = $param->{address};
+
+	return get_detail_statistics($param->{type}, $param);
+    }});
+
+# FIXME: change for PMG 7.0 - remove support for providing addresses as path
+# addresses can contain '/' which breaks API path resolution - hardcode
+# 'detail' for the path pattern.
 __PACKAGE__->register_method ({
     name => 'contactdetails',
     path => 'contact/{contact}',
@@ -425,6 +482,9 @@ __PACKAGE__->register_method ({
 	return $res;
     }});
 
+# FIXME: change for PMG 7.0 - remove support for providing addresses as path
+# addresses can contain '/' which breaks API path resolution - hardcode
+# 'detail' for the path pattern.
 __PACKAGE__->register_method ({
     name => 'senderdetails',
     path => 'sender/{sender}',
@@ -536,6 +596,9 @@ __PACKAGE__->register_method ({
 	return $res;
     }});
 
+# FIXME: change for PMG 7.0 - remove support for providing addresses as path
+# addresses can contain '/' which breaks API path resolution - hardcode
+# 'detail' for the path pattern.
 __PACKAGE__->register_method ({
     name => 'receiverdetails',
     path => 'receiver/{receiver}',
-- 
2.20.1