From 50e084dcda9c33e2cf18c3c4d56eec0457886a3c Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 6 Aug 2015 11:32:17 -0700 Subject: [PATCH] Add a rough `bin/mail volume` command for showing mail volume Summary: Ref T7013. This might help us understand the problem better. Test Plan: ``` $ ./bin/mail volume +==============+============+ | User | Unfiltered | +==============+============+ | admin | 136 | | dog | 31 | | epriestley | 24 | | ducksey | 18 | | saurus | 8 | | example-list | 7 | | squeakybirdo | 3 | | nnn | 3 | | facebooker | 2 | +==============+============+ Mail sent in the last 30 days. "Unfiltered" is raw volume before preferences were applied. ``` Reviewers: chad Reviewed By: chad Maniphest Tasks: T7013 Differential Revision: https://secure.phabricator.com/D13813 --- src/__phutil_library_map__.php | 2 + ...habricatorMailManagementVolumeWorkflow.php | 79 +++++++++++++++++++ .../query/PhabricatorMetaMTAMailQuery.php | 48 ++++++++--- 3 files changed, 116 insertions(+), 13 deletions(-) create mode 100644 src/applications/metamta/management/PhabricatorMailManagementVolumeWorkflow.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index fbdbc739fa..296a4d7703 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -2257,6 +2257,7 @@ phutil_register_library_map(array( 'PhabricatorMailManagementSendTestWorkflow' => 'applications/metamta/management/PhabricatorMailManagementSendTestWorkflow.php', 'PhabricatorMailManagementShowInboundWorkflow' => 'applications/metamta/management/PhabricatorMailManagementShowInboundWorkflow.php', 'PhabricatorMailManagementShowOutboundWorkflow' => 'applications/metamta/management/PhabricatorMailManagementShowOutboundWorkflow.php', + 'PhabricatorMailManagementVolumeWorkflow' => 'applications/metamta/management/PhabricatorMailManagementVolumeWorkflow.php', 'PhabricatorMailManagementWorkflow' => 'applications/metamta/management/PhabricatorMailManagementWorkflow.php', 'PhabricatorMailReceiver' => 'applications/metamta/receiver/PhabricatorMailReceiver.php', 'PhabricatorMailReceiverTestCase' => 'applications/metamta/receiver/__tests__/PhabricatorMailReceiverTestCase.php', @@ -6183,6 +6184,7 @@ phutil_register_library_map(array( 'PhabricatorMailManagementSendTestWorkflow' => 'PhabricatorMailManagementWorkflow', 'PhabricatorMailManagementShowInboundWorkflow' => 'PhabricatorMailManagementWorkflow', 'PhabricatorMailManagementShowOutboundWorkflow' => 'PhabricatorMailManagementWorkflow', + 'PhabricatorMailManagementVolumeWorkflow' => 'PhabricatorMailManagementWorkflow', 'PhabricatorMailManagementWorkflow' => 'PhabricatorManagementWorkflow', 'PhabricatorMailReceiver' => 'Phobject', 'PhabricatorMailReceiverTestCase' => 'PhabricatorTestCase', diff --git a/src/applications/metamta/management/PhabricatorMailManagementVolumeWorkflow.php b/src/applications/metamta/management/PhabricatorMailManagementVolumeWorkflow.php new file mode 100644 index 0000000000..c907bf1050 --- /dev/null +++ b/src/applications/metamta/management/PhabricatorMailManagementVolumeWorkflow.php @@ -0,0 +1,79 @@ +setName('volume') + ->setSynopsis( + pht('Show how much mail users have received in the last 30 days.')) + ->setExamples( + '**volume**') + ->setArguments( + array( + )); + } + + public function execute(PhutilArgumentParser $args) { + $console = PhutilConsole::getConsole(); + $viewer = $this->getViewer(); + + $since = (PhabricatorTime::getNow() - phutil_units('30 days in seconds')); + $until = PhabricatorTime::getNow(); + + $mails = id(new PhabricatorMetaMTAMailQuery()) + ->setViewer($viewer) + ->withDateCreatedBetween($since, $until) + ->execute(); + + $unfiltered = array(); + + foreach ($mails as $mail) { + $unfiltered_actors = mpull($mail->loadAllActors(), 'getPHID'); + foreach ($unfiltered_actors as $phid) { + if (empty($unfiltered[$phid])) { + $unfiltered[$phid] = 0; + } + $unfiltered[$phid]++; + } + } + + arsort($unfiltered); + + $table = id(new PhutilConsoleTable()) + ->setBorders(true) + ->addColumn( + 'user', + array( + 'title' => pht('User'), + )) + ->addColumn( + 'unfiltered', + array( + 'title' => pht('Unfiltered'), + )); + + $handles = $viewer->loadHandles(array_keys($unfiltered)); + $names = mpull(iterator_to_array($handles), 'getName', 'getPHID'); + + foreach ($unfiltered as $phid => $count) { + $table->addRow( + array( + 'user' => idx($names, $phid), + 'unfiltered' => $count, + )); + } + + $table->draw(); + + echo "\n"; + echo pht('Mail sent in the last 30 days.')."\n"; + echo pht( + '"Unfiltered" is raw volume before preferences were applied.')."\n"; + echo "\n"; + + return 0; + } + +} diff --git a/src/applications/metamta/query/PhabricatorMetaMTAMailQuery.php b/src/applications/metamta/query/PhabricatorMetaMTAMailQuery.php index a0965b7133..e0dadb5f13 100644 --- a/src/applications/metamta/query/PhabricatorMetaMTAMailQuery.php +++ b/src/applications/metamta/query/PhabricatorMetaMTAMailQuery.php @@ -7,6 +7,8 @@ final class PhabricatorMetaMTAMailQuery private $phids; private $actorPHIDs; private $recipientPHIDs; + private $createdMin; + private $createdMax; public function withIDs(array $ids) { $this->ids = $ids; @@ -28,53 +30,73 @@ final class PhabricatorMetaMTAMailQuery return $this; } + public function withDateCreatedBetween($min, $max) { + $this->createdMin = $min; + $this->createdMax = $max; + return $this; + } + protected function loadPage() { return $this->loadStandardPage($this->newResultObject()); } - protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { - $where = array(); + protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { + $where = parent::buildWhereClauseParts($conn); if ($this->ids !== null) { $where[] = qsprintf( - $conn_r, + $conn, 'mail.id IN (%Ld)', $this->ids); } if ($this->phids !== null) { $where[] = qsprintf( - $conn_r, + $conn, 'mail.phid IN (%Ls)', $this->phids); } if ($this->actorPHIDs !== null) { $where[] = qsprintf( - $conn_r, + $conn, 'mail.actorPHID IN (%Ls)', $this->actorPHIDs); } if ($this->recipientPHIDs !== null) { $where[] = qsprintf( - $conn_r, + $conn, 'recipient.dst IN (%Ls)', $this->recipientPHIDs); } if ($this->actorPHIDs === null && $this->recipientPHIDs === null) { $viewer = $this->getViewer(); - $where[] = qsprintf( - $conn_r, - 'edge.dst = %s OR actorPHID = %s', - $viewer->getPHID(), - $viewer->getPHID()); + if (!$viewer->isOmnipotent()) { + $where[] = qsprintf( + $conn, + 'edge.dst = %s OR actorPHID = %s', + $viewer->getPHID(), + $viewer->getPHID()); + } } - $where[] = $this->buildPagingClause($conn_r); + if ($this->createdMin !== null) { + $where[] = qsprintf( + $conn, + 'mail.dateCreated >= %d', + $this->createdMin); + } - return $this->formatWhereClause($where); + if ($this->createdMax !== null) { + $where[] = qsprintf( + $conn, + 'mail.dateCreated <= %d', + $this->createdMax); + } + + return $where; } protected function buildJoinClause(AphrontDatabaseConnection $conn) {