Differential D13878 Diff 33512 src/applications/metamta/management/PhabricatorMailManagementVolumeWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/metamta/management/PhabricatorMailManagementVolumeWorkflow.php
Show All 22 Lines | public function execute(PhutilArgumentParser $args) { | ||||
$until = PhabricatorTime::getNow(); | $until = PhabricatorTime::getNow(); | ||||
$mails = id(new PhabricatorMetaMTAMailQuery()) | $mails = id(new PhabricatorMetaMTAMailQuery()) | ||||
->setViewer($viewer) | ->setViewer($viewer) | ||||
->withDateCreatedBetween($since, $until) | ->withDateCreatedBetween($since, $until) | ||||
->execute(); | ->execute(); | ||||
$unfiltered = array(); | $unfiltered = array(); | ||||
$delivered = array(); | |||||
foreach ($mails as $mail) { | foreach ($mails as $mail) { | ||||
// Count messages we attempted to deliver. This includes messages which | |||||
// were voided by preferences or other rules. | |||||
$unfiltered_actors = mpull($mail->loadAllActors(), 'getPHID'); | $unfiltered_actors = mpull($mail->loadAllActors(), 'getPHID'); | ||||
foreach ($unfiltered_actors as $phid) { | foreach ($unfiltered_actors as $phid) { | ||||
if (empty($unfiltered[$phid])) { | if (empty($unfiltered[$phid])) { | ||||
$unfiltered[$phid] = 0; | $unfiltered[$phid] = 0; | ||||
} | } | ||||
$unfiltered[$phid]++; | $unfiltered[$phid]++; | ||||
} | } | ||||
// Now, count mail we actually delivered. | |||||
$result = $mail->getDeliveredActors(); | |||||
if ($result) { | |||||
foreach ($result as $actor_phid => $actor_info) { | |||||
if (!$actor_info['deliverable']) { | |||||
continue; | |||||
} | |||||
if (empty($delivered[$actor_phid])) { | |||||
$delivered[$actor_phid] = 0; | |||||
} | |||||
$delivered[$actor_phid]++; | |||||
} | |||||
} | |||||
} | } | ||||
// Sort users by delivered mail, then unfiltered mail. | |||||
arsort($delivered); | |||||
arsort($unfiltered); | arsort($unfiltered); | ||||
$delivered = $delivered + array_fill_keys(array_keys($unfiltered), 0); | |||||
$table = id(new PhutilConsoleTable()) | $table = id(new PhutilConsoleTable()) | ||||
->setBorders(true) | ->setBorders(true) | ||||
->addColumn( | ->addColumn( | ||||
'user', | 'user', | ||||
array( | array( | ||||
'title' => pht('User'), | 'title' => pht('User'), | ||||
)) | )) | ||||
->addColumn( | ->addColumn( | ||||
'unfiltered', | 'unfiltered', | ||||
array( | array( | ||||
'title' => pht('Unfiltered'), | 'title' => pht('Unfiltered'), | ||||
)) | |||||
->addColumn( | |||||
'delivered', | |||||
array( | |||||
'title' => pht('Delivered'), | |||||
)); | )); | ||||
$handles = $viewer->loadHandles(array_keys($unfiltered)); | $handles = $viewer->loadHandles(array_keys($unfiltered)); | ||||
$names = mpull(iterator_to_array($handles), 'getName', 'getPHID'); | $names = mpull(iterator_to_array($handles), 'getName', 'getPHID'); | ||||
foreach ($unfiltered as $phid => $count) { | foreach ($delivered as $phid => $delivered_count) { | ||||
$unfiltered_count = idx($unfiltered, $phid, 0); | |||||
$table->addRow( | $table->addRow( | ||||
array( | array( | ||||
'user' => idx($names, $phid), | 'user' => idx($names, $phid), | ||||
'unfiltered' => $count, | 'unfiltered' => $unfiltered_count, | ||||
'delivered' => $delivered_count, | |||||
)); | )); | ||||
} | } | ||||
$table->draw(); | $table->draw(); | ||||
echo "\n"; | echo "\n"; | ||||
echo pht('Mail sent in the last 30 days.')."\n"; | echo pht('Mail sent in the last 30 days.')."\n"; | ||||
echo pht( | echo pht( | ||||
'"Unfiltered" is raw volume before preferences were applied.')."\n"; | '"Unfiltered" is raw volume before rules applied.')."\n"; | ||||
echo pht( | |||||
'"Delivered" shows email actually sent.')."\n"; | |||||
echo "\n"; | echo "\n"; | ||||
return 0; | return 0; | ||||
} | } | ||||
} | } |