Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 45 |
DedupReport | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
20 | |
0.00% |
0 / 45 |
process | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 43 |
|||
saveReport | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\Deduplication\Console\Commands; |
4 | |
5 | use Qmp\Laravel\Deduplication\Exceptions\AggregationFailed; |
6 | use Qmp\Laravel\Deduplication\Models\Session; |
7 | |
8 | class DedupReport extends BaseCommand |
9 | { |
10 | /** |
11 | * The name and signature of the console command. |
12 | * |
13 | * @var string |
14 | */ |
15 | protected $signature = 'dedup:report {sessionId} {--debug} {--throw-error : Throw errors if catch}'; |
16 | |
17 | /** |
18 | * The console command description. |
19 | * |
20 | * @var string |
21 | */ |
22 | protected $description = 'Generate Report after deduplication'; |
23 | |
24 | /** |
25 | * Undocumented variable |
26 | * |
27 | * @var array |
28 | */ |
29 | protected $jsFiles = ['report']; |
30 | |
31 | /** |
32 | * The current session model |
33 | */ |
34 | protected $sessionConfig; |
35 | |
36 | /** |
37 | * Error report structure |
38 | * |
39 | * @var array |
40 | */ |
41 | protected $errorReport = [ |
42 | 'input_count' => 0, |
43 | 'duplicates_on_advertiser_list' => 0, |
44 | 'duplicates_intra_file' => 0, |
45 | 'scoring' => '', |
46 | 'duplicates_between_all' => 0, |
47 | 'output_count' => 0, |
48 | 'diff' => '0%', |
49 | 'errors' => null, |
50 | 'website' => '', |
51 | 'input_filename' => '', |
52 | 'output_filename' => '', |
53 | 'uploaded_at' => '' |
54 | ]; |
55 | |
56 | /** |
57 | * Execute the aggregation |
58 | * |
59 | * @return void |
60 | */ |
61 | protected function process(): void |
62 | { |
63 | $this->info("Building report ..."); |
64 | if ($this->aggregationResult) { |
65 | |
66 | $this->sessionConfig = Session::findOrFail($this->argument('sessionId')); |
67 | $session = collect($this->sessionConfig)->recursive(); |
68 | |
69 | $allReports = collect($this->aggregationResult)->recursive()->first(); |
70 | $allReports->forget('_id')->get('allReports')->map(function ($UserReports) use ($session) { |
71 | |
72 | $publisherDatas = $session->get('sites') |
73 | ->where('user_id', $UserReports->get('user_id')) |
74 | ->first(); |
75 | $UserReports->put('entity', $publisherDatas->get('user_name')); |
76 | |
77 | $UserReports->get('reports')->map(function ($report) use ($session) { |
78 | $siteData = $session->get('sites')->where('id', $report->get('site_id'))->first(); |
79 | $report->put('errors', null); |
80 | $report->put('scoring', $siteData->get('ponderation')); |
81 | $report->put('website', $siteData->get('name')); |
82 | $report->put('input_filename', pathinfo($siteData->dotGet('input_file.base'))['basename']); |
83 | $report->put('output_filename', pathinfo($siteData->dotGet('output_file.file'))['basename']); |
84 | $report->put('uploaded_at', $siteData->dotGet('input_file.uploaded_at')); |
85 | |
86 | $sign = $report->get('diff') > 0 ? '-' : ''; |
87 | $report->put('diff', $sign . $report->get('diff') . '%'); |
88 | }); |
89 | |
90 | // ERRORS |
91 | $session->get('sites') |
92 | ->where('user_id', $UserReports->get('user_id')) |
93 | ->whereNotNull('input_file.errors') |
94 | ->map(function ($errorSite) use ($session) { |
95 | |
96 | $siteData = $session->get('sites')->where('id', $errorSite->get('id'))->first(); |
97 | |
98 | $currentReport = collect([ |
99 | 'input_count' => (int) $errorSite->dotGet('input_file.count'), |
100 | 'scoring' => $errorSite->get('ponderation'), |
101 | 'website' => $siteData->get('name'), |
102 | 'input_filename' => pathinfo($siteData->dotGet('input_file.base'))['basename'], |
103 | 'uploaded_at' => $siteData->dotGet('input_file.uploaded_at'), |
104 | 'errors' => $errorSite->dotGet('input_file.errors'), |
105 | 'site_id' => $errorSite->get('id') |
106 | ]); |
107 | |
108 | return collect($this->errorReport)->merge($currentReport); |
109 | })->each(function ($error) use ($UserReports) { |
110 | $UserReports->get('reports')->push($error); |
111 | }); |
112 | $UserReports->forget('user_id'); |
113 | }); |
114 | |
115 | $this->saveReport($allReports->toArray()); |
116 | } else { |
117 | throw new AggregationFailed("No aggregation results"); |
118 | } |
119 | } |
120 | |
121 | /** |
122 | * Undocumented function |
123 | * |
124 | * @param array $report |
125 | * @return void |
126 | */ |
127 | protected function saveReport(array $report): void |
128 | { |
129 | $this->sessionConfig->update(['report' => $report, 'lastDedup' => (now())->toDateTimeString()]); |
130 | } |
131 | } |