1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-24 22:40:55 +01:00

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
This commit is contained in:
epriestley 2015-08-06 11:32:17 -07:00
parent 580790cd6e
commit 50e084dcda
3 changed files with 116 additions and 13 deletions

View file

@ -2257,6 +2257,7 @@ phutil_register_library_map(array(
'PhabricatorMailManagementSendTestWorkflow' => 'applications/metamta/management/PhabricatorMailManagementSendTestWorkflow.php', 'PhabricatorMailManagementSendTestWorkflow' => 'applications/metamta/management/PhabricatorMailManagementSendTestWorkflow.php',
'PhabricatorMailManagementShowInboundWorkflow' => 'applications/metamta/management/PhabricatorMailManagementShowInboundWorkflow.php', 'PhabricatorMailManagementShowInboundWorkflow' => 'applications/metamta/management/PhabricatorMailManagementShowInboundWorkflow.php',
'PhabricatorMailManagementShowOutboundWorkflow' => 'applications/metamta/management/PhabricatorMailManagementShowOutboundWorkflow.php', 'PhabricatorMailManagementShowOutboundWorkflow' => 'applications/metamta/management/PhabricatorMailManagementShowOutboundWorkflow.php',
'PhabricatorMailManagementVolumeWorkflow' => 'applications/metamta/management/PhabricatorMailManagementVolumeWorkflow.php',
'PhabricatorMailManagementWorkflow' => 'applications/metamta/management/PhabricatorMailManagementWorkflow.php', 'PhabricatorMailManagementWorkflow' => 'applications/metamta/management/PhabricatorMailManagementWorkflow.php',
'PhabricatorMailReceiver' => 'applications/metamta/receiver/PhabricatorMailReceiver.php', 'PhabricatorMailReceiver' => 'applications/metamta/receiver/PhabricatorMailReceiver.php',
'PhabricatorMailReceiverTestCase' => 'applications/metamta/receiver/__tests__/PhabricatorMailReceiverTestCase.php', 'PhabricatorMailReceiverTestCase' => 'applications/metamta/receiver/__tests__/PhabricatorMailReceiverTestCase.php',
@ -6183,6 +6184,7 @@ phutil_register_library_map(array(
'PhabricatorMailManagementSendTestWorkflow' => 'PhabricatorMailManagementWorkflow', 'PhabricatorMailManagementSendTestWorkflow' => 'PhabricatorMailManagementWorkflow',
'PhabricatorMailManagementShowInboundWorkflow' => 'PhabricatorMailManagementWorkflow', 'PhabricatorMailManagementShowInboundWorkflow' => 'PhabricatorMailManagementWorkflow',
'PhabricatorMailManagementShowOutboundWorkflow' => 'PhabricatorMailManagementWorkflow', 'PhabricatorMailManagementShowOutboundWorkflow' => 'PhabricatorMailManagementWorkflow',
'PhabricatorMailManagementVolumeWorkflow' => 'PhabricatorMailManagementWorkflow',
'PhabricatorMailManagementWorkflow' => 'PhabricatorManagementWorkflow', 'PhabricatorMailManagementWorkflow' => 'PhabricatorManagementWorkflow',
'PhabricatorMailReceiver' => 'Phobject', 'PhabricatorMailReceiver' => 'Phobject',
'PhabricatorMailReceiverTestCase' => 'PhabricatorTestCase', 'PhabricatorMailReceiverTestCase' => 'PhabricatorTestCase',

View file

@ -0,0 +1,79 @@
<?php
final class PhabricatorMailManagementVolumeWorkflow
extends PhabricatorMailManagementWorkflow {
protected function didConstruct() {
$this
->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;
}
}

View file

@ -7,6 +7,8 @@ final class PhabricatorMetaMTAMailQuery
private $phids; private $phids;
private $actorPHIDs; private $actorPHIDs;
private $recipientPHIDs; private $recipientPHIDs;
private $createdMin;
private $createdMax;
public function withIDs(array $ids) { public function withIDs(array $ids) {
$this->ids = $ids; $this->ids = $ids;
@ -28,53 +30,73 @@ final class PhabricatorMetaMTAMailQuery
return $this; return $this;
} }
public function withDateCreatedBetween($min, $max) {
$this->createdMin = $min;
$this->createdMax = $max;
return $this;
}
protected function loadPage() { protected function loadPage() {
return $this->loadStandardPage($this->newResultObject()); return $this->loadStandardPage($this->newResultObject());
} }
protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
$where = array(); $where = parent::buildWhereClauseParts($conn);
if ($this->ids !== null) { if ($this->ids !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn,
'mail.id IN (%Ld)', 'mail.id IN (%Ld)',
$this->ids); $this->ids);
} }
if ($this->phids !== null) { if ($this->phids !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn,
'mail.phid IN (%Ls)', 'mail.phid IN (%Ls)',
$this->phids); $this->phids);
} }
if ($this->actorPHIDs !== null) { if ($this->actorPHIDs !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn,
'mail.actorPHID IN (%Ls)', 'mail.actorPHID IN (%Ls)',
$this->actorPHIDs); $this->actorPHIDs);
} }
if ($this->recipientPHIDs !== null) { if ($this->recipientPHIDs !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn,
'recipient.dst IN (%Ls)', 'recipient.dst IN (%Ls)',
$this->recipientPHIDs); $this->recipientPHIDs);
} }
if ($this->actorPHIDs === null && $this->recipientPHIDs === null) { if ($this->actorPHIDs === null && $this->recipientPHIDs === null) {
$viewer = $this->getViewer(); $viewer = $this->getViewer();
$where[] = qsprintf( if (!$viewer->isOmnipotent()) {
$conn_r, $where[] = qsprintf(
'edge.dst = %s OR actorPHID = %s', $conn,
$viewer->getPHID(), 'edge.dst = %s OR actorPHID = %s',
$viewer->getPHID()); $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) { protected function buildJoinClause(AphrontDatabaseConnection $conn) {