diff options
author | Arthur Schiwon <blizzz@arthur-schiwon.de> | 2021-04-22 19:24:42 +0300 |
---|---|---|
committer | Arthur Schiwon <blizzz@arthur-schiwon.de> | 2021-04-23 14:11:58 +0300 |
commit | 495775c4cc91893c299f8fdb1919d1376d88bb5e (patch) | |
tree | 4f0ed6734438ca7cc37cf3cd2b7da17e4e75d781 /lib | |
parent | 1a972cc7bf55602d317008af201f56ccf2082be7 (diff) |
move counting storage statistics to the background
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Commands/UpdateStorageStats.php | 59 | ||||
-rw-r--r-- | lib/Jobs/UpdateStorageStats.php | 51 | ||||
-rw-r--r-- | lib/StorageStatistics.php | 50 |
3 files changed, 145 insertions, 15 deletions
diff --git a/lib/Commands/UpdateStorageStats.php b/lib/Commands/UpdateStorageStats.php new file mode 100644 index 0000000..a561662 --- /dev/null +++ b/lib/Commands/UpdateStorageStats.php @@ -0,0 +1,59 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de> + * + * @author Arthur Schiwon <blizzz@arthur-schiwon.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\ServerInfo\Commands; + +use OC\Core\Command\Base; +use OCA\ServerInfo\StorageStatistics; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class UpdateStorageStats extends Base { + /** @var StorageStatistics */ + private $storageStatistics; + + public function __construct(StorageStatistics $storageStatistics) { + parent::__construct(); + + $this->storageStatistics = $storageStatistics; + } + + public function configure() { + parent::configure(); + $this->setName('serverinfo:update-storage-statistics') + ->setDescription('Triggers an update of the counts related to storages used in serverinfo'); + } + + public function execute(InputInterface $input, OutputInterface $output) { + if ($output->isVeryVerbose()) { + $this->writeMixedInOutputFormat($input, $output, 'Updating database counts. This might take a while.'); + } + $this->storageStatistics->updateStorageCounts(); + if ($output->isVerbose()) { + $this->writeArrayInOutputFormat($input, $output, $this->storageStatistics->getStorageStatistics()); + } + return 0; + } +} diff --git a/lib/Jobs/UpdateStorageStats.php b/lib/Jobs/UpdateStorageStats.php new file mode 100644 index 0000000..034395c --- /dev/null +++ b/lib/Jobs/UpdateStorageStats.php @@ -0,0 +1,51 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de> + * + * @author Arthur Schiwon <blizzz@arthur-schiwon.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\ServerInfo\Jobs; + +use OCA\ServerInfo\StorageStatistics; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\TimedJob; +use OCP\IConfig; + +class UpdateStorageStats extends TimedJob { + + /** @var StorageStatistics */ + private $storageStatistics; + + public function __construct(ITimeFactory $time, StorageStatistics $storageStatistics, IConfig $config) { + $this->setInterval((int)$config->getAppValue('serverinfo', 'job_interval_storage_stats', (string)(60 * 60 * 3))); + parent::__construct($time); + + $this->storageStatistics = $storageStatistics; + } + + /** + * @inheritDoc + */ + protected function run($argument) { + $this->storageStatistics->updateStorageCounts(); + } +} diff --git a/lib/StorageStatistics.php b/lib/StorageStatistics.php index ce5586b..e922016 100644 --- a/lib/StorageStatistics.php +++ b/lib/StorageStatistics.php @@ -22,27 +22,31 @@ namespace OCA\ServerInfo; +use OCP\IConfig; use OCP\IDBConnection; class StorageStatistics { /** @var IDBConnection */ private $connection; + /** @var IConfig */ + private $config; /** * SystemStatistics constructor. * * @param IDBConnection $connection */ - public function __construct(IDBConnection $connection) { + public function __construct(IDBConnection $connection, IConfig $config) { $this->connection = $connection; + $this->config = $config; } public function getStorageStatistics() { return [ 'num_users' => $this->countUserEntries(), - 'num_files' => $this->countEntries('filecache'), - 'num_storages' => $this->countEntries('storages'), + 'num_files' => $this->getCountOf('filecache'), + 'num_storages' => $this->getCountOf('storages'), 'num_storages_local' => $this->countStorages('local'), 'num_storages_home' => $this->countStorages('home'), 'num_storages_other' => $this->countStorages('other'), @@ -65,18 +69,34 @@ class StorageStatistics { return (int) $row['num_entries']; } - /** - * @param string $tableName - * @return int - */ - protected function countEntries($tableName) { - $query = $this->connection->getQueryBuilder(); - $query->selectAlias($query->createFunction('COUNT(*)'), 'num_entries') - ->from($tableName); - $result = $query->execute(); - $row = $result->fetch(); - $result->closeCursor(); - return (int) $row['num_entries']; + protected function getCountOf(string $table): int { + return (int)$this->config->getAppValue('serverinfo', 'cached_count_' . $table, '0'); + } + + public function updateStorageCounts(): void { + $storageCount = 0; + $fileCount = 0; + + $fileQuery = $this->connection->getQueryBuilder(); + $fileQuery->select($fileQuery->func()->count()) + ->from('filecache') + ->where($fileQuery->expr()->eq('storage', $fileQuery->createParameter('storageId'))); + + $storageQuery = $this->connection->getQueryBuilder(); + $storageQuery->selectAlias('numeric_id', 'id') + ->from('storages'); + $storageResult = $storageQuery->execute(); + while ($storageRow = $storageResult->fetch()) { + $storageCount++; + $fileQuery->setParameter('storageId', $storageRow['id']); + $fileResult = $fileQuery->execute(); + $fileCount += (int)$fileResult->fetchOne(); + $fileResult->closeCursor(); + } + $storageResult->closeCursor(); + + $this->config->setAppValue('serverinfo', 'cached_count_filecache' , (string)$fileCount); + $this->config->setAppValue('serverinfo', 'cached_count_storages', (string)$storageCount); } /** |