Path: blob/master/src/applications/metamta/management/PhabricatorMailManagementVolumeWorkflow.php
12256 views
<?php12final class PhabricatorMailManagementVolumeWorkflow3extends PhabricatorMailManagementWorkflow {45protected function didConstruct() {6$this7->setName('volume')8->setSynopsis(9pht('Show how much mail users have received recently.'))10->setExamples(11'**volume**')12->setArguments(13array(14array(15'name' => 'days',16'param' => 'days',17'default' => 30,18'help' => pht(19'Number of days back (default 30).'),20),21));22}2324public function execute(PhutilArgumentParser $args) {25$console = PhutilConsole::getConsole();26$viewer = $this->getViewer();2728$days = (int)$args->getArg('days');29if ($days < 1) {30throw new PhutilArgumentUsageException(31pht(32'Period specified with --days must be at least 1.'));33}3435$duration = phutil_units("{$days} days in seconds");3637$since = (PhabricatorTime::getNow() - $duration);38$until = PhabricatorTime::getNow();3940$mails = id(new PhabricatorMetaMTAMailQuery())41->setViewer($viewer)42->withDateCreatedBetween($since, $until)43->execute();4445$unfiltered = array();46$delivered = array();4748foreach ($mails as $mail) {49// Count messages we attempted to deliver. This includes messages which50// were voided by preferences or other rules.51$unfiltered_actors = mpull($mail->loadAllActors(), 'getPHID');52foreach ($unfiltered_actors as $phid) {53if (empty($unfiltered[$phid])) {54$unfiltered[$phid] = 0;55}56$unfiltered[$phid]++;57}5859// Now, count mail we actually delivered.60$result = $mail->getDeliveredActors();61if ($result) {62foreach ($result as $actor_phid => $actor_info) {63if (!$actor_info['deliverable']) {64continue;65}66if (empty($delivered[$actor_phid])) {67$delivered[$actor_phid] = 0;68}69$delivered[$actor_phid]++;70}71}72}7374// Sort users by delivered mail, then unfiltered mail.75arsort($delivered);76arsort($unfiltered);77$delivered = $delivered + array_fill_keys(array_keys($unfiltered), 0);7879$table = id(new PhutilConsoleTable())80->setBorders(true)81->addColumn(82'user',83array(84'title' => pht('User'),85))86->addColumn(87'unfiltered',88array(89'title' => pht('Unfiltered'),90))91->addColumn(92'delivered',93array(94'title' => pht('Delivered'),95));9697$handles = $viewer->loadHandles(array_keys($unfiltered));98$names = mpull(iterator_to_array($handles), 'getName', 'getPHID');99100foreach ($delivered as $phid => $delivered_count) {101$unfiltered_count = idx($unfiltered, $phid, 0);102$table->addRow(103array(104'user' => idx($names, $phid),105'unfiltered' => $unfiltered_count,106'delivered' => $delivered_count,107));108}109110$table->draw();111112echo "\n";113echo pht(114'Mail sent in the last %s day(s).',115new PhutilNumber($days))."\n";116echo pht(117'"Unfiltered" is raw volume before rules applied.')."\n";118echo pht(119'"Delivered" shows email actually sent.')."\n";120echo "\n";121122return 0;123}124125}126127128