@@ -27,14 +27,20 @@ class ImportCSV implements ShouldQueue
27
27
*/
28
28
protected $ meta ;
29
29
30
+ protected $ iterationCount ;
31
+
32
+ protected $ useOldApproach ;
33
+
30
34
/**
31
35
* Create a new job instance.
32
36
*
33
37
* @return void
34
38
*/
35
- public function __construct (ImportMeta $ meta )
39
+ public function __construct (ImportMeta $ meta, bool $ useOldApproach = false , int $ iterationCount = 10 )
36
40
{
37
41
$ this ->meta = $ meta ;
42
+ $ this ->useOldApproach = $ useOldApproach ;
43
+ $ this ->iterationCount = $ iterationCount ;
38
44
}
39
45
40
46
/**
@@ -45,15 +51,23 @@ public function __construct(ImportMeta $meta)
45
51
public function handle (CSVImportReader $ reader )
46
52
{
47
53
Log::info ("======= Start import CSV ======= " );
54
+ $ filepath = Storage::disk ('local ' )
55
+ ->path ('mismatch-files/ ' . $ this ->meta ->filename );
56
+ $ lineCount = $ reader ->lines ($ filepath )->count ();
57
+
48
58
$ start = hrtime (true );
49
59
$ maxTimespan = 0.0 ;
50
60
$ minTimespan = 999999999.0 ;
51
61
52
62
$ totalTimespan = 0.0 ;
53
- $ iterationCount = 10 ;
63
+ $ iterationCount = $ this -> iterationCount ;
54
64
for ($ i = 0 ; $ i < $ iterationCount ; $ i ++) {
55
65
$ startIteration = hrtime (true );
56
- $ this ->handleOld ($ reader );
66
+ if (!$ this ->useOldApproach ) {
67
+ $ this ->handleNew ($ reader );
68
+ } else {
69
+ $ this ->handleOld ($ reader );
70
+ }
57
71
$ endIteration = (hrtime (true ) - $ startIteration ) / 1000000 ;
58
72
if ($ endIteration > $ maxTimespan ) {
59
73
$ maxTimespan = $ endIteration ;
@@ -65,14 +79,17 @@ public function handle(CSVImportReader $reader)
65
79
}
66
80
$ meanTimespan = $ totalTimespan / $ iterationCount ;
67
81
$ timespan = (hrtime (true ) - $ start ) / 1000000 ;
68
- Log::info ("Iteration count: \t {$ iterationCount }" );
82
+ $ approach = $ this ->useOldApproach ? 'Old ' : 'New ' ;
83
+ Log::info ("Approach: \t\t $ approach " );
84
+ Log::info ("Mismatch count: \t $ lineCount " );
85
+ Log::info ("Iteration count: \t $ iterationCount " );
69
86
Log::info ("Shortest timespan: \t {$ minTimespan }ms " );
70
87
Log::info ("Longest timespan: \t {$ maxTimespan }ms " );
71
88
Log::info ("Average timespan: \t {$ meanTimespan }ms " );
72
89
Log::info ("======= End import CSV in {$ timespan }ms ======= " );
73
90
}
74
91
75
- private function handleOld (CSVImportReader $ reader )
92
+ private function handleNew (CSVImportReader $ reader )
76
93
{
77
94
$ filepath = Storage::disk ('local ' )
78
95
->path ('mismatch-files/ ' . $ this ->meta ->filename );
@@ -111,6 +128,27 @@ private function handleOld(CSVImportReader $reader)
111
128
});
112
129
}
113
130
131
+
132
+ private function handleOld (CSVImportReader $ reader )
133
+ {
134
+ $ filepath = Storage::disk ('local ' )
135
+ ->path ('mismatch-files/ ' . $ this ->meta ->filename );
136
+
137
+ DB ::transaction (function () use ($ reader , $ filepath ) {
138
+ $ reader ->lines ($ filepath )->each (function ($ mismatchLine ) {
139
+ $ mismatch = Mismatch::make ($ mismatchLine );
140
+ if ($ mismatch ->type == null ) {
141
+ $ mismatch ->type = 'statement ' ;
142
+ }
143
+ $ mismatch ->importMeta ()->associate ($ this ->meta );
144
+ $ mismatch ->save ();
145
+ });
146
+
147
+ $ this ->meta ->status = 'completed ' ;
148
+ $ this ->meta ->save ();
149
+ });
150
+ }
151
+
114
152
/**
115
153
* Handle a job failure.
116
154
*
0 commit comments