Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
50.00% |
1 / 2 |
CRAP | |
92.86% |
26 / 28 |
TrackerSync | |
0.00% |
0 / 1 |
|
50.00% |
1 / 2 |
4.01 | |
92.86% |
26 / 28 |
handle | |
0.00% |
0 / 1 |
3.00 | |
92.00% |
23 / 25 |
|||
getLocalAndDistantDB | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\Tracker\Console\Commands; |
4 | |
5 | use Illuminate\Support\Str; |
6 | use Qmp\Laravel\CommandsLaravel\Middleware\Library\Monitoring\CreateParentProcess; |
7 | use Qmp\Laravel\CommandsLaravel\Middleware\Library\Monitoring\UpdateParentProcess; |
8 | use Qmp\Laravel\Tracker\Models\Config; |
9 | use Qmp\Laravel\Tracker\Services\DeployData\Senders\AbstractSender; |
10 | use Qmp\Laravel\DBConnector\Connectors\MongoConnector; |
11 | use Qmp\Laravel\ToolsLaravel\Traits\Timer; |
12 | |
13 | class TrackerSync extends \Commando |
14 | { |
15 | Use Timer; |
16 | /* |
17 | * The name and signature of the console command. |
18 | * |
19 | * @var string |
20 | */ |
21 | protected $signature = 'tracker:sync {configHash}'; |
22 | |
23 | /** |
24 | * The console command description. |
25 | * |
26 | * @var string |
27 | */ |
28 | protected $description = 'Sync distant tracker'; |
29 | |
30 | /** |
31 | * Undocumented variable |
32 | * |
33 | * @var integer |
34 | */ |
35 | protected $chunk = 10000; |
36 | |
37 | /** |
38 | * Command middlewares |
39 | * |
40 | * @var array |
41 | */ |
42 | protected $middleware = [ |
43 | CreateParentProcess::class, |
44 | UpdateParentProcess::class |
45 | ]; |
46 | |
47 | /** |
48 | * Execute the console command. |
49 | */ |
50 | public function handle() |
51 | { |
52 | $this->startTimer('total'); |
53 | |
54 | $data = $this->middlewareData(UpdateParentProcess::class) |
55 | ->transfert(CreateParentProcess::class, ['id']); |
56 | |
57 | $config = Config::findOrFail(['id' => $this->argument('configHash')])->first(); |
58 | |
59 | $this->info("Config tracker name : " . $config->name); |
60 | try { |
61 | [$localDB, $trackerDB] = $this->getLocalAndDistantDB($config); |
62 | |
63 | foreach ($localDB->getMongoDB()->listCollections() as $collection) { |
64 | |
65 | $collectionName = $collection->getName(); |
66 | |
67 | $this->info("Collection : " . $collectionName); |
68 | $distantCollection = $trackerDB->collection($collectionName); |
69 | $distantCollection->drop(); |
70 | |
71 | $currentLog = [ |
72 | 'collection' => $collectionName, |
73 | 'inserted' => 0 |
74 | ]; |
75 | |
76 | $datum = $localDB->collection($collectionName)->get(); |
77 | $datum = $datum->chunk($this->chunk)->each(function ($chunk) use ($distantCollection, &$currentLog) { |
78 | $this->line("Inserting " . $chunk->count() . " " . Str::plural("entry", $chunk->count()) . " ..."); |
79 | $currentLog['inserted'] += $chunk->count(); |
80 | |
81 | $distantCollection->insertMany($chunk->values()->toArray()); |
82 | }); |
83 | $this->line(''); |
84 | $data->log['synced'][] = $currentLog; |
85 | |
86 | } |
87 | |
88 | $data->log['duration'] = $this->getTimer('total'); |
89 | |
90 | } catch (\Exception $e) { |
91 | $data->error = $this->reportException($e); |
92 | } |
93 | } |
94 | |
95 | /** |
96 | * Undocumented function |
97 | * |
98 | * @param [type] $config |
99 | * @return void |
100 | */ |
101 | protected function getLocalAndDistantDB($config) |
102 | { |
103 | $trackerDB = new MongoConnector(); |
104 | $trackerDB->connection(AbstractSender::getDbConnectorConfig($config)); |
105 | |
106 | return [AbstractSender::getLocalDB(), $trackerDB]; |
107 | } |
108 | } |