Merge pull request #9001 from nextcloud/fix/classification/delete-historic-data

fix(classification): Delete historic data
This commit is contained in:
Christoph Wurst 2023-10-25 16:19:33 +02:00 коммит произвёл GitHub
Родитель 7cb233dd6b 8e1e5ea65f
Коммит 09b7d46364
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 55 добавлений и 1 удалений

Просмотреть файл

@ -58,4 +58,16 @@ class ClassifierMapper extends QBMapper {
return $this->findEntity($select);
}
public function findHistoric(int $threshold, int $limit) {
$qb = $this->db->getQueryBuilder();
$select = $qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->lte('created_at', $qb->createNamedParameter($threshold, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT),
)
->orderBy('created_at', 'asc')
->setMaxResults($limit);
return $this->findEntities($select);
}
}

Просмотреть файл

@ -210,6 +210,42 @@ class PersistenceService {
return $estimator;
}
public function cleanUp(): void {
$threshold = $this->timeFactory->getTime() - 2 * 30 * 24 * 60 * 60;
$classifiers = $this->mapper->findHistoric($threshold, 100);
foreach ($classifiers as $classifier) {
try {
$this->deleteModel($classifier->getId());
$this->mapper->delete($classifier);
} catch (NotPermittedException $e) {
// Log and continue. This is not critical
$this->logger->warning('Could not clean-up old classifier', [
'id' => $classifier->getId(),
'exception' => $e,
]);
}
}
}
/**
* @throws NotPermittedException
*/
private function deleteModel(int $id): void {
$this->logger->debug('Deleting serialized classifier from app data', [
'id' => $id,
]);
try {
$modelsFolder = $this->appData->getFolder(self::ADD_DATA_FOLDER);
$modelFile = $modelsFolder->getFile((string) $id);
$modelFile->delete();
} catch (NotFoundException $e) {
$this->logger->debug("Classifier model $id does not exist", [
'exception' => $e,
]);
}
}
private function getCacheKey(int $id): string {
return "mail_classifier_$id";
}

Просмотреть файл

@ -32,6 +32,7 @@ use OCA\Mail\Db\MessageMapper;
use OCA\Mail\Db\MessageRetentionMapper;
use OCA\Mail\Db\MessageSnoozeMapper;
use OCA\Mail\Db\TagMapper;
use OCA\Mail\Service\Classification\PersistenceService;
class CleanupService {
/** @var AliasMapper */
@ -53,13 +54,16 @@ class CleanupService {
private MessageSnoozeMapper $messageSnoozeMapper;
private PersistenceService $classifierPersistenceService;
public function __construct(AliasMapper $aliasMapper,
MailboxMapper $mailboxMapper,
MessageMapper $messageMapper,
CollectedAddressMapper $collectedAddressMapper,
TagMapper $tagMapper,
MessageRetentionMapper $messageRetentionMapper,
MessageSnoozeMapper $messageSnoozeMapper) {
MessageSnoozeMapper $messageSnoozeMapper,
PersistenceService $classifierPersistenceService) {
$this->aliasMapper = $aliasMapper;
$this->mailboxMapper = $mailboxMapper;
$this->messageMapper = $messageMapper;
@ -67,6 +71,7 @@ class CleanupService {
$this->tagMapper = $tagMapper;
$this->messageRetentionMapper = $messageRetentionMapper;
$this->messageSnoozeMapper = $messageSnoozeMapper;
$this->classifierPersistenceService = $classifierPersistenceService;
}
public function cleanUp(): void {
@ -78,5 +83,6 @@ class CleanupService {
$this->tagMapper->deleteDuplicates();
$this->messageRetentionMapper->deleteOrphans();
$this->messageSnoozeMapper->deleteOrphans();
$this->classifierPersistenceService->cleanUp();
}
}