From bcb456e92695d34c13f0066774730651c2e8c577 Mon Sep 17 00:00:00 2001 From: jygaulier Date: Wed, 26 Jun 2024 18:35:39 +0200 Subject: [PATCH 1/9] new route `/api/v3/monitor/data/?oauth_token=xxx&blocksize=16ko` --- .../Api/V3/V3MonitorDataController.php | 97 +++++++++++++++++++ .../Phrasea/ControllerProvider/Api/V3.php | 11 +++ 2 files changed, 108 insertions(+) create mode 100644 lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php diff --git a/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php b/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php new file mode 100644 index 0000000000..cefcca7cf2 --- /dev/null +++ b/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php @@ -0,0 +1,97 @@ + [] + ]; + $matches = []; + if(preg_match("/^(\\d+)\\s*(ko|mo|go)?$/i", $request->get('blocksize', '1'), $matches) !== 1) { + throw new Exception("bad 'blocksize' parameter"); + } + $matches[] = 'o'; // if no unit, force + $blocksize = (int)($matches[1]) * $this->unitToMultiplier($matches[2]); + + $sql = "SELECT COALESCE(r.`coll_id`, '?') AS `coll_id`, + COALESCE(c.`asciiname`, CONCAT('_',r.`coll_id`), '?') AS `asciiname`, s.`name`, + SUM(1) AS n, SUM(s.`size`) AS `size`, SUM(CEIL(s.`size` / " . $blocksize . ") * " . $blocksize . ") AS `disksize` + FROM `subdef` AS s LEFT JOIN `record` AS r ON r.`record_id`=s.`record_id` + LEFT JOIN `coll` AS c ON r.`coll_id`=c.`coll_id` + GROUP BY r.`coll_id`, s.`name`;"; + + foreach($this->app->getDataboxes() as $databox) { + $collections = []; + $subdefs = []; + $stmt = $databox->get_connection()->prepare($sql); + $stmt->execute(); + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + if(!array_key_exists($row['coll_id'], $collections)) { + $collections[$row['coll_id']] = [ + 'coll_id' => $row['coll_id'], + 'name' => $row['asciiname'], + 'subdefs' => [] + ]; + } + $collections[$row['coll_id']]['subdefs'][$row['name']] = [ + 'count' => $row['n'], + 'size' => $row['size'], + 'disksize' => $row['disksize'] + ]; + if(!array_key_exists($row['name'], $subdefs)) { + $subdefs[$row['name']] = [ + 'count' => 0, + 'size' => 0, + 'disksize' => 0 + ]; + } + $subdefs[$row['name']]['count'] += $row['n']; + $subdefs[$row['name']]['size'] += $row['size']; + $subdefs[$row['name']]['disksize'] += $row['disksize']; + } + $ret['databoxes'][$databox->get_sbas_id()] = [ + 'sbas_id' => $databox->get_sbas_id(), + 'viewname' => $databox->get_viewname(), + 'collections' => $collections, + 'subdefs' => $subdefs + ]; + } + return Result::create($request, $ret)->createResponse([$stopwatch]); + } + +} diff --git a/lib/Alchemy/Phrasea/ControllerProvider/Api/V3.php b/lib/Alchemy/Phrasea/ControllerProvider/Api/V3.php index dafff86a2f..5bc6b69ad0 100644 --- a/lib/Alchemy/Phrasea/ControllerProvider/Api/V3.php +++ b/lib/Alchemy/Phrasea/ControllerProvider/Api/V3.php @@ -5,6 +5,7 @@ use Alchemy\Phrasea\Application as PhraseaApplication; use Alchemy\Phrasea\Controller\Api\V1Controller; use Alchemy\Phrasea\Controller\Api\V3\V3Controller; +use Alchemy\Phrasea\Controller\Api\V3\V3MonitorDataController; use Alchemy\Phrasea\Controller\Api\V3\V3RecordController; use Alchemy\Phrasea\Controller\Api\V3\V3ResultHelpers; use Alchemy\Phrasea\Controller\Api\V3\V3SearchController; @@ -50,6 +51,9 @@ public function register(Application $app) ->setInstanceId($app['conf']) ; }); + $app['controller.api.v3.monitorData'] = $app->share(function (PhraseaApplication $app) { + return (new V3MonitorDataController($app)); + }); $app['controller.api.v3.searchraw'] = $app->share(function (PhraseaApplication $app) { return (new V3SearchRawController($app)); }); @@ -96,6 +100,13 @@ public function connect(Application $app) ->assert('record_id', '\d+') ->value('must_be_story', true); + /** + * @uses V3MonitorDataController::indexAction() + */ + $controllers->get('/monitor/data/', 'controller.api.v3.monitorData:indexAction') + ->before('controller.api.v1:ensureAdmin') + ; + /** * @uses V3SearchController::helloAction() */ From 8744a23f9c5766394e2df1ff1f656e32f71d2861 Mon Sep 17 00:00:00 2001 From: jygaulier Date: Thu, 27 Jun 2024 09:39:55 +0200 Subject: [PATCH 2/9] add `unit` parameter for size restults ('', 'o', ''ko', 'mo', 'go'), default '' (=octets, same as 'o') --- .../Api/V3/V3MonitorDataController.php | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php b/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php index cefcca7cf2..72b024b231 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php @@ -19,6 +19,17 @@ class V3MonitorDataController extends Controller use DispatcherAware; use InstanceIdAware; + private function unitToMultiplier(string $unit) + { + static $map = [''=>1, 'o'=>1, 'ko'=>1<<10, 'mo'=>1<<20, 'go'=>1<<30]; + try { + return $map[strtolower($unit)]; + } + catch (\Exception $e) { + return false; + } + } + /** * monitor infos for app * @@ -26,35 +37,33 @@ class V3MonitorDataController extends Controller * * @return Response */ - private function unitToMultiplier(string $unit) - { - static $map = ['o', 'ko', 'mo', 'go']; - if(($i = array_search(strtolower($unit), $map)) === false) { - return false; - } - return 1 << ($i * 10); - } - public function indexAction(Request $request) { $stopwatch = new Stopwatch("controller"); - $ret = [ - 'databoxes' => [] - ]; $matches = []; if(preg_match("/^(\\d+)\\s*(ko|mo|go)?$/i", $request->get('blocksize', '1'), $matches) !== 1) { throw new Exception("bad 'blocksize' parameter"); } - $matches[] = 'o'; // if no unit, force + $matches[] = ''; // if no unit, force $blocksize = (int)($matches[1]) * $this->unitToMultiplier($matches[2]); + if( ($divider = $this->unitToMultiplier($unit = $request->get('unit', '')) ) === false) { + throw new Exception("bad 'unit' parameter"); + } + $sqlDivider = $divider === 1 ? '' : (' / ' . $divider); + $sql = "SELECT COALESCE(r.`coll_id`, '?') AS `coll_id`, COALESCE(c.`asciiname`, CONCAT('_',r.`coll_id`), '?') AS `asciiname`, s.`name`, - SUM(1) AS n, SUM(s.`size`) AS `size`, SUM(CEIL(s.`size` / " . $blocksize . ") * " . $blocksize . ") AS `disksize` + SUM(1) AS n, SUM(s.`size`) " . $sqlDivider . " AS `size`, + SUM(CEIL(s.`size` / " . $blocksize . ") * " . $blocksize . ") " . $sqlDivider . " AS `disksize` FROM `subdef` AS s LEFT JOIN `record` AS r ON r.`record_id`=s.`record_id` LEFT JOIN `coll` AS c ON r.`coll_id`=c.`coll_id` GROUP BY r.`coll_id`, s.`name`;"; + $ret = [ + 'unit' => ucfirst($unit), + 'databoxes' => [] + ]; foreach($this->app->getDataboxes() as $databox) { $collections = []; $subdefs = []; @@ -91,6 +100,7 @@ public function indexAction(Request $request) 'subdefs' => $subdefs ]; } + return Result::create($request, $ret)->createResponse([$stopwatch]); } From 65a5bc39d07773c4de2538e50fce1074b56157f7 Mon Sep 17 00:00:00 2001 From: jygaulier Date: Thu, 27 Jun 2024 09:56:30 +0200 Subject: [PATCH 3/9] new units "octet", "octets" --- .../Controller/Api/V3/V3MonitorDataController.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php b/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php index 72b024b231..ee6dde1e98 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php @@ -21,7 +21,7 @@ class V3MonitorDataController extends Controller private function unitToMultiplier(string $unit) { - static $map = [''=>1, 'o'=>1, 'ko'=>1<<10, 'mo'=>1<<20, 'go'=>1<<30]; + static $map = [''=>1, 'o'=>1, 'octet'=>1, 'octets'=>1, 'ko'=>1<<10, 'mo'=>1<<20, 'go'=>1<<30]; try { return $map[strtolower($unit)]; } @@ -41,11 +41,14 @@ public function indexAction(Request $request) { $stopwatch = new Stopwatch("controller"); $matches = []; - if(preg_match("/^(\\d+)\\s*(ko|mo|go)?$/i", $request->get('blocksize', '1'), $matches) !== 1) { + if(preg_match("/^(\\d+)\\s*([a-z]*)$/i", $request->get('blocksize', '1'), $matches) !== 1) { throw new Exception("bad 'blocksize' parameter"); } $matches[] = ''; // if no unit, force - $blocksize = (int)($matches[1]) * $this->unitToMultiplier($matches[2]); + if(($mutiplier = $this->unitToMultiplier($matches[2])) === false) { + throw new Exception("bad 'blocksize' unit"); + } + $blocksize = (int)($matches[1]) * $mutiplier; if( ($divider = $this->unitToMultiplier($unit = $request->get('unit', '')) ) === false) { throw new Exception("bad 'unit' parameter"); From 0ab38c6dbb19d2327de0073826407352ae91fcf0 Mon Sep 17 00:00:00 2001 From: jygaulier Date: Thu, 27 Jun 2024 10:25:03 +0200 Subject: [PATCH 4/9] fix round error (don't sum rounded values: round the real sum) --- .../Api/V3/V3MonitorDataController.php | 48 +++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php b/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php index ee6dde1e98..70b6b0c0f0 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php @@ -55,7 +55,12 @@ public function indexAction(Request $request) } $sqlDivider = $divider === 1 ? '' : (' / ' . $divider); - $sql = "SELECT COALESCE(r.`coll_id`, '?') AS `coll_id`, + $ret = [ + 'unit' => $divider === 1 ? $unit : ucfirst($unit), // octet => octet ; mo => Mo + 'databoxes' => [] + ]; + + $sqlByColl = "SELECT COALESCE(r.`coll_id`, '?') AS `coll_id`, COALESCE(c.`asciiname`, CONCAT('_',r.`coll_id`), '?') AS `asciiname`, s.`name`, SUM(1) AS n, SUM(s.`size`) " . $sqlDivider . " AS `size`, SUM(CEIL(s.`size` / " . $blocksize . ") * " . $blocksize . ") " . $sqlDivider . " AS `disksize` @@ -63,14 +68,15 @@ public function indexAction(Request $request) LEFT JOIN `coll` AS c ON r.`coll_id`=c.`coll_id` GROUP BY r.`coll_id`, s.`name`;"; - $ret = [ - 'unit' => ucfirst($unit), - 'databoxes' => [] - ]; + $sqlByName = "SELECT s.`name`, + SUM(1) AS n, SUM(s.`size`) " . $sqlDivider . " AS `size`, + SUM(CEIL(s.`size` / " . $blocksize . ") * " . $blocksize . ") " . $sqlDivider . " AS `disksize` + FROM `subdef` AS s + GROUP BY s.`name`;"; + foreach($this->app->getDataboxes() as $databox) { $collections = []; - $subdefs = []; - $stmt = $databox->get_connection()->prepare($sql); + $stmt = $databox->get_connection()->prepare($sqlByColl); $stmt->execute(); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { if(!array_key_exists($row['coll_id'], $collections)) { @@ -81,21 +87,23 @@ public function indexAction(Request $request) ]; } $collections[$row['coll_id']]['subdefs'][$row['name']] = [ - 'count' => $row['n'], - 'size' => $row['size'], - 'disksize' => $row['disksize'] + 'count' => (int)$row['n'], + 'size' => floatval($row['size']), + 'disksize' => floatval($row['disksize']) ]; - if(!array_key_exists($row['name'], $subdefs)) { - $subdefs[$row['name']] = [ - 'count' => 0, - 'size' => 0, - 'disksize' => 0 - ]; - } - $subdefs[$row['name']]['count'] += $row['n']; - $subdefs[$row['name']]['size'] += $row['size']; - $subdefs[$row['name']]['disksize'] += $row['disksize']; } + $stmt->closeCursor(); + + $subdefs = []; + $stmt = $databox->get_connection()->prepare($sqlByName); + $stmt->execute(); + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + $subdefs[$row['name']]['count'] = (int)$row['n']; + $subdefs[$row['name']]['size'] = floatval($row['size']); + $subdefs[$row['name']]['disksize'] = floatval($row['disksize']); + } + $stmt->closeCursor(); + $ret['databoxes'][$databox->get_sbas_id()] = [ 'sbas_id' => $databox->get_sbas_id(), 'viewname' => $databox->get_viewname(), From db021b6e002a2a553b2bac303d78cf07cdd8264f Mon Sep 17 00:00:00 2001 From: jygaulier Date: Thu, 27 Jun 2024 14:34:42 +0200 Subject: [PATCH 5/9] add infos about downloads; round sizes to 2 decimals --- .../Api/V3/V3MonitorDataController.php | 65 +++++++++++++++++-- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php b/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php index 70b6b0c0f0..829991adc7 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php @@ -74,7 +74,14 @@ public function indexAction(Request $request) FROM `subdef` AS s GROUP BY s.`name`;"; + $sqlByDb = "SELECT SUM(1) AS n, SUM(s.`size`) " . $sqlDivider . " AS `size`, + SUM(CEIL(s.`size` / " . $blocksize . ") * " . $blocksize . ") " . $sqlDivider . " AS `disksize` + FROM `subdef` AS s"; + foreach($this->app->getDataboxes() as $databox) { + + // get volumes grouped by collection and subdef + $collections = []; $stmt = $databox->get_connection()->prepare($sqlByColl); $stmt->execute(); @@ -88,30 +95,78 @@ public function indexAction(Request $request) } $collections[$row['coll_id']]['subdefs'][$row['name']] = [ 'count' => (int)$row['n'], - 'size' => floatval($row['size']), - 'disksize' => floatval($row['disksize']) + 'size' => round($row['size'], 2), + 'disksize' => round($row['disksize'], 2) ]; } $stmt->closeCursor(); + // get volumes by subdef + $subdefs = []; $stmt = $databox->get_connection()->prepare($sqlByName); $stmt->execute(); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $subdefs[$row['name']]['count'] = (int)$row['n']; - $subdefs[$row['name']]['size'] = floatval($row['size']); - $subdefs[$row['name']]['disksize'] = floatval($row['disksize']); + $subdefs[$row['name']]['size'] = round($row['size'], 2); + $subdefs[$row['name']]['disksize'] = round($row['disksize'], 2); } $stmt->closeCursor(); + // get volumes by db + + $stmt = $databox->get_connection()->prepare($sqlByDb); + $stmt->execute(); + $row = $stmt->fetch(PDO::FETCH_ASSOC); + $stmt->closeCursor(); + $ret['databoxes'][$databox->get_sbas_id()] = [ 'sbas_id' => $databox->get_sbas_id(), 'viewname' => $databox->get_viewname(), 'collections' => $collections, - 'subdefs' => $subdefs + 'subdefs' => $subdefs, + 'count' => (int)$row['n'], + 'size' => round($row['size'], 2), + 'disksize' => round($row['disksize'], 2) ]; } + // get volumes of downloads + + $sql = "SELECT `data` FROM `Tokens` WHERE `type`='download'"; + $stmt = $this->getApplicationBox()->get_connection()->prepare($sql); + $stmt->execute(); + $size = 0; + $disksize = 0; + $n = 0; + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + try { + $data = unserialize($row['data']); + $size += $data['size']; + $disksize += ceil($data['size'] / $blocksize) * $blocksize; + $n++; + } + catch (\Exception $e) { + // ignore + } + } + $stmt->closeCursor(); + + $sql = "SELECT DATEDIFF(NOW(), MIN(`created`)) AS `oldest`, SUM(IF(NOW()>`expiration`, 1, 0)) AS `expired` FROM `Tokens` WHERE `type`='download'"; + $stmt = $this->getApplicationBox()->get_connection()->prepare($sql); + $stmt->execute(); + $row = $stmt->fetch(PDO::FETCH_ASSOC); + $stmt->closeCursor(); + + $ret['downloads'] = [ + 'count' => $n, + 'days_oldest' => (int)$row['oldest'], + 'expired' => (int)$row['expired'], + 'size' => round($size / $divider, 2), + 'disksize' => round($disksize / $divider, 2) + ]; + + return Result::create($request, $ret)->createResponse([$stopwatch]); } From 82ef3d053249ca11919fce8a4b48fa0bf617e299 Mon Sep 17 00:00:00 2001 From: jygaulier Date: Thu, 27 Jun 2024 18:24:18 +0200 Subject: [PATCH 6/9] add `...&details=1` url parameter to get values by collection and subdef name --- .../Api/V3/V3MonitorDataController.php | 74 +++++++++++-------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php b/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php index 829991adc7..8c6d741213 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php @@ -39,6 +39,8 @@ private function unitToMultiplier(string $unit) */ public function indexAction(Request $request) { + $getDetails = $request->get('details', '0') === '1'; + $stopwatch = new Stopwatch("controller"); $matches = []; if(preg_match("/^(\\d+)\\s*([a-z]*)$/i", $request->get('blocksize', '1'), $matches) !== 1) { @@ -60,7 +62,9 @@ public function indexAction(Request $request) 'databoxes' => [] ]; - $sqlByColl = "SELECT COALESCE(r.`coll_id`, '?') AS `coll_id`, + if($getDetails) { + + $sqlByColl = "SELECT COALESCE(r.`coll_id`, '?') AS `coll_id`, COALESCE(c.`asciiname`, CONCAT('_',r.`coll_id`), '?') AS `asciiname`, s.`name`, SUM(1) AS n, SUM(s.`size`) " . $sqlDivider . " AS `size`, SUM(CEIL(s.`size` / " . $blocksize . ") * " . $blocksize . ") " . $sqlDivider . " AS `disksize` @@ -68,50 +72,53 @@ public function indexAction(Request $request) LEFT JOIN `coll` AS c ON r.`coll_id`=c.`coll_id` GROUP BY r.`coll_id`, s.`name`;"; - $sqlByName = "SELECT s.`name`, + $sqlByName = "SELECT s.`name`, SUM(1) AS n, SUM(s.`size`) " . $sqlDivider . " AS `size`, SUM(CEIL(s.`size` / " . $blocksize . ") * " . $blocksize . ") " . $sqlDivider . " AS `disksize` FROM `subdef` AS s GROUP BY s.`name`;"; - + } $sqlByDb = "SELECT SUM(1) AS n, SUM(s.`size`) " . $sqlDivider . " AS `size`, SUM(CEIL(s.`size` / " . $blocksize . ") * " . $blocksize . ") " . $sqlDivider . " AS `disksize` FROM `subdef` AS s"; foreach($this->app->getDataboxes() as $databox) { - // get volumes grouped by collection and subdef - - $collections = []; - $stmt = $databox->get_connection()->prepare($sqlByColl); - $stmt->execute(); - while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { - if(!array_key_exists($row['coll_id'], $collections)) { - $collections[$row['coll_id']] = [ - 'coll_id' => $row['coll_id'], - 'name' => $row['asciiname'], - 'subdefs' => [] + if($getDetails) { + + // get volumes grouped by collection and subdef + + $collections = []; + $stmt = $databox->get_connection()->prepare($sqlByColl); + $stmt->execute(); + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + if (!array_key_exists($row['coll_id'], $collections)) { + $collections[$row['coll_id']] = [ + 'coll_id' => $row['coll_id'], + 'name' => $row['asciiname'], + 'subdefs' => [] + ]; + } + $collections[$row['coll_id']]['subdefs'][$row['name']] = [ + 'count' => (int)$row['n'], + 'size' => round($row['size'], 2), + 'disksize' => round($row['disksize'], 2) ]; } - $collections[$row['coll_id']]['subdefs'][$row['name']] = [ - 'count' => (int)$row['n'], - 'size' => round($row['size'], 2), - 'disksize' => round($row['disksize'], 2) - ]; - } - $stmt->closeCursor(); + $stmt->closeCursor(); - // get volumes by subdef + // get volumes by subdef - $subdefs = []; - $stmt = $databox->get_connection()->prepare($sqlByName); - $stmt->execute(); - while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { - $subdefs[$row['name']]['count'] = (int)$row['n']; - $subdefs[$row['name']]['size'] = round($row['size'], 2); - $subdefs[$row['name']]['disksize'] = round($row['disksize'], 2); + $subdefs = []; + $stmt = $databox->get_connection()->prepare($sqlByName); + $stmt->execute(); + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + $subdefs[$row['name']]['count'] = (int)$row['n']; + $subdefs[$row['name']]['size'] = round($row['size'], 2); + $subdefs[$row['name']]['disksize'] = round($row['disksize'], 2); + } + $stmt->closeCursor(); } - $stmt->closeCursor(); // get volumes by db @@ -123,12 +130,15 @@ public function indexAction(Request $request) $ret['databoxes'][$databox->get_sbas_id()] = [ 'sbas_id' => $databox->get_sbas_id(), 'viewname' => $databox->get_viewname(), - 'collections' => $collections, - 'subdefs' => $subdefs, 'count' => (int)$row['n'], 'size' => round($row['size'], 2), 'disksize' => round($row['disksize'], 2) ]; + + if($getDetails) { + $ret['databoxes'][$databox->get_sbas_id()]['collections'] = $collections; + $ret['databoxes'][$databox->get_sbas_id()]['subdefs'] = $subdefs; + } } // get volumes of downloads From 5757e000dd530d852c3bdefce9f4cb2aa37b95a0 Mon Sep 17 00:00:00 2001 From: aynsix Date: Thu, 4 Jul 2024 16:50:37 +0300 Subject: [PATCH 7/9] add column size in LazaretFiles table add command bin/maintenance lazaret:set_sizes --- bin/maintenance | 3 ++ lib/Alchemy/Phrasea/Border/Manager.php | 2 + .../LazaretFilesSetSizeCommand.php | 54 +++++++++++++++++++ .../Phrasea/Model/Entities/LazaretFile.php | 28 ++++++++++ 4 files changed, 87 insertions(+) create mode 100644 lib/Alchemy/Phrasea/Command/Maintenance/LazaretFilesSetSizeCommand.php diff --git a/bin/maintenance b/bin/maintenance index ed76b69e8f..30daa3fd6f 100755 --- a/bin/maintenance +++ b/bin/maintenance @@ -15,6 +15,7 @@ use Alchemy\Phrasea\Command\Maintenance\CleanRightsCommand; use Alchemy\Phrasea\Command\Maintenance\CleanWebhookLogsCommand; use Alchemy\Phrasea\Command\Maintenance\CleanWorkerRunningJobCommand; use Alchemy\Phrasea\Command\Maintenance\SessionsCommand; +use Alchemy\Phrasea\Command\Maintenance\LazaretFilesSetSizeCommand; require_once __DIR__ . '/../lib/autoload.php'; @@ -59,4 +60,6 @@ $cli->command(new CleanLogViewCommand()); $cli->command(new CleanWebhookLogsCommand()); +$cli->command(new LazaretFilesSetSizeCommand()); + $cli->run(); diff --git a/lib/Alchemy/Phrasea/Border/Manager.php b/lib/Alchemy/Phrasea/Border/Manager.php index de52ea8878..9c3b222f8b 100644 --- a/lib/Alchemy/Phrasea/Border/Manager.php +++ b/lib/Alchemy/Phrasea/Border/Manager.php @@ -405,6 +405,8 @@ protected function createLazaret(File $file, Visa $visa, LazaretSession $session $lazaretFile->setSession($session); + $lazaretFile->setSize($file->getFile()->getSize()); + $this->app['orm.em']->persist($lazaretFile); foreach ($file->getAttributes() as $fileAttribute) { diff --git a/lib/Alchemy/Phrasea/Command/Maintenance/LazaretFilesSetSizeCommand.php b/lib/Alchemy/Phrasea/Command/Maintenance/LazaretFilesSetSizeCommand.php new file mode 100644 index 0000000000..3316523bff --- /dev/null +++ b/lib/Alchemy/Phrasea/Command/Maintenance/LazaretFilesSetSizeCommand.php @@ -0,0 +1,54 @@ +setDescription('Set the null size in the LazaretFiles table') + ->addOption('dry-run', null, InputOption::VALUE_NONE, 'dry run, count') + + ->setHelp(''); + } + + public function doExecute(InputInterface $input, OutputInterface $output) + { + /** @var LazaretFileRepository $lazaretRepository */ + $lazaretRepository = $this->container['repo.lazaret-files']; + + $lazaretNullSizes = $lazaretRepository->findBy(['size' => null]); + + $path = $this->container['tmp.lazaret.path']; + /** @var EntityManager $em */ + $em = $this->container['orm.em']; + + if (!$input->getOption('dry-run')) { + /** @var LazaretFile $lazaretNullSize */ + foreach ($lazaretNullSizes as $lazaretNullSize) { + $lazaretFileName = $path .'/'.$lazaretNullSize->getFilename(); + $media = $this->container->getMediaFromUri($lazaretFileName); + + $lazaretNullSize->setSize($media->getFile()->getSize()); + $em->persist($lazaretNullSize); + } + + $em->flush(); + + $output->writeln(sprintf("%d LazaretFiles done!", count($lazaretNullSizes))); + } else { + $output->writeln(sprintf("%d LazaretFiles to update!", count($lazaretNullSizes))); + } + } +} diff --git a/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php b/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php index 676c7d6625..9246eed15a 100644 --- a/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php +++ b/lib/Alchemy/Phrasea/Model/Entities/LazaretFile.php @@ -96,6 +96,11 @@ class LazaretFile */ private $session; + /** + * @ORM\Column(type="bigint", nullable=true) + */ + private $size; + /** * Constructor */ @@ -322,6 +327,29 @@ public function setUpdated(\DateTime $updated) return $this; } + /** + * Set size + * + * @param integer $size + * @return LazaretFile + */ + public function setSize($size) + { + $this->size = $size; + + return $this; + } + + /** + * Get size + * + * @return integer + */ + public function getSize() + { + return $this->size; + } + /** * Get updated * From d27a62df9be2a7e0f3837c7c4763b4d923455bd1 Mon Sep 17 00:00:00 2001 From: aynsix Date: Thu, 4 Jul 2024 16:54:30 +0300 Subject: [PATCH 8/9] add api monitor data by databox --- .../Api/V3/V3MonitorDataController.php | 312 +++++++++++++----- .../Phrasea/ControllerProvider/Api/V3.php | 8 + 2 files changed, 231 insertions(+), 89 deletions(-) diff --git a/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php b/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php index 8c6d741213..3dffa73825 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php @@ -39,87 +39,16 @@ private function unitToMultiplier(string $unit) */ public function indexAction(Request $request) { - $getDetails = $request->get('details', '0') === '1'; - $stopwatch = new Stopwatch("controller"); - $matches = []; - if(preg_match("/^(\\d+)\\s*([a-z]*)$/i", $request->get('blocksize', '1'), $matches) !== 1) { - throw new Exception("bad 'blocksize' parameter"); - } - $matches[] = ''; // if no unit, force - if(($mutiplier = $this->unitToMultiplier($matches[2])) === false) { - throw new Exception("bad 'blocksize' unit"); - } - $blocksize = (int)($matches[1]) * $mutiplier; - if( ($divider = $this->unitToMultiplier($unit = $request->get('unit', '')) ) === false) { - throw new Exception("bad 'unit' parameter"); - } - $sqlDivider = $divider === 1 ? '' : (' / ' . $divider); + list($getDetails, $blocksize, $divider, $unit, $sqlByColl, $sqlByName, $sqlByDb) = $this->getParamsFromRequest($request); $ret = [ 'unit' => $divider === 1 ? $unit : ucfirst($unit), // octet => octet ; mo => Mo 'databoxes' => [] ]; - if($getDetails) { - - $sqlByColl = "SELECT COALESCE(r.`coll_id`, '?') AS `coll_id`, - COALESCE(c.`asciiname`, CONCAT('_',r.`coll_id`), '?') AS `asciiname`, s.`name`, - SUM(1) AS n, SUM(s.`size`) " . $sqlDivider . " AS `size`, - SUM(CEIL(s.`size` / " . $blocksize . ") * " . $blocksize . ") " . $sqlDivider . " AS `disksize` - FROM `subdef` AS s LEFT JOIN `record` AS r ON r.`record_id`=s.`record_id` - LEFT JOIN `coll` AS c ON r.`coll_id`=c.`coll_id` - GROUP BY r.`coll_id`, s.`name`;"; - - $sqlByName = "SELECT s.`name`, - SUM(1) AS n, SUM(s.`size`) " . $sqlDivider . " AS `size`, - SUM(CEIL(s.`size` / " . $blocksize . ") * " . $blocksize . ") " . $sqlDivider . " AS `disksize` - FROM `subdef` AS s - GROUP BY s.`name`;"; - } - $sqlByDb = "SELECT SUM(1) AS n, SUM(s.`size`) " . $sqlDivider . " AS `size`, - SUM(CEIL(s.`size` / " . $blocksize . ") * " . $blocksize . ") " . $sqlDivider . " AS `disksize` - FROM `subdef` AS s"; - - foreach($this->app->getDataboxes() as $databox) { - - if($getDetails) { - - // get volumes grouped by collection and subdef - - $collections = []; - $stmt = $databox->get_connection()->prepare($sqlByColl); - $stmt->execute(); - while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { - if (!array_key_exists($row['coll_id'], $collections)) { - $collections[$row['coll_id']] = [ - 'coll_id' => $row['coll_id'], - 'name' => $row['asciiname'], - 'subdefs' => [] - ]; - } - $collections[$row['coll_id']]['subdefs'][$row['name']] = [ - 'count' => (int)$row['n'], - 'size' => round($row['size'], 2), - 'disksize' => round($row['disksize'], 2) - ]; - } - $stmt->closeCursor(); - - // get volumes by subdef - - $subdefs = []; - $stmt = $databox->get_connection()->prepare($sqlByName); - $stmt->execute(); - while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { - $subdefs[$row['name']]['count'] = (int)$row['n']; - $subdefs[$row['name']]['size'] = round($row['size'], 2); - $subdefs[$row['name']]['disksize'] = round($row['disksize'], 2); - } - $stmt->closeCursor(); - } - + foreach ($this->app->getDataboxes() as $databox) { // get volumes by db $stmt = $databox->get_connection()->prepare($sqlByDb); @@ -128,16 +57,18 @@ public function indexAction(Request $request) $stmt->closeCursor(); $ret['databoxes'][$databox->get_sbas_id()] = [ - 'sbas_id' => $databox->get_sbas_id(), - 'viewname' => $databox->get_viewname(), - 'count' => (int)$row['n'], - 'size' => round($row['size'], 2), - 'disksize' => round($row['disksize'], 2) + 'sbas_id' => $databox->get_sbas_id(), + 'viewname' => $databox->get_viewname(), + 'count' => (int)$row['n'], + 'size' => round($row['size'], 2), + 'disksize' => round($row['disksize'], 2) ]; - if($getDetails) { - $ret['databoxes'][$databox->get_sbas_id()]['collections'] = $collections; - $ret['databoxes'][$databox->get_sbas_id()]['subdefs'] = $subdefs; + if ($getDetails) { + list($collections, $subdefs) = $this->getVolumeDetails($databox, $sqlByColl, $sqlByName); + + $ret['databoxes'][$databox->get_sbas_id()]['collections'] = $collections; + $ret['databoxes'][$databox->get_sbas_id()]['subdefs'] = $subdefs; } } @@ -151,9 +82,9 @@ public function indexAction(Request $request) $n = 0; while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { try { - $data = unserialize($row['data']); - $size += $data['size']; - $disksize += ceil($data['size'] / $blocksize) * $blocksize; + $data = unserialize($row['data']); + $size += $data['size']; + $disksize += ceil($data['size'] / $blocksize) * $blocksize; $n++; } catch (\Exception $e) { @@ -169,15 +100,218 @@ public function indexAction(Request $request) $stmt->closeCursor(); $ret['downloads'] = [ - 'count' => $n, - 'days_oldest' => (int)$row['oldest'], - 'expired' => (int)$row['expired'], - 'size' => round($size / $divider, 2), - 'disksize' => round($disksize / $divider, 2) + 'count' => $n, + 'days_oldest' => (int)$row['oldest'], + 'expired' => (int)$row['expired'], + 'size' => round($size / $divider, 2), + 'disksize' => round($disksize / $divider, 2) + ]; + + $sql = "SELECT count(*) AS n , SUM(`size`) AS size FROM `LazaretFiles` WHERE size IS NOT NULL"; + $stmt = $this->getApplicationBox()->get_connection()->prepare($sql); + $stmt->execute(); + + $row = $stmt->fetch(PDO::FETCH_ASSOC); + $stmt->closeCursor(); + + $disksize = ceil($row['size'] / $blocksize) * $blocksize;; + + $ret['lazaret'] = [ + 'count' => $row['n'], + 'size' => round($row['size'] / $divider, 2), + 'disksize' => round($disksize / $divider, 2) + ]; + + return Result::create($request, $ret)->createResponse([$stopwatch]); + } + + /** + * monitor info for app by databox + * @param Request $request + */ + public function perDataboxAction(Request $request) + { + $stopwatch = new Stopwatch("controller"); + $databoxId = $request->get('databox_id'); + + list($getDetails, $blocksize, $divider, $unit, $sqlByColl, $sqlByName, $sqlByDb) = $this->getParamsFromRequest($request); + + $ret = [ + 'unit' => $divider === 1 ? $unit : ucfirst($unit), // octet => octet ; mo => Mo + 'databox' => [] + ]; + + $databox = $this->findDataboxById($databoxId); + + // get volumes by db + + $stmt = $databox->get_connection()->prepare($sqlByDb); + $stmt->execute(); + $row = $stmt->fetch(PDO::FETCH_ASSOC); + $stmt->closeCursor(); + + $ret['databox'] = [ + 'sbas_id' => $databox->get_sbas_id(), + 'viewname' => $databox->get_viewname(), + 'count' => (int)$row['n'], + 'size' => round($row['size'], 2), + 'disksize' => round($row['disksize'], 2) + ]; + + if ($getDetails) { + list($collections, $subdefs) = $this->getVolumeDetails($databox, $sqlByColl, $sqlByName); + + $ret['databox']['collections'] = $collections; + $ret['databox']['subdefs'] = $subdefs; + } + + // get volumes of downloads + + $sql = "SELECT `data` FROM `Tokens` WHERE `type`='download'"; + $stmt = $this->getApplicationBox()->get_connection()->prepare($sql); + $stmt->execute(); + $size = 0; + $disksize = 0; + $n = 0; + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + try { + $found = false; + $data = unserialize($row['data']); + foreach ($data['files'] as $file) { + // get only for the needed databoxId + if ($file['databox_id'] == $databoxId) { + $found = true; + foreach ($file['subdefs'] as $subdef) { + $size += $subdef['size']; + $disksize += ceil($subdef['size'] / $blocksize) * $blocksize; + } + } + } + + if ($found) { + $n++; + } + } + catch (\Exception $e) { + // ignore + } + } + + $stmt->closeCursor(); + + $ret['downloads'] = [ + 'sbas_id' => $databoxId, + 'count' => $n, + 'size' => round($size / $divider, 2), + 'disksize' => round($disksize / $divider, 2) ]; + // get lazaret volume for the databox + + $sql = "SELECT count(*) AS n , SUM(`size`) AS size ". + " FROM `LazaretFiles` AS L ". + " LEFT JOIN `bas` AS b ON L.`base_id`=b.`base_id`". + " WHERE L.`size` IS NOT NULL AND b.`sbas_id`=". $databoxId; + + + $stmt = $this->getApplicationBox()->get_connection()->prepare($sql); + $stmt->execute(); + + $row = $stmt->fetch(PDO::FETCH_ASSOC); + $stmt->closeCursor(); + + $disksize = ceil($row['size'] / $blocksize) * $blocksize;; + + $ret['lazaret'] = [ + 'sbas_id' => $databoxId, + 'count' => $row['n'], + 'size' => round($row['size'] / $divider, 2), + 'disksize' => round($disksize / $divider, 2) + ]; return Result::create($request, $ret)->createResponse([$stopwatch]); } + private function getParamsFromRequest(Request $request) + { + $getDetails = $request->get('details', '0') === '1'; + + $matches = []; + if(preg_match("/^(\\d+)\\s*([a-z]*)$/i", $request->get('blocksize', '1'), $matches) !== 1) { + throw new Exception("bad 'blocksize' parameter"); + } + $matches[] = ''; // if no unit, force + if(($mutiplier = $this->unitToMultiplier($matches[2])) === false) { + throw new Exception("bad 'blocksize' unit"); + } + $blocksize = (int)($matches[1]) * $mutiplier; + + if( ($divider = $this->unitToMultiplier($unit = $request->get('unit', '')) ) === false) { + throw new Exception("bad 'unit' parameter"); + } + $sqlDivider = $divider === 1 ? '' : (' / ' . $divider); + + $sqlByColl = ""; + $sqlByName = ""; + + if ($getDetails) { + + $sqlByColl = "SELECT COALESCE(r.`coll_id`, '?') AS `coll_id`, + COALESCE(c.`asciiname`, CONCAT('_',r.`coll_id`), '?') AS `asciiname`, s.`name`, + SUM(1) AS n, SUM(s.`size`) " . $sqlDivider . " AS `size`, + SUM(CEIL(s.`size` / " . $blocksize . ") * " . $blocksize . ") " . $sqlDivider . " AS `disksize` + FROM `subdef` AS s LEFT JOIN `record` AS r ON r.`record_id`=s.`record_id` + LEFT JOIN `coll` AS c ON r.`coll_id`=c.`coll_id` + GROUP BY r.`coll_id`, s.`name`;"; + + $sqlByName = "SELECT s.`name`, + SUM(1) AS n, SUM(s.`size`) " . $sqlDivider . " AS `size`, + SUM(CEIL(s.`size` / " . $blocksize . ") * " . $blocksize . ") " . $sqlDivider . " AS `disksize` + FROM `subdef` AS s + GROUP BY s.`name`;"; + } + $sqlByDb = "SELECT SUM(1) AS n, SUM(s.`size`) " . $sqlDivider . " AS `size`, + SUM(CEIL(s.`size` / " . $blocksize . ") * " . $blocksize . ") " . $sqlDivider . " AS `disksize` + FROM `subdef` AS s"; + + return [$getDetails, $blocksize, $divider, $unit, $sqlByColl, $sqlByName, $sqlByDb]; + } + + private function getVolumeDetails(\databox $databox, $sqlByColl, $sqlByName) + { + // get volumes grouped by collection and subdef + + $collections = []; + $stmt = $databox->get_connection()->prepare($sqlByColl); + $stmt->execute(); + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + if (!array_key_exists($row['coll_id'], $collections)) { + $collections[$row['coll_id']] = [ + 'coll_id' => $row['coll_id'], + 'name' => $row['asciiname'], + 'subdefs' => [] + ]; + } + $collections[$row['coll_id']]['subdefs'][$row['name']] = [ + 'count' => (int)$row['n'], + 'size' => round($row['size'], 2), + 'disksize' => round($row['disksize'], 2) + ]; + } + $stmt->closeCursor(); + + // get volumes by subdef + + $subdefs = []; + $stmt = $databox->get_connection()->prepare($sqlByName); + $stmt->execute(); + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { + $subdefs[$row['name']]['count'] = (int)$row['n']; + $subdefs[$row['name']]['size'] = round($row['size'], 2); + $subdefs[$row['name']]['disksize'] = round($row['disksize'], 2); + } + $stmt->closeCursor(); + + return [$collections, $subdefs]; + } } diff --git a/lib/Alchemy/Phrasea/ControllerProvider/Api/V3.php b/lib/Alchemy/Phrasea/ControllerProvider/Api/V3.php index 5bc6b69ad0..211d3113a4 100644 --- a/lib/Alchemy/Phrasea/ControllerProvider/Api/V3.php +++ b/lib/Alchemy/Phrasea/ControllerProvider/Api/V3.php @@ -107,6 +107,14 @@ public function connect(Application $app) ->before('controller.api.v1:ensureAdmin') ; + /** + * @uses V3MonitorDataController::perDataboxAction() + */ + $controllers->get('databoxes/{databox_id}/monitor/data/', 'controller.api.v3.monitorData:perDataboxAction') + ->before('controller.api.v1:ensureAdmin') + ->assert('databox_id', '\d+') + ; + /** * @uses V3SearchController::helloAction() */ From c93bd1849e9331ad23dd6e89e672d3b2f40d51ad Mon Sep 17 00:00:00 2001 From: aynsix Date: Wed, 10 Jul 2024 15:14:24 +0300 Subject: [PATCH 9/9] fix size --- .../LazaretFilesSetSizeCommand.php | 17 +++++++----- .../Api/V3/V3MonitorDataController.php | 27 ++++++++++--------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/lib/Alchemy/Phrasea/Command/Maintenance/LazaretFilesSetSizeCommand.php b/lib/Alchemy/Phrasea/Command/Maintenance/LazaretFilesSetSizeCommand.php index 3316523bff..be90198b86 100644 --- a/lib/Alchemy/Phrasea/Command/Maintenance/LazaretFilesSetSizeCommand.php +++ b/lib/Alchemy/Phrasea/Command/Maintenance/LazaretFilesSetSizeCommand.php @@ -18,7 +18,7 @@ public function __construct() $this ->setDescription('Set the null size in the LazaretFiles table') - ->addOption('dry-run', null, InputOption::VALUE_NONE, 'dry run, count') + ->addOption('dry', null, InputOption::VALUE_NONE, 'dry run, count') ->setHelp(''); } @@ -34,13 +34,18 @@ public function doExecute(InputInterface $input, OutputInterface $output) /** @var EntityManager $em */ $em = $this->container['orm.em']; - if (!$input->getOption('dry-run')) { + if (!$input->getOption('dry')) { /** @var LazaretFile $lazaretNullSize */ foreach ($lazaretNullSizes as $lazaretNullSize) { - $lazaretFileName = $path .'/'.$lazaretNullSize->getFilename(); - $media = $this->container->getMediaFromUri($lazaretFileName); - - $lazaretNullSize->setSize($media->getFile()->getSize()); + try { + $lazaretFileName = $path .'/'.$lazaretNullSize->getFilename(); + $media = $this->container->getMediaFromUri($lazaretFileName); + $size = $media->getFile()->getSize(); + } catch (\Exception $e) { + $size = 0; + } + + $lazaretNullSize->setSize($size); $em->persist($lazaretNullSize); } diff --git a/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php b/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php index 3dffa73825..6c3a5b80ad 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php @@ -41,7 +41,7 @@ public function indexAction(Request $request) { $stopwatch = new Stopwatch("controller"); - list($getDetails, $blocksize, $divider, $unit, $sqlByColl, $sqlByName, $sqlByDb) = $this->getParamsFromRequest($request); + list($getDetails, $blocksize, $divider, $sqlDivider, $unit, $sqlByColl, $sqlByName, $sqlByDb) = $this->getParamsFromRequest($request); $ret = [ 'unit' => $divider === 1 ? $unit : ucfirst($unit), // octet => octet ; mo => Mo @@ -107,19 +107,20 @@ public function indexAction(Request $request) 'disksize' => round($disksize / $divider, 2) ]; - $sql = "SELECT count(*) AS n , SUM(`size`) AS size FROM `LazaretFiles` WHERE size IS NOT NULL"; + $sql = "SELECT count(*) AS n , SUM(`size`) " . $sqlDivider . " AS size, " + . " SUM(CEIL(`size` / " . $blocksize . ") * " . $blocksize . ") " . $sqlDivider . " AS disksize" + . " FROM `LazaretFiles` WHERE size IS NOT NULL"; + $stmt = $this->getApplicationBox()->get_connection()->prepare($sql); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); - $disksize = ceil($row['size'] / $blocksize) * $blocksize;; - $ret['lazaret'] = [ 'count' => $row['n'], - 'size' => round($row['size'] / $divider, 2), - 'disksize' => round($disksize / $divider, 2) + 'size' => round($row['size'], 2), + 'disksize' => round($row['disksize'], 2), ]; return Result::create($request, $ret)->createResponse([$stopwatch]); @@ -134,7 +135,7 @@ public function perDataboxAction(Request $request) $stopwatch = new Stopwatch("controller"); $databoxId = $request->get('databox_id'); - list($getDetails, $blocksize, $divider, $unit, $sqlByColl, $sqlByName, $sqlByDb) = $this->getParamsFromRequest($request); + list($getDetails, $blocksize, $divider, $sqlDivider, $unit, $sqlByColl, $sqlByName, $sqlByDb) = $this->getParamsFromRequest($request); $ret = [ 'unit' => $divider === 1 ? $unit : ucfirst($unit), // octet => octet ; mo => Mo @@ -208,7 +209,8 @@ public function perDataboxAction(Request $request) // get lazaret volume for the databox - $sql = "SELECT count(*) AS n , SUM(`size`) AS size ". + $sql = "SELECT count(*) AS n , SUM(`L`.`size`) " . $sqlDivider . " AS size, ". + " SUM(CEIL(`L`.`size` / " . $blocksize . ") * " . $blocksize . ") " . $sqlDivider . " AS disksize" . " FROM `LazaretFiles` AS L ". " LEFT JOIN `bas` AS b ON L.`base_id`=b.`base_id`". " WHERE L.`size` IS NOT NULL AND b.`sbas_id`=". $databoxId; @@ -220,13 +222,11 @@ public function perDataboxAction(Request $request) $row = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); - $disksize = ceil($row['size'] / $blocksize) * $blocksize;; - $ret['lazaret'] = [ 'sbas_id' => $databoxId, 'count' => $row['n'], - 'size' => round($row['size'] / $divider, 2), - 'disksize' => round($disksize / $divider, 2) + 'size' => round($row['size'], 2), + 'disksize' => round($row['disksize'], 2), ]; return Result::create($request, $ret)->createResponse([$stopwatch]); @@ -270,11 +270,12 @@ private function getParamsFromRequest(Request $request) FROM `subdef` AS s GROUP BY s.`name`;"; } + $sqlByDb = "SELECT SUM(1) AS n, SUM(s.`size`) " . $sqlDivider . " AS `size`, SUM(CEIL(s.`size` / " . $blocksize . ") * " . $blocksize . ") " . $sqlDivider . " AS `disksize` FROM `subdef` AS s"; - return [$getDetails, $blocksize, $divider, $unit, $sqlByColl, $sqlByName, $sqlByDb]; + return [$getDetails, $blocksize, $divider, $sqlDivider, $unit, $sqlByColl, $sqlByName, $sqlByDb]; } private function getVolumeDetails(\databox $databox, $sqlByColl, $sqlByName)