Skip to content

Commit 98da94c

Browse files
committed
fix: adjust log file generation (#64)
1 parent 38cee0d commit 98da94c

File tree

3 files changed

+455
-80
lines changed

3 files changed

+455
-80
lines changed

src/Migration/History/HistoryService.php

Lines changed: 100 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
#[Package('fundamentals@after-sales')]
3232
class HistoryService implements HistoryServiceInterface
3333
{
34-
private const LOG_FETCH_LIMIT = 50;
35-
private const LOG_TIME_FORMAT = 'd.m.Y h:i:s e';
34+
public const LOG_FETCH_LIMIT = 50;
35+
public const LOG_TIME_FORMAT = 'Y-m-d H:i:s T';
3636

3737
/**
3838
* @param EntityRepository<SwagMigrationRunCollection> $runRepo
@@ -66,6 +66,7 @@ public function getGroupedLogsOfRun(
6666
);
6767

6868
$result = $this->loggingRepo->aggregate($criteria, $context);
69+
6970
/** @var TermsResult $termsResult */
7071
$termsResult = $result->get('count');
7172
$aggregateResult = $termsResult->getBuckets();
@@ -75,6 +76,7 @@ public function getGroupedLogsOfRun(
7576
}
7677

7778
$cleanResult = [];
79+
7880
foreach ($aggregateResult as $bucket) {
7981
$detailInformation = $this->extractBucketInformation($bucket);
8082
$cleanResult[] = $detailInformation;
@@ -88,30 +90,39 @@ public function getGroupedLogsOfRun(
8890
*/
8991
public function downloadLogsOfRun(string $runUuid, Context $context): \Closure
9092
{
91-
$offset = 0;
92-
$total = $this->getTotalLogCount($runUuid, $context);
9393
$run = $this->getMigrationRun($runUuid, $context);
9494

95-
return function () use ($run, $runUuid, $offset, $total, $context): void {
96-
if ($run !== null) {
97-
\printf('%s%s', $this->getPrefixLogInformation($run), \PHP_EOL);
95+
if ($run === null) {
96+
throw MigrationException::entityNotExists(
97+
SwagMigrationRunEntity::class,
98+
$runUuid
99+
);
100+
}
101+
102+
$total = $this->getTotalLogCount($runUuid, $context);
103+
104+
return function () use ($run, $runUuid, $total, $context): void {
105+
$offset = 0;
106+
107+
\printf('%s', $this->getPrefixLogInformation($run));
108+
109+
if ($total === 0) {
110+
\printf('%sNo log entries found for this migration run.%s', \PHP_EOL, \PHP_EOL);
98111
}
99112

100113
while ($offset < $total) {
101114
$logChunk = $this->getLogChunk($runUuid, $offset, $context);
102115

103116
foreach ($logChunk->getElements() as $logEntry) {
104-
\printf('[%s] %s%s', $logEntry->getLevel(), $logEntry->getCode(), \PHP_EOL);
105-
\printf('%s%s', $logEntry->getProfileName(), \PHP_EOL);
106-
\printf('%s%s%s', $logEntry->getGatewayName(), \PHP_EOL, \PHP_EOL);
117+
if (!$logEntry instanceof SwagMigrationLoggingEntity) {
118+
continue;
119+
}
120+
121+
$this->printLogEntry($logEntry);
107122
}
108123

109124
$offset += self::LOG_FETCH_LIMIT;
110125
}
111-
112-
if ($run !== null) {
113-
\printf('%s%s%s', $this->getSuffixLogInformation($run), \PHP_EOL, \PHP_EOL);
114-
}
115126
};
116127
}
117128

@@ -142,6 +153,51 @@ public function isMediaProcessing(): bool
142153
return (int) $unprocessedCount !== 0;
143154
}
144155

156+
private function printLogEntry(SwagMigrationLoggingEntity $logEntry): void
157+
{
158+
\printf('----- Log Entry #%d -----%s', $logEntry->getAutoIncrement(), \PHP_EOL);
159+
\printf('ID: %s%s', $logEntry->getId(), \PHP_EOL);
160+
\printf('Level: %s%s', $logEntry->getLevel(), \PHP_EOL);
161+
\printf('Code: %s%s', $logEntry->getCode(), \PHP_EOL);
162+
\printf('Profile name: %s%s', $logEntry->getProfileName(), \PHP_EOL);
163+
\printf('Gateway name: %s%s', $logEntry->getGatewayName(), \PHP_EOL);
164+
\printf('Created at: %s%s', $logEntry->getCreatedAt()?->format(self::LOG_TIME_FORMAT) ?? '-', \PHP_EOL);
165+
166+
if ($logEntry->getEntityName()) {
167+
\printf('Entity: %s%s', $logEntry->getEntityName(), \PHP_EOL);
168+
}
169+
170+
if ($logEntry->getFieldName()) {
171+
\printf('Field: %s%s', $logEntry->getFieldName(), \PHP_EOL);
172+
}
173+
174+
if ($logEntry->getFieldSourcePath()) {
175+
\printf('Source path: %s%s', $logEntry->getFieldSourcePath(), \PHP_EOL);
176+
}
177+
178+
if ($logEntry->getExceptionMessage()) {
179+
\printf('Exception message: %s%s', $logEntry->getExceptionMessage(), \PHP_EOL);
180+
}
181+
182+
if ($logEntry->getSourceData()) {
183+
\printf('Source data (JSON):%s%s%s', \PHP_EOL, \json_encode($logEntry->getSourceData(), \JSON_PRETTY_PRINT), \PHP_EOL);
184+
}
185+
186+
if ($logEntry->getConvertedData()) {
187+
\printf('Converted data (JSON):%s%s%s', \PHP_EOL, \json_encode($logEntry->getConvertedData(), \JSON_PRETTY_PRINT), \PHP_EOL);
188+
}
189+
190+
if ($logEntry->getUsedMapping()) {
191+
\printf('Used mapping (JSON):%s%s%s', \PHP_EOL, \json_encode($logEntry->getUsedMapping(), \JSON_PRETTY_PRINT), \PHP_EOL);
192+
}
193+
194+
if ($logEntry->getExceptionTrace()) {
195+
\printf('Exception trace (JSON):%s%s%s', \PHP_EOL, \json_encode($logEntry->getExceptionTrace(), \JSON_PRETTY_PRINT), \PHP_EOL);
196+
}
197+
198+
\printf(\PHP_EOL);
199+
}
200+
145201
private function extractBucketInformation(Bucket $bucket): array
146202
{
147203
/** @var TermsResult|null $levelResult */
@@ -150,6 +206,7 @@ private function extractBucketInformation(Bucket $bucket): array
150206

151207
if ($levelResult !== null) {
152208
$levelBuckets = $levelResult->getBuckets();
209+
153210
if (!empty($levelBuckets)) {
154211
$levelString = $levelBuckets[0]->getKey();
155212
}
@@ -169,6 +226,7 @@ private function getTotalLogCount(string $runUuid, Context $context): int
169226
$criteria->addAggregation(new CountAggregation('count', 'id'));
170227

171228
$result = $this->loggingRepo->aggregate($criteria, $context);
229+
172230
/** @var CountResult $countResult */
173231
$countResult = $result->get('count');
174232

@@ -190,6 +248,7 @@ private function getLogChunk(string $runUuid, int $offset, Context $context): En
190248
{
191249
$criteria = new Criteria();
192250
$criteria->addFilter(new EqualsFilter('runId', $runUuid));
251+
$criteria->addFilter(new EqualsFilter('userFixable', 0));
193252
$criteria->addSorting(new FieldSorting('autoIncrement', FieldSorting::ASCENDING));
194253
$criteria->setOffset($offset);
195254
$criteria->setLimit(self::LOG_FETCH_LIMIT);
@@ -203,38 +262,38 @@ private function getPrefixLogInformation(SwagMigrationRunEntity $run): string
203262
$profileName = '-';
204263
$gatewayName = '-';
205264
$connectionName = '-';
265+
266+
$premapping = 'Associated connection not found';
267+
206268
if ($connection !== null) {
207269
$connectionName = $connection->getName();
208270
$profileName = $connection->getProfileName();
209271
$gatewayName = $connection->getGatewayName();
272+
$premapping = $connection->getPremapping();
210273
}
211274

212-
$updatedAt = $run->getUpdatedAt();
213-
if ($updatedAt !== null) {
214-
$updatedAt = $updatedAt->format(self::LOG_TIME_FORMAT);
215-
} else {
216-
$updatedAt = '-';
217-
}
218-
219-
$createdAt = $run->getCreatedAt();
220-
if ($createdAt !== null) {
221-
$createdAt = $createdAt->format(self::LOG_TIME_FORMAT);
222-
} else {
223-
$createdAt = '-';
224-
}
275+
$updatedAt = $run->getUpdatedAt()?->format(self::LOG_TIME_FORMAT) ?? '-';
276+
$createdAt = $run->getCreatedAt()?->format(self::LOG_TIME_FORMAT) ?? '-';
225277

226278
return \sprintf(
227-
'Migration log generated at %s' . \PHP_EOL
228-
. 'Run id: %s' . \PHP_EOL
279+
'########## MIGRATION LOG ##########' . \PHP_EOL . \PHP_EOL
280+
. '########## RUN INFORMATION ##########' . \PHP_EOL
281+
. 'Generated at: %s' . \PHP_EOL
282+
. 'Run ID: %s' . \PHP_EOL
229283
. 'Status: %s' . \PHP_EOL
230284
. 'Created at: %s' . \PHP_EOL
231-
. 'Updated at: %s' . \PHP_EOL
232-
. 'Connection id: %s' . \PHP_EOL
285+
. 'Updated at: %s' . \PHP_EOL . \PHP_EOL
286+
. '########## CONNECTION INFORMATION ##########' . \PHP_EOL
287+
. 'Connection ID: %s' . \PHP_EOL
233288
. 'Connection name: %s' . \PHP_EOL
234-
. 'Profile: %s' . \PHP_EOL
235-
. 'Gateway: %s' . \PHP_EOL . \PHP_EOL
236-
. 'Selected dataSets:' . \PHP_EOL . '%s' . \PHP_EOL
237-
. '--------------------Log-entries---------------------' . \PHP_EOL,
289+
. 'Profile name: %s' . \PHP_EOL
290+
. 'Gateway name: %s' . \PHP_EOL . \PHP_EOL
291+
. '########## SELECTED DATASETS ##########' . \PHP_EOL
292+
. '%s' . \PHP_EOL
293+
. '########## ADDITIONAL METADATA ##########' . \PHP_EOL
294+
. 'Environment information (JSON):' . \PHP_EOL . '%s' . \PHP_EOL . \PHP_EOL
295+
. 'Pre-mapping (JSON):' . \PHP_EOL . '%s' . \PHP_EOL . \PHP_EOL
296+
. '########## LOG ENTRIES ##########' . \PHP_EOL,
238297
\date(self::LOG_TIME_FORMAT),
239298
$run->getId(),
240299
$run->getStepValue(),
@@ -244,38 +303,24 @@ private function getPrefixLogInformation(SwagMigrationRunEntity $run): string
244303
$connectionName,
245304
$profileName,
246305
$gatewayName,
247-
$this->getFormattedSelectedDataSets($run->getProgress())
306+
$this->getFormattedSelectedDataSets($run->getProgress()),
307+
\json_encode($run->getEnvironmentInformation(), \JSON_PRETTY_PRINT),
308+
\json_encode($premapping, \JSON_PRETTY_PRINT)
248309
);
249310
}
250311

251312
private function getFormattedSelectedDataSets(?MigrationProgress $progress): string
252313
{
253314
if ($progress === null || $progress->getDataSets()->count() < 1) {
254-
return '';
315+
return 'No datasets selected.' . \PHP_EOL;
255316
}
256317

257318
$output = '';
319+
258320
foreach ($progress->getDataSets() as $dataSet) {
259-
$output .= \sprintf('- %s (total: %d)' . \PHP_EOL, $dataSet->getEntityName(), $dataSet->getTotal());
321+
$output .= \sprintf('- %s (Total: %d)' . \PHP_EOL, $dataSet->getEntityName(), $dataSet->getTotal());
260322
}
261323

262324
return $output;
263325
}
264-
265-
private function getSuffixLogInformation(SwagMigrationRunEntity $run): string
266-
{
267-
$connection = $run->getConnection();
268-
$premapping = 'Associated connection not found';
269-
if ($connection !== null) {
270-
$premapping = $connection->getPremapping();
271-
}
272-
273-
return \sprintf(
274-
'--------------------Additional-metadata---------------------' . \PHP_EOL
275-
. 'Environment information {JSON}:' . \PHP_EOL . '%s' . \PHP_EOL . \PHP_EOL
276-
. 'Premapping {JSON}: ----------------------------------------------------' . \PHP_EOL . '%s' . \PHP_EOL,
277-
\json_encode($run->getEnvironmentInformation(), \JSON_PRETTY_PRINT),
278-
\json_encode($premapping, \JSON_PRETTY_PRINT)
279-
);
280-
}
281326
}

tests/Migration/Controller/HistoryControllerTest.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Shopware\Core\Framework\Context;
1212
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
1313
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
14-
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
1514
use Shopware\Core\Framework\Log\Package;
1615
use Shopware\Core\Framework\Routing\RoutingException;
1716
use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour;
@@ -160,30 +159,6 @@ public function testGetLogChunk(): void
160159
static::assertNotNull($result->first());
161160
}
162161

163-
public function testGetPrefixLogInformation(): void
164-
{
165-
$result = $this->runRepo->search(new Criteria([$this->runUuid]), $this->context);
166-
$run = $result->first();
167-
$result = $this->invokeMethod($this->historyService, 'getPrefixLogInformation', [$run]);
168-
169-
static::assertIsString($result);
170-
static::assertStringContainsString('Migration log generated at', $result);
171-
static::assertStringContainsString('Run id:', $result);
172-
static::assertStringContainsString('Connection name: myConnection', $result);
173-
}
174-
175-
public function testGetSuffixLogInformation(): void
176-
{
177-
$result = $this->runRepo->search(new Criteria([$this->runUuid]), $this->context);
178-
$run = $result->first();
179-
$result = $this->invokeMethod($this->historyService, 'getSuffixLogInformation', [$run]);
180-
181-
static::assertIsString($result);
182-
static::assertStringContainsString('--------------------Additional-metadata---------------------', $result);
183-
static::assertStringContainsString('Environment information {JSON}:', $result);
184-
static::assertStringContainsString('Premapping {JSON}: ----------------------------------------------------', $result);
185-
}
186-
187162
/**
188163
* @param array<Context|string|int|Entity|null> $parameters
189164
*

0 commit comments

Comments
 (0)