Skip to content

Commit

Permalink
Merge pull request #9003 from nextcloud/backport/9001/stable3.4
Browse files Browse the repository at this point in the history
[stable3.4] fix(classification): Delete historic data
  • Loading branch information
JohannesGGE authored Oct 25, 2023
2 parents 03abe00 + 42815bd commit 531b5b6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/Db/ClassifierMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,16 @@ public function findLatest(int $id): Classifier {

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);
}
}
36 changes: 36 additions & 0 deletions lib/Service/Classification/PersistenceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,42 @@ public function load(int $id): Estimator {
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";
}
Expand Down
8 changes: 7 additions & 1 deletion lib/Service/CleanupService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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 */
Expand All @@ -53,20 +54,24 @@ 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;
$this->collectedAddressMapper = $collectedAddressMapper;
$this->tagMapper = $tagMapper;
$this->messageRetentionMapper = $messageRetentionMapper;
$this->messageSnoozeMapper = $messageSnoozeMapper;
$this->classifierPersistenceService = $classifierPersistenceService;
}

public function cleanUp(): void {
Expand All @@ -78,5 +83,6 @@ public function cleanUp(): void {
$this->tagMapper->deleteDuplicates();
$this->messageRetentionMapper->deleteOrphans();
$this->messageSnoozeMapper->deleteOrphans();
$this->classifierPersistenceService->cleanUp();
}
}

0 comments on commit 531b5b6

Please sign in to comment.