Skip to content

Commit

Permalink
new route /api/v3/monitor/data/?oauth_token=xxx&blocksize=16ko
Browse files Browse the repository at this point in the history
  • Loading branch information
jygaulier committed Jun 26, 2024
1 parent 4e62334 commit d8335f4
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
97 changes: 97 additions & 0 deletions lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Alchemy\Phrasea\Controller\Api\V3;

use Alchemy\Phrasea\Application\Helper\DispatcherAware;
use Alchemy\Phrasea\Application\Helper\JsonBodyAware;
use Alchemy\Phrasea\Controller\Api\InstanceIdAware;
use Alchemy\Phrasea\Controller\Api\Result;
use Alchemy\Phrasea\Controller\Controller;
use Alchemy\Phrasea\Controller\Exception;
use Alchemy\Phrasea\Utilities\Stopwatch;
use PDO;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class V3MonitorDataController extends Controller
{
use JsonBodyAware;
use DispatcherAware;
use InstanceIdAware;

/**
* monitor infos for app
*
* @param Request $request
*
* @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
$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]);
}

}
11 changes: 11 additions & 0 deletions lib/Alchemy/Phrasea/ControllerProvider/Api/V3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
});
Expand Down Expand Up @@ -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()
*/
Expand Down

0 comments on commit d8335f4

Please sign in to comment.