Skip to content

Commit b628264

Browse files
committed
fixed lines counter for exported files
1 parent 40b3b60 commit b628264

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

Service/ExportService.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,8 @@ public function exportIndex(
6262
$progress->setRedrawFrequency(100);
6363
$progress->start();
6464

65-
$counter = 0;
66-
$fileCounter = 1;
67-
68-
if ($results->count() <= $maxLinesInFile) {
69-
$count = $results->count();
70-
} elseif (($results->count() - ($fileCounter * $maxLinesInFile)) > $maxLinesInFile) {
71-
$count = $results->count() - ($fileCounter * $maxLinesInFile);
72-
} else {
73-
$count = $maxLinesInFile;
74-
}
65+
$counter = $fileCounter = 0;
66+
$count = $this->getFileCount($results->count(), $maxLinesInFile, $fileCounter);
7567

7668
$date = date(\DateTime::ISO8601);
7769
$metadata = [
@@ -87,13 +79,13 @@ public function exportIndex(
8779
$writer->finalize();
8880
$writer = null;
8981
$fileCounter++;
90-
$counter = 0;
91-
82+
$count = $this->getFileCount($results->count(), $maxLinesInFile, $fileCounter);
9283
$metadata = [
9384
'count' => $count,
9485
'date' => $date,
9586
];
9687
$writer = $this->getWriter($this->getFilePath($filename."_".$fileCounter.".json"), $metadata);
88+
$counter = 0;
9789
}
9890

9991
$doc = array_intersect_key($data, array_flip(['_id', '_type', '_source', 'fields']));
@@ -135,4 +127,23 @@ protected function getWriter($filename, $metadata)
135127
{
136128
return new JsonWriter($filename, $metadata);
137129
}
130+
131+
/**
132+
* @param int $resultsCount
133+
* @param int $maxLinesInFile
134+
* @param int $fileCounter
135+
*
136+
* @return int
137+
*/
138+
protected function getFileCount($resultsCount, $maxLinesInFile, $fileCounter)
139+
{
140+
$leftToInsert = $resultsCount - ($fileCounter * $maxLinesInFile);
141+
if ($leftToInsert <= $maxLinesInFile) {
142+
$count = $leftToInsert;
143+
} else {
144+
$count = $maxLinesInFile;
145+
}
146+
147+
return $count;
148+
}
138149
}

0 commit comments

Comments
 (0)