Commit fead7d77 authored by Chivy Lim's avatar Chivy Lim

Merge branch 'KIME-4583' into 'master'

[FEATURE] Logging

refs KIME-4583

See merge request !17
parents a3f6abc0 40f05dc8
......@@ -13,7 +13,6 @@ namespace WE\SpreadsheetImport\Command;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Cli\CommandController;
use TYPO3\Flow\Exception;
use WE\SpreadsheetImport\Domain\Model\SpreadsheetImport;
/**
......@@ -33,6 +32,12 @@ class SpreadsheetImportCommandController extends CommandController {
*/
protected $spreadsheetImportService;
/**
* @Flow\Inject
* @var \WE\SpreadsheetImport\Log\SpreadsheetImportLoggerInterface
*/
protected $logger;
/**
* @Flow\Inject
* @var \TYPO3\Flow\Persistence\PersistenceManagerInterface
......@@ -57,6 +62,8 @@ class SpreadsheetImportCommandController extends CommandController {
/** @var SpreadsheetImport $spreadsheetImport */
$spreadsheetImport = $this->spreadsheetImportRepository->findNextInQueue();
if ($spreadsheetImport instanceof SpreadsheetImport) {
$message = 'Spreadsheet import started.';
$this->log($spreadsheetImport, $message, LOG_INFO);
// mark importing status as "Progressing" before continuing the importing
$spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_IN_PROGRESS);
$this->spreadsheetImportRepository->update($spreadsheetImport);
......@@ -67,16 +74,19 @@ class SpreadsheetImportCommandController extends CommandController {
try {
$this->spreadsheetImportService->import();
$spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_COMPLETED);
$this->outputLine('Spreadsheet has been imported. %d inserted, %d updated, %d deleted, %d skipped',
array($spreadsheetImport->getTotalInserted(), $spreadsheetImport->getTotalUpdated(), $spreadsheetImport->getTotalDeleted(), $spreadsheetImport->getTotalSkipped()));
$args = array($spreadsheetImport->getTotalInserted(), $spreadsheetImport->getTotalUpdated(), $spreadsheetImport->getTotalDeleted(), $spreadsheetImport->getTotalSkipped());
$message = vsprintf('Spreadsheet import complete: %d inserted, %d updated, %d deleted, %d skipped', $args);
$this->log($spreadsheetImport, $message, LOG_INFO);
} catch (\Exception $e) {
$spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_FAILED);
$this->outputLine('Spreadsheet import failed.');
$message = 'Spreadsheet import failed.';
$this->log($spreadsheetImport, $message, LOG_ERR, $e->getMessage());
}
try {
$this->spreadsheetImportRepository->update($spreadsheetImport);
} catch (\Exception $e) {
$this->outputLine('Spreadsheet import status update error. It remains in progress until cleanup.');
$message = 'Spreadsheet import status update error. It remains in progress until cleanup.';
$this->log($spreadsheetImport, $message, LOG_ERR, $e->getMessage());
}
} else {
$this->outputFormatted('No spreadsheet import in queue.');
......@@ -108,8 +118,9 @@ class SpreadsheetImportCommandController extends CommandController {
/** @var SpreadsheetImport $spreadsheetImport */
foreach ($spreadsheetImports as $spreadsheetImport) {
$this->spreadsheetImportRepository->remove($spreadsheetImport);
$message = 'Spreadsheet import removed.';
$this->log($spreadsheetImport, $message, LOG_INFO, NULL);
}
$this->outputLine('%d spreadsheet imports removed.', array($spreadsheetImports->count()));
}
/**
......@@ -125,7 +136,21 @@ class SpreadsheetImportCommandController extends CommandController {
foreach ($spreadsheetImports as $spreadsheetImport) {
$spreadsheetImport->setImportingStatus(SpreadsheetImport::IMPORTING_STATUS_FAILED);
$this->spreadsheetImportRepository->update($spreadsheetImport);
$message = 'Spreadsheet import exceeded max execution threashold. Status set to failed.';
$this->log($spreadsheetImport, $message, LOG_NOTICE);
}
$this->outputLine('%d spreadsheet imports set to failed.', array($spreadsheetImports->count()));
}
/**
* @param SpreadsheetImport $spreadsheetImport
* @param string $message
* @param int $severity
* @param null $additionalData
*/
private function log(SpreadsheetImport $spreadsheetImport, $message, $severity = LOG_INFO, $additionalData = NULL) {
$name = ucfirst($spreadsheetImport->getContext());
$message = vsprintf('[%s] ' . $message, array($name));
$this->logger->log($message, $severity, $additionalData, 'SpreadsheetImport');
$this->outputLine($message);
}
}
<?php
namespace WE\SpreadsheetImport\Log;
/* *
* This script belongs to the Flow package "SpreadsheetImport". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
/**
* Logger interface for the SpreadsheetImport package
*/
interface SpreadsheetImportLoggerInterface extends \TYPO3\Flow\Log\LoggerInterface {
}
......@@ -45,6 +45,12 @@ class SpreadsheetImportService {
*/
protected $settings;
/**
* @Flow\Inject
* @var \WE\SpreadsheetImport\Log\SpreadsheetImportLoggerInterface
*/
protected $logger;
/**
* @Flow\Inject
* @var \TYPO3\Flow\Reflection\ReflectionService
......@@ -212,19 +218,24 @@ class SpreadsheetImportService {
$totalCount = 0;
/** @var \PHPExcel_Worksheet_Row $row */
foreach ($sheet->getRowIterator(2) as $row) {
$recordNumber = $row->getRowIndex() - 1;
$totalCount++;
$object = $this->findExistingObjectByRow($row);
if (is_object($object)) {
$processedObjectIds[] = $this->persistenceManager->getIdentifierByObject($object);
$id = $this->persistenceManager->getIdentifierByObject($object);
$processedObjectIds[] = $id;
if ($this->spreadsheetImport->isUpdating()) {
$this->setObjectPropertiesByRow($object, $row);
$validationResult = $objectValidator->validate($object);
if ($validationResult->hasErrors()) {
$this->log(vsprintf('Object %s for record %d skipped. Update object validation failed.', array($id, $recordNumber)), LOG_INFO);
continue;
}
$objectRepository->update($object);
$this->log(vsprintf('Object %s for record %d updated.', array($id, $recordNumber)), LOG_INFO);
$totalUpdated++;
} else {
$this->log(vsprintf('Object %s for record %d skipped. Updating is disabled.', array($id, $recordNumber)), LOG_INFO);
continue;
}
} elseif ($this->spreadsheetImport->isInserting()) {
......@@ -232,12 +243,16 @@ class SpreadsheetImportService {
$this->setObjectPropertiesByRow($newObject, $row);
$validationResult = $objectValidator->validate($newObject);
if ($validationResult->hasErrors()) {
$this->log(vsprintf('Record %d skipped. Insert object validation failed.', array($recordNumber)), LOG_INFO);
continue;
}
$objectRepository->add($newObject);
$processedObjectIds[] = $this->persistenceManager->getIdentifierByObject($newObject);
$id = $this->persistenceManager->getIdentifierByObject($object);
$processedObjectIds[] = $id;
$this->log(vsprintf('Object %s for record %d inserted.', array($id, $recordNumber)), LOG_INFO);
$totalInserted++;
} else {
$this->log(vsprintf('Record %d skipped. Inserting is disabled.', array($recordNumber)), LOG_INFO);
continue;
}
if ($totalCount % $persistRecordsChunkSize === 0) {
......@@ -247,6 +262,8 @@ class SpreadsheetImportService {
if ($this->spreadsheetImport->isDeleting()) {
$notExistingObjects = $this->findObjectsByArgumentsAndExcludedIds($processedObjectIds);
foreach ($notExistingObjects as $object) {
$id = $this->persistenceManager->getIdentifierByObject($object);
$this->log(vsprintf('Object %s deleted.', array($id)), LOG_INFO);
$objectRepository->remove($object);
if (++$totalDeleted % $persistRecordsChunkSize === 0) {
$this->persistenceManager->persistAll();
......@@ -357,12 +374,12 @@ class SpreadsheetImportService {
* @param \PHPExcel_Worksheet_Row $row
*/
private function setObjectMappingProperties($object, $row) {
$inversedMappingProperties = $this->getInverseMappingProperties();
$inverseMappingProperties = $this->getInverseMappingProperties();
/** @var \PHPExcel_Cell $cell */
foreach ($row->getCellIterator() as $cell) {
$column = $cell->getColumn();
if (array_key_exists($column, $inversedMappingProperties)) {
$properties = $inversedMappingProperties[$column];
if (array_key_exists($column, $inverseMappingProperties)) {
$properties = $inverseMappingProperties[$column];
foreach ($properties as $propertyMapping) {
$property = $propertyMapping['property'];
/** @var Mapping $mapping */
......@@ -456,4 +473,15 @@ class SpreadsheetImportService {
$repository = $this->objectManager->get($repositoryClassName);
return $repository;
}
/**
* @param string $message
* @param int $severity
* @param null $additionalData
*/
private function log($message, $severity = LOG_INFO, $additionalData = NULL) {
$name = ucfirst($this->spreadsheetImport->getContext());
$message = vsprintf('[%s] ' . $message, array($name));
$this->logger->log($message, $severity, $additionalData, 'SpreadsheetImport');
}
}
WE\SpreadsheetImport\Log\SpreadsheetImportLoggerInterface:
scope: singleton
factoryObjectName: TYPO3\Flow\Log\LoggerFactory
arguments:
1:
value: 'SystemLogger'
2:
setting: TYPO3.Flow.log.systemLogger.logger
3:
setting: TYPO3.Flow.log.systemLogger.backend
4:
setting: TYPO3.Flow.log.systemLogger.backendOptions
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment