Optimize database queries, add resilience with is_countable, refine UI for progress bars and tables, update scripts for dynamic resizing, and correct navbar link case sensitivity.

This commit is contained in:
2025-08-20 20:38:01 +02:00
parent 70af042b7b
commit a17bb63889
6 changed files with 96 additions and 51 deletions

View File

@@ -67,7 +67,10 @@
</div>
<!-- TABLE -->
<div>
<?php // DATA
<?php
// =================================================================
// ETAPE 1: Récupérer la liste des serveurs (inchangé)
// =================================================================
$aix = $linux = $dun = $azt = $other = "Checked";
$where = " and OS_TYPE IN (";
if (isset($_GET['AIX'])) {
@@ -123,39 +126,62 @@
$sql = "select hostname, os_type from srvall where decomtime is null and (ucase(filter) not like 'X_%' or filter is null) $where order by hostname";
$hosts = Invoke_aixcmdb($sql);
// Initialisation des compteurs
$taix = $aixok = $aixko = $tlinux = $linuxok = $linuxko = 0;
#print_r($hosts);
$resultsByHost = []; // Tableau pour stocker les résultats par host
// =================================================================
// ETAPE 2: Récupérer TOUS les résultats en une seule requête
// =================================================================
if (!empty($hosts)) {
// Créer une liste de noms de serveurs pour la clause IN
$hostnames = array_map(function ($h) {
// Important: échapper les noms pour éviter l'injection SQL
return "'" . strtoupper($h['HOSTNAME']) . "'";
}, $hosts);
$hostnamesList = implode(',', $hostnames);
// La deuxième et dernière requête SQL
$sqlResults = "SELECT * FROM x_stdout WHERE cmd = '$script' AND host IN ($hostnamesList)";
$allAnswers = Invoke_Infra($sqlResults);
// =================================================================
// ETAPE 3: Organiser les résultats dans un tableau associatif pour un accès rapide
// La clé du tableau sera le nom du serveur
// =================================================================
if (is_array($allAnswers)) {
foreach ($allAnswers as $answer) {
$resultsByHost[strtoupper($answer['host'])] = $answer;
}
}
}
?>
<form class="row">
<div class="col border rounded">
<b>Filters : </b>
<div class="form-check form-check-inline form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="AIX" <?php echo $aix; ?>
name="AIX" value="1">
<input class="form-check-input" type="checkbox" role="switch" id="AIX" <?php echo $aix; ?> name="AIX" value="1">
<label class="form-check-label" for="AIX">AIX</label>
</div>
&nbsp;
<div class="form-check form-check-inline form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="linux" <?php echo $linux; ?>
name="linux" value="1">
<input class="form-check-input" type="checkbox" role="switch" id="linux" <?php echo $linux; ?> name="linux" value="1">
<label class="form-check-label" for="linux">Linux</label>
</div>
&nbsp; | &nbsp;
<div class="form-check form-check-inline form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="DUN" <?php echo $dun; ?>
name="DUN" value="1">
<input class="form-check-input" type="checkbox" role="switch" id="DUN" <?php echo $dun; ?> name="DUN" value="1">
<label class="form-check-label" for="DUN">DMV/DUN/MDK</label>
</div>
&nbsp;
<div class="form-check form-check-inline form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="AZT" <?php echo $azt; ?>
name="AZT" value="1">
<input class="form-check-input" type="checkbox" role="switch" id="AZT" <?php echo $azt; ?> name="AZT" value="1">
<label class="form-check-label" for="AZT">Azure</label>
</div>
&nbsp;
<div class="form-check form-check-inline form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="other" <?php echo $other; ?>
name="other" value="1">
<input class="form-check-input" type="checkbox" role="switch" id="other" <?php echo $other; ?> name="other" value="1">
<label class="form-check-label" for="other">Other</label>
</div>
<input type="hidden" name="s" value="<?php echo $script; ?>">
@@ -164,55 +190,58 @@
</form>
<table class='table table-bordered table-hover table-sm' id='t1' data-height="620" data-toggle="table"
data-search="true" data-show-columns="true" data-export-types="['xlsx','csv','json']"
data-show-export="true" data-sortable="true" data-sort-name="vm">
<thead> <!-- Header -->
<tr>
<table class='table table-bordered table-hover table-sm' id='t1' data-height="620" data-toggle="table" data-search="true" data-show-columns="true" data-export-types="['xlsx','csv','json']" data-show-export="true" data-sortable="true" data-sort-name="vm">
<thead> <tr>
<th data-field='host' data-sortable='true'>Host</th>
<th data-field='OS' data-sortable='true'>OS</th>
<th data-field='TimeStamp' data-sortable='true'>TimeStamp</th>
<th data-field='RC' data-sortable='true'>RC</th>
<th data-field='Result' data-sortable='true'>StdOut</th>
</tr>
</thead>
<tbody> <!-- Body -->
<?php
<tbody> <?php
// =================================================================
// ETAPE 4: Boucle pour afficher les données (maintenant très rapide)
// =================================================================
foreach ($hosts as $h) {
$host = strtoupper($h['HOSTNAME']);
$os = strtoupper($h['OS_TYPE']);
if (strtoupper($h['OS_TYPE']) == "AIX") {
// Comptage initial
if ($os == "AIX") {
$taix++;
} else {
$tlinux++;
}
$answer = Invoke_infra("SELECT * FROM x_stdout where cmd ='$script' and host ='$host'");
if (is_array($answer)) {
$rc = $answer[0]['rc'];
$stdout = $answer[0]['stdout'];
$ts = $answer[0]['ts'];
if (strtoupper($h['OS_TYPE']) == "AIX") {
// Récupération des résultats depuis notre tableau, SANS requête !
$answer = $resultsByHost[$host] ?? null; // Utilise l'opérateur null coalescent
if ($answer) {
$rc = $answer['rc'];
$stdout = $answer['stdout'];
$ts = $answer['ts'];
// Comptage des status OK/KO
if ($os == "AIX") {
if ($rc == 0 && (string)$rc <> "") {
$aixok++;
} else {
if ((string)$rc <> "") {
$aixko++;
}
} elseif ((string)$rc <> "") {
$aixko++;
}
} else {
} else { // Linux
if ($rc == 0 && (string)$rc <> "") {
$linuxok++;
} else {
if ((string)$rc <> "") {
$linuxko++;
}
} elseif ((string)$rc <> "") {
$linuxko++;
}
}
} else {
// Si aucun résultat n'a été trouvé pour ce host
$rc = $stdout = $ts = "";
}
// Affichage du tableau HTML (inchangé)
echo "<tr><td><b>$host</b></td>";
echo "<td>$os</td>";
echo "<td>" . $ts . "</td>";
@@ -231,12 +260,14 @@
</tbody>
<br>
<?php
// Calcul et affichage final des totaux (inchangé)
$aixNO = $taix - $aixok - $aixko;
$linuxNO = $tlinux - $linuxok - $linuxko;
echo "<div class='row'><div class='col text-center h4'><b>$taix AIX : </b>";
echo "<span class='badge bg-success'>$aixok</span>&nbsp;<span class='badge bg-danger'>$aixko</span>&nbsp;<span class='badge bg-secondary'>$aixNO</span></div>";
echo "<div class='col text-center h4'><b>$tlinux Linux : </b>";
echo "<span class='badge bg-success'>$linuxok</span>&nbsp;<span class='badge bg-danger'>$linuxko</span>&nbsp;<span class='badge bg-secondary'>$linuxNO</span></div></div> "; ?>
echo "<span class='badge bg-success'>$linuxok</span>&nbsp;<span class='badge bg-danger'>$linuxko</span>&nbsp;<span class='badge bg-secondary'>$linuxNO</span></div></div> ";
?>
</div>
</div>
<!-- End of main content -->

View File

@@ -63,16 +63,29 @@
</div>
<!-- TABLE -->
<div>
<?php // DATA
$answers = Invoke_infra("SELECT distinct(cmd),max(ts) from x_stdout group by cmd order by max(ts) desc");
echo "<div class='row'>";
foreach($answers as $cmd){
$ts = Invoke_infra("SELECT max(ts) as fin, min(ts) as debut FROM x_stdout where cmd = '".$cmd['cmd']."'");
echo "<div class='col-3 mb-3'><a href='StdOut-detail.php?s=".$cmd['cmd']."&AIX=1&linux=1&DUN=1' class='btn btn-primary btn-lg' role='button'>";
echo "<b>".$cmd['cmd']."</b><br><small>".$ts[0]['debut']." --> ".$ts[0]['fin']."</small>";
echo "</a></div> ";
<?php // DATA OPTIMIZED
// The one and only query to get everything we need
$sql = "SELECT cmd, MIN(ts) AS debut, MAX(ts) AS fin FROM x_stdout GROUP BY cmd ORDER BY fin DESC";
$results = Invoke_infra($sql);
echo "<div class='row'>";
// Loop through the results (now very fast, no more queries inside)
if (is_array($results)) {
foreach ($results as $script) {
$scriptName = $script['cmd'];
$startTime = $script['debut'];
$endTime = $script['fin'];
echo "<div class='col-3 mb-3'>";
echo "<a href='StdOut-detail.php?s={$scriptName}&AIX=1&linux=1&DUN=1' class='btn btn-primary btn-lg' role='button'>";
echo "<b>{$scriptName}</b><br><small>{$startTime} --> {$endTime}</small>";
echo "</a>";
echo "</div>";
}
}
echo "</div>";
?>
</div>