Remove redundant comments, streamline query logic, and clean up variable handling for improved code readability and efficiency in StdOut.php and StdOut-detail.php.
This commit is contained in:
@@ -68,9 +68,6 @@
|
||||
<!-- TABLE -->
|
||||
<div>
|
||||
<?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'])) {
|
||||
@@ -126,29 +123,16 @@
|
||||
$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;
|
||||
$resultsByHost = []; // Tableau pour stocker les résultats par host
|
||||
|
||||
// =================================================================
|
||||
// ETAPE 2: Récupérer TOUS les résultats en une seule requête
|
||||
// =================================================================
|
||||
$resultsByHost = [];
|
||||
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;
|
||||
@@ -200,29 +184,23 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<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']);
|
||||
|
||||
// Comptage initial
|
||||
if ($os == "AIX") {
|
||||
$taix++;
|
||||
} else {
|
||||
$tlinux++;
|
||||
}
|
||||
|
||||
// Récupération des résultats depuis notre tableau, SANS requête !
|
||||
$answer = $resultsByHost[$host] ?? null; // Utilise l'opérateur null coalescent
|
||||
$answer = $resultsByHost[$host] ?? null;
|
||||
|
||||
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++;
|
||||
@@ -237,11 +215,9 @@
|
||||
}
|
||||
}
|
||||
} 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>";
|
||||
@@ -260,7 +236,6 @@
|
||||
</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>";
|
||||
|
||||
13
X/StdOut.php
13
X/StdOut.php
@@ -63,29 +63,22 @@
|
||||
</div>
|
||||
<!-- TABLE -->
|
||||
<div>
|
||||
<?php // DATA OPTIMIZED
|
||||
|
||||
// The one and only query to get everything we need
|
||||
<?php
|
||||
$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 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>
|
||||
|
||||
Reference in New Issue
Block a user