<?php

namespace App\Services;

use App\Core\Database;

class HomepageReliabilityService
{

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

$db=
Database::getInstance();

$out=[];

$queries=[

'sections'=>

"

SELECT COUNT(*)

FROM conference_home_sections

WHERE conference_id=?

",

'blocks'=>

"

SELECT COUNT(*)

FROM conference_homepage_blocks

WHERE conference_id=?

",

'deployments'=>

"

SELECT COUNT(*)

FROM conference_home_deployments

WHERE conference_id=?

",

'incidents'=>

"

SELECT COUNT(*)

FROM conference_home_incidents

WHERE conference_id=?

AND incident_state='open'

"

];

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

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

$stmt
->execute([

$conferenceId

]);

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

}

$out[
'availability'
]=

max(

0,

100
-
(
$out[
'incidents'
]
*
10
)

);

$out[
'sla'
]=

$out[
'availability'
]>=95

?

'healthy'

:

'risk';

return
$out;

}

public static function heal(
int $conferenceId
): bool
{

$db=
Database::getInstance();

return
$db
->prepare("

INSERT INTO
conference_home_recovery(

conference_id,

recovery_type,

recovery_result

)

VALUES(

?,

'self_heal',

'completed'

)

")

->execute([

$conferenceId

]);

}

}
