Skip to content
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

fix(classification): Delete historic data #9001

Merged
merged 1 commit into from
Oct 25, 2023
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
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();
}
}