Skip to content

Commit

Permalink
fixup! feat: check connection performance of mail service
Browse files Browse the repository at this point in the history
Signed-off-by: SebastianKrupinski <[email protected]>
  • Loading branch information
SebastianKrupinski committed Feb 11, 2025
1 parent b52b333 commit eb4028b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
17 changes: 17 additions & 0 deletions lib/Db/MailAccountMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,21 @@ public function getAllUserIdsWithAccounts(): array {

return $this->findEntities($query);
}

public function getRandomAccountIdsByImapHost(string $host, int $limit = 3): array {
$query = $this->db->getQueryBuilder();
$query->select('id')
->from($this->getTableName())
->where($query->expr()->eq('inbound_host', $query->createNamedParameter($host), IQueryBuilder::PARAM_STR))
->setMaxResults(1000);
$result = $query->executeQuery();
$ids = $result->fetchAll(\PDO::FETCH_COLUMN);
$result->closeCursor();

Check warning on line 246 in lib/Db/MailAccountMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/MailAccountMapper.php#L238-L246

Added lines #L238 - L246 were not covered by tests
// Pick 3 random accounts or any available
if (count($ids) >= $limit) {
$rids = array_rand($ids, $limit);

Check failure on line 249 in lib/Db/MailAccountMapper.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

ArgumentTypeCoercion

lib/Db/MailAccountMapper.php:249:23: ArgumentTypeCoercion: Argument 1 of array_rand expects non-empty-array<array-key, mixed>, but parent type array<array-key, mixed> provided (see https://psalm.dev/193)
return array_intersect_key($ids, array_flip($rids));

Check failure on line 250 in lib/Db/MailAccountMapper.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyInvalidArgument

lib/Db/MailAccountMapper.php:250:48: PossiblyInvalidArgument: Argument 1 of array_flip expects array<array-key, mixed>, but possibly different type array-key|list<array-key> provided (see https://psalm.dev/092)

Check warning on line 250 in lib/Db/MailAccountMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/MailAccountMapper.php#L248-L250

Added lines #L248 - L250 were not covered by tests
}
return $ids;

Check warning on line 252 in lib/Db/MailAccountMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/MailAccountMapper.php#L252

Added line #L252 was not covered by tests
}
}
13 changes: 13 additions & 0 deletions lib/Db/ProvisioningMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,17 @@ public function get(int $id): ?Provisioning {
return null;
}
}

/**
* @since 4.2.0
*/
public function uniqueImapHosts(): array {
$query = $this->db->getQueryBuilder();
$query->selectDistinct('imap_host')
->from('mail_provisionings');
$result = $query->executeQuery();
$data = $result->fetchAll(\PDO::FETCH_COLUMN);
$result->closeCursor();
return $data;

Check warning on line 152 in lib/Db/ProvisioningMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/ProvisioningMapper.php#L145-L152

Added lines #L145 - L152 were not covered by tests
}
}
32 changes: 10 additions & 22 deletions lib/SetupChecks/MailConnectionPerformance.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use OCA\Mail\Account;
use OCA\Mail\Db\MailAccountMapper;
use OCA\Mail\Db\ProvisioningMapper;
use OCA\Mail\IMAP\FolderMapper;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCP\DB\QueryBuilder\IQueryBuilder;
Expand All @@ -26,6 +27,7 @@ public function __construct(
private IL10N $l10n,
private LoggerInterface $logger,
private IDBConnection $db,
private ProvisioningMapper $provisioningMapper,
private MailAccountMapper $accountMapper,
private IMAPClientFactory $clientFactory,
private FolderMapper $folderMapper,
Expand All @@ -41,29 +43,15 @@ public function getCategory(): string {
}

public function run(): SetupResult {

Check warning on line 45 in lib/SetupChecks/MailConnectionPerformance.php

View check run for this annotation

Codecov / codecov/patch

lib/SetupChecks/MailConnectionPerformance.php#L45

Added line #L45 was not covered by tests

// retrieve distinct host(s)
$query = $this->db->getQueryBuilder();
$query->selectDistinct('imap_host')
->from('mail_provisionings');
$hosts = $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
// retrieve random accounts for each host
$query = $this->db->getQueryBuilder();
$query->select('id')
->from('mail_accounts')
->where($query->expr()->eq('inbound_host', $query->createParameter('host'), IQueryBuilder::PARAM_STR))
->setMaxResults(1000);
// retrieve unique imap hosts for provisionings and abort if none exists
$hosts = $this->provisioningMapper->uniqueImapHosts();
if (empty($hosts)) {
return SetupResult::success();

Check warning on line 49 in lib/SetupChecks/MailConnectionPerformance.php

View check run for this annotation

Codecov / codecov/patch

lib/SetupChecks/MailConnectionPerformance.php#L47-L49

Added lines #L47 - L49 were not covered by tests
}
// retrieve random account ids for each host
$accounts = [];
foreach ($hosts as $host) {
$query->setParameter('host', $host, IQueryBuilder::PARAM_STR);
$ids = $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
// Pick 3 random accounts or any available
if (count($ids) >= 3) {
$rids = array_rand($ids, 3);
$accounts[$host] = array_intersect_key($ids, array_flip($rids));
} else {
$accounts[$host] = $ids;
}
$accounts[$host] = $this->accountMapper->getRandomAccountIdsByImapHost($host);

Check warning on line 54 in lib/SetupChecks/MailConnectionPerformance.php

View check run for this annotation

Codecov / codecov/patch

lib/SetupChecks/MailConnectionPerformance.php#L52-L54

Added lines #L52 - L54 were not covered by tests
}
// test accounts
$tests = [];
Expand All @@ -84,7 +72,7 @@ public function run(): SetupResult {

$tests[$host][$accountId] = ['start' => $tStart, 'login' => $tLogin, 'operation' => $tOperation];
} catch (Throwable $e) {
$this->logger->warning('Error occurred while performing system check on mail account');
$this->logger->warning('Error occurred while performing system check on mail account: ' . $account->getId());

Check warning on line 75 in lib/SetupChecks/MailConnectionPerformance.php

View check run for this annotation

Codecov / codecov/patch

lib/SetupChecks/MailConnectionPerformance.php#L73-L75

Added lines #L73 - L75 were not covered by tests
} finally {
$client->close();

Check failure on line 77 in lib/SetupChecks/MailConnectionPerformance.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyUndefinedVariable

lib/SetupChecks/MailConnectionPerformance.php:77:6: PossiblyUndefinedVariable: Possibly undefined variable $client defined in try block (see https://psalm.dev/018)

Check warning on line 77 in lib/SetupChecks/MailConnectionPerformance.php

View check run for this annotation

Codecov / codecov/patch

lib/SetupChecks/MailConnectionPerformance.php#L77

Added line #L77 was not covered by tests
}
Expand Down

0 comments on commit eb4028b

Please sign in to comment.