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