Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 24 |
| TransferToColdBase | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 24 |
| handle | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 24 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\Deduplication\Console\Commands; |
| 4 | |
| 5 | use Exception; |
| 6 | use MongoDB\BSON\UTCDateTime; |
| 7 | use MongoDB\Driver\WriteConcern; |
| 8 | use Qmp\Laravel\CommandsLaravel\Middleware\Library\Monitoring\CreateParentProcess; |
| 9 | use Qmp\Laravel\CommandsLaravel\Middleware\Library\Monitoring\UpdateParentProcess; |
| 10 | use Qmp\Laravel\Deduplication\Models\SessionData; |
| 11 | use Qmp\Laravel\ToolsLaravel\Traits\Timer; |
| 12 | |
| 13 | class TransferToColdBase extends \Commando |
| 14 | { |
| 15 | use Timer; |
| 16 | |
| 17 | const DAYS_TO_KEEP = 90; |
| 18 | |
| 19 | /** |
| 20 | * The name and signature of the console command. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | protected $signature = 'dedup:cold-base'; |
| 25 | |
| 26 | /** |
| 27 | * The console command description. |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | protected $description = 'Transfer old data to cold collection'; |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * The command middlewares |
| 36 | * |
| 37 | * @var array |
| 38 | */ |
| 39 | protected $middleware = [ |
| 40 | CreateParentProcess::class, |
| 41 | UpdateParentProcess::class |
| 42 | ]; |
| 43 | |
| 44 | /** |
| 45 | * Execute the console command. |
| 46 | * |
| 47 | * @return mixed |
| 48 | */ |
| 49 | public function handle(): void |
| 50 | { |
| 51 | |
| 52 | $data = $this->middlewareData(UpdateParentProcess::class) |
| 53 | ->transfert(CreateParentProcess::class, ['id']); |
| 54 | |
| 55 | try { |
| 56 | $this->startTimer('total'); |
| 57 | |
| 58 | $dateToTransfer = new \DateTime(); |
| 59 | $dateToTransfer->modify('-' . self::DAYS_TO_KEEP . ' days'); |
| 60 | |
| 61 | $wc = new WriteConcern(0); |
| 62 | $utc = new UTCDateTime($dateToTransfer); |
| 63 | |
| 64 | SessionData::pipeline() |
| 65 | ->match('updated_at', ['$lt' => $utc]) |
| 66 | ->merge([ |
| 67 | 'into' => 'session_data_cold', |
| 68 | 'on' => '_id' |
| 69 | ]) |
| 70 | ->aggregate([ |
| 71 | 'allowDiskUse' => true, |
| 72 | "writeConcern" => $wc |
| 73 | ]); |
| 74 | |
| 75 | $old = SessionData::where(['updated_at' => ['$lt' => $utc]]); |
| 76 | $deleted = 0; |
| 77 | |
| 78 | if($old->count()) { |
| 79 | $deleted = $old->delete(["writeConcern" => $wc]); |
| 80 | } |
| 81 | |
| 82 | $data->log = []; |
| 83 | $data->log['archived'] = $deleted; |
| 84 | $data->log['duration'] = $this->getTimer('total'); |
| 85 | } catch (Exception $e) { |
| 86 | $data->error = $this->reportException($e); |
| 87 | } |
| 88 | } |
| 89 | } |