From d8335f4804deac1a8fbcf7e7b80068b79abaa0c2 Mon Sep 17 00:00:00 2001 From: jygaulier Date: Wed, 26 Jun 2024 18:35:39 +0200 Subject: [PATCH] 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 00000000000..cefcca7cf2d --- /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 dafff86a2f0..5bc6b69ad0a 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() */