Skip to content

Commit

Permalink
add unit parameter for size restults ('', 'o', ''ko', 'mo', 'go'), …
Browse files Browse the repository at this point in the history
…default '' (=octets, same as 'o')
  • Loading branch information
jygaulier committed Jun 27, 2024
1 parent d8335f4 commit 5f49025
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions lib/Alchemy/Phrasea/Controller/Api/V3/V3MonitorDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,51 @@ 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
*
* @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
$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 = [];
Expand Down Expand Up @@ -91,6 +100,7 @@ public function indexAction(Request $request)
'subdefs' => $subdefs
];
}

return Result::create($request, $ret)->createResponse([$stopwatch]);
}

Expand Down

0 comments on commit 5f49025

Please sign in to comment.