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

perf: use more optimized way to get user storage info in ocs user info #49476

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
61 changes: 38 additions & 23 deletions apps/provisioning_api/lib/Controller/AUserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\Files\FileInfo;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Group\ISubAdmin;
use OCP\IConfig;
Expand Down Expand Up @@ -56,6 +58,7 @@ public function __construct(
protected IAccountManager $accountManager,
protected ISubAdmin $subAdminManager,
protected IFactory $l10nFactory,
protected IRootFolder $rootFolder,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -119,7 +122,7 @@ protected function getUserData(string $userId, bool $includeScopes = false): ?ar
$data['lastLogin'] = $targetUserObject->getLastLogin() * 1000;
$data['backend'] = $targetUserObject->getBackendClassName();
$data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
$data[self::USER_FIELD_QUOTA] = $this->fillStorageInfo($targetUserObject->getUID());
$data[self::USER_FIELD_QUOTA] = $this->fillStorageInfo($targetUserObject);
$managers = $this->getManagers($targetUserObject);
$data[self::USER_FIELD_MANAGER] = empty($managers) ? '' : $managers[0];

Expand Down Expand Up @@ -243,34 +246,48 @@ protected function getUserSubAdminGroupsData(string $userId): array {
}

/**
* @param string $userId
* @param IUser $user
* @return Provisioning_APIUserDetailsQuota
* @throws OCSException
*/
protected function fillStorageInfo(string $userId): array {
protected function fillStorageInfo(IUser $user): array {
$userId = $user->getUID();

$quota = $user->getQuota();
if ($quota === 'none') {
$quota = FileInfo::SPACE_UNLIMITED;
} else {
$quota = OC_Helper::computerFileSize($quota);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$quota = OC_Helper::computerFileSize($quota);
$quota = \OCP\Util::computerFileSize($quota);

if ($quota === false) {
$quota = FileInfo::SPACE_UNLIMITED;
}
}

try {
\OC_Util::tearDownFS();
\OC_Util::setupFS($userId);
$storage = OC_Helper::getStorageInfo('/', null, true, false);
$userFileInfo = $this->rootFolder->getUserFolder($userId)->getStorage()->getCache()->get('');
$used = $userFileInfo->getSize();

if ($quota > 0) {
// prevent division by zero or error codes (negative values)
$relative = round(($used / $quota) * 10000) / 100;
$free = $quota - $used;
$total = $quota;
} else {
$relative = 0;
$free = FileInfo::SPACE_UNLIMITED;
$total = $used;
}

$data = [
'free' => $storage['free'],
'used' => $storage['used'],
'total' => $storage['total'],
'relative' => $storage['relative'],
self::USER_FIELD_QUOTA => $storage['quota'],
'free' => $free,
'used' => $used,
'total' => $total,
'relative' => $relative,
self::USER_FIELD_QUOTA => $quota,
];
} catch (NotFoundException $ex) {
// User fs is not setup yet
$user = $this->userManager->get($userId);
if ($user === null) {
throw new OCSException('User does not exist', 101);
}
$quota = $user->getQuota();
if ($quota !== 'none') {
$quota = OC_Helper::computerFileSize($quota);
}
$data = [
self::USER_FIELD_QUOTA => $quota !== false ? $quota : 'none',
self::USER_FIELD_QUOTA => $quota >= 0 ? $quota : 'none',
'used' => 0
];
} catch (\Exception $e) {
Expand All @@ -282,8 +299,6 @@ protected function fillStorageInfo(string $userId): array {
'exception' => $e,
]
);
/* In case the Exception left things in a bad state */
\OC_Util::tearDownFS();
return [];
}
return $data;
Expand Down
5 changes: 4 additions & 1 deletion apps/provisioning_api/lib/Controller/GroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\Files\IRootFolder;
use OCP\Group\ISubAdmin;
use OCP\IConfig;
use OCP\IGroup;
Expand Down Expand Up @@ -48,6 +49,7 @@ public function __construct(
IAccountManager $accountManager,
ISubAdmin $subAdminManager,
IFactory $l10nFactory,
IRootFolder $rootFolder,
private LoggerInterface $logger,
) {
parent::__construct($appName,
Expand All @@ -58,7 +60,8 @@ public function __construct(
$userSession,
$accountManager,
$subAdminManager,
$l10nFactory
$l10nFactory,
$rootFolder,
);
}

Expand Down
5 changes: 4 additions & 1 deletion apps/provisioning_api/lib/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\Group\ISubAdmin;
use OCP\HintException;
use OCP\IConfig;
Expand Down Expand Up @@ -67,6 +68,7 @@ public function __construct(
IAccountManager $accountManager,
ISubAdmin $subAdminManager,
IFactory $l10nFactory,
IRootFolder $rootFolder,
private IURLGenerator $urlGenerator,
private LoggerInterface $logger,
private NewUserMailHelper $newUserMailHelper,
Expand All @@ -85,7 +87,8 @@ public function __construct(
$userSession,
$accountManager,
$subAdminManager,
$l10nFactory
$l10nFactory,
$rootFolder,
);

$this->l10n = $l10nFactory->get($appName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCA\Provisioning_API\Controller\GroupsController;
use OCP\Accounts\IAccountManager;
use OCP\AppFramework\OCS\OCSException;
use OCP\Files\IRootFolder;
use OCP\Group\ISubAdmin;
use OCP\IConfig;
use OCP\IGroup;
Expand Down Expand Up @@ -46,6 +47,8 @@ class GroupsControllerTest extends \Test\TestCase {
/** @var GroupsController|\PHPUnit\Framework\MockObject\MockObject */
protected $api;

private IRootFolder $rootFolder;


protected function setUp(): void {
parent::setUp();
Expand All @@ -59,6 +62,7 @@ protected function setUp(): void {
$this->subAdminManager = $this->createMock(ISubAdmin::class);
$this->l10nFactory = $this->createMock(IFactory::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->rootFolder = $this->createMock(IRootFolder::class);

$this->groupManager
->method('getSubAdmin')
Expand All @@ -75,6 +79,7 @@ protected function setUp(): void {
$this->accountManager,
$this->subAdminManager,
$this->l10nFactory,
$this->rootFolder,
$this->logger
])
->setMethods(['fillStorageInfo'])
Expand Down
13 changes: 10 additions & 3 deletions apps/provisioning_api/tests/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\Group\ISubAdmin;
use OCP\IConfig;
use OCP\IGroup;
Expand Down Expand Up @@ -76,6 +77,7 @@ class UsersControllerTest extends TestCase {
private $knownUserService;
/** @var IEventDispatcher|MockObject */
private $eventDispatcher;
private IRootFolder $rootFolder;
/** @var IPhoneNumberUtil */
private $phoneNumberUtil;

Expand All @@ -98,6 +100,7 @@ protected function setUp(): void {
$this->knownUserService = $this->createMock(KnownUserService::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->phoneNumberUtil = new PhoneNumberUtil();
$this->rootFolder = $this->createMock(IRootFolder::class);

$l10n = $this->createMock(IL10N::class);
$l10n->method('t')->willReturnCallback(fn (string $txt, array $replacement = []) => sprintf($txt, ...$replacement));
Expand All @@ -114,6 +117,7 @@ protected function setUp(): void {
$this->accountManager,
$this->subAdminManager,
$this->l10nFactory,
$this->rootFolder,
$this->urlGenerator,
$this->logger,
$this->newUserMailHelper,
Expand Down Expand Up @@ -508,6 +512,7 @@ public function testAddUserSuccessfulWithDisplayName(): void {
$this->accountManager,
$this->subAdminManager,
$this->l10nFactory,
$this->rootFolder,
$this->urlGenerator,
$this->logger,
$this->newUserMailHelper,
Expand Down Expand Up @@ -1127,7 +1132,7 @@ public function testGetUserDataAsAdmin(): void {
$this->api
->expects($this->once())
->method('fillStorageInfo')
->with('UID')
->with($targetUser)
->willReturn(['DummyValue']);

$backend = $this->createMock(UserInterface::class);
Expand Down Expand Up @@ -1254,7 +1259,7 @@ public function testGetUserDataAsSubAdminAndUserIsAccessible(): void {
$this->api
->expects($this->once())
->method('fillStorageInfo')
->with('UID')
->with($targetUser)
->willReturn(['DummyValue']);

$backend = $this->createMock(UserInterface::class);
Expand Down Expand Up @@ -1429,7 +1434,7 @@ public function testGetUserDataAsSubAdminSelfLookup(): void {
$this->api
->expects($this->once())
->method('fillStorageInfo')
->with('UID')
->with($targetUser)
->willReturn(['DummyValue']);

$backend = $this->createMock(UserInterface::class);
Expand Down Expand Up @@ -3788,6 +3793,7 @@ public function testGetCurrentUserLoggedIn(): void {
$this->accountManager,
$this->subAdminManager,
$this->l10nFactory,
$this->rootFolder,
$this->urlGenerator,
$this->logger,
$this->newUserMailHelper,
Expand Down Expand Up @@ -3878,6 +3884,7 @@ public function testGetUser(): void {
$this->accountManager,
$this->subAdminManager,
$this->l10nFactory,
$this->rootFolder,
$this->urlGenerator,
$this->logger,
$this->newUserMailHelper,
Expand Down
Loading