1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EmailCampaigns\Commands; |
4
|
|
|
|
5
|
|
|
use Carbon\CarbonInterval; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Spatie\EmailCampaigns\Models\Campaign; |
9
|
|
|
use Spatie\EmailCampaigns\Jobs\CalculateStatisticsJob; |
10
|
|
|
|
11
|
|
|
class CalculateStatisticsCommand extends Command |
12
|
|
|
{ |
13
|
|
|
public $signature = 'email-campaigns:calculate-statistics {campaignId?}'; |
14
|
|
|
|
15
|
|
|
public $description = 'Calculate the statistics of the recently sent campaigns'; |
16
|
|
|
|
17
|
|
|
/** @var \Illuminate\Support\Carbon */ |
18
|
|
|
protected $now; |
19
|
|
|
|
20
|
|
|
public function handle() |
21
|
|
|
{ |
22
|
|
|
$this->comment('Start calculating statistics...'); |
23
|
|
|
|
24
|
|
|
$campaignId = $this->argument('campaignId'); |
25
|
|
|
|
26
|
|
|
$campaignId |
27
|
|
|
? dispatch_now(new CalculateStatisticsJob(Campaign::find($campaignId))) |
28
|
|
|
: $this->calculateStatisticsOfRecentCampaigns(); |
29
|
|
|
|
30
|
|
|
$this->comment('All done!'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function calculateStatisticsOfRecentCampaigns(): void |
34
|
|
|
{ |
35
|
|
|
$this->now = now(); |
36
|
|
|
|
37
|
|
|
collect([ |
38
|
|
|
[CarbonInterval::minute(0), CarbonInterval::minute(5), CarbonInterval::minute(0)], |
39
|
|
|
[CarbonInterval::minute(5), CarbonInterval::hour(2), CarbonInterval::minute(10)], |
40
|
|
|
[CarbonInterval::hour(2), CarbonInterval::day(), CarbonInterval::hour()], |
41
|
|
|
[CarbonInterval::day(), CarbonInterval::weeks(2), CarbonInterval::hour(4)], |
42
|
|
|
])->eachSpread(function (CarbonInterval $startInterval, CarbonInterval $endInterval, CarbonInterval $recalculateThreshold) { |
43
|
|
|
$this |
44
|
|
|
->findCampaignsWithStatisticsToRecalculate($startInterval, $endInterval, $recalculateThreshold) |
45
|
|
|
->each(function (Campaign $campaign) { |
46
|
|
|
$this->info("Calculating statistics for campaign id {$campaign->id}..."); |
47
|
|
|
dispatch(new CalculateStatisticsJob($campaign)); |
48
|
|
|
}); |
49
|
|
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function findCampaignsWithStatisticsToRecalculate( |
53
|
|
|
CarbonInterval $startInterval, |
54
|
|
|
CarbonInterval $endInterval, |
55
|
|
|
CarbonInterval $recalculateThreshold |
56
|
|
|
): Collection { |
57
|
|
|
$periodEnd = $this->now->copy()->subtract($startInterval); |
58
|
|
|
$periodStart = $this->now->copy()->subtract($endInterval); |
59
|
|
|
|
60
|
|
|
return Campaign::sentBetween($periodStart, $periodEnd) |
61
|
|
|
->get() |
62
|
|
|
->filter(function (Campaign $campaign) use ($periodEnd, $periodStart, $recalculateThreshold) { |
63
|
|
|
if (is_null($campaign->statistics_calculated_at)) { |
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$threshold = $this->now->copy()->subtract($recalculateThreshold); |
68
|
|
|
|
69
|
|
|
return $campaign->statistics_calculated_at->isBefore($threshold); |
70
|
|
|
}); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|