<?php

namespace App\Services;

use App\Core\Database;

class PlatformHypervisorService
{

public static function dashboard(
int $orgId,
int $conferenceId
): array
{

$db=
Database::getInstance();

$out=[];

$queries=[

'tenants'=>

"

SELECT COUNT(*)

FROM platform_tenant_runtime

WHERE org_id=?

",

'usage'=>

"

SELECT COUNT(*)

FROM platform_resource_usage

WHERE org_id=?

",

'events'=>

"

SELECT COUNT(*)

FROM platform_event_bus

WHERE conference_id=?

",

'runtime'=>

"

SELECT COUNT(*)

FROM platform_runtime

WHERE conference_id=?

"

];

foreach(
$queries
as $k=>$sql
){

$stmt=
$db
->prepare(
$sql
);

if(
in_array(
$k,
[
'events',
'runtime'
]
)
){

$stmt
->execute([

$conferenceId

]);

}
else{

$stmt
->execute([

$orgId

]);

}

$out[
$k
]=
(int)
$stmt
->fetchColumn();

}

$out[
'capacity'
]=

min(

100,

60+

(
$out[
'runtime'
]
*2
)

-

(
$out[
'events'
]/10
)

);

$out[
'state'
]=

$out[
'capacity'
]>=80

?

'balanced'

:

'review';

return
$out;

}

}
