<?php
/**
 * Demo Oracle — optimisation SQL (section « config/sqltune »).
 *
 * Portage de modules/oracle/_report_config_sqltune.php : tache automatique
 * Auto SQL Tuning Advisor, parametres, profils SQL, historique d'execution.
 *
 * Les deux commandes du module complet — modifier un parametre de tuning,
 * supprimer un profil SQL — restent affichees, desactivees. Elles n'ont aucun
 * gestionnaire en face : DemoOracleConfigSqltuneRepository ne porte que des
 * lectures, dropSqlProfile et setAutoTuningParam n'ont pas ete reprises.
 *
 * Inclus par report.php. Variables : $oraDb, $db, $currentUser, $settings.
 */

require_once __DIR__ . '/lib/DemoOracleOciConnect.php';
require_once __DIR__ . '/lib/DemoOracleConfigStatsRepository.php';
require_once __DIR__ . '/lib/DemoOracleConfigSqltuneRepository.php';

/** @var array    $oraDb       */
/** @var Database $db          */
/** @var object   $settings    */
/** @var array    $currentUser */

$ociError   = null;
$fetchError = null;
$conn       = demo_oracle_oci_open($oraDb, $db, $ociError, '', 'config/sqltune');

$autoTask    = [];
$params      = [];
$profiles    = [];
$executions  = [];
$autoHistory = [];

if (!$conn) {
    $fetchError = $ociError
        ? ('ORA-' . $ociError['code'] . ': ' . $ociError['message'])
        : t('demo_oracle.connect_error');
} else {
    $autoTask    = DemoOracleConfigStatsRepository::fetchAutoTask($conn, 'sql tuning advisor');
    $params      = DemoOracleConfigSqltuneRepository::fetchAutoTuningParams($conn);
    $profiles    = DemoOracleConfigSqltuneRepository::fetchSqlProfiles($conn, 50);
    $executions  = DemoOracleConfigSqltuneRepository::fetchTuningExecutions($conn, 20);
    $autoHistory = DemoOracleConfigStatsRepository::fetchAutoTaskHistory($conn, 'sql tuning advisor', 15);
    oci_close($conn);
}

View::display(__DIR__ . '/views/report/config_sqltune.php', [
    'fetchError'  => $fetchError,
    'autoTask'    => $autoTask,
    'params'      => $params,
    'profiles'    => $profiles,
    'executions'  => $executions,
    'autoHistory' => $autoHistory,
    'settings'    => $settings,
]);
