New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve performance of backend module with many log entries #198
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8f76b59
[TASK] Improve performance of backend module with many log entries [T…
bmgrieger 5c5b5f5
[TASK] Run PHP code style fixer [TER-187] [TER-188]
bmgrieger 6b933c1
[TASK] Implement code improvements [TER-187] [TER-188]
bmgrieger af70d1f
Merge branch 'master' into feature/TER-187-v12
bmgrieger 31feac7
[TASK] Prepare release of version 6.0.2 [TER-187] [TER-188]
bmgrieger File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,154 +17,149 @@ | |
use Leuchtfeuer\SecureDownloads\Domain\Model\Log; | ||
use Leuchtfeuer\SecureDownloads\Domain\Transfer\Filter; | ||
use Leuchtfeuer\SecureDownloads\Domain\Transfer\Token\AbstractToken; | ||
use TYPO3\CMS\Core\Database\Connection; | ||
use TYPO3\CMS\Core\Database\ConnectionPool; | ||
use TYPO3\CMS\Core\Database\Query\QueryBuilder; | ||
use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException; | ||
use TYPO3\CMS\Core\Resource\ResourceFactory; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException; | ||
use TYPO3\CMS\Extbase\Persistence\QueryInterface; | ||
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface; | ||
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper; | ||
use TYPO3\CMS\Extbase\Persistence\Repository; | ||
|
||
class LogRepository extends Repository | ||
{ | ||
protected $defaultOrderings = [ | ||
'tstamp' => QueryInterface::ORDER_DESCENDING, | ||
]; | ||
const TABLENAME = 'tx_securedownloads_domain_model_log'; | ||
|
||
/** | ||
* Initializes the query and applies default options. | ||
* | ||
* @return QueryInterface The generated query object. | ||
*/ | ||
public function createQuery(): QueryInterface | ||
public function __construct( | ||
private readonly ConnectionPool $connectionPool, | ||
private readonly DataMapper $dataMapper | ||
) { | ||
} | ||
|
||
public function createQueryBuilder(): QueryBuilder | ||
{ | ||
$query = parent::createQuery(); | ||
$querySettings = $query->getQuerySettings(); | ||
$querySettings->setRespectStoragePage(false); | ||
$querySettings->setRespectSysLanguage(false); | ||
$query->setQuerySettings($querySettings); | ||
$queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::TABLENAME); | ||
$queryBuilder->getRestrictions()->removeAll(); | ||
$queryBuilder | ||
->from(self::TABLENAME) | ||
->orderBy('tstamp', 'DESC'); | ||
|
||
return $query; | ||
return $queryBuilder; | ||
} | ||
|
||
/** | ||
* Finds log data and applies filter. | ||
* | ||
* @param Filter|null $filter The filter object. | ||
* | ||
* @return QueryResultInterface The query result. | ||
*/ | ||
public function findByFilter(?Filter $filter): QueryResultInterface | ||
public function findByFilter(?Filter $filter, int $currentPage = 1, int $itemsPerPage = 20): array | ||
{ | ||
$query = $this->createQuery(); | ||
$queryBuilder = $this->createQueryBuilder(); | ||
|
||
if ($filter instanceof Filter) { | ||
try { | ||
$this->applyFilter($query, $filter); | ||
} catch (InvalidQueryException $exception) { | ||
// Do nothing for now. | ||
} | ||
} | ||
$this->applyFilter($queryBuilder, $filter); | ||
|
||
return $query->execute(); | ||
$result = $queryBuilder | ||
->select('*') | ||
->setMaxResults($itemsPerPage) | ||
->setFirstResult($itemsPerPage * ($currentPage - 1)) | ||
->executeQuery() | ||
->fetchAllAssociative() ?? []; | ||
return $this->dataMapper->map(Log::class, $result); | ||
} | ||
|
||
/** | ||
* Applies the filter to a query object. | ||
* | ||
* @param QueryInterface $query The query object | ||
* @param Filter $filter The filter object | ||
* @throws InvalidQueryException | ||
*/ | ||
protected function applyFilter(QueryInterface &$query, Filter $filter): void | ||
public function countByFilter(?Filter $filter): int | ||
{ | ||
$queryBuilder = $this->createQueryBuilder(); | ||
|
||
$this->applyFilter($queryBuilder, $filter); | ||
|
||
return (int)($queryBuilder | ||
->count('uid') | ||
->executeQuery() | ||
->fetchOne() ?? 0); | ||
} | ||
|
||
public function getFirstTimestampByFilter(?Filter $filter, bool $reverse = false): int | ||
{ | ||
$queryBuilder = $this->createQueryBuilder(); | ||
|
||
$this->applyFilter($queryBuilder, $filter); | ||
|
||
return (int)($queryBuilder | ||
->select('tstamp') | ||
->orderBy('tstamp', $reverse ? 'DESC' : 'ASC') | ||
->executeQuery() | ||
->fetchOne() ?? 0); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
|
||
public function getTrafficSumByFilter(?Filter $filter): float | ||
{ | ||
$queryBuilder = $this->createQueryBuilder(); | ||
|
||
$this->applyFilter($queryBuilder, $filter); | ||
|
||
return (float)($queryBuilder | ||
->selectLiteral('SUM(file_size) AS sum') | ||
->executeQuery() | ||
->fetchOne() ?? 0.0); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
|
||
protected function applyFilter(QueryBuilder &$queryBuilder, Filter $filter): void | ||
{ | ||
$constraints = []; | ||
|
||
// FileType | ||
$this->applyFileTypePropertyToFilter($filter->getFileType(), $query, $constraints); | ||
if ($filter instanceof Filter) { | ||
try { | ||
// FileType | ||
$this->applyFileTypePropertyToFilter($filter->getFileType(), $queryBuilder, $constraints); | ||
|
||
// User Type | ||
$this->applyUserTypePropertyToFilter($filter, $query, $constraints); | ||
// User Type | ||
$this->applyUserTypePropertyToFilter($filter, $queryBuilder, $constraints); | ||
|
||
// Period | ||
$this->applyPeriodPropertyToFilter($filter, $query, $constraints); | ||
// Period | ||
$this->applyPeriodPropertyToFilter($filter, $queryBuilder, $constraints); | ||
|
||
// User and Page | ||
$this->applyEqualPropertyToFilter((int)$filter->getFeUserId(), 'user', $query, $constraints); | ||
$this->applyEqualPropertyToFilter((int)$filter->getPageId(), 'page', $query, $constraints); | ||
// User and Page | ||
$this->applyEqualPropertyToFilter((int)$filter->getFeUserId(), 'user', $queryBuilder, $constraints); | ||
$this->applyEqualPropertyToFilter((int)$filter->getPageId(), 'page', $queryBuilder, $constraints); | ||
|
||
if (count($constraints) > 0) { | ||
$query->matching($query->logicalAnd(...$constraints)); | ||
if (count($constraints) > 0) { | ||
$queryBuilder->where(...$constraints); | ||
} | ||
} catch (InvalidQueryException $exception) { | ||
// Do nothing for now. | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Applies the file type property of the filter to the query object. | ||
* | ||
* @param mixed $fileType Identifier of the file type | ||
* @param QueryInterface $query The query object | ||
* @param array $constraints Array containing all previously applied constraints | ||
*/ | ||
protected function applyFileTypePropertyToFilter($fileType, QueryInterface $query, array &$constraints): void | ||
protected function applyFileTypePropertyToFilter(string $fileType, QueryBuilder $queryBuilder, array &$constraints): void | ||
{ | ||
if ($fileType !== '' && $fileType !== '0') { | ||
$constraints[] = $query->equals('mediaType', $fileType); | ||
$constraints[] = $queryBuilder->expr()->eq('media_type', $queryBuilder->createNamedParameter($fileType)); | ||
} | ||
} | ||
|
||
/** | ||
* Applies the user type property of the filter to the query object. | ||
* | ||
* @param Filter $filter The filter object | ||
* @param QueryInterface $query The query object | ||
* @param array $constraints Array containing all previously applied constraints | ||
*/ | ||
protected function applyUserTypePropertyToFilter(Filter $filter, QueryInterface $query, array &$constraints): void | ||
protected function applyUserTypePropertyToFilter(Filter $filter, QueryBuilder $queryBuilder, array &$constraints): void | ||
{ | ||
if ($filter->getUserType() != 0) { | ||
$userQuery = $query->equals('user', null); | ||
|
||
if ($filter->getUserType() === Filter::USER_TYPE_LOGGED_ON) { | ||
$constraints[] = $query->logicalNot($userQuery); | ||
} | ||
if ($filter->getUserType() === Filter::USER_TYPE_LOGGED_OFF) { | ||
$constraints[] = $userQuery; | ||
} | ||
if ($filter->getUserType() === Filter::USER_TYPE_LOGGED_ON) { | ||
$constraints[] = $queryBuilder->expr()->gt('user', $queryBuilder->createNamedParameter(0, Connection::PARAM_INT)); | ||
} | ||
if ($filter->getUserType() === Filter::USER_TYPE_LOGGED_OFF) { | ||
$constraints[] = $queryBuilder->expr()->eq('user', $queryBuilder->createNamedParameter(0, Connection::PARAM_INT)); | ||
} | ||
} | ||
|
||
/** | ||
* Applies the period properties of the filter to the query object. | ||
* | ||
* @param Filter $filter The filter object | ||
* @param QueryInterface $query The query object | ||
* @param array $constraints Array containing all previously applied constraints | ||
* @throws InvalidQueryException | ||
*/ | ||
protected function applyPeriodPropertyToFilter(Filter $filter, QueryInterface $query, array &$constraints): void | ||
protected function applyPeriodPropertyToFilter(Filter $filter, QueryBuilder $queryBuilder, array &$constraints): void | ||
{ | ||
if ((int)$filter->getFrom() !== 0) { | ||
$constraints[] = $query->greaterThanOrEqual('tstamp', $filter->getFrom()); | ||
$constraints[] = $queryBuilder->expr()->gte('tstamp', $queryBuilder->createNamedParameter($filter->getFrom(), Connection::PARAM_INT)); | ||
} | ||
|
||
if ((int)$filter->getTill() !== 0) { | ||
$constraints[] = $query->lessThanOrEqual('tstamp', $filter->getTill()); | ||
$constraints[] = $queryBuilder->expr()->lte('tstamp', $queryBuilder->createNamedParameter($filter->getTill(), Connection::PARAM_INT)); | ||
} | ||
} | ||
|
||
/** | ||
* Applies given property of the filter to the query object. | ||
* | ||
* @param int $property The value of the property | ||
* @param string $propertyName The property name | ||
* @param QueryInterface $query The query object | ||
* @param array $constraints Array containing all previously applied constraints | ||
*/ | ||
protected function applyEqualPropertyToFilter(int $property, string $propertyName, QueryInterface $query, array &$constraints): void | ||
protected function applyEqualPropertyToFilter(int $property, string $propertyName, QueryBuilder $queryBuilder, array &$constraints): void | ||
{ | ||
if ($property !== 0) { | ||
$constraints[] = $query->equals($propertyName, $property); | ||
$constraints[] = $queryBuilder->expr()->eq($propertyName, $queryBuilder->createNamedParameter($property, Connection::PARAM_INT)); | ||
} | ||
} | ||
|
||
|
@@ -196,7 +191,10 @@ public function logDownload(AbstractToken $token, int $fileSize, string $mimeTyp | |
$log->setFileId((string)$fileObject->getUid()); | ||
} | ||
|
||
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_securedownloads_domain_model_log'); | ||
$queryBuilder->insert('tx_securedownloads_domain_model_log')->values($log->toArray())->executeStatement(); | ||
$queryBuilder = $this->createQueryBuilder(); | ||
$queryBuilder | ||
->insert(self::TABLENAME) | ||
->values($log->toArray()) | ||
->executeStatement(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would put
$queryBuilder->...->fetchOne() ?? 0
into parentheses, so that the cast goes against the whole construct (not really necessary here, but looks better to me).