Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/metamta/management/PhabricatorMailManagementVolumeWorkflow.php
12256 views
1
<?php
2
3
final class PhabricatorMailManagementVolumeWorkflow
4
extends PhabricatorMailManagementWorkflow {
5
6
protected function didConstruct() {
7
$this
8
->setName('volume')
9
->setSynopsis(
10
pht('Show how much mail users have received recently.'))
11
->setExamples(
12
'**volume**')
13
->setArguments(
14
array(
15
array(
16
'name' => 'days',
17
'param' => 'days',
18
'default' => 30,
19
'help' => pht(
20
'Number of days back (default 30).'),
21
),
22
));
23
}
24
25
public function execute(PhutilArgumentParser $args) {
26
$console = PhutilConsole::getConsole();
27
$viewer = $this->getViewer();
28
29
$days = (int)$args->getArg('days');
30
if ($days < 1) {
31
throw new PhutilArgumentUsageException(
32
pht(
33
'Period specified with --days must be at least 1.'));
34
}
35
36
$duration = phutil_units("{$days} days in seconds");
37
38
$since = (PhabricatorTime::getNow() - $duration);
39
$until = PhabricatorTime::getNow();
40
41
$mails = id(new PhabricatorMetaMTAMailQuery())
42
->setViewer($viewer)
43
->withDateCreatedBetween($since, $until)
44
->execute();
45
46
$unfiltered = array();
47
$delivered = array();
48
49
foreach ($mails as $mail) {
50
// Count messages we attempted to deliver. This includes messages which
51
// were voided by preferences or other rules.
52
$unfiltered_actors = mpull($mail->loadAllActors(), 'getPHID');
53
foreach ($unfiltered_actors as $phid) {
54
if (empty($unfiltered[$phid])) {
55
$unfiltered[$phid] = 0;
56
}
57
$unfiltered[$phid]++;
58
}
59
60
// Now, count mail we actually delivered.
61
$result = $mail->getDeliveredActors();
62
if ($result) {
63
foreach ($result as $actor_phid => $actor_info) {
64
if (!$actor_info['deliverable']) {
65
continue;
66
}
67
if (empty($delivered[$actor_phid])) {
68
$delivered[$actor_phid] = 0;
69
}
70
$delivered[$actor_phid]++;
71
}
72
}
73
}
74
75
// Sort users by delivered mail, then unfiltered mail.
76
arsort($delivered);
77
arsort($unfiltered);
78
$delivered = $delivered + array_fill_keys(array_keys($unfiltered), 0);
79
80
$table = id(new PhutilConsoleTable())
81
->setBorders(true)
82
->addColumn(
83
'user',
84
array(
85
'title' => pht('User'),
86
))
87
->addColumn(
88
'unfiltered',
89
array(
90
'title' => pht('Unfiltered'),
91
))
92
->addColumn(
93
'delivered',
94
array(
95
'title' => pht('Delivered'),
96
));
97
98
$handles = $viewer->loadHandles(array_keys($unfiltered));
99
$names = mpull(iterator_to_array($handles), 'getName', 'getPHID');
100
101
foreach ($delivered as $phid => $delivered_count) {
102
$unfiltered_count = idx($unfiltered, $phid, 0);
103
$table->addRow(
104
array(
105
'user' => idx($names, $phid),
106
'unfiltered' => $unfiltered_count,
107
'delivered' => $delivered_count,
108
));
109
}
110
111
$table->draw();
112
113
echo "\n";
114
echo pht(
115
'Mail sent in the last %s day(s).',
116
new PhutilNumber($days))."\n";
117
echo pht(
118
'"Unfiltered" is raw volume before rules applied.')."\n";
119
echo pht(
120
'"Delivered" shows email actually sent.')."\n";
121
echo "\n";
122
123
return 0;
124
}
125
126
}
127
128