<?php
/**
 * Demo Oracle — lectures de sw$bash_snapper.
 *
 * Extrait de modules/oracle/lib/OracleBashSnapperRepository.php, qui compte
 * 1736 lignes : la demonstration n'en reprend que TROIS methodes de lecture,
 * celles qui alimentent le rapport d'historique quand le Diagnostic Pack n'est
 * pas sous licence.
 *
 * Tout le reste — installer, desinstaller, demarrer, arreter, purger le
 * paquetage, modifier ses reglages — est absent : la demonstration peut
 * UTILISER un sw$bash_snapper deja en place, elle ne sait pas en deployer un.
 * C'est DemoOracleAshSource qui decide laquelle des deux sources interroger.
 *
 * Safe to include multiple times (guard sur class_exists).
 */
if (class_exists('DemoOracleBashSnapperRepository')) return;

class DemoOracleBashSnapperRepository
{
/**
     * Hourly ASH stacked bar — same shape as OracleAshRepository::fetchAshHourly().
     */
    public static function fetchAshHourly($conn, int $days): array
    {
        $rows = [];
        $sql = "SELECT TO_CHAR(TRUNC(sample_time, 'HH24'), 'YYYY-MM-DD HH24:MI') sample_bucket,
                       CASE WHEN session_state = 'ON CPU' THEN 'CPU'
                            ELSE NVL(wait_class, 'Other') END category,
                       COUNT(*) cnt
                FROM SW\$BASH_HIST_ACTIVE_SESS_HISTORY
                WHERE sample_time >= SYSDATE - :days
                  AND session_type != 'BACKGROUND'
                GROUP BY TRUNC(sample_time, 'HH24'),
                         CASE WHEN session_state = 'ON CPU' THEN 'CPU'
                              ELSE NVL(wait_class, 'Other') END
                ORDER BY 1, 2";
        $stm = @oci_parse($conn, $sql);
        if ($stm) {
            oci_bind_by_name($stm, ':days', $days, -1, SQLT_INT);
            if (@oci_execute($stm, OCI_NO_AUTO_COMMIT)) {
                while ($r = oci_fetch_assoc($stm)) {
                    $rows[] = [
                        'sample_bucket' => (string)$r['SAMPLE_BUCKET'],
                        'category'      => (string)$r['CATEGORY'],
                        'cnt'           => (int)$r['CNT'],
                    ];
                }
            }
            oci_free_statement($stm);
        }
        return $rows;
    }

/**
     * Heatmap charge — same shape as OracleAshRepository::fetchHeatmapCharge().
     */
    public static function fetchHeatmapCharge($conn, int $days): array
    {
        $rows = [];
        $sql = "SELECT TO_CHAR(TRUNC(sample_time, 'DD'), 'YYYY-MM-DD') day_bucket,
                       TO_CHAR(sample_time, 'HH24') hour_of_day,
                       COUNT(*) cnt
                FROM SW\$BASH_HIST_ACTIVE_SESS_HISTORY
                WHERE sample_time >= SYSDATE - :days
                  AND session_type != 'BACKGROUND'
                GROUP BY TRUNC(sample_time, 'DD'), TO_CHAR(sample_time, 'HH24')
                ORDER BY 1, 2";
        $stm = @oci_parse($conn, $sql);
        if ($stm) {
            oci_bind_by_name($stm, ':days', $days, -1, SQLT_INT);
            if (@oci_execute($stm, OCI_NO_AUTO_COMMIT)) {
                while ($r = oci_fetch_assoc($stm)) {
                    $rows[] = [
                        'day'  => (string)$r['DAY_BUCKET'],
                        'hour' => (int)$r['HOUR_OF_DAY'],
                        'cnt'  => (int)$r['CNT'],
                    ];
                }
            }
            oci_free_statement($stm);
        }
        return $rows;
    }

/**
     * Heatmap I/O — same shape as OracleAshRepository::fetchHeatmapIo().
     */
    public static function fetchHeatmapIo($conn, int $days): array
    {
        $rows = [];
        $sql = "SELECT TO_CHAR(TRUNC(sample_time, 'DD'), 'YYYY-MM-DD') day_bucket,
                       TO_CHAR(sample_time, 'HH24') hour_of_day,
                       COUNT(*) cnt
                FROM SW\$BASH_HIST_ACTIVE_SESS_HISTORY
                WHERE sample_time >= SYSDATE - :days
                  AND session_type != 'BACKGROUND'
                  AND wait_class IN ('User I/O', 'System I/O')
                GROUP BY TRUNC(sample_time, 'DD'), TO_CHAR(sample_time, 'HH24')
                ORDER BY 1, 2";
        $stm = @oci_parse($conn, $sql);
        if ($stm) {
            oci_bind_by_name($stm, ':days', $days, -1, SQLT_INT);
            if (@oci_execute($stm, OCI_NO_AUTO_COMMIT)) {
                while ($r = oci_fetch_assoc($stm)) {
                    $rows[] = [
                        'day'  => (string)$r['DAY_BUCKET'],
                        'hour' => (int)$r['HOUR_OF_DAY'],
                        'cnt'  => (int)$r['CNT'],
                    ];
                }
            }
            oci_free_statement($stm);
        }
        return $rows;
    }
}
