<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Page Title -->
  <title>Web Infra Reports</title>
  <link rel="shortcut icon" type="image/png" href="/include/favicon-32x32.png">

  <!-- JQuery -->
  <script src="/js/jquery-3.6.1.min.js"></script>
  <!-- Bootstrap -->
  <link rel="stylesheet" href="/css/bootstrap.min.css">
  <link rel="stylesheet" href="/css/bootstrap-icons/bootstrap-icons.css">
  <script src="/js/bootstrap.bundle.min.js"></script>
  <!-- Bootstrap-tables -->
  <link rel="stylesheet" href="/css/bootstrap-table.min.css">
  <script src="/js/bootstrap-table.min.js"></script>
  <script src="/js/bootstrap-table-fr-FR.min.js"></script>
  <!-- Chart -->
  <script src="/js/chart.min.js"></script>
  <script src="/js/apexcharts.min.js"></script>
  <!-- Gauge -->
  <link rel="stylesheet" href="/css/Gauge.css">
  <script src="/js/jquery.AshAlom.gaugeMeter-2.0.0.min.js"></script>

</head>

<body class="bg-light text-dark">
<?php include $_SERVER['DOCUMENT_ROOT'] . "/include/all.php"; ?> <!-- Database connexion -->
<div class="container-fluid">
  <div class="row flex-nowrap">
    <!-- Left NAVBAR -->
    <div class="col-auto col-md-2 col-xl-2 px-sm-2 px-0 bg-dark vh-100 position-sticky top-0" style="-ms-flex: 0 0 230px;flex: 0 0 230px;">
      <?php include $_SERVER['DOCUMENT_ROOT'] . "/navbar.html"; ?> <!-- Left Navbar -->
    </div>
    <!-- Display -->
    <div class="col py-3">
      <!-- Page Title -->
      <h1><span class="badge text-bg-secondary font-weight-bold"
                style="width:100%;"><?php echo $_GET['cluster'] . " Details"; ?></span></h1>

      <!-- Main content -->
      <?php
        $clusters = Invoke_Infra("select * from cmdb_HyperV_Hosts where cluster ='" . $_GET['cluster'] . "' order by node");
        $vmmem = (Invoke_Infra("select sum(cast(vm_memory as int)) as vmem from cmdb_HyperV_Hosts where cluster ='" . $_GET['cluster'] . "'"))[0]['vmem'];
        $clumem = (invoke_infra("select min(node_ram) as nmem from cmdb_HyperV_Hosts where cluster ='" . $_GET['cluster'] . "'"))[0]['nmem'];
        $nb = (invoke_infra("select count(node) as nb from cmdb_HyperV_Hosts where cluster ='" . $_GET['cluster'] . "'"))[0]['nb'];
        $clmem = $clumem - 32;
        if ($nb == 4) {
          $clmem = $clmem * 2;
        }

      ?>
      <div class="container-fluid" style="zoom:100%">
        <h2><span class="badge text-bg-secondary font-weight-bold"
                  style="width:100%;"><?php echo $_GET['cluster']; ?></span></h2>
        <?php
          echo "<b>Worst Failover scenario available memory : </b>" . ($clmem - $vmmem) . " / " . $clmem . " GB";
          $pcent = round(($clmem - $vmmem) / $clmem * 100);
          $pcent_restant = 100 - $pcent;
          if ($pcent <= 5) {
            $color = "bg-danger";
            $border = "red";
          } else {
            if ($pcent <= 10) {
              $color = "bg-warning";
              $border = "yellow";
            } else {
              $color = "bg-success";
              $border = "green";
            }
          }
          echo "
                <div class='progress' style='border: 2px solid $border; height: 25px;'>
                  <div class='progress-bar bg-dark progress-bar-striped' role='progressbar' style='width: $pcent_restant%;' aria-valuenow='$pcent_restant' aria-valuemin='0' aria-valuemax='100'></div>
                  <div class='progress-bar $color' role='progressbar' style='width: $pcent%;' aria-valuenow='$pcent' aria-valuemin='0' aria-valuemax='100'></div>
                </div>";
          echo "<br>";
        ?>
        <div class="row">
          <?php
            foreach ($clusters as $cluster) {
              echo "<div class='col-6'>";
              $nbvm = Invoke_Infra("select count(*) as nbvm, sum(memory) as vmmem from cmdb_vms where owner ='" . $cluster['node'] . "' and decomtime is null");
              echo '<h3><span class="badge text-bg-secondary font-weight-bold" style="width:100%;">' . $cluster['node'] . "  - " . $nbvm[0]['nbvm'] . " VMs   <small><small> <i class='bi bi-clock'></i> " . $cluster['ts'] . "</small></small></span></h3><br>";
              #Memory
              echo "<div class='row'>";
              echo "<div class='col'>";
              echo "<b>Free Memory : </b>" . ($cluster['node_ram'] - 32 - $nbvm[0]['vmmem']) . " / " . ($cluster['node_ram'] - 32) . " GB";
              $pcent = round(($cluster['node_ram'] - 32 - $nbvm[0]['vmmem']) / ($cluster['node_ram'] - 32) * 100);
              $pcent_restant = 100 - $pcent;
              if ($pcent <= 5) {
                $color = "bg-danger";
              } else {
                if ($pcent <= 10) {
                  $color = "bg-warning";
                } else {
                  $color = "bg-success";
                }
              }
              $progress_bar_height = '25px';
              $marker_overlap = 5;
              $marker_height = (int)$progress_bar_height + (2 * $marker_overlap);
              $marker_top = -$marker_overlap;
              echo "</div>";
              echo "
                    <div style='position: relative; display: inline-block;'> 
                      <div class='progress' style='border: 1px solid grey; height: " . $progress_bar_height . "; margin-bottom: " . (2 * $marker_overlap) . "px;'> 
                        <div class='progress-bar bg-dark progress-bar-striped' role='progressbar' style='width:" . $pcent_restant . "%;' aria-valuenow='" . $pcent_restant . "' aria-valuemin='0' aria-valuemax='100'></div>
                        <div class='progress-bar " . $color . "' role='progressbar' style='width: " . $pcent . "%;' aria-valuenow='" . $pcent . "' aria-valuemin='0' aria-valuemax='100'></div>
                      </div>

                      <div style='
                          position: absolute;
                          left: 50%;
                          transform: translateX(-50%);
                          top: " . $marker_top . "px;
                          height: " . $marker_height . "px;
                          width: 2px;
                          background-color: orange;
                          z-index: 1;
                        '></div>

                    </div>";
              echo "</div>";
              echo "<br>";

              #Storage
              echo "<div class='row'>";
              foreach (explode("|", $cluster['csvs']) as $csv) {
                if (strpos($csv, ";")) {
                  echo "<div class='col'>";
                  echo "<b>" . explode(";", $csv)[0];
                  $total = explode(";", $csv)[1];
                  $used = explode(";", $csv)[2];
                  $free = $total - $used;
                  echo " Free : </b>" . $free . "/" . $total . " GB";
                  echo "</div>";
                  echo "<div class='col'>";
                  $pcent = round($free / $total * 100);
                  $pcent_restant = 100 - $pcent;
                  if ($pcent <= 10) {
                    $color = "bg-danger";
                  } else {
                    if ($pcent <= 20) {
                      $color = "bg-warning";
                    } else {
                      $color = "bg-success";
                    }
                  }
                  echo "
                          <div class='progress' style='border: 1px solid grey; height: 25px;'>
                            <div class='progress-bar bg-dark progress-bar-striped' role='progressbar' style='width: $pcent_restant%;' aria-valuenow='$pcent_restant' aria-valuemin='0' aria-valuemax='100'></div>
                            <div class='progress-bar $color' role='progressbar' style='width: $pcent%;' aria-valuenow='$pcent' aria-valuemin='0' aria-valuemax='100'></div>
                          </div>";
                  echo "</div>";
                } else {
                  echo "<div class='col'></div><br>";
                }
              }
              echo "</div>";
              echo "<hr>";

              #VMs
              $vms = Invoke_Infra("select * from cmdb_vms where owner ='" . $cluster['node'] . "' and decomtime is null order by name");
              foreach ($vms as $vm) {
                echo "<div class='row'>";
                echo "<div class='col'>";
                $state = '<i class="bi bi-question-square-fill text-primary"></i> ';
                if ($vm['State'] == "Running") {
                  $state = '<i class="bi bi-play-btn-fill text-success"></i> ';
                }
                if ($vm['State'] == "Off" || $vm['State'] == "Stopping") {
                  $state = '<i class="bi bi-stop-btn-fill text-danger"></i> ';
                }
                if ($vm['State'] == "Paused") {
                  $state = '<i class="bi bi-pause-btn-fill text-warning"></i> ';
                }
                if (str_starts_with($vm['Name'], 'WS')) {
                  echo $state . " <b>" . substr($vm['Name'], 0, 10) . "</b>";
                } else {
                  echo $state . " <b>" . $vm['Name'] . "</b>";
                }
                echo "</div>";

                echo "<div class='col'>";
                echo '<span class="badge rounded-pill bg-secondary text-light">' . $vm['Memory'] . 'GB / ' . $vm['CPU'] . ' CPU</span> ';
                if ($vm['WWPNs'] != "") {
                  echo '<span class="badge rounded-pill bg-primary text-light">LUN(s)</span> ';
                }
                if ($vm['IsClustered'] != "True") {
                  echo '<span class="badge rounded-pill bg-danger text-dark"><small>Unclustered</small></span> ';
                } else {
                  if ($vm['Owner'] != $vm['PreferredOwner']) {
                    echo '<span class="badge rounded-pill bg-warning text-dark"><small>Wrong Owner</small></span> ';
                  }
                }
                echo "</div>";

                echo "<div class='col'>";
                echo "</div>";

                echo "</div>";
              }
              echo "<br>";
              echo "</div>";
            }
            echo "</div>";
          ?>
        </div>
        <!-- End of main content -->
      </div>
    </div>
  </div>
  <script src="/js/switch.js"></script>
</body>

<script>
  $(".GaugeMeter").gaugeMeter();
</script>