Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 100 additions & 55 deletions src/Migration/History/HistoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#[Package('fundamentals@after-sales')]
class HistoryService implements HistoryServiceInterface
{
private const LOG_FETCH_LIMIT = 50;
private const LOG_TIME_FORMAT = 'd.m.Y h:i:s e';
public const LOG_FETCH_LIMIT = 50;
public const LOG_TIME_FORMAT = 'Y-m-d H:i:s T';

/**
* @param EntityRepository<SwagMigrationRunCollection> $runRepo
Expand Down Expand Up @@ -66,6 +66,7 @@ public function getGroupedLogsOfRun(
);

$result = $this->loggingRepo->aggregate($criteria, $context);

/** @var TermsResult $termsResult */
$termsResult = $result->get('count');
$aggregateResult = $termsResult->getBuckets();
Expand All @@ -75,6 +76,7 @@ public function getGroupedLogsOfRun(
}

$cleanResult = [];

foreach ($aggregateResult as $bucket) {
$detailInformation = $this->extractBucketInformation($bucket);
$cleanResult[] = $detailInformation;
Expand All @@ -88,30 +90,39 @@ public function getGroupedLogsOfRun(
*/
public function downloadLogsOfRun(string $runUuid, Context $context): \Closure
{
$offset = 0;
$total = $this->getTotalLogCount($runUuid, $context);
$run = $this->getMigrationRun($runUuid, $context);

return function () use ($run, $runUuid, $offset, $total, $context): void {
if ($run !== null) {
\printf('%s%s', $this->getPrefixLogInformation($run), \PHP_EOL);
if ($run === null) {
throw MigrationException::entityNotExists(
SwagMigrationRunEntity::class,
$runUuid
);
}

$total = $this->getTotalLogCount($runUuid, $context);

return function () use ($run, $runUuid, $total, $context): void {
$offset = 0;

\printf('%s', $this->getPrefixLogInformation($run));

if ($total === 0) {
\printf('%sNo log entries found for this migration run.%s', \PHP_EOL, \PHP_EOL);
}

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

foreach ($logChunk->getElements() as $logEntry) {
\printf('[%s] %s%s', $logEntry->getLevel(), $logEntry->getCode(), \PHP_EOL);
\printf('%s%s', $logEntry->getProfileName(), \PHP_EOL);
\printf('%s%s%s', $logEntry->getGatewayName(), \PHP_EOL, \PHP_EOL);
if (!$logEntry instanceof SwagMigrationLoggingEntity) {
continue;
}

$this->printLogEntry($logEntry);
}

$offset += self::LOG_FETCH_LIMIT;
}

if ($run !== null) {
\printf('%s%s%s', $this->getSuffixLogInformation($run), \PHP_EOL, \PHP_EOL);
}
};
}

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

private function printLogEntry(SwagMigrationLoggingEntity $logEntry): void
{
\printf('----- Log Entry #%d -----%s', $logEntry->getAutoIncrement(), \PHP_EOL);
\printf('ID: %s%s', $logEntry->getId(), \PHP_EOL);
\printf('Level: %s%s', $logEntry->getLevel(), \PHP_EOL);
\printf('Code: %s%s', $logEntry->getCode(), \PHP_EOL);
\printf('Profile name: %s%s', $logEntry->getProfileName(), \PHP_EOL);
\printf('Gateway name: %s%s', $logEntry->getGatewayName(), \PHP_EOL);
\printf('Created at: %s%s', $logEntry->getCreatedAt()?->format(self::LOG_TIME_FORMAT) ?? '-', \PHP_EOL);

if ($logEntry->getEntityName()) {
\printf('Entity: %s%s', $logEntry->getEntityName(), \PHP_EOL);
}

if ($logEntry->getFieldName()) {
\printf('Field: %s%s', $logEntry->getFieldName(), \PHP_EOL);
}

if ($logEntry->getFieldSourcePath()) {
\printf('Source path: %s%s', $logEntry->getFieldSourcePath(), \PHP_EOL);
}

if ($logEntry->getExceptionMessage()) {
\printf('Exception message: %s%s', $logEntry->getExceptionMessage(), \PHP_EOL);
}

if ($logEntry->getSourceData()) {
\printf('Source data (JSON):%s%s%s', \PHP_EOL, \json_encode($logEntry->getSourceData(), \JSON_PRETTY_PRINT), \PHP_EOL);
}

if ($logEntry->getConvertedData()) {
\printf('Converted data (JSON):%s%s%s', \PHP_EOL, \json_encode($logEntry->getConvertedData(), \JSON_PRETTY_PRINT), \PHP_EOL);
}

if ($logEntry->getUsedMapping()) {
\printf('Used mapping (JSON):%s%s%s', \PHP_EOL, \json_encode($logEntry->getUsedMapping(), \JSON_PRETTY_PRINT), \PHP_EOL);
}

if ($logEntry->getExceptionTrace()) {
\printf('Exception trace (JSON):%s%s%s', \PHP_EOL, \json_encode($logEntry->getExceptionTrace(), \JSON_PRETTY_PRINT), \PHP_EOL);
}

\printf(\PHP_EOL);
}

private function extractBucketInformation(Bucket $bucket): array
{
/** @var TermsResult|null $levelResult */
Expand All @@ -150,6 +206,7 @@ private function extractBucketInformation(Bucket $bucket): array

if ($levelResult !== null) {
$levelBuckets = $levelResult->getBuckets();

if (!empty($levelBuckets)) {
$levelString = $levelBuckets[0]->getKey();
}
Expand All @@ -169,6 +226,7 @@ private function getTotalLogCount(string $runUuid, Context $context): int
$criteria->addAggregation(new CountAggregation('count', 'id'));

$result = $this->loggingRepo->aggregate($criteria, $context);

/** @var CountResult $countResult */
$countResult = $result->get('count');

Expand All @@ -190,6 +248,7 @@ private function getLogChunk(string $runUuid, int $offset, Context $context): En
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('runId', $runUuid));
$criteria->addFilter(new EqualsFilter('userFixable', 0));
$criteria->addSorting(new FieldSorting('autoIncrement', FieldSorting::ASCENDING));
$criteria->setOffset($offset);
$criteria->setLimit(self::LOG_FETCH_LIMIT);
Expand All @@ -203,38 +262,38 @@ private function getPrefixLogInformation(SwagMigrationRunEntity $run): string
$profileName = '-';
$gatewayName = '-';
$connectionName = '-';

$premapping = 'Associated connection not found';

if ($connection !== null) {
$connectionName = $connection->getName();
$profileName = $connection->getProfileName();
$gatewayName = $connection->getGatewayName();
$premapping = $connection->getPremapping();
}

$updatedAt = $run->getUpdatedAt();
if ($updatedAt !== null) {
$updatedAt = $updatedAt->format(self::LOG_TIME_FORMAT);
} else {
$updatedAt = '-';
}

$createdAt = $run->getCreatedAt();
if ($createdAt !== null) {
$createdAt = $createdAt->format(self::LOG_TIME_FORMAT);
} else {
$createdAt = '-';
}
$updatedAt = $run->getUpdatedAt()?->format(self::LOG_TIME_FORMAT) ?? '-';
$createdAt = $run->getCreatedAt()?->format(self::LOG_TIME_FORMAT) ?? '-';

return \sprintf(
'Migration log generated at %s' . \PHP_EOL
. 'Run id: %s' . \PHP_EOL
'########## MIGRATION LOG ##########' . \PHP_EOL . \PHP_EOL
. '########## RUN INFORMATION ##########' . \PHP_EOL
. 'Generated at: %s' . \PHP_EOL
. 'Run ID: %s' . \PHP_EOL
. 'Status: %s' . \PHP_EOL
. 'Created at: %s' . \PHP_EOL
. 'Updated at: %s' . \PHP_EOL
. 'Connection id: %s' . \PHP_EOL
. 'Updated at: %s' . \PHP_EOL . \PHP_EOL
. '########## CONNECTION INFORMATION ##########' . \PHP_EOL
. 'Connection ID: %s' . \PHP_EOL
. 'Connection name: %s' . \PHP_EOL
. 'Profile: %s' . \PHP_EOL
. 'Gateway: %s' . \PHP_EOL . \PHP_EOL
. 'Selected dataSets:' . \PHP_EOL . '%s' . \PHP_EOL
. '--------------------Log-entries---------------------' . \PHP_EOL,
. 'Profile name: %s' . \PHP_EOL
. 'Gateway name: %s' . \PHP_EOL . \PHP_EOL
. '########## SELECTED DATASETS ##########' . \PHP_EOL
. '%s' . \PHP_EOL
. '########## ADDITIONAL METADATA ##########' . \PHP_EOL
. 'Environment information (JSON):' . \PHP_EOL . '%s' . \PHP_EOL . \PHP_EOL
. 'Pre-mapping (JSON):' . \PHP_EOL . '%s' . \PHP_EOL . \PHP_EOL
. '########## LOG ENTRIES ##########' . \PHP_EOL,
\date(self::LOG_TIME_FORMAT),
$run->getId(),
$run->getStepValue(),
Expand All @@ -244,38 +303,24 @@ private function getPrefixLogInformation(SwagMigrationRunEntity $run): string
$connectionName,
$profileName,
$gatewayName,
$this->getFormattedSelectedDataSets($run->getProgress())
$this->getFormattedSelectedDataSets($run->getProgress()),
\json_encode($run->getEnvironmentInformation(), \JSON_PRETTY_PRINT),
\json_encode($premapping, \JSON_PRETTY_PRINT)
);
}

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

$output = '';

foreach ($progress->getDataSets() as $dataSet) {
$output .= \sprintf('- %s (total: %d)' . \PHP_EOL, $dataSet->getEntityName(), $dataSet->getTotal());
$output .= \sprintf('- %s (Total: %d)' . \PHP_EOL, $dataSet->getEntityName(), $dataSet->getTotal());
}

return $output;
}

private function getSuffixLogInformation(SwagMigrationRunEntity $run): string
{
$connection = $run->getConnection();
$premapping = 'Associated connection not found';
if ($connection !== null) {
$premapping = $connection->getPremapping();
}

return \sprintf(
'--------------------Additional-metadata---------------------' . \PHP_EOL
. 'Environment information {JSON}:' . \PHP_EOL . '%s' . \PHP_EOL . \PHP_EOL
. 'Premapping {JSON}: ----------------------------------------------------' . \PHP_EOL . '%s' . \PHP_EOL,
\json_encode($run->getEnvironmentInformation(), \JSON_PRETTY_PRINT),
\json_encode($premapping, \JSON_PRETTY_PRINT)
);
}
}
25 changes: 0 additions & 25 deletions tests/Migration/Controller/HistoryControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Routing\RoutingException;
use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour;
Expand Down Expand Up @@ -160,30 +159,6 @@ public function testGetLogChunk(): void
static::assertNotNull($result->first());
}

public function testGetPrefixLogInformation(): void
{
$result = $this->runRepo->search(new Criteria([$this->runUuid]), $this->context);
$run = $result->first();
$result = $this->invokeMethod($this->historyService, 'getPrefixLogInformation', [$run]);

static::assertIsString($result);
static::assertStringContainsString('Migration log generated at', $result);
static::assertStringContainsString('Run id:', $result);
static::assertStringContainsString('Connection name: myConnection', $result);
}

public function testGetSuffixLogInformation(): void
{
$result = $this->runRepo->search(new Criteria([$this->runUuid]), $this->context);
$run = $result->first();
$result = $this->invokeMethod($this->historyService, 'getSuffixLogInformation', [$run]);

static::assertIsString($result);
static::assertStringContainsString('--------------------Additional-metadata---------------------', $result);
static::assertStringContainsString('Environment information {JSON}:', $result);
static::assertStringContainsString('Premapping {JSON}: ----------------------------------------------------', $result);
}

/**
* @param array<Context|string|int|Entity|null> $parameters
*
Expand Down
Loading